|
| 1 | +import pytest |
| 2 | +from fastapi import FastAPI |
| 3 | +from fastapi.testclient import TestClient |
| 4 | + |
| 5 | +from fastcrud import EndpointCreator |
| 6 | +from fastcrud.endpoint.helper import FilterConfig |
| 7 | +from tests.sqlalchemy.conftest import ModelTest, CreateSchemaTest, UpdateSchemaTest |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def app(async_session): |
| 12 | + app = FastAPI() |
| 13 | + endpoint_creator = EndpointCreator( |
| 14 | + session=lambda: async_session, |
| 15 | + model=ModelTest, |
| 16 | + create_schema=CreateSchemaTest, |
| 17 | + update_schema=UpdateSchemaTest, |
| 18 | + path="/test", |
| 19 | + filter_config=FilterConfig(id=None, name=None, tier_id=None), |
| 20 | + ) |
| 21 | + endpoint_creator.add_routes_to_router() |
| 22 | + app.include_router(endpoint_creator.router) |
| 23 | + return app |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def client(app): |
| 28 | + return TestClient(app) |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.asyncio |
| 32 | +async def test_get_multi_with_sort_ascending(client, async_session, test_data): |
| 33 | + # Add test data |
| 34 | + for item in test_data: |
| 35 | + async_session.add(ModelTest(**item)) |
| 36 | + await async_session.commit() |
| 37 | + |
| 38 | + # Test ascending sort by name |
| 39 | + response = client.get("/test/?sort=name") |
| 40 | + print("Response:", response.status_code, response.json()) |
| 41 | + assert response.status_code == 200 |
| 42 | + |
| 43 | + data = response.json()["data"] |
| 44 | + sorted_data = sorted(test_data, key=lambda x: x["name"]) |
| 45 | + |
| 46 | + assert len(data) == len(sorted_data) |
| 47 | + assert [item["name"] for item in data] == [item["name"] for item in sorted_data] |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.asyncio |
| 51 | +async def test_get_multi_with_sort_descending(client, async_session, test_data): |
| 52 | + # Add test data |
| 53 | + for item in test_data: |
| 54 | + async_session.add(ModelTest(**item)) |
| 55 | + await async_session.commit() |
| 56 | + |
| 57 | + # Test descending sort by name |
| 58 | + response = client.get("/test/?sort=-name") |
| 59 | + assert response.status_code == 200 |
| 60 | + |
| 61 | + data = response.json()["data"] |
| 62 | + sorted_data = sorted(test_data, key=lambda x: x["name"], reverse=True) |
| 63 | + |
| 64 | + assert len(data) == len(sorted_data) |
| 65 | + assert [item["name"] for item in data] == [item["name"] for item in sorted_data] |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.asyncio |
| 69 | +async def test_get_multi_with_multiple_sort_fields(client, async_session, test_data): |
| 70 | + # Add test data |
| 71 | + for item in test_data: |
| 72 | + async_session.add(ModelTest(**item)) |
| 73 | + await async_session.commit() |
| 74 | + |
| 75 | + # Test multiple sort fields (tier_id ascending, name descending) |
| 76 | + response = client.get("/test/?sort=tier_id,-name") |
| 77 | + assert response.status_code == 200 |
| 78 | + |
| 79 | + data = response.json()["data"] |
| 80 | + |
| 81 | + # Sort first by tier_id (ascending) then by name (descending) |
| 82 | + sorted_data = sorted(test_data, key=lambda x: (x["tier_id"], -ord(x["name"][0]))) |
| 83 | + |
| 84 | + assert len(data) == len(sorted_data) |
| 85 | + |
| 86 | + # Group by tier_id and check that names are in descending order within each group |
| 87 | + tier_groups = {} |
| 88 | + for item in data: |
| 89 | + tier_id = item["tier_id"] |
| 90 | + if tier_id not in tier_groups: |
| 91 | + tier_groups[tier_id] = [] |
| 92 | + tier_groups[tier_id].append(item["name"]) |
| 93 | + |
| 94 | + for tier_id, names in tier_groups.items(): |
| 95 | + if len(names) > 1: |
| 96 | + for i in range(len(names) - 1): |
| 97 | + assert names[i] >= names[i + 1], f"Names in tier {tier_id} are not in descending order" |
| 98 | + |
| 99 | + |
| 100 | +@pytest.mark.asyncio |
| 101 | +async def test_get_multi_with_sort_and_pagination(client, async_session, test_data): |
| 102 | + # Add test data |
| 103 | + for item in test_data: |
| 104 | + async_session.add(ModelTest(**item)) |
| 105 | + await async_session.commit() |
| 106 | + |
| 107 | + # Test sorting with pagination |
| 108 | + response = client.get("/test/?sort=name&page=1&itemsPerPage=5") |
| 109 | + assert response.status_code == 200 |
| 110 | + |
| 111 | + data = response.json()["data"] |
| 112 | + sorted_data = sorted(test_data, key=lambda x: x["name"])[:5] |
| 113 | + |
| 114 | + assert len(data) <= 5 |
| 115 | + assert [item["name"] for item in data] == [item["name"] for item in sorted_data] |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.asyncio |
| 119 | +async def test_get_multi_with_sort_and_filtering(client, async_session, test_data): |
| 120 | + # Add test data |
| 121 | + for item in test_data: |
| 122 | + async_session.add(ModelTest(**item)) |
| 123 | + await async_session.commit() |
| 124 | + |
| 125 | + # Test sorting with filtering |
| 126 | + tier_id_to_filter = 1 |
| 127 | + response = client.get(f"/test/?sort=name&tier_id={tier_id_to_filter}") |
| 128 | + print("Response:", response.status_code, response.json()) |
| 129 | + assert response.status_code == 200 |
| 130 | + |
| 131 | + data = response.json()["data"] |
| 132 | + filtered_data = [item for item in test_data if item["tier_id"] == tier_id_to_filter] |
| 133 | + sorted_filtered_data = sorted(filtered_data, key=lambda x: x["name"]) |
| 134 | + |
| 135 | + assert len(data) == len(sorted_filtered_data) |
| 136 | + assert all(item["tier_id"] == tier_id_to_filter for item in data) |
| 137 | + assert [item["name"] for item in data] == [item["name"] for item in sorted_filtered_data] |
0 commit comments