@@ -1084,6 +1084,78 @@ def get_pages(self, url):
10841084 return (previous , current , next , previous_url , next_url )
10851085
10861086
1087+ class NullableCursorPaginationModel (models .Model ):
1088+ created = models .IntegerField (null = True )
1089+
1090+
1091+ class TestCursorPaginationWithNulls (TestCase ):
1092+ """
1093+ Unit tests for `pagination.CursorPagination` with ordering on a nullable field.
1094+ """
1095+
1096+ def setUp (self ):
1097+ class ExamplePagination (pagination .CursorPagination ):
1098+ page_size = 1
1099+ ordering = 'created'
1100+
1101+ self .pagination = ExamplePagination ()
1102+ data = [
1103+ None , None , 3 , 4
1104+ ]
1105+ for idx in data :
1106+ NullableCursorPaginationModel .objects .create (created = idx )
1107+
1108+ self .queryset = NullableCursorPaginationModel .objects .all ()
1109+
1110+ get_pages = TestCursorPagination .get_pages
1111+
1112+ def test_ascending (self ):
1113+ (previous , current , next , previous_url , next_url ) = self .get_pages ('/' )
1114+
1115+ assert previous is None
1116+ assert current == [None ]
1117+ assert next == [None ]
1118+ assert previous_url is None
1119+
1120+ (previous , current , next , previous_url , next_url ) = self .get_pages (next_url )
1121+
1122+ assert previous == [None ]
1123+ assert current == [None ]
1124+ assert next == [3 ]
1125+
1126+ (previous , current , next , previous_url , next_url ) = self .get_pages (next_url )
1127+
1128+ assert previous == [3 ] # [None] paging artifact documented at https://github.com/ddelange/django-rest-framework/blob/3.14.0/rest_framework/pagination.py#L789
1129+ assert current == [3 ]
1130+ assert next == [4 ]
1131+
1132+ (previous , current , next , previous_url , next_url ) = self .get_pages (next_url )
1133+
1134+ assert previous == [3 ]
1135+ assert current == [4 ]
1136+ assert next is None
1137+ assert next_url is None
1138+
1139+ (previous , current , next , previous_url , next_url ) = self .get_pages (previous_url )
1140+
1141+ assert previous == [None ]
1142+ assert current == [3 ]
1143+ assert next == [4 ]
1144+
1145+ (previous , current , next , previous_url , next_url ) = self .get_pages (previous_url )
1146+
1147+ assert previous == [None ]
1148+ assert current == [None ]
1149+ assert next == [None ] # [3] paging artifact documented at https://github.com/ddelange/django-rest-framework/blob/3.14.0/rest_framework/pagination.py#L731
1150+
1151+ (previous , current , next , previous_url , next_url ) = self .get_pages (previous_url )
1152+
1153+ assert previous is None
1154+ assert current == [None ]
1155+ assert next == [None ]
1156+ assert previous_url is None
1157+
1158+
10871159def test_get_displayed_page_numbers ():
10881160 """
10891161 Test our contextual page display function.
0 commit comments