@@ -158,7 +158,7 @@ def engine(
158
158
person_tag_model , person_single_tag_model , person_model ,
159
159
computer_model , string_json_attribute_person_model ,
160
160
address_model
161
- ):
161
+ ):
162
162
engine = create_engine ("sqlite:///:memory:" )
163
163
person_tag_model .metadata .create_all (engine )
164
164
person_single_tag_model .metadata .create_all (engine )
@@ -208,6 +208,17 @@ def computer(session, computer_model):
208
208
session_ .commit ()
209
209
210
210
211
+ @pytest .fixture ()
212
+ def computer_2 (session , computer_model ):
213
+ computer_ = computer_model (serial = "2" )
214
+ session_ = session
215
+ session_ .add (computer_ )
216
+ session_ .commit ()
217
+ yield computer_
218
+ session_ .delete (computer_ )
219
+ session_ .commit ()
220
+
221
+
211
222
@pytest .fixture ()
212
223
def address (session , address_model ):
213
224
address_ = address_model (state = 'NYC' )
@@ -400,7 +411,7 @@ class PersonList(ResourceList):
400
411
data_layer = {
401
412
"model" : person_model ,
402
413
"session" : session ,
403
- "mzthods " : {"before_create_object" : before_create_object },
414
+ "methods " : {"before_create_object" : before_create_object },
404
415
}
405
416
get_decorators = [dummy_decorator ]
406
417
post_decorators = [dummy_decorator ]
@@ -410,6 +421,43 @@ class PersonList(ResourceList):
410
421
yield PersonList
411
422
412
423
424
+ @pytest .fixture (scope = "module" )
425
+ def custom_query_string_manager ():
426
+ class QS (QSManager ):
427
+ def _simple_filters (self , dict_ ):
428
+ return [{"name" : key , "op" : "in" if isinstance (value , list ) else "eq" , "val" : value }
429
+ for (key , value ) in dict_ .items ()]
430
+
431
+ yield QS
432
+
433
+
434
+ @pytest .fixture (scope = "module" )
435
+ def person_list_custom_qs_manager (session , person_model , person_schema , custom_query_string_manager ):
436
+ class PersonList (ResourceList ):
437
+ schema = person_schema
438
+ data_layer = {
439
+ "model" : person_model ,
440
+ "session" : session ,
441
+ }
442
+ get_schema_kwargs = dict ()
443
+ qs_manager_class = custom_query_string_manager
444
+
445
+ yield PersonList
446
+
447
+
448
+ @pytest .fixture (scope = "module" )
449
+ def person_list_2 (session , person_model , person_schema ):
450
+ class PersonList (ResourceList ):
451
+ schema = person_schema
452
+ data_layer = {
453
+ "model" : person_model ,
454
+ "session" : session ,
455
+ }
456
+ get_schema_kwargs = dict ()
457
+
458
+ yield PersonList
459
+
460
+
413
461
@pytest .fixture (scope = "module" )
414
462
def person_detail (session , person_model , dummy_decorator , person_schema , before_update_object , before_delete_object ):
415
463
class PersonDetail (ResourceDetail ):
@@ -507,7 +555,7 @@ def fixed_count_for_collection_count():
507
555
508
556
@pytest .fixture (scope = "module" )
509
557
def computer_list_resource_with_disable_collection_count (
510
- session , computer_model , computer_schema , fixed_count_for_collection_count
558
+ session , computer_model , computer_schema , fixed_count_for_collection_count
511
559
):
512
560
class ComputerList (ResourceList ):
513
561
disable_collection_count = True , fixed_count_for_collection_count
@@ -538,7 +586,7 @@ class ComputerOwnerRelationship(ResourceRelationship):
538
586
539
587
@pytest .fixture (scope = "module" )
540
588
def string_json_attribute_person_detail (
541
- session , string_json_attribute_person_model , string_json_attribute_person_schema
589
+ session , string_json_attribute_person_model , string_json_attribute_person_schema
542
590
):
543
591
class StringJsonAttributePersonDetail (ResourceDetail ):
544
592
schema = string_json_attribute_person_schema
@@ -562,27 +610,42 @@ def api_blueprint(client):
562
610
yield bp
563
611
564
612
613
+ @pytest .fixture (scope = "module" )
614
+ def register_routes_custom_qs (
615
+ client ,
616
+ app ,
617
+ api_blueprint ,
618
+ custom_query_string_manager ,
619
+ person_list_2 ,
620
+ ):
621
+ api = Api (blueprint = api_blueprint , qs_manager_class = custom_query_string_manager )
622
+ api .route (person_list_2 , "person_list_qs" , "/qs/persons" )
623
+ api .init_app (app )
624
+
625
+
565
626
@pytest .fixture (scope = "module" )
566
627
def register_routes (
567
- client ,
568
- app ,
569
- api_blueprint ,
570
- person_list ,
571
- person_detail ,
572
- person_computers ,
573
- person_list_raise_jsonapiexception ,
574
- person_list_raise_exception ,
575
- person_list_response ,
576
- person_list_without_schema ,
577
- computer_list ,
578
- computer_detail ,
579
- computer_list_resource_with_disable_collection_count ,
580
- computer_owner ,
581
- string_json_attribute_person_detail ,
582
- string_json_attribute_person_list ,
628
+ client ,
629
+ app ,
630
+ api_blueprint ,
631
+ person_list ,
632
+ person_detail ,
633
+ person_computers ,
634
+ person_list_custom_qs_manager ,
635
+ person_list_raise_jsonapiexception ,
636
+ person_list_raise_exception ,
637
+ person_list_response ,
638
+ person_list_without_schema ,
639
+ computer_list ,
640
+ computer_detail ,
641
+ computer_list_resource_with_disable_collection_count ,
642
+ computer_owner ,
643
+ string_json_attribute_person_detail ,
644
+ string_json_attribute_person_list ,
583
645
):
584
646
api = Api (blueprint = api_blueprint )
585
647
api .route (person_list , "person_list" , "/persons" )
648
+ api .route (person_list_custom_qs_manager , "person_list_custom_qs_manager" , "/persons_qs" )
586
649
api .route (person_detail , "person_detail" , "/persons/<int:person_id>" )
587
650
api .route (person_computers , "person_computers" , "/persons/<int:person_id>/relationships/computers" )
588
651
api .route (person_computers , "person_computers_owned" , "/persons/<int:person_id>/relationships/computers-owned" )
@@ -775,6 +838,47 @@ def test_get_list_with_simple_filter(client, register_routes, person, person_2):
775
838
)
776
839
response = client .get ("/persons" + "?" + querystring , content_type = "application/vnd.api+json" )
777
840
assert response .status_code == 200
841
+ assert response .json ["meta" ]["count" ] == 1
842
+
843
+
844
+ def test_get_list_with_simple_filter_relationship_custom_qs (session , client , register_routes , person , person_2 ,
845
+ computer , computer_2 ):
846
+ computer .person = person
847
+ computer_2 .person = person_2
848
+ session .commit ()
849
+ with client :
850
+ querystring = urlencode (
851
+ {
852
+ "filter[computers.id]" : f'{ computer_2 .id } ,{ computer .id } ' ,
853
+ "include" : "computers" ,
854
+ "sort" : "-name" ,
855
+ }
856
+ )
857
+ response = client .get ("/persons_qs" + "?" + querystring , content_type = "application/vnd.api+json" )
858
+ assert response .status_code == 200
859
+ assert len (response .json ['data' ]) == 2
860
+ assert response .json ['data' ][0 ]['id' ] == str (person_2 .person_id )
861
+ assert response .json ['data' ][1 ]['id' ] == str (person .person_id )
862
+
863
+
864
+ def test_get_list_with_simple_filter_relationship_custom_qs_api (session , client , register_routes_custom_qs , person ,
865
+ person_2 , computer , computer_2 ):
866
+ computer .person = person
867
+ computer_2 .person = person_2
868
+ session .commit ()
869
+ with client :
870
+ querystring = urlencode (
871
+ {
872
+ "filter[computers.id]" : f'{ computer_2 .id } ,{ computer .id } ' ,
873
+ "include" : "computers" ,
874
+ "sort" : "-name" ,
875
+ }
876
+ )
877
+ response = client .get ("/qs/persons" + "?" + querystring , content_type = "application/vnd.api+json" )
878
+ assert response .status_code == 200
879
+ assert len (response .json ['data' ]) == 2
880
+ assert response .json ['data' ][0 ]['id' ] == str (person_2 .person_id )
881
+ assert response .json ['data' ][1 ]['id' ] == str (person .person_id )
778
882
779
883
780
884
def test_get_list_disable_pagination (client , register_routes ):
@@ -1154,6 +1258,22 @@ class PersonDetail(ResourceDetail):
1154
1258
PersonDetail ()
1155
1259
1156
1260
1261
+ def test_get_list_with_simple_filter_relationship_error (session , client , register_routes , person , person_2 ,
1262
+ computer , computer_2 ):
1263
+ computer .person = person
1264
+ computer_2 .person = person_2
1265
+ session .commit ()
1266
+ with client :
1267
+ querystring = urlencode (
1268
+ {
1269
+ "filter[computers.id]" : f'{ computer_2 .id } ,{ computer .id } ' ,
1270
+ "include" : "computers"
1271
+ }
1272
+ )
1273
+ response = client .get ("/persons" + "?" + querystring , content_type = "application/vnd.api+json" )
1274
+ assert response .status_code == 500
1275
+
1276
+
1157
1277
def test_get_list_jsonapiexception (client , register_routes ):
1158
1278
with client :
1159
1279
response = client .get ("/persons_jsonapiexception" , content_type = "application/vnd.api+json" )
@@ -1547,7 +1667,7 @@ def test_post_relationship_missing_type(client, register_routes, computer, perso
1547
1667
1548
1668
1549
1669
def test_post_relationship_missing_id (client , register_routes , computer , person ):
1550
- payload = {"data" : [{"type" : "computer" ,}]}
1670
+ payload = {"data" : [{"type" : "computer" , }]}
1551
1671
1552
1672
with client :
1553
1673
response = client .post (
@@ -1629,7 +1749,7 @@ def test_patch_relationship_missing_type(client, register_routes, computer, pers
1629
1749
1630
1750
1631
1751
def test_patch_relationship_missing_id (client , register_routes , computer , person ):
1632
- payload = {"data" : [{"type" : "computer" ,}]}
1752
+ payload = {"data" : [{"type" : "computer" , }]}
1633
1753
1634
1754
with client :
1635
1755
response = client .patch (
@@ -1711,7 +1831,7 @@ def test_delete_relationship_missing_type(client, register_routes, computer, per
1711
1831
1712
1832
1713
1833
def test_delete_relationship_missing_id (client , register_routes , computer , person ):
1714
- payload = {"data" : [{"type" : "computer" ,}]}
1834
+ payload = {"data" : [{"type" : "computer" , }]}
1715
1835
1716
1836
with client :
1717
1837
response = client .delete (
0 commit comments