Example #1
0
def main():
    collection = prepare()
    collection.insert_many([{'_id': i} for i in range(7)])

    search_condictions = {
        'collection': collection,
        'filter': {},
        'sort': [('_id', 1)],
        'limit': 5,
    }

    # gets the first page
    cursor1 = get_keyset_cursor(**search_condictions)
    print('page1:', list(cursor1))  # [{'_id': 0}, {'_id': 1}, {'_id': 2}, {'_id': 3}, {'_id': 4}]

    # gets the second page
    cursor2 = get_keyset_cursor(**search_condictions, position=cursor1.paging.next_position)
    print('page2:', list(cursor2))  # [{'_id': 5}, {'_id': 6}]

    collection.insert({'_id': -1})

    # the first page again, backwards from the previous page
    cursor1 = get_keyset_cursor(**search_condictions, position=cursor2.paging.previous_position)
    print('page1:', list(cursor1))  # [{'_id': 0}, {'_id': 1}, {'_id': 2}, {'_id': 3}, {'_id': 4}]

    # what if new items were added at the start?
    if cursor1.paging.has_previous:
        cursor0 = get_keyset_cursor(**search_condictions, position=cursor1.paging.previous_position)
        print('page0:', list(cursor0))  # [{'_id': -1}]
Example #2
0
    def test_single_sort_case(self):
        search_condictions = {
            'collection': self.collect,
            'limit': 3,
            'sort': [('_id', 1)],
        }

        cursor1 = get_keyset_cursor(**search_condictions)
        self.assert_cursor(cursor1, self.objs[:3], has_next=True)

        cursor2 = get_keyset_cursor(**search_condictions,
                                    position=cursor1.paging.next_position)
        self.assert_cursor(cursor2, self.objs[3:6], has_next=True)

        cursor3 = get_keyset_cursor(**search_condictions,
                                    position=cursor2.paging.next_position)
        self.assert_cursor(cursor3, self.objs[6:], has_next=False)

        cursor2 = get_keyset_cursor(**search_condictions,
                                    position=cursor3.paging.previous_position)
        self.assert_cursor(cursor2, self.objs[3:6], has_previous=True)

        cursor1 = get_keyset_cursor(**search_condictions,
                                    position=cursor2.paging.previous_position)
        self.assert_cursor(cursor1, self.objs[:3], has_previous=False)
Example #3
0
    def test_mutil_sort_cast(self):
        search_condictions = {
            'collection': self.collect,
            'limit': 3,
            'sort': [('m', 1), ('n', 1), ('_id', 1)],
            # TODO test some edge index cases
        }

        cursor1 = get_keyset_cursor(**search_condictions)
        self.assert_cursor(cursor1, self.objs[:3], has_next=True)

        cursor2 = get_keyset_cursor(**search_condictions,
                                    position=cursor1.paging.next_position)
        self.assert_cursor(cursor2, self.objs[3:6], has_next=True)

        cursor3 = get_keyset_cursor(**search_condictions,
                                    position=cursor2.paging.next_position)
        self.assert_cursor(cursor3, self.objs[6:], has_next=False)

        cursor2 = get_keyset_cursor(**search_condictions,
                                    position=cursor3.paging.previous_position)
        self.assert_cursor(cursor2, self.objs[3:6], has_previous=True)

        cursor1 = get_keyset_cursor(**search_condictions,
                                    position=cursor2.paging.previous_position)
        self.assert_cursor(cursor1, self.objs[:3], has_previous=False)
Example #4
0
    def test_mulit_filter(self):
        params = dict(filter={'a': {'$gt': 0}, 'b': 0}, limit=2, sort=[['a', 1]], collection=self.collect)

        cursor1 = get_keyset_cursor(**params)
        self.assertEqual([i['a'] for i in cursor1], [2, 4])

        cursor2 = get_keyset_cursor(**params, position=cursor1.paging.next_position)
        self.assertEqual([i['a'] for i in cursor2], [6, 8])
Example #5
0
    def test_single_filter(self):
        params = dict(filter={'b': 0}, limit=2, sort=[['a', 1]], collection=self.collect)

        cursor1 = get_keyset_cursor(**params)
        self.assertEqual([i['a'] for i in cursor1], [0, 2])

        cursor2 = get_keyset_cursor(**params, position=cursor1.paging.next_position)
        self.assertEqual([i['a'] for i in cursor2], [4, 6])
Example #6
0
    def test_next(self):
        params = dict(collection=self.collect, sort=[['a.b', 1]], limit=20)

        cursor1 = get_keyset_cursor(**params)
        self.assertEqual([i['a']['b'] for i in cursor1], list(range(0, 20)))

        cursor2 = get_keyset_cursor(**params, position=cursor1.paging.next_position)
        self.assertEqual([i['a']['b'] for i in cursor2], list(range(20, 40)))

        cursor1 = get_keyset_cursor(**params, position=cursor2.paging.previous_position)
        self.assertEqual([i['a']['b'] for i in cursor1], list(range(0, 20)))
Example #7
0
    def test_exclusion_projection(self):
        condictions = dict(limit=2, sort=[['a', 1], ['b', 1]], collection=self.collect)

        for projection in [{'b': 0}, {'b': 0, '_id': 1}, {'b': 0, '_id': 0}]:
            with self.assertRaises(ValueError):
                get_keyset_cursor(**condictions, projection=projection)
Example #8
0
 def test_default_projection(self):
     cursor1 = get_keyset_cursor(filter={'b': 0}, limit=2, sort=[['a', 1]], collection=self.collect)
     self.assertEqual(cursor1[0].get('b'), 0)
Example #9
0
 def test_desc_arg(self):
     cursor = get_keyset_cursor(sort=[['a', DESCENDING]], limit=3, collection=self.collect)
     self.assertEqual([i['a'] for i in cursor], [9, 8, 7])
Example #10
0
 def test_asc_arg(self):
     cursor = get_keyset_cursor(sort=[['a', ASCENDING]], limit=3, collection=self.collect)
     self.assertEqual([i['a'] for i in cursor], [0, 1, 2])