Add terminal review resolution workflow
This commit is contained in:
@@ -99,11 +99,12 @@ class PurchaseLogTests(unittest.TestCase):
|
||||
}
|
||||
]
|
||||
|
||||
rows = build_purchases.build_purchase_rows(
|
||||
rows, _observed, _canon, _links = build_purchases.build_purchase_rows(
|
||||
[giant_row],
|
||||
[costco_row],
|
||||
giant_orders,
|
||||
costco_orders,
|
||||
[],
|
||||
)
|
||||
|
||||
self.assertEqual(2, len(rows))
|
||||
@@ -195,6 +196,9 @@ class PurchaseLogTests(unittest.TestCase):
|
||||
costco_items_enriched_csv=str(costco_items),
|
||||
giant_orders_csv=str(giant_orders),
|
||||
costco_orders_csv=str(costco_orders),
|
||||
resolutions_csv=str(Path(tmpdir) / "review_resolutions.csv"),
|
||||
catalog_csv=str(Path(tmpdir) / "canonical_catalog.csv"),
|
||||
links_csv=str(Path(tmpdir) / "product_links.csv"),
|
||||
output_csv=str(purchases_csv),
|
||||
examples_csv=str(examples_csv),
|
||||
)
|
||||
@@ -208,6 +212,56 @@ class PurchaseLogTests(unittest.TestCase):
|
||||
self.assertEqual(2, len(purchase_rows))
|
||||
self.assertEqual(1, len(example_rows))
|
||||
|
||||
def test_build_purchase_rows_applies_manual_resolution(self):
|
||||
fieldnames = enrich_costco.OUTPUT_FIELDS
|
||||
giant_row = {field: "" for field in fieldnames}
|
||||
giant_row.update(
|
||||
{
|
||||
"retailer": "giant",
|
||||
"order_id": "g1",
|
||||
"line_no": "1",
|
||||
"observed_item_key": "giant:g1:1",
|
||||
"order_date": "2026-03-01",
|
||||
"item_name": "SB BAGGED ICE 20LB",
|
||||
"item_name_norm": "BAGGED ICE",
|
||||
"retailer_item_id": "100",
|
||||
"upc": "",
|
||||
"qty": "1",
|
||||
"unit": "EA",
|
||||
"line_total": "3.50",
|
||||
"unit_price": "3.50",
|
||||
"measure_type": "each",
|
||||
"raw_order_path": "giant_output/raw/g1.json",
|
||||
"is_discount_line": "false",
|
||||
"is_coupon_line": "false",
|
||||
"is_fee": "false",
|
||||
}
|
||||
)
|
||||
observed_rows, _canonical_rows, _link_rows, _observed_id_by_key, _canonical_by_observed = (
|
||||
build_purchases.build_link_state([giant_row])
|
||||
)
|
||||
observed_product_id = observed_rows[0]["observed_product_id"]
|
||||
rows, _observed, _canon, _links = build_purchases.build_purchase_rows(
|
||||
[giant_row],
|
||||
[],
|
||||
[{"order_id": "g1", "store_name": "Giant", "store_number": "42", "store_city": "Springfield", "store_state": "VA"}],
|
||||
[],
|
||||
[
|
||||
{
|
||||
"observed_product_id": observed_product_id,
|
||||
"canonical_product_id": "gcan_manual_ice",
|
||||
"resolution_action": "create",
|
||||
"status": "approved",
|
||||
"resolution_notes": "manual ice merge",
|
||||
"reviewed_at": "2026-03-16",
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
self.assertEqual("gcan_manual_ice", rows[0]["canonical_product_id"])
|
||||
self.assertEqual("approved", rows[0]["review_status"])
|
||||
self.assertEqual("create", rows[0]["resolution_action"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
100
tests/test_review_workflow.py
Normal file
100
tests/test_review_workflow.py
Normal file
@@ -0,0 +1,100 @@
|
||||
import csv
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import review_products
|
||||
|
||||
|
||||
class ReviewWorkflowTests(unittest.TestCase):
|
||||
def test_build_review_queue_groups_unresolved_purchases(self):
|
||||
queue_rows = review_products.build_review_queue(
|
||||
[
|
||||
{
|
||||
"observed_product_id": "gobs_1",
|
||||
"canonical_product_id": "",
|
||||
"retailer": "giant",
|
||||
"raw_item_name": "SB BAGGED ICE 20LB",
|
||||
"normalized_item_name": "BAGGED ICE",
|
||||
"upc": "",
|
||||
"line_total": "3.50",
|
||||
},
|
||||
{
|
||||
"observed_product_id": "gobs_1",
|
||||
"canonical_product_id": "",
|
||||
"retailer": "giant",
|
||||
"raw_item_name": "SB BAG ICE CUBED 10LB",
|
||||
"normalized_item_name": "BAG ICE",
|
||||
"upc": "",
|
||||
"line_total": "2.50",
|
||||
},
|
||||
],
|
||||
[],
|
||||
)
|
||||
|
||||
self.assertEqual(1, len(queue_rows))
|
||||
self.assertEqual("gobs_1", queue_rows[0]["observed_product_id"])
|
||||
self.assertIn("SB BAGGED ICE 20LB", queue_rows[0]["raw_item_names"])
|
||||
|
||||
def test_review_products_creates_canonical_and_resolution(self):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
purchases_csv = Path(tmpdir) / "purchases.csv"
|
||||
queue_csv = Path(tmpdir) / "review_queue.csv"
|
||||
resolutions_csv = Path(tmpdir) / "review_resolutions.csv"
|
||||
catalog_csv = Path(tmpdir) / "canonical_catalog.csv"
|
||||
|
||||
with purchases_csv.open("w", newline="", encoding="utf-8") as handle:
|
||||
writer = csv.DictWriter(
|
||||
handle,
|
||||
fieldnames=[
|
||||
"observed_product_id",
|
||||
"canonical_product_id",
|
||||
"retailer",
|
||||
"raw_item_name",
|
||||
"normalized_item_name",
|
||||
"upc",
|
||||
"line_total",
|
||||
],
|
||||
)
|
||||
writer.writeheader()
|
||||
writer.writerow(
|
||||
{
|
||||
"observed_product_id": "gobs_ice",
|
||||
"canonical_product_id": "",
|
||||
"retailer": "giant",
|
||||
"raw_item_name": "SB BAGGED ICE 20LB",
|
||||
"normalized_item_name": "BAGGED ICE",
|
||||
"upc": "",
|
||||
"line_total": "3.50",
|
||||
}
|
||||
)
|
||||
|
||||
with mock.patch.object(
|
||||
review_products.click,
|
||||
"prompt",
|
||||
side_effect=["n", "ICE", "frozen", "ice", "manual merge", "q"],
|
||||
):
|
||||
review_products.main.callback(
|
||||
purchases_csv=str(purchases_csv),
|
||||
queue_csv=str(queue_csv),
|
||||
resolutions_csv=str(resolutions_csv),
|
||||
catalog_csv=str(catalog_csv),
|
||||
limit=1,
|
||||
refresh_only=False,
|
||||
)
|
||||
|
||||
self.assertTrue(queue_csv.exists())
|
||||
self.assertTrue(resolutions_csv.exists())
|
||||
self.assertTrue(catalog_csv.exists())
|
||||
with resolutions_csv.open(newline="", encoding="utf-8") as handle:
|
||||
resolution_rows = list(csv.DictReader(handle))
|
||||
with catalog_csv.open(newline="", encoding="utf-8") as handle:
|
||||
catalog_rows = list(csv.DictReader(handle))
|
||||
self.assertEqual("create", resolution_rows[0]["resolution_action"])
|
||||
self.assertEqual("approved", resolution_rows[0]["status"])
|
||||
self.assertEqual("ICE", catalog_rows[0]["canonical_name"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user