@@ -373,7 +373,7 @@ def get(self):
373373
374374
375375@pytest .fixture (scope = "module" )
376- def query ():
376+ def query (computer_model , person_model ):
377377 def query_ (self , view_kwargs ):
378378 if view_kwargs .get ('person_id' ) is not None :
379379 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):
641641 response = client .get ('/persons' + '?' + querystring , content_type = 'application/vnd.api+json' )
642642 assert response .status_code == 200
643643
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+
644664def test_get_list_disable_pagination (client , register_routes ):
645665 with client :
646666 querystring = urlencode ({'page[size]' : 0 })
@@ -1804,3 +1824,4 @@ def test_api_resources(app, person_list):
18041824def test_relationship_containing_hyphens (client , register_routes , person_computers , computer_schema , person ):
18051825 response = client .get ('/persons/{}/relationships/computers-owned' .format (person .person_id ), content_type = 'application/vnd.api+json' )
18061826 assert response .status_code == 200
1827+
0 commit comments