Skip to content

Commit

Permalink
Handle comparable string edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
Javex committed Mar 8, 2024
1 parent 66a74f3 commit a7e531c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
25 changes: 25 additions & 0 deletions hotprices_au/sites/coles.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,37 @@ def get_quantity_and_unit(item):
# If that didn't work we can now try to get the info from standard sizes (e.g. per 100g)
if 'ofMeasureUnits' in unit_data:
unit = unit_data['ofMeasureUnits']
elif item.get('pricing',{}).get('comparable'):
return parse_comparable(item)
else:
raise

return quantity, unit


def parse_comparable(item):
comparable = item['pricing']['comparable']
m = re.match(r'\$([\.0-9]+) per (.*)', comparable)
if not m:
raise RuntimeError(f"Unable to parse comparable {comparable}")

price_str, per_str = m.group(1), m.group(2)
price_per = float(price_str)
if price_per != item['pricing']['now']:
raise RuntimeError(
f"Price from {comparable} extracted as {price_per} "
f"does not match expected price of {item['pricing']['now']}"
)

if per_str == '1ea':
quantity, unit = 1, 'ea'
else:
raise RuntimeError(
f"Unable to understad what {per_str} means from "
f"{comparable}"
)
return quantity, unit

def parse_str_unit(size):
# Try coles-special methods before going to the generic function
size = size.lower()
Expand Down
12 changes: 11 additions & 1 deletion tests/stores/test_coles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from hotprices_au.sites import coles

def get_item(ofMeasureUnits=None, quantity=None, isWeighted=True, pricing=True, size=None, **kwargs):
def get_item(ofMeasureUnits=None, quantity=None, isWeighted=True, pricing=True,
size=None, comparable=None, **kwargs):
defaults = {
'_type': 'PRODUCT',
'id': '1',
Expand All @@ -26,6 +27,9 @@ def get_item(ofMeasureUnits=None, quantity=None, isWeighted=True, pricing=True,
if quantity is not None:
defaults['pricing']['unit']['quantity'] = quantity

if comparable is not None:
defaults['pricing']['comparable'] = comparable

if pricing is None:
defaults['pricing'] = None
defaults.update(kwargs)
Expand Down Expand Up @@ -133,6 +137,12 @@ def test_get_canonical():
assert can_item['quantity'] == 200
assert not can_item['isWeighted']

item = get_item(comparable='$10.00 per 1ea', quantity=0)
can_item = coles.get_canonical(item, today)
assert can_item['unit'] == 'ea'
assert can_item['quantity'] == 1
assert can_item['price'] == 10


if __name__ == '__main__':
test_get_canonical()

0 comments on commit a7e531c

Please sign in to comment.