Skip to content

Commit ea37a13

Browse files
karlwaldmanclaude
andcommitted
test(fuel-surcharge): pin local refusals to ValidationError(field, value, status_code=None) (#101)
Review follow-up on #144. The carrier, service_level, page and per_page guards already raised ValidationError with status_code=None (the _url._reject convention); the tests only asserted that for carrier on latest(). Every method that takes a carrier or service level, and both history routes' pagination, now assert the exact type, status_code None, is_client_error False, field, value and zero transport calls, on sync and async. Red-capability: swapping the slug guard to a raw ValueError fails 86 of 86 selected refusal tests. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015ao5paex73xXvuM424Libo
1 parent 0f5d158 commit ea37a13

1 file changed

Lines changed: 49 additions & 14 deletions

File tree

‎tests/unit/test_fuel_surcharge_resource.py‎

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -727,32 +727,67 @@ def test_malformed_error_keeps_the_raw_body(transport, call):
727727
# --- local validation happens before the network ----------------------------------
728728

729729

730+
def _assert_local_refusal(info, field, value, transport):
731+
"""A local refusal is a ValidationError with no HTTP status: nothing was sent."""
732+
error = info.value
733+
assert type(error) is ValidationError
734+
assert error.status_code is None
735+
assert error.is_client_error is False
736+
assert error.field == field
737+
assert error.value == value
738+
assert transport.calls == []
739+
740+
730741
@pytest.mark.parametrize("carrier", ["", " ", "odfl/latest", "odfl?x=1", "../prices", None, 7])
731-
def test_invalid_carrier_is_refused_locally(transport, call, carrier):
742+
@pytest.mark.parametrize(
743+
"method",
744+
[
745+
lambda c, v: c.fuel_surcharge.latest(v),
746+
lambda c, v: c.fuel_surcharge.history(v),
747+
lambda c, v: c.fuel_surcharge.parcel_latest(v),
748+
lambda c, v: c.fuel_surcharge.parcel_latest_rate(v, "ground"),
749+
lambda c, v: c.fuel_surcharge.parcel_history(v, "ground"),
750+
],
751+
ids=["latest", "history", "parcel_latest", "parcel_latest_rate", "parcel_history"],
752+
)
753+
def test_invalid_carrier_is_refused_locally(transport, call, carrier, method):
732754
with pytest.raises(ValidationError) as info:
733-
call(lambda c: c.fuel_surcharge.latest(carrier))
755+
call(lambda c: method(c, carrier))
734756

735-
assert info.value.status_code is None
736-
assert transport.calls == []
757+
_assert_local_refusal(info, "carrier", carrier, transport)
737758

738759

739760
@pytest.mark.parametrize("service_level", ["", " ", "ground/x", None])
740-
def test_invalid_service_level_is_refused_locally(transport, call, service_level):
741-
with pytest.raises(ValidationError):
742-
call(lambda c: c.fuel_surcharge.parcel_history("ups", service_level))
761+
@pytest.mark.parametrize(
762+
"method",
763+
[
764+
lambda c, v: c.fuel_surcharge.parcel_latest_rate("ups", v),
765+
lambda c, v: c.fuel_surcharge.parcel_history("ups", v),
766+
],
767+
ids=["parcel_latest_rate", "parcel_history"],
768+
)
769+
def test_invalid_service_level_is_refused_locally(transport, call, service_level, method):
770+
with pytest.raises(ValidationError) as info:
771+
call(lambda c: method(c, service_level))
743772

744-
assert transport.calls == []
773+
_assert_local_refusal(info, "service_level", service_level, transport)
745774

746775

747776
@pytest.mark.parametrize(
748-
"kwargs",
749-
[{"page": 0}, {"page": -1}, {"per_page": 0}, {"per_page": 101}, {"per_page": True}, {"page": "2"}],
777+
"field,value",
778+
[("page", 0), ("page", -1), ("page", "2"), ("per_page", 0), ("per_page", 101), ("per_page", True)],
750779
)
751-
def test_out_of_range_pagination_is_refused_not_silently_clamped(transport, call, kwargs):
752-
with pytest.raises(ValidationError):
753-
call(lambda c: c.fuel_surcharge.history("odfl", **kwargs))
780+
@pytest.mark.parametrize("parcel", [False, True], ids=["ltl", "parcel"])
781+
def test_out_of_range_pagination_is_refused_not_silently_clamped(transport, call, field, value, parcel):
782+
kwargs = {field: value}
783+
if parcel:
784+
fn = lambda c: c.fuel_surcharge.parcel_history("ups", "ground", **kwargs) # noqa: E731
785+
else:
786+
fn = lambda c: c.fuel_surcharge.history("odfl", **kwargs) # noqa: E731
787+
with pytest.raises(ValidationError) as info:
788+
call(fn)
754789

755-
assert transport.calls == []
790+
_assert_local_refusal(info, field, value, transport)
756791

757792

758793
def test_per_page_100_is_the_accepted_maximum(transport, call):

0 commit comments

Comments
 (0)