import unittest import tests.test_data as test_data import products.repository as products_db from db import connect, create import importlib def reload_test_data(): global test_data test_data = importlib.reload(test_data) class TestProductsDb(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self): self.conn = await connect(":memory:") await create(self.conn) reload_test_data() return await super().asyncSetUp() async def asyncTearDown(self) -> None: await self.conn.close() return await super().asyncTearDown() 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) 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 class TestWoolworths(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self) -> None: local_path = "./tests/sample_files/woolworths" woolworths._get_client = lambda: httpx_mocks.MockAsyncClient(local_path) # woolworths._get_client = lambda: httpx_mocks.RecordingAsyncClient(local_path) return await super().asyncSetUp() async def test_get_product_id(self) -> None: params = [ ("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), ] for url, id in params: self.assertEqual(woolworths.get_product_id(url), id) async def test_get_strawberries(self) -> None: details, raw_data = await woolworths.scrape("144607") expected = { "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", } for key, value in expected.items(): self.assertEqual(details[key], value, msg=key) from products import coles class TestColes(unittest.IsolatedAsyncioTestCase): async def asyncSetUp(self) -> None: local_path = "./tests/sample_files/coles" coles._get_client = lambda: httpx_mocks.MockAsyncClient(local_path) # coles._get_client = lambda: httpx_mocks.RecordingAsyncClient(local_path) return await super().asyncSetUp() async def test_get_product_id(self) -> None: params = [ ( "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), ] for url, id in params: self.assertEqual(coles.get_product_id(url), id) async def test_get_strawberries(self) -> None: details, raw_data = await coles.scrape("coles-strawberries-250g-5191256") expected = { "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", } for key, value in expected.items(): self.assertEqual(details[key], value, msg=key)