Skip to content

Commit

Permalink
chore: add test on lazy join decorator (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbarreau authored Jan 6, 2025
1 parent 402f795 commit b6fa3c0
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def setUpClass(cls) -> None:
foreign_key_target="id",
type=FieldType.MANY_TO_ONE,
),
"editor_id": Column(column_type=PrimitiveType.NUMBER, type=FieldType.COLUMN),
"editor": ManyToOne(
foreign_collection="Person",
foreign_key="editor_id",
foreign_key_target="id",
type=FieldType.MANY_TO_ONE,
),
"title": Column(
column_type=PrimitiveType.STRING,
type=FieldType.COLUMN,
Expand Down Expand Up @@ -126,6 +133,30 @@ def test_should_join_when_projection_ask_for_multiple_fields_in_foreign_collecti
result,
)

def test_should_work_with_multiple_relations(self):
with patch.object(
self.collection_book,
"list",
new_callable=AsyncMock,
return_value=[{"id": 1, "author_id": 2, "editor_id": 3}, {"id": 2, "author_id": 5, "editor_id": 6}],
) as mock_list:
result = self.loop.run_until_complete(
self.decorated_book_collection.list(
self.mocked_caller,
PaginatedFilter({}),
Projection("id", "author:id", "editor:id"),
)
)
mock_list.assert_awaited_once_with(
self.mocked_caller, PaginatedFilter({}), Projection("id", "author_id", "editor_id")
)

# should contain author object, without author_id FK
self.assertEqual(
[{"id": 1, "author": {"id": 2}, "editor": {"id": 3}}, {"id": 2, "author": {"id": 5}, "editor": {"id": 6}}],
result,
)

def test_should_not_join_when_condition_tree_is_on_foreign_key_target(self):
with patch.object(
self.collection_book,
Expand Down

0 comments on commit b6fa3c0

Please sign in to comment.