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
117 lines
4.2 KiB
Python
117 lines
4.2 KiB
Python
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)
|