BuyingAgent/backend/tests/unit/test_discovery_urlgen.py

109 lines
3.2 KiB
Python
Raw Normal View History

from app.discovery.mapping import verify_roundtrip
from app.discovery.urlgen import generate_search_url
CRITERIA = [
{"id": "ram", "criterion": "ram_gb", "operator": ">=", "value": 24, "mandatory": True},
{"id": "price", "criterion": "price_eur", "operator": "<=", "value": 2000, "mandatory": True},
{"id": "weight", "criterion": "weight_kg", "operator": "<=", "value": 1.6, "mandatory": True},
]
MAPPINGS = [
{
"criterion_key": "ram_gb",
"external_filter_key": "17023",
"source_value": "ab 24GB",
"encoding": {"url_param": "xf"},
"status": "VERIFIED",
},
{
"criterion_key": "price_eur",
"external_filter_key": "price",
"source_value": None,
"encoding": {"url_param": "maxp"},
"status": "VERIFIED",
},
]
def test_ram_and_price_url_is_reproducible():
first, internal = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA,
mappings=MAPPINGS,
)
second, _ = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA,
mappings=MAPPINGS,
)
assert first == second
assert "xf=17023_ab+24GB" in first
assert "maxp=2000" in first
assert internal == ["weight_kg"]
def test_internal_only_does_not_change_url():
without_weight, _ = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA[:2],
mappings=MAPPINGS,
)
with_weight, internal = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA,
mappings=MAPPINGS,
)
assert without_weight == with_weight
assert "weight_kg" in internal
def test_broken_mapping_is_not_used():
url, internal = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=CRITERIA,
mappings=[{**MAPPINGS[0], "status": "BROKEN"}, MAPPINGS[1]],
)
assert "17023" not in url
assert "ram_gb" in internal
def test_multiple_xf_filters_join_with_tilde():
url, _ = generate_search_url(
category_url="https://geizhals.de/?cat=nb",
criteria_items=[
{"criterion": "ram_gb", "operator": ">=", "value": 16},
{"criterion": "gpu", "operator": ">=", "value": 8},
],
mappings=[
{
"criterion_key": "ram_gb",
"external_filter_key": "12",
"source_value": "16384",
"encoding": {"url_param": "xf"},
"status": "VERIFIED",
},
{
"criterion_key": "gpu",
"external_filter_key": "7436",
"source_value": "8192",
"encoding": {"url_param": "xf"},
"status": "VERIFIED",
},
],
)
assert "xf=12_16384~7436_8192" in url or "xf=7436_8192~12_16384" in url
def test_roundtrip_verifies_xf_and_maxp():
assert verify_roundtrip(
category_url="https://geizhals.de/?cat=nb",
criterion=CRITERIA[0],
mapping=MAPPINGS[0],
)
assert verify_roundtrip(
category_url="https://geizhals.de/?cat=nb",
criterion=CRITERIA[1],
mapping=MAPPINGS[1],
)