From fdd12b7d4b10cde8582ab4acffc3f8b29274001f Mon Sep 17 00:00:00 2001 From: Dmitry Date: Wed, 6 Dec 2023 12:46:01 +0300 Subject: [PATCH] test filter by null values --- tests/conftest.py | 1 + tests/fixtures/entities.py | 13 +++++++++ tests/test_api/test_api_sqla_with_includes.py | 28 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 45ecccfa..5c18aa3a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -43,6 +43,7 @@ user_2_comment_for_one_u1_post, user_2_posts, user_3, + user_4, workplace_1, workplace_2, ) diff --git a/tests/fixtures/entities.py b/tests/fixtures/entities.py index 23eb3547..c70e2bf2 100644 --- a/tests/fixtures/entities.py +++ b/tests/fixtures/entities.py @@ -70,6 +70,19 @@ async def user_3(async_session: AsyncSession): await async_session.commit() +@async_fixture() +async def user_4(async_session: AsyncSession): + user = build_user( + email=None + ) + async_session.add(user) + await async_session.commit() + await async_session.refresh(user) + yield user + await async_session.delete(user) + await async_session.commit() + + async def build_user_bio(async_session: AsyncSession, user: User, **fields): bio = UserBio(user=user, **fields) async_session.add(bio) diff --git a/tests/test_api/test_api_sqla_with_includes.py b/tests/test_api/test_api_sqla_with_includes.py index b245132b..5da5f944 100644 --- a/tests/test_api/test_api_sqla_with_includes.py +++ b/tests/test_api/test_api_sqla_with_includes.py @@ -1751,6 +1751,34 @@ async def test_field_filters_with_values_from_different_models( "meta": {"count": 0, "totalPages": 1}, } + @mark.parametrize("filter_dict, expected_email_is_null", [ + param([{"name": "email", "op": "is_", "val": None}], True), + param([{"name": "email", "op": "isnot", "val": None}], False) + ]) + async def test_filter_by_null( + self, + app: FastAPI, + client: AsyncClient, + user_1: User, + user_4: User, + filter_dict, + expected_email_is_null + ): + assert user_1.email is not None + assert user_4.email is None + + url = app.url_path_for("get_user_list") + params = {"filter": dumps(filter_dict)} + + response = await client.get(url, params=params) + assert response.status_code == 200, response.text + + data = response.json() + + assert len(data['data']) == 1 + assert (data['data'][0]['attributes']['email'] is None) == expected_email_is_null + + async def test_composite_filter_by_one_field( self, app: FastAPI,