@@ -1243,6 +1243,7 @@ class Query(graphene.ObjectType):
1243
1243
}
1244
1244
1245
1245
1246
+ @pytest .mark .parametrize ("max_limit" , [100 , 4 ])
1246
1247
class TestBackwardPagination :
1247
1248
def setup_schema (self , graphene_settings , max_limit ):
1248
1249
graphene_settings .RELAY_CONNECTION_MAX_LIMIT = max_limit
@@ -1261,8 +1262,8 @@ class Query(graphene.ObjectType):
1261
1262
schema = graphene .Schema (query = Query )
1262
1263
return schema
1263
1264
1264
- def do_queries (self , schema ):
1265
- # Simply last 3
1265
+ def test_query_last (self , graphene_settings , max_limit ):
1266
+ schema = self . setup_schema ( graphene_settings , max_limit = max_limit )
1266
1267
query_last = """
1267
1268
query {
1268
1269
allReporters(last: 3) {
@@ -1282,7 +1283,8 @@ def do_queries(self, schema):
1282
1283
e ["node" ]["firstName" ] for e in result .data ["allReporters" ]["edges" ]
1283
1284
] == ["First 3" , "First 4" , "First 5" ]
1284
1285
1285
- # Use a combination of first and last
1286
+ def test_query_first_and_last (self , graphene_settings , max_limit ):
1287
+ schema = self .setup_schema (graphene_settings , max_limit = max_limit )
1286
1288
query_first_and_last = """
1287
1289
query {
1288
1290
allReporters(first: 4, last: 3) {
@@ -1302,7 +1304,8 @@ def do_queries(self, schema):
1302
1304
e ["node" ]["firstName" ] for e in result .data ["allReporters" ]["edges" ]
1303
1305
] == ["First 1" , "First 2" , "First 3" ]
1304
1306
1305
- # Use a combination of first and last and after
1307
+ def test_query_first_last_and_after (self , graphene_settings , max_limit ):
1308
+ schema = self .setup_schema (graphene_settings , max_limit = max_limit )
1306
1309
query_first_last_and_after = """
1307
1310
query queryAfter($after: String) {
1308
1311
allReporters(first: 4, last: 3, after: $after) {
@@ -1317,28 +1320,44 @@ def do_queries(self, schema):
1317
1320
1318
1321
after = base64 .b64encode (b"arrayconnection:0" ).decode ()
1319
1322
result = schema .execute (
1320
- query_first_last_and_after , variable_values = dict (after = after )
1323
+ query_first_last_and_after ,
1324
+ variable_values = dict (after = after ),
1321
1325
)
1322
1326
assert not result .errors
1323
1327
assert len (result .data ["allReporters" ]["edges" ]) == 3
1324
1328
assert [
1325
1329
e ["node" ]["firstName" ] for e in result .data ["allReporters" ]["edges" ]
1326
1330
] == ["First 2" , "First 3" , "First 4" ]
1327
1331
1328
- def test_should_query (self , graphene_settings ):
1329
- """
1330
- Backward pagination should work as expected
1332
+ def test_query_last_and_before (self , graphene_settings , max_limit ):
1333
+ schema = self .setup_schema (graphene_settings , max_limit = max_limit )
1334
+ query_first_last_and_after = """
1335
+ query queryAfter($before: String) {
1336
+ allReporters(last: 1, before: $before) {
1337
+ edges {
1338
+ node {
1339
+ firstName
1340
+ }
1341
+ }
1342
+ }
1343
+ }
1331
1344
"""
1332
- schema = self .setup_schema (graphene_settings , max_limit = 100 )
1333
- self .do_queries (schema )
1334
1345
1335
- def test_should_query_with_low_max_limit (self , graphene_settings ):
1336
- """
1337
- When doing backward pagination (using last) in combination with a max limit higher than the number of objects
1338
- we should really retrieve the last ones.
1339
- """
1340
- schema = self .setup_schema (graphene_settings , max_limit = 4 )
1341
- self .do_queries (schema )
1346
+ result = schema .execute (
1347
+ query_first_last_and_after ,
1348
+ )
1349
+ assert not result .errors
1350
+ assert len (result .data ["allReporters" ]["edges" ]) == 1
1351
+ assert result .data ["allReporters" ]["edges" ][0 ]["node" ]["firstName" ] == "First 5"
1352
+
1353
+ before = base64 .b64encode (b"arrayconnection:5" ).decode ()
1354
+ result = schema .execute (
1355
+ query_first_last_and_after ,
1356
+ variable_values = dict (before = before ),
1357
+ )
1358
+ assert not result .errors
1359
+ assert len (result .data ["allReporters" ]["edges" ]) == 1
1360
+ assert result .data ["allReporters" ]["edges" ][0 ]["node" ]["firstName" ] == "First 4"
1342
1361
1343
1362
1344
1363
def test_should_preserve_prefetch_related (django_assert_num_queries ):
0 commit comments