munch-ease-backend/tests/test_products.py

118 lines
4.2 KiB
Python
Raw Normal View History

2024-09-29 05:04:10 +00:00
import unittest
import tests.test_data as test_data
Squashed commit of the following: commit fcd005b8624023547f28b7b28e59e6099bcfc7d4 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 20:24:07 2025 +1100 Openapi tightening commit f93bd8f641d561052c7bd075bae321b4ff3b676d Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 19:03:52 2025 +1100 Removed refactor strategy doc commit 0c5a61092f522be0c47cbbe86917c8a7e4e2d339 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 17:48:33 2025 +1100 mypy & ruff checks commit 23d66d6b18984127e17c73c3063f6120385935e9 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 16:49:35 2025 +1100 Final removal of db.py files commit f454aed1ca9783cc558cc203f29a7fe31b62a975 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:42:31 2025 +1100 Finalise restructure, remove db.py files commit 7187f6dd89489521538791c6bdebb426514beb99 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:34:54 2025 +1100 commit 6fea227ae20d32b8eb1e7a4885006a620fcc7bb1 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:32:53 2025 +1100 commit 27415e7e02d89195ad514cb017a9dbbf84d7a5e4 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:31:10 2025 +1100 commit b773428033d855f9ad82005602e049c1a2e3c585 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:28:58 2025 +1100 commit 116592c95278d995f4c516e87f2cea43cf5b7735 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:25:21 2025 +1100 commit 03ec565faea088971968ee2f9bb83e2de16b21f3 Author: jableader <jacobdunk@gmail.com> Date: Sun Oct 19 15:21:29 2025 +1100 Plan
2025-10-19 09:24:23 +00:00
import products.repository as products_db
2024-09-29 05:04:10 +00:00
from db import connect, create
import importlib
2025-10-18 03:26:42 +00:00
2024-09-29 05:04:10 +00:00
def reload_test_data():
global test_data
test_data = importlib.reload(test_data)
2025-10-18 03:26:42 +00:00
2024-09-29 05:04:10 +00:00
class TestProductsDb(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
2025-10-18 03:26:42 +00:00
self.conn = await connect(":memory:")
2024-09-29 05:04:10 +00:00
await create(self.conn)
reload_test_data()
return await super().asyncSetUp()
async def asyncTearDown(self) -> None:
await self.conn.close()
return await super().asyncTearDown()
2025-10-18 03:26:42 +00:00
2024-09-29 05:04:10 +00:00
async def testCreateAndFind(self) -> None:
product = test_data.Products.broccoli
await products_db.insert_product(self.conn, product, {})
self.assertIsNotNone(product)
self.assertGreater(product.id, 0)
2025-10-18 03:26:42 +00:00
2024-09-29 05:04:10 +00:00
product_by_id = await products_db.find_product_by_id(self.conn, product.id)
self.assertIsNotNone(product_by_id)
self.assertEqual(product_by_id.id, product.id)
self.assertEqual(product_by_id.name, product.name)
self.assertEqual(product_by_id.link, product.link)
self.assertEqual(product_by_id.img_large, product.img_large)
self.assertEqual(product_by_id.img_small, product.img_small)
from . import httpx_mocks
from products import woolworths
2025-10-18 03:26:42 +00:00
2024-09-29 05:04:10 +00:00
class TestWoolworths(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
2025-10-18 03:26:42 +00:00
local_path = "./tests/sample_files/woolworths"
2024-09-29 05:04:10 +00:00
woolworths._get_client = lambda: httpx_mocks.MockAsyncClient(local_path)
# woolworths._get_client = lambda: httpx_mocks.RecordingAsyncClient(local_path)
return await super().asyncSetUp()
2025-10-18 03:26:42 +00:00
2024-09-29 05:04:10 +00:00
async def test_get_product_id(self) -> None:
params = [
2025-10-18 03:26:42 +00:00
("https://www.woolworths.com.au/shop/productdetails/144607/strawberries", "144607"),
(
"https://www.woolworths.com.au/shop/productdetails/133211/cavendish-bananas",
"133211",
),
("https://www.coles.com.au/product/coles-strawberries-250g-5191256", None),
2024-09-29 05:04:10 +00:00
]
for url, id in params:
self.assertEqual(woolworths.get_product_id(url), id)
async def test_get_strawberries(self) -> None:
2025-10-18 03:26:42 +00:00
details, raw_data = await woolworths.scrape("144607")
2024-09-29 05:04:10 +00:00
expected = {
2025-10-18 03:26:42 +00:00
"name": "Strawberries",
"quantity": 250,
"unit": "g Punnet",
"img_small": "https://cdn0.woolworths.media/content/wowproductimages/small/144607.jpg",
"img_large": "https://cdn0.woolworths.media/content/wowproductimages/large/144607.jpg",
2024-09-29 05:04:10 +00:00
}
for key, value in expected.items():
self.assertEqual(details[key], value, msg=key)
from products import coles
2025-10-18 03:26:42 +00:00
2024-09-29 05:04:10 +00:00
class TestColes(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
2025-10-18 03:26:42 +00:00
local_path = "./tests/sample_files/coles"
2024-09-29 05:04:10 +00:00
coles._get_client = lambda: httpx_mocks.MockAsyncClient(local_path)
# coles._get_client = lambda: httpx_mocks.RecordingAsyncClient(local_path)
return await super().asyncSetUp()
2025-10-18 03:26:42 +00:00
2024-09-29 05:04:10 +00:00
async def test_get_product_id(self) -> None:
params = [
2025-10-18 03:26:42 +00:00
(
"https://www.coles.com.au/product/coles-strawberries-250g-5191256",
"coles-strawberries-250g-5191256",
),
(
"https://www.coles.com.au/product/coles-blueberries-170g-3571948",
"coles-blueberries-170g-3571948",
),
("https://www.woolworths.com.au/shop/productdetails/144607/strawberries", None),
2024-09-29 05:04:10 +00:00
]
for url, id in params:
self.assertEqual(coles.get_product_id(url), id)
async def test_get_strawberries(self) -> None:
2025-10-18 03:26:42 +00:00
details, raw_data = await coles.scrape("coles-strawberries-250g-5191256")
2024-09-29 05:04:10 +00:00
expected = {
2025-10-18 03:26:42 +00:00
"name": "Strawberries",
"quantity": 250,
"unit": "g",
"img_small": "https://shop.coles.com.au/wcsstore/Coles-CAS/images/5/1/9/5191256-th.jpg",
"img_large": "https://shop.coles.com.au/wcsstore/Coles-CAS/images/5/1/9/5191256.jpg",
2024-09-29 05:04:10 +00:00
}
for key, value in expected.items():
self.assertEqual(details[key], value, msg=key)