File size: 2,721 Bytes
3ea44ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Test Managers
-------------

Tests for `django-cursor-pagination` managers module.
"""
from __future__ import absolute_import

from .base import CursorBaseTestCase
from .models import ExampleModel


class TestCursorManagerByPrimaryKey(CursorBaseTestCase):
    def test_ordering_ascending(self):
        qs = ExampleModel.objects.order_by('pk')
        first_group = qs[:self.PAGE_SIZE]
        cursor = first_group.cursor()

        qs2 = qs.from_cursor(cursor)

        second_group = qs2[self.PAGE_SIZE:2 * self.PAGE_SIZE]

        first_pks = set(x.pk for x in first_group)
        second_pks = set(x.pk for x in second_group)

        self.assertFalse(first_pks & second_pks)
        for count in second_pks:
            self.assertGreater(
                count, max(first_pks),
                "Cursor didn't maintain ordering"
            )

    def test_ordering_descending(self):
        qs = ExampleModel.objects.order_by('-pk')
        first_group = qs[:self.PAGE_SIZE]
        cursor = first_group.cursor()

        qs2 = qs.from_cursor(cursor)

        second_group = qs2[self.PAGE_SIZE:2 * self.PAGE_SIZE]

        first_pks = set(x.pk for x in first_group)
        second_pks = set(x.pk for x in second_group)

        self.assertFalse(first_pks & second_pks)
        for count in second_pks:
            self.assertLess(
                count, min(first_pks),
                "Cursor didn't maintain ordering"
            )


class TestCursorManagerByInteger(CursorBaseTestCase):
    def test_ordering_ascending(self):
        qs = ExampleModel.objects.order_by('count_field')
        first_group = qs[:self.PAGE_SIZE]
        cursor = first_group.cursor()

        qs2 = qs.from_cursor(cursor)

        second_group = qs2[self.PAGE_SIZE:2 * self.PAGE_SIZE]

        first_counts = set(x.count_field for x in first_group)
        second_counts = set(x.count_field for x in second_group)

        for count in second_counts:
            self.assertGreaterEqual(
                count, max(first_counts),
                "Cursor didn't maintain ordering"
            )

    def test_ordering_descending(self):
        qs = ExampleModel.objects.order_by('-count_field')
        first_group = qs[:self.PAGE_SIZE]
        cursor = first_group.cursor()

        qs2 = qs.from_cursor(cursor)

        second_group = qs2[self.PAGE_SIZE:2 * self.PAGE_SIZE]

        first_counts = set(x.count_field for x in first_group)
        second_counts = set(x.count_field for x in second_group)

        for count in second_counts:
            self.assertLessEqual(
                count, min(first_counts),
                "Cursor didn't maintain ordering"
            )