Skip to content

Commit

Permalink
new tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbenav committed May 4, 2024
1 parent 8b0acfe commit c433c25
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 2 deletions.
27 changes: 26 additions & 1 deletion tests/sqlalchemy/crud/test_get_joined.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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."
55 changes: 55 additions & 0 deletions tests/sqlalchemy/crud/test_get_multi_joined.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
27 changes: 26 additions & 1 deletion tests/sqlmodel/crud/test_get_joined.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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."
55 changes: 55 additions & 0 deletions tests/sqlmodel/crud/test_get_multi_joined.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."

0 comments on commit c433c25

Please sign in to comment.