2024-09-29 05:04:10 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import tests.test_data as test_data
|
|
|
|
|
|
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)
|