Skip to content

Commit b56891f

Browse files
Dmitrymahenzon
authored andcommitted
test filter by null values
1 parent 238cf7c commit b56891f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
user_2_comment_for_one_u1_post,
4444
user_2_posts,
4545
user_3,
46+
user_4,
4647
workplace_1,
4748
workplace_2,
4849
)

tests/fixtures/entities.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ async def user_3(async_session: AsyncSession):
7070
await async_session.commit()
7171

7272

73+
@async_fixture()
74+
async def user_4(async_session: AsyncSession):
75+
user = build_user(
76+
email=None
77+
)
78+
async_session.add(user)
79+
await async_session.commit()
80+
await async_session.refresh(user)
81+
yield user
82+
await async_session.delete(user)
83+
await async_session.commit()
84+
85+
7386
async def build_user_bio(async_session: AsyncSession, user: User, **fields):
7487
bio = UserBio(user=user, **fields)
7588
async_session.add(bio)

tests/test_api/test_api_sqla_with_includes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,34 @@ async def test_field_filters_with_values_from_different_models(
20252025
"meta": {"count": 0, "totalPages": 1},
20262026
}
20272027

2028+
@mark.parametrize("filter_dict, expected_email_is_null", [
2029+
param([{"name": "email", "op": "is_", "val": None}], True),
2030+
param([{"name": "email", "op": "isnot", "val": None}], False)
2031+
])
2032+
async def test_filter_by_null(
2033+
self,
2034+
app: FastAPI,
2035+
client: AsyncClient,
2036+
user_1: User,
2037+
user_4: User,
2038+
filter_dict,
2039+
expected_email_is_null
2040+
):
2041+
assert user_1.email is not None
2042+
assert user_4.email is None
2043+
2044+
url = app.url_path_for("get_user_list")
2045+
params = {"filter": dumps(filter_dict)}
2046+
2047+
response = await client.get(url, params=params)
2048+
assert response.status_code == 200, response.text
2049+
2050+
data = response.json()
2051+
2052+
assert len(data['data']) == 1
2053+
assert (data['data'][0]['attributes']['email'] is None) == expected_email_is_null
2054+
2055+
20282056
async def test_composite_filter_by_one_field(
20292057
self,
20302058
app: FastAPI,

0 commit comments

Comments
 (0)