|
| 1 | +import asyncio |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | + |
| 6 | +from parseapi import AsyncParseAPI, ParseAPI, ParseAPIError |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.parametrize("async_client", [False, True]) |
| 10 | +@pytest.mark.parametrize("extra", [{}, {"lineage": None}, {"lineage": []}, {"lineage": ["Live horses", "Other horses"]}]) |
| 11 | +def test_tariff_search_preserves_parent_context_and_older_results(async_client, extra): |
| 12 | + body = {"q": "horses & ponies", "revision": "fixture", "lines": [{"hts": "0101.29.00.90", "description": "Other", "general": None, **extra, "future": True}]} |
| 13 | + |
| 14 | + def respond(request): |
| 15 | + assert request.url.path == "/tariff" |
| 16 | + assert dict(request.url.params) == {"q": "horses & ponies"} |
| 17 | + return httpx.Response(200, json=body) |
| 18 | + |
| 19 | + transport = httpx.MockTransport(respond) |
| 20 | + if async_client: |
| 21 | + async def run(): |
| 22 | + async with AsyncParseAPI("test", transport=transport) as client: |
| 23 | + assert await client.tariff.search("horses & ponies") == body |
| 24 | + asyncio.run(run()) |
| 25 | + else: |
| 26 | + with ParseAPI("test", transport=transport) as client: |
| 27 | + assert client.tariff.search("horses & ponies") == body |
| 28 | + |
| 29 | +@pytest.mark.parametrize("async_client", [False, True]) |
| 30 | +def test_tariff_edition_date_roundtrip(async_client): |
| 31 | + edition = "a" * 64 |
| 32 | + body = {"hts": "0101", "revision": "fixture", "edition": edition, "date": "2026-09-15", "deep": {"effective_rate": None, "reason": "future_reason", "measures": []}} |
| 33 | + seen = [] |
| 34 | + |
| 35 | + def respond(request): |
| 36 | + seen.append(dict(request.url.params)) |
| 37 | + return httpx.Response(200, json={**body, "date": request.url.params.get("date")}) |
| 38 | + |
| 39 | + transport = httpx.MockTransport(respond) |
| 40 | + if async_client: |
| 41 | + async def run(): |
| 42 | + async with AsyncParseAPI("test", transport=transport) as client: |
| 43 | + assert await client.tariff("0101", deep=True, origin="CA", edition=edition, date="2026-09-15") == body |
| 44 | + assert await client.tariff.search("horses", edition=edition, date="2026-09-15") == body |
| 45 | + await client.tariff("0101", edition=edition) |
| 46 | + asyncio.run(run()) |
| 47 | + else: |
| 48 | + with ParseAPI("test", transport=transport) as client: |
| 49 | + assert client.tariff("0101", deep=True, origin="CA", edition=edition, date="2026-09-15") == body |
| 50 | + assert client.tariff.search("horses", edition=edition, date="2026-09-15") == body |
| 51 | + client.tariff("0101", edition=edition) |
| 52 | + assert seen == [{"deep": "true", "origin": "CA", "edition": edition, "date": "2026-09-15"}, {"q": "horses", "edition": edition, "date": "2026-09-15"}, {"edition": edition}] |
| 53 | + |
| 54 | +@pytest.mark.parametrize("async_client", [False, True]) |
| 55 | +def test_tariff_rejects_ignored_selection(async_client): |
| 56 | + transport = httpx.MockTransport(lambda request: httpx.Response(200, json={"revision": "old"})) |
| 57 | + if async_client: |
| 58 | + async def run(): |
| 59 | + async with AsyncParseAPI("test", transport=transport) as client: |
| 60 | + with pytest.raises(Exception, match="did not confirm"): |
| 61 | + await client.tariff("0101", edition="a" * 64) |
| 62 | + with pytest.raises(Exception, match="did not confirm"): |
| 63 | + await client.tariff.search("horses", date="2026-09-15") |
| 64 | + asyncio.run(run()) |
| 65 | + else: |
| 66 | + with ParseAPI("test", transport=transport) as client: |
| 67 | + with pytest.raises(Exception, match="did not confirm"): |
| 68 | + client.tariff("0101", edition="a" * 64) |
| 69 | + with pytest.raises(Exception, match="did not confirm"): |
| 70 | + client.tariff.search("horses", date="2026-09-15") |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("async_client", [False, True]) |
| 74 | +@pytest.mark.parametrize("edition", ["legacy", "", "A" * 64, "a" * 64 + "\n"]) |
| 75 | +def test_tariff_rejects_invalid_returned_edition(async_client, edition): |
| 76 | + transport = httpx.MockTransport(lambda request: httpx.Response(200, json={"edition": edition, "date": "2026-09-15"})) |
| 77 | + if async_client: |
| 78 | + async def run(): |
| 79 | + async with AsyncParseAPI("test", transport=transport) as client: |
| 80 | + for operation, value in [(client.tariff, "0101"), (client.tariff.search, "horses")]: |
| 81 | + with pytest.raises(ParseAPIError) as error: |
| 82 | + await operation(value, date="2026-09-15") |
| 83 | + assert error.value.code == "tariff_selection_mismatch" |
| 84 | + assert error.value.status == 0 |
| 85 | + asyncio.run(run()) |
| 86 | + else: |
| 87 | + with ParseAPI("test", transport=transport) as client: |
| 88 | + for operation, value in [(client.tariff, "0101"), (client.tariff.search, "horses")]: |
| 89 | + with pytest.raises(ParseAPIError) as error: |
| 90 | + operation(value, date="2026-09-15") |
| 91 | + assert error.value.code == "tariff_selection_mismatch" |
| 92 | + assert error.value.status == 0 |
0 commit comments