-
-
Notifications
You must be signed in to change notification settings - Fork 607
unit tests for Union type look-up during queries #4088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
unit tests for Union type look-up during queries #4088
Conversation
Reviewer's GuideAdds a comprehensive test suite for StrawberryUnion.get_type_resolver to validate correct union member resolution, is_type_of handling, error behavior, and prioritization of union types when looking up GraphQL types in the schema type map. File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey - I've left some high level feedback:
- Several tests duplicate the pattern of iterating over
schema.schema_converter.type_mapto locate the firstStrawberryUnion; consider extracting a small helper (e.g.get_union(schema, predicate)) to make these tests shorter and easier to read. - The
test_type_resolver_prioritizes_union_typestest relies on the generated GraphQL type name containing"AContainer", which is somewhat brittle; if possible, assert based on the union’s underlying Strawberry types (e.g. matchingContainer[A]andA) rather than on the specific name-mangling convention.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Several tests duplicate the pattern of iterating over `schema.schema_converter.type_map` to locate the first `StrawberryUnion`; consider extracting a small helper (e.g. `get_union(schema, predicate)`) to make these tests shorter and easier to read.
- The `test_type_resolver_prioritizes_union_types` test relies on the generated GraphQL type name containing `"AContainer"`, which is somewhat brittle; if possible, assert based on the union’s underlying Strawberry types (e.g. matching `Container[A]` and `A`) rather than on the specific name-mangling convention.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Greptile SummaryAdded comprehensive unit tests for
The tests use proper mocking with Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Test as Test Suite
participant Union as StrawberryUnion
participant Resolver as Type Resolver Function
participant TypeMap as Schema Type Map
participant GraphQL as GraphQL Union Type
Note over Test,GraphQL: Test Setup Phase
Test->>Union: Create union with type_annotations
Test->>TypeMap: Build schema with types A, B
Test->>Union: get_type_resolver(type_map)
Union->>Resolver: Create _resolve_union_type function
Union-->>Test: Return resolver function
Note over Test,GraphQL: Type Resolution Test
Test->>Resolver: Call with instance & mock info
Resolver->>Resolver: Check if root has_object_definition
alt Root has object definition
Resolver->>TypeMap: Iterate union types first
Resolver->>TypeMap: Check if type.is_implemented_by(root)
TypeMap-->>Resolver: Return matching type
Resolver->>GraphQL: Verify type in union.types
Resolver-->>Test: Return type name (e.g., "A")
else Root uses is_type_of
Resolver->>GraphQL: Iterate union.types
Resolver->>GraphQL: Call is_type_of(root, info)
alt is_type_of matches
GraphQL-->>Resolver: Return True
Resolver-->>Test: Return type name
else No is_type_of matches
Resolver-->>Test: Raise WrongReturnTypeForUnion
end
else Type not in union
Resolver-->>Test: Raise UnallowedReturnTypeForUnion
end
|
Greptile found no issues!From now on, if a review finishes and we haven't found any issues, we will not post anything, but you can confirm that we reviewed your changes in the status check section. This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR". |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4088 +/- ##
==========================================
- Coverage 94.22% 91.33% -2.89%
==========================================
Files 541 546 +5
Lines 35519 36937 +1418
Branches 1877 1925 +48
==========================================
+ Hits 33469 33738 +269
- Misses 1739 2906 +1167
+ Partials 311 293 -18 🚀 New features to boost your workflow:
|
bellini666
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small ask, and also, tests seems to be failing
| for name, concrete_type in schema.schema_converter.type_map.items(): | ||
| if isinstance(concrete_type.definition, StrawberryUnion): | ||
| # Check if this union name contains "AContainer" (indicating Container[A] | A) | ||
| # and check if A is in the union types | ||
| union_types = concrete_type.definition.types | ||
| has_a = A in union_types | ||
| if "AContainer" in name and has_a: | ||
| union_name = name | ||
| union_def = concrete_type.definition | ||
| break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this iteration? Since this is a test, we might be able to just hardcode what we are expecting, so it is easier to understand what we are doing
Same for similar for loops in this file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure will look into this
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
|
Description
Added unit test for issue #1468
Types of Changes
Issues Fixed or Closed by This PR #1468
Checklist
Summary by Sourcery
Add comprehensive tests for union type resolution to ensure StrawberryUnion.get_type_resolver correctly handles various return scenarios.
Tests: