From 9257466caae3e74676139c4d6e1863acc178d94a Mon Sep 17 00:00:00 2001 From: jableader Date: Sun, 28 Apr 2024 13:37:01 +1000 Subject: [PATCH] Better Test Data --- db.py | 13 +-- main.py | 2 +- persons/db.py | 7 +- test_data.py | 231 ++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 208 insertions(+), 45 deletions(-) diff --git a/db.py b/db.py index 573d135..ef471c8 100644 --- a/db.py +++ b/db.py @@ -17,15 +17,4 @@ async def create(conn: aiosqlite.Connection): await person_db.create(conn) import meals.db as meals_db - await meals_db.create(conn) - -if __name__ == '__main__': - import asyncio - async def initdb(): - conn = await connect() - await create(conn) - await conn.commit() - - await conn.close() - - asyncio.run(initdb()) \ No newline at end of file + await meals_db.create(conn) \ No newline at end of file diff --git a/main.py b/main.py index df393bd..eefb541 100644 --- a/main.py +++ b/main.py @@ -173,7 +173,7 @@ async def get_persons(conn: sqlite3.Connection = Depends(get_db)) -> List[meals. @app.post("/persons/") async def create_person(person: persons.Person, conn: sqlite3.Connection = Depends(get_db)) -> persons.Person: - await persons.insert(conn, person) + await persons.insert_person(conn, person) await conn.commit() return person diff --git a/persons/db.py b/persons/db.py index d76064a..675370a 100644 --- a/persons/db.py +++ b/persons/db.py @@ -11,11 +11,6 @@ async def create(conn): id INTEGER PRIMARY KEY, name TEXT UNIQUE );''') - - await insert(conn, Person(id=1, name='Jacob')) - await insert(conn, Person(id=2, name='Ryan')) - await insert(conn, Person(id=3, name='Ellie')) - await insert(conn, Person(id=4, name='Chris')) async def get_by_name(conn, name: str) -> Person: cursor = await conn.execute(''' @@ -47,7 +42,7 @@ async def get_all(conn) -> List[Person]: async for row in cursor: yield Person(id=row[0], name=row[1]) -async def insert(conn, person: Person) -> Person: +async def insert_person(conn, person: Person) -> Person: cursor = await conn.execute(''' INSERT INTO Person (name) VALUES (?) diff --git a/test_data.py b/test_data.py index 275cf87..419920f 100644 --- a/test_data.py +++ b/test_data.py @@ -1,51 +1,98 @@ -from persons import db as persons_db +import persons class Persons: - jacob = persons_db.Person( + jacob = persons.Person( id=1, name='Jacob') - ryan = persons_db.Person( + ryan = persons.Person( id=2, name='Ryan') - ellie = persons_db.Person( + ellie = persons.Person( id=3, name='Ellie') - chris = persons_db.Person( + chris = persons.Person( id=4, name='Chris') -from products import db as products_db +import products class Products: - broccoli = products_db.Product( + broccoli = products.Product( id=0, - product_id='69', - name='Broccoli', - link='https://en.wikipedia.org/wiki/Broccoli', - tags=['vegetable'], - img_small='https://upload.wikimedia.org/wikipedia/commons/thumb/0/03/Broccoli_and_cross_section_edit.jpg/800px-Broccoli_and_cross_section_edit.jpg', - img_large='https://upload.wikimedia.org/wikipedia/commons/0/03/Broccoli_and_cross_section_edit.jpg', + name="Fresh Broccoli", + product_id="134681", + link="https://www.woolworths.com.au/shop/productdetails/134681/fresh-broccoli", + img_small="https://cdn0.woolworths.media/content/wowproductimages/small/134681.jpg", + img_large="https://cdn0.woolworths.media/content/wowproductimages/large/134681.jpg", raw_data={}, ) - garlic_bread = products_db.Product( + garlic_bread = products.Product( id=0, - product_id='420', - name='Garlic Bread', - link='https://en.wikipedia.org/wiki/Garlic_bread', - tags=['bread', 'garlic'], - img_small='https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Garlic_bread.jpg/800px-Garlic_bread.jpg', - img_large='https://upload.wikimedia.org/wikipedia/commons/4/4b/Garlic_bread.jpg', + name="La Famiglia Garlic Bread", + product_id="294517", + link="https://www.woolworths.com.au/shop/productdetails/294517/la-famiglia-garlic-bread", + img_small="https://cdn0.woolworths.media/content/wowproductimages/small/294517.jpg", + img_large="https://cdn0.woolworths.media/content/wowproductimages/large/294517.jpg", raw_data={}, ) -from ingredients import db as ingredients_db + beans_round = products.Product( + id=0, + name="Beans Round", + product_id="134072", + link="https://www.woolworths.com.au/shop/productdetails/134072/beans-round", + img_small="https://cdn0.woolworths.media/content/wowproductimages/small/134072.jpg", + img_large="https://cdn0.woolworths.media/content/wowproductimages/large/134072.jpg", + raw_data={}, + ) + + western_star_unsalted_butter_chefs_choice = products.Product( + id=0, + name="Western Star Unsalted Butter Chef's Choice", + product_id="712251", + link="https://www.woolworths.com.au/shop/productdetails/712251/western-star-unsalted-butter-chef-s-choice", + img_small="https://cdn0.woolworths.media/content/wowproductimages/small/712251.jpg", + img_large="https://cdn0.woolworths.media/content/wowproductimages/large/712251.jpg", + raw_data={}, + ) + + saxa_iodised_table_salt_shaker = products.Product( + id=0, + name="Saxa Iodised Table Salt Shaker", + product_id="33245", + link="https://www.woolworths.com.au/shop/productdetails/33245/saxa-iodised-table-salt-shaker", + img_small="https://cdn0.woolworths.media/content/wowproductimages/small/033245.jpg", + img_large="https://cdn0.woolworths.media/content/wowproductimages/large/033245.jpg", + raw_data={}, + ) + + mckenzies_pepper_black_ground = products.Product( + id=0, + name="Mckenzie's Pepper Black Ground", + product_id="75194", + link="https://www.woolworths.com.au/shop/productdetails/75194/mckenzie-s-pepper-black-ground", + img_small="https://cdn0.woolworths.media/content/wowproductimages/small/075194.jpg", + img_large="https://cdn0.woolworths.media/content/wowproductimages/large/075194.jpg", + raw_data={}, + ) + + _tags = { + broccoli.product_id: ['broccoli', 'fresh broccoli'], + garlic_bread.product_id: ['garlic bread', 'bread', 'garlic', 'frozen garlic bread'], + beans_round.product_id: ['beans', 'green beans', 'fresh green beans', 'fresh beans'], + western_star_unsalted_butter_chefs_choice.product_id: ['butter', 'unsalted butter', 'salted butter'], + saxa_iodised_table_salt_shaker.product_id: ['salt', 'iodised salt', 'kosher salt'], + mckenzies_pepper_black_ground.product_id: ['pepper', 'black pepper', 'ground pepper', 'fresh ground pepper'], + } + +import ingredients class Ingredients: - broccoli_chopped_1kg = ingredients_db.Ingredient( + broccoli_chopped_1kg = ingredients.Ingredient( id=0, line='1kg Broccoli, Chopped', name='Broccoli', @@ -55,7 +102,7 @@ class Ingredients: product=Products.broccoli, ) - garlic_bread_1_loaf = ingredients_db.Ingredient( + garlic_bread_1_loaf = ingredients.Ingredient( id=0, line='1 Loaf Garlic Bread', name='Garlic Bread', @@ -65,10 +112,50 @@ class Ingredients: product=Products.garlic_bread, ) -from recipes import db as recipes_db + green_beans = ingredients.Ingredient( + id=0, + line="1 pound green beans (fresh)", + name="green beans", + unit="Pound", + quantity=1.0, + preparation="", + product=Products.beans_round, + ) + + butter = ingredients.Ingredient( + id=0, + line="1 to 2 tablespoons butter", + name="butter", + unit="Tablespoon", + quantity=1.0, + preparation="", + product=Products.western_star_unsalted_butter_chefs_choice, + ) + + salt = ingredients.Ingredient( + id=0, + line="Salt (to taste)", + name="Salt", + unit="Items", + quantity=1.0, + preparation="", + product=Products.saxa_iodised_table_salt_shaker, + ) + + freshly_ground_black_pepper = ingredients.Ingredient( + id=0, + line="Freshly ground black pepper (to taste)", + name="Freshly ground black pepper", + unit="Items", + quantity=1.0, + preparation="", + product=Products.mckenzies_pepper_black_ground, + ) + +import recipes class Recipes: - broccoli_soup = recipes_db.Recipe( + broccoli_soup = recipes.Recipe( id=0, name='Broccoli Soup', link='https://www.bbcgoodfood.com/recipes/broccoli-soup', @@ -77,6 +164,15 @@ class Recipes: created_by_id=Persons.jacob.id, ) + how_to_steam_green_beans = recipes.Recipe( + id=0, + name="How to Steam Green Beans", + link="https://www.thespruceeats.com/steamed-green-beans-3057051", + image_urls=["https://www.thespruceeats.com/thmb/CLROdq9dlYbjKjlOlA_kmFdunTY=/1500x0/filters:no_upscale():max_bytes(150000):strip_icc()/steamed-green-beans-3057051-hero-01-b1c4f894da5b4bc0a01cd43886df0100.jpg"], + ingredients=[Ingredients.green_beans,Ingredients.butter,Ingredients.salt,Ingredients.freshly_ground_black_pepper], + created_by_id=Persons.jacob.id, + ) + from meals import db as meals_db from datetime import datetime @@ -91,4 +187,87 @@ class Meals: consumers=[Persons.ellie, Persons.chris], recipes=[Recipes.broccoli_soup], extra_ingredients=[Ingredients.garlic_bread_1_loaf], - ) \ No newline at end of file + ) + +def class_fields(obj): + return {k:v for k,v in obj.__dict__.items() if not k.startswith('_')} + +if __name__ == '__main__': + import asyncio + from db import connect, create + + async def initdb(): + conn = await connect() + await create(conn) + await conn.commit() + + for person in class_fields(Persons).values(): + await persons.insert_person(conn, person) + + for product in class_fields(Products).values(): + await products.insert_product(conn, product, {}) + await products.add_missing_tags(conn, product, Products._tags[product.product_id]) + + for ingredient in class_fields(Ingredients).values(): + await ingredients.insert_ingredient(conn, ingredient) + + for recipe in class_fields(Recipes).values(): + await recipes.insert_recipe(conn, recipe) + + for meal in class_fields(Meals).values(): + await meals_db.insert_meal(conn, meal) + + await conn.commit() + await conn.close() + + asyncio.run(initdb()) + +""" +import re +def to_name(thing): + return re.sub(r'\W', '', thing['name'].lower().replace(' ', '_')) + +products_order= ['id', 'name', 'product_id', 'link', 'tags', 'img_small', 'img_large', 'raw_data'] +ingredients_order = 'id line name unit quantity preparation product'.split(' ') + +def stringify(k: str, v) -> str: + if k == 'id': + return '0' + if k == 'ingredients' and isinstance(v, dict): + return f'Ingredients.{to_name(v)}' + if k == 'product': + return f'Products.{to_name(v)}' + if k == 'created_by_id': + names = ['jacob', 'ryan', 'ellie', 'chris'] + return f'Persons.{names[v - 1]}.id' + if v is None: + if k == 'raw_data': + return '{}' + if k == 'tags': + return '[]' + return 'None' + if isinstance(v, str): + return f'"{v}"' + if isinstance(v, list): + return f'[{",".join([stringify(k, x) for x in v])}]' + return str(v) + +def to_param_list(item, order): + results = [] + for k in order: + v = item[k] if k in item else None + results.append(f'{k}={stringify(k, v)}') + return results + +def to_create_statements(items, type_name, order): + s = [] + for item in items: + name = to_name(item) + params = to_param_list(item, order) + s.append(f'{name} = {type_name}(') + for p in params: + s.append(f'\t{p},') + s.append(')') + s.append('') + return '\n'.join(s) +""" \ No newline at end of file