@@ -1243,6 +1243,7 @@ class Query(graphene.ObjectType):
12431243 }
12441244
12451245
1246+ @pytest .mark .parametrize ("max_limit" , [100 , 4 ])
12461247class TestBackwardPagination :
12471248 def setup_schema (self , graphene_settings , max_limit ):
12481249 graphene_settings .RELAY_CONNECTION_MAX_LIMIT = max_limit
@@ -1261,8 +1262,8 @@ class Query(graphene.ObjectType):
12611262 schema = graphene .Schema (query = Query )
12621263 return schema
12631264
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 )
12661267 query_last = """
12671268 query {
12681269 allReporters(last: 3) {
@@ -1282,7 +1283,8 @@ def do_queries(self, schema):
12821283 e ["node" ]["firstName" ] for e in result .data ["allReporters" ]["edges" ]
12831284 ] == ["First 3" , "First 4" , "First 5" ]
12841285
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 )
12861288 query_first_and_last = """
12871289 query {
12881290 allReporters(first: 4, last: 3) {
@@ -1302,7 +1304,8 @@ def do_queries(self, schema):
13021304 e ["node" ]["firstName" ] for e in result .data ["allReporters" ]["edges" ]
13031305 ] == ["First 1" , "First 2" , "First 3" ]
13041306
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 )
13061309 query_first_last_and_after = """
13071310 query queryAfter($after: String) {
13081311 allReporters(first: 4, last: 3, after: $after) {
@@ -1317,28 +1320,44 @@ def do_queries(self, schema):
13171320
13181321 after = base64 .b64encode (b"arrayconnection:0" ).decode ()
13191322 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 ),
13211325 )
13221326 assert not result .errors
13231327 assert len (result .data ["allReporters" ]["edges" ]) == 3
13241328 assert [
13251329 e ["node" ]["firstName" ] for e in result .data ["allReporters" ]["edges" ]
13261330 ] == ["First 2" , "First 3" , "First 4" ]
13271331
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+ }
13311344 """
1332- schema = self .setup_schema (graphene_settings , max_limit = 100 )
1333- self .do_queries (schema )
13341345
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"
13421361
13431362
13441363def test_should_preserve_prefetch_related (django_assert_num_queries ):
0 commit comments