@@ -373,7 +373,7 @@ def get(self):
373
373
374
374
375
375
@pytest .fixture (scope = "module" )
376
- def query ():
376
+ def query (computer_model , person_model ):
377
377
def query_ (self , view_kwargs ):
378
378
if view_kwargs .get ('person_id' ) is not None :
379
379
return self .session .query (computer_model ).join (person_model ).filter_by (person_id = view_kwargs ['person_id' ])
@@ -641,6 +641,26 @@ def test_get_list_with_simple_filter(client, register_routes, person, person_2):
641
641
response = client .get ('/persons' + '?' + querystring , content_type = 'application/vnd.api+json' )
642
642
assert response .status_code == 200
643
643
644
+ def test_get_list_relationship_filter (client , register_routes , person_computers ,
645
+ computer_schema , person , person_2 , computer , session ):
646
+ """
647
+ Tests the ability to filter over a relationship using IDs, for example
648
+ `GET /comments?filter[post]=1`
649
+ Refer to the spec: https://jsonapi.org/recommendations/#filtering
650
+ """
651
+ # We have two people in the database, one of which owns a computer
652
+ person .computers .append (computer )
653
+ session .commit ()
654
+
655
+ querystring = urlencode ({
656
+ 'filter[owner]' : person .person_id
657
+ })
658
+ response = client .get ('/computers?' + querystring , content_type = 'application/vnd.api+json' )
659
+
660
+ # Check that the request worked, and returned the one computer we wanted
661
+ assert response .status_code == 200
662
+ assert len (response .json ['data' ]) == 1
663
+
644
664
def test_get_list_disable_pagination (client , register_routes ):
645
665
with client :
646
666
querystring = urlencode ({'page[size]' : 0 })
@@ -1804,3 +1824,4 @@ def test_api_resources(app, person_list):
1804
1824
def test_relationship_containing_hyphens (client , register_routes , person_computers , computer_schema , person ):
1805
1825
response = client .get ('/persons/{}/relationships/computers-owned' .format (person .person_id ), content_type = 'application/vnd.api+json' )
1806
1826
assert response .status_code == 200
1827
+
0 commit comments