From 2b4e77641535fb7801ce4f118002716ff178ef42 Mon Sep 17 00:00:00 2001 From: Julien Barreau Date: Mon, 6 Jan 2025 17:25:46 +0100 Subject: [PATCH] chore: add test on lazy join decorator --- .../lazy_join/test_lazy_join_decorator.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/datasource_toolkit/tests/decorators/lazy_join/test_lazy_join_decorator.py b/src/datasource_toolkit/tests/decorators/lazy_join/test_lazy_join_decorator.py index 62b582289..d3c578362 100644 --- a/src/datasource_toolkit/tests/decorators/lazy_join/test_lazy_join_decorator.py +++ b/src/datasource_toolkit/tests/decorators/lazy_join/test_lazy_join_decorator.py @@ -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, @@ -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,