77from fastapi import APIRouter , FastAPI , status
88from httpx import AsyncClient
99from pytest import mark , param # noqa PT013
10+ from sqlalchemy .ext .asyncio import AsyncSession
1011
1112from fastapi_jsonapi import RoutersJSONAPI
1213from fastapi_jsonapi .views .view_base import ViewBase
1314from tests .fixtures .app import build_app_plain
14- from tests .fixtures .entities import create_user
15+ from tests .fixtures .entities import build_workplace , create_user
1516from tests .fixtures .views import DetailViewBaseGeneric , ListViewBaseGeneric
1617from tests .misc .utils import fake
1718from tests .models import (
@@ -1799,6 +1800,90 @@ async def test_composite_filter_with_mutually_exclusive_conditions(
17991800 "meta" : {"count" : 0 , "totalPages" : 1 },
18001801 }
18011802
1803+ async def test_filter_with_nested_conditions (
1804+ self ,
1805+ app : FastAPI ,
1806+ async_session : AsyncSession ,
1807+ client : AsyncClient ,
1808+ ):
1809+ workplace_name = "Common workplace name"
1810+
1811+ workplace_1 , workplace_2 , workplace_3 , workplace_4 = (
1812+ await build_workplace (async_session , name = workplace_name ),
1813+ await build_workplace (async_session , name = workplace_name ),
1814+ await build_workplace (async_session , name = workplace_name ),
1815+ await build_workplace (async_session , name = workplace_name ),
1816+ )
1817+
1818+ user_1 , user_2 , _ , user_4 = (
1819+ await create_user (async_session , name = "John Doe" , age = 20 , workplace = workplace_1 ),
1820+ await create_user (async_session , name = "Jane Doe" , age = 25 , workplace = workplace_2 ),
1821+ await create_user (async_session , name = "Jonny Doe" , age = 30 , workplace = workplace_3 ),
1822+ await create_user (async_session , name = "Mary Jane" , age = 21 , workplace = workplace_4 ),
1823+ )
1824+
1825+ params = {
1826+ "filter" : dumps (
1827+ [
1828+ {
1829+ "name" : "workplace.name" ,
1830+ "op" : "eq" ,
1831+ "val" : workplace_name ,
1832+ },
1833+ {
1834+ "or" : [
1835+ {
1836+ "not" : {
1837+ "name" : "name" ,
1838+ "op" : "ne" ,
1839+ "val" : "Mary Jane" ,
1840+ },
1841+ },
1842+ {
1843+ "and" : [
1844+ {
1845+ "name" : "name" ,
1846+ "op" : "like" ,
1847+ "val" : "%Doe%" ,
1848+ },
1849+ {
1850+ "name" : "age" ,
1851+ "op" : "lt" ,
1852+ "val" : 30 ,
1853+ },
1854+ ],
1855+ },
1856+ ],
1857+ },
1858+ ],
1859+ ),
1860+ }
1861+
1862+ url = app .url_path_for ("get_user_list" )
1863+ res = await client .get (url , params = params )
1864+ assert res .status_code == status .HTTP_200_OK , res .text
1865+ assert res .json () == {
1866+ "data" : [
1867+ {
1868+ "attributes" : UserAttributesBaseSchema .from_orm (user_1 ),
1869+ "id" : str (user_1 .id ),
1870+ "type" : "user" ,
1871+ },
1872+ {
1873+ "attributes" : UserAttributesBaseSchema .from_orm (user_2 ),
1874+ "id" : str (user_2 .id ),
1875+ "type" : "user" ,
1876+ },
1877+ {
1878+ "attributes" : UserAttributesBaseSchema .from_orm (user_4 ),
1879+ "id" : str (user_4 .id ),
1880+ "type" : "user" ,
1881+ },
1882+ ],
1883+ "jsonapi" : {"version" : "1.0" },
1884+ "meta" : {"count" : 3 , "totalPages" : 1 },
1885+ }
1886+
18021887
18031888ASCENDING = ""
18041889DESCENDING = "-"
@@ -1819,7 +1904,7 @@ async def test_sort(
18191904 self ,
18201905 app : FastAPI ,
18211906 client : AsyncClient ,
1822- async_session ,
1907+ async_session : AsyncSession ,
18231908 order : str ,
18241909 ):
18251910 user_1 , _ , user_3 = (
0 commit comments