125 lines
4.6 KiB
Python
125 lines
4.6 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
import build_observed_products
|
|
import build_review_queue
|
|
from layer_helpers import write_csv_rows
|
|
|
|
|
|
class ReviewQueueTests(unittest.TestCase):
|
|
def test_build_review_queue_preserves_existing_status(self):
|
|
observed_rows = [
|
|
{
|
|
"observed_product_id": "gobs_1",
|
|
"retailer": "giant",
|
|
"representative_upc": "111",
|
|
"representative_image_url": "",
|
|
"representative_name_norm": "GALA APPLE",
|
|
"times_seen": "2",
|
|
"distinct_item_names_count": "2",
|
|
"distinct_upcs_count": "1",
|
|
"is_fee": "false",
|
|
}
|
|
]
|
|
item_rows = [
|
|
{
|
|
"observed_product_id": "gobs_1",
|
|
"item_name": "SB GALA APPLE 5LB",
|
|
"item_name_norm": "GALA APPLE",
|
|
"line_total": "7.99",
|
|
},
|
|
{
|
|
"observed_product_id": "gobs_1",
|
|
"item_name": "SB GALA APPLE 5 LB",
|
|
"item_name_norm": "GALA APPLE",
|
|
"line_total": "8.49",
|
|
},
|
|
]
|
|
existing = {
|
|
build_review_queue.stable_id("rvw", "gobs_1|missing_image"): {
|
|
"status": "approved",
|
|
"resolution_notes": "looked fine",
|
|
"created_at": "2026-03-15",
|
|
}
|
|
}
|
|
|
|
queue = build_review_queue.build_review_queue(
|
|
observed_rows, item_rows, existing, "2026-03-16"
|
|
)
|
|
|
|
self.assertEqual(2, len(queue))
|
|
missing_image = [row for row in queue if row["reason_code"] == "missing_image"][0]
|
|
self.assertEqual("approved", missing_image["status"])
|
|
self.assertEqual("looked fine", missing_image["resolution_notes"])
|
|
|
|
def test_review_queue_main_writes_output(self):
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
observed_path = Path(tmpdir) / "products_observed.csv"
|
|
items_path = Path(tmpdir) / "items_enriched.csv"
|
|
output_path = Path(tmpdir) / "review_queue.csv"
|
|
|
|
observed_rows = [
|
|
{
|
|
"observed_product_id": "gobs_1",
|
|
"retailer": "giant",
|
|
"observed_key": "giant|upc=111|name=GALA APPLE",
|
|
"representative_upc": "111",
|
|
"representative_item_name": "SB GALA APPLE 5LB",
|
|
"representative_name_norm": "GALA APPLE",
|
|
"representative_brand": "SB",
|
|
"representative_variant": "",
|
|
"representative_size_value": "5",
|
|
"representative_size_unit": "lb",
|
|
"representative_pack_qty": "",
|
|
"representative_measure_type": "weight",
|
|
"representative_image_url": "",
|
|
"is_store_brand": "true",
|
|
"is_fee": "false",
|
|
"first_seen_date": "2026-01-01",
|
|
"last_seen_date": "2026-01-10",
|
|
"times_seen": "2",
|
|
"example_order_id": "1",
|
|
"example_item_name": "SB GALA APPLE 5LB",
|
|
"raw_name_examples": "SB GALA APPLE 5LB | SB GALA APPLE 5 LB",
|
|
"normalized_name_examples": "GALA APPLE",
|
|
"example_prices": "7.99 | 8.49",
|
|
"distinct_item_names_count": "2",
|
|
"distinct_upcs_count": "1",
|
|
}
|
|
]
|
|
item_rows = [
|
|
{
|
|
"retailer": "giant",
|
|
"order_id": "1",
|
|
"line_no": "1",
|
|
"item_name": "SB GALA APPLE 5LB",
|
|
"item_name_norm": "GALA APPLE",
|
|
"upc": "111",
|
|
"size_value": "5",
|
|
"size_unit": "lb",
|
|
"pack_qty": "",
|
|
"measure_type": "weight",
|
|
"is_store_brand": "true",
|
|
"is_fee": "false",
|
|
"line_total": "7.99",
|
|
}
|
|
]
|
|
|
|
write_csv_rows(
|
|
observed_path, observed_rows, build_observed_products.OUTPUT_FIELDS
|
|
)
|
|
write_csv_rows(items_path, item_rows, list(item_rows[0].keys()))
|
|
|
|
build_review_queue.main.callback(
|
|
observed_csv=str(observed_path),
|
|
items_enriched_csv=str(items_path),
|
|
output_csv=str(output_path),
|
|
)
|
|
|
|
self.assertTrue(output_path.exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|