From c433c25e95d7fca043ff95f02384055a37b2748a Mon Sep 17 00:00:00 2001 From: Igor Magalhaes Date: Sat, 4 May 2024 16:09:10 -0300 Subject: [PATCH] new tests added --- tests/sqlalchemy/crud/test_get_joined.py | 27 ++++++++- .../sqlalchemy/crud/test_get_multi_joined.py | 55 +++++++++++++++++++ tests/sqlmodel/crud/test_get_joined.py | 27 ++++++++- tests/sqlmodel/crud/test_get_multi_joined.py | 55 +++++++++++++++++++ 4 files changed, 162 insertions(+), 2 deletions(-) diff --git a/tests/sqlalchemy/crud/test_get_joined.py b/tests/sqlalchemy/crud/test_get_joined.py index d2a2956..39afe23 100644 --- a/tests/sqlalchemy/crud/test_get_joined.py +++ b/tests/sqlalchemy/crud/test_get_joined.py @@ -468,7 +468,7 @@ async def test_get_joined_nest_joins(async_session, test_data, test_data_tier): @pytest.mark.asyncio -async def test_get_joined_no_prefix_provided(async_session, test_data, test_data_tier): +async def test_get_joined_nested_no_prefix_provided(async_session, test_data, test_data_tier): for tier_item in test_data_tier: async_session.add(TierModel(**tier_item)) await async_session.commit() @@ -491,3 +491,28 @@ async def test_get_joined_no_prefix_provided(async_session, test_data, test_data assert ( "name" in result[TierModel.__tablename__] ), f"Expected 'name' field inside nested '{TierModel.__tablename__}' dictionary." + + +@pytest.mark.asyncio +async def test_get_joined_no_prefix_no_nesting(async_session, test_data, test_data_tier): + for tier_item in test_data_tier: + async_session.add(TierModel(**tier_item)) + await async_session.commit() + + for user_item in test_data: + async_session.add(ModelTest(**user_item)) + await async_session.commit() + + crud = FastCRUD(ModelTest) + + result = await crud.get_joined( + db=async_session, + join_model=TierModel, + schema_to_select=CreateSchemaTest, + join_schema_to_select=TierSchemaTest, + ) + + assert result is not None, "Expected to retrieve a result from joined query." + assert "name" in result, "Expected 'name' field from the primary model in the result." + assert "tier_id" in result, "Expected 'tier_id' (foreign key) in the result." + assert "tier_name" not in result, "Field 'tier_name' should not exist unless specifically prefixed or nested." diff --git a/tests/sqlalchemy/crud/test_get_multi_joined.py b/tests/sqlalchemy/crud/test_get_multi_joined.py index d8d0317..24721c6 100644 --- a/tests/sqlalchemy/crud/test_get_multi_joined.py +++ b/tests/sqlalchemy/crud/test_get_multi_joined.py @@ -730,3 +730,58 @@ async def test_get_multi_joined_with_nesting(async_session, test_data, test_data assert ( "category_" not in item["category"] ), "No prefix should be present in the nested category keys" + + +@pytest.mark.asyncio +async def test_get_multi_joined_no_prefix_regular(async_session, test_data, test_data_tier): + for tier_item in test_data_tier: + async_session.add(TierModel(**tier_item)) + await async_session.commit() + + for user_item in test_data: + async_session.add(ModelTest(**user_item)) + await async_session.commit() + + crud = FastCRUD(ModelTest) + + result = await crud.get_multi_joined( + db=async_session, + join_model=TierModel, + schema_to_select=CreateSchemaTest, + join_schema_to_select=TierSchemaTest, + limit=10 + ) + + print(result) + assert result and result["data"], "Expected data in the result." + for item in result["data"]: + assert "name" in item, "Expected user name in each item." + assert "name_1" in item, "Expected tier name in each item without prefix." + + +@pytest.mark.asyncio +async def test_get_multi_joined_no_prefix_nested(async_session, test_data, test_data_tier): + for tier_item in test_data_tier: + async_session.add(TierModel(**tier_item)) + await async_session.commit() + + for user_item in test_data: + async_session.add(ModelTest(**user_item)) + await async_session.commit() + + crud = FastCRUD(ModelTest) + + result = await crud.get_multi_joined( + db=async_session, + join_model=TierModel, + schema_to_select=CreateSchemaTest, + join_schema_to_select=TierSchemaTest, + nest_joins=True, + limit=10 + ) + + assert result and result["data"], "Expected data in the result." + for item in result["data"]: + assert "name" in item, "Expected user name in each item." + assert TierModel.__tablename__ in item, f"Expected nested '{TierModel.__tablename__}' key in each item." + assert "name" in item[TierModel.__tablename__], f"Expected 'name' field inside nested '{TierModel.__tablename__}' dictionary." diff --git a/tests/sqlmodel/crud/test_get_joined.py b/tests/sqlmodel/crud/test_get_joined.py index 672a461..7d9e674 100644 --- a/tests/sqlmodel/crud/test_get_joined.py +++ b/tests/sqlmodel/crud/test_get_joined.py @@ -468,7 +468,7 @@ async def test_get_joined_nest_joins(async_session, test_data, test_data_tier): @pytest.mark.asyncio -async def test_get_joined_no_prefix_provided(async_session, test_data, test_data_tier): +async def test_get_joined_nested_no_prefix_provided(async_session, test_data, test_data_tier): for tier_item in test_data_tier: async_session.add(TierModel(**tier_item)) await async_session.commit() @@ -492,3 +492,28 @@ async def test_get_joined_no_prefix_provided(async_session, test_data, test_data assert ( "name" in result["tier"] ), "Expected 'name' field inside nested 'tier' dictionary." + + +@pytest.mark.asyncio +async def test_get_joined_no_prefix_no_nesting(async_session, test_data, test_data_tier): + for tier_item in test_data_tier: + async_session.add(TierModel(**tier_item)) + await async_session.commit() + + for user_item in test_data: + async_session.add(ModelTest(**user_item)) + await async_session.commit() + + crud = FastCRUD(ModelTest) + + result = await crud.get_joined( + db=async_session, + join_model=TierModel, + schema_to_select=CreateSchemaTest, + join_schema_to_select=TierSchemaTest, + ) + + assert result is not None, "Expected to retrieve a result from joined query." + assert "name" in result, "Expected 'name' field from the primary model in the result." + assert "tier_id" in result, "Expected 'tier_id' (foreign key) in the result." + assert "tier_name" not in result, "Field 'tier_name' should not exist unless specifically prefixed or nested." diff --git a/tests/sqlmodel/crud/test_get_multi_joined.py b/tests/sqlmodel/crud/test_get_multi_joined.py index ea21d0b..7e3ce83 100644 --- a/tests/sqlmodel/crud/test_get_multi_joined.py +++ b/tests/sqlmodel/crud/test_get_multi_joined.py @@ -730,3 +730,58 @@ async def test_get_multi_joined_with_nesting(async_session, test_data, test_data assert ( "category_" not in item["category"] ), "No prefix should be present in the nested category keys" + + +@pytest.mark.asyncio +async def test_get_multi_joined_no_prefix_regular(async_session, test_data, test_data_tier): + for tier_item in test_data_tier: + async_session.add(TierModel(**tier_item)) + await async_session.commit() + + for user_item in test_data: + async_session.add(ModelTest(**user_item)) + await async_session.commit() + + crud = FastCRUD(ModelTest) + + result = await crud.get_multi_joined( + db=async_session, + join_model=TierModel, + schema_to_select=CreateSchemaTest, + join_schema_to_select=TierSchemaTest, + limit=10 + ) + + print(result) + assert result and result["data"], "Expected data in the result." + for item in result["data"]: + assert "name" in item, "Expected user name in each item." + assert "name_1" in item, "Expected tier name in each item without prefix." + + +@pytest.mark.asyncio +async def test_get_multi_joined_no_prefix_nested(async_session, test_data, test_data_tier): + for tier_item in test_data_tier: + async_session.add(TierModel(**tier_item)) + await async_session.commit() + + for user_item in test_data: + async_session.add(ModelTest(**user_item)) + await async_session.commit() + + crud = FastCRUD(ModelTest) + + result = await crud.get_multi_joined( + db=async_session, + join_model=TierModel, + schema_to_select=CreateSchemaTest, + join_schema_to_select=TierSchemaTest, + nest_joins=True, + limit=10 + ) + + assert result and result["data"], "Expected data in the result." + for item in result["data"]: + assert "name" in item, "Expected user name in each item." + assert TierModel.__tablename__ in item, f"Expected nested '{TierModel.__tablename__}' key in each item." + assert "name" in item[TierModel.__tablename__], f"Expected 'name' field inside nested '{TierModel.__tablename__}' dictionary."