Skip to content

feat(testing): Laravel-style fluent JSON assertions#126

Merged
bedus-creation merged 4 commits into
mainfrom
feat/fluent-json-assertions
Jun 15, 2026
Merged

feat(testing): Laravel-style fluent JSON assertions#126
bedus-creation merged 4 commits into
mainfrom
feat/fluent-json-assertions

Conversation

@bedus-creation

Copy link
Copy Markdown
Contributor

Summary

Adds a Laravel AssertableJson-style fluent assertion API for HTTP tests (TASK #107). HTTP test responses are now chainable and scope-aware.

response.assert_ok().assert_json(lambda json: (
    json.where("id", 1)
        .where("name", "Bedu")
        .where("active", lambda v: v is True)
        .has("profile", lambda p: p.where("email", "a@b.com").etc())
        .etc()
))

What's included

  • AssertableJson (fastapi/testing/assertable_json.py)
    • Matching: where, where_not, where_all, where_type (incl. "string|null" unions; bool is not integer)
    • Presence: has, has_all, has_any, missing, missing_all, count
    • has(key, callback) / has(key, length, callback) nested scoping
    • Traversal: first, each
    • Strict interaction model (_verify): un-asserted object keys fail unless .etc() is called — catches unexpected/leaked response keys
    • Dotted-key paths and callable predicate matchers; errors include the full key path
    • Phase-2 methods (where_contains, count_between, where_all_type) stubbed with NotImplementedError
  • TestResponse (fastapi/testing/test_response.py): thin httpx.Response wrapper adding assert_status, assert_ok, assert_created, assert_no_content, assert_json (incl. exact=). __getattr__ passthrough keeps existing tests green.
  • HttpTestCase get/post/put/patch/delete now return TestResponse.

Tests

  • New unit tests: happy path, predicate matcher, where_type, nested scoping, first/each, etc(), strict un-asserted-prop failure, TestResponse helpers (40 tests).
  • Full suite green: 1493 passed, 7 skipped. Ruff clean.

🤖 Generated with Claude Code

bedus-creation and others added 4 commits June 15, 2026 12:41
Add AssertableJson and TestResponse to fastapi.testing, giving HTTP
tests a chainable, scope-aware assertion API modeled on Laravel's
Illuminate\Testing\Fluent\AssertableJson.

- AssertableJson: where/where_not/where_all/where_type, has/has_all/
  has_any/missing/missing_all/count, first/each, etc(), with a strict
  interaction model (_verify) that fails on un-asserted object keys.
  Dotted-key paths and callable predicate matchers supported. Phase-2
  methods (where_contains/count_between/where_all_type) are stubbed.
- TestResponse: thin httpx.Response wrapper adding assert_status/
  assert_ok/assert_created/assert_no_content/assert_json, with
  __getattr__ passthrough so existing tests stay green.
- HttpTestCase.get/post/put/patch/delete now return TestResponse.
- Unit tests for happy path, predicate matcher, nested scoping, etc(),
  strict un-asserted-prop failure, and TestResponse helpers.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…paths

- assert_json_structure(structure) on TestResponse with '*' wildcard to
  apply a nested structure to every element of a list.
- Promote where_all_type from stub to a working method (asserts each
  {key: type} pair via where_type).
- Dotted keys now index into lists (e.g. has('teams.0', ...) and
  where('teams.0.name', ...)), enabling nested scoping over array items.
- Tests for structure (simple/wildcard/nested/leaf), where_all_type, and
  list-index scoping.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@bedus-creation bedus-creation merged commit e2773a4 into main Jun 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant