Exemplo n.º 1
0
    def test_cursor(self):

        k = Key('/')

        self.assertRaises(ValueError, Cursor, None, None)
        self.assertRaises(ValueError, Cursor, Query(Key('/')), None)
        self.assertRaises(ValueError, Cursor, None, [1])
        c = Cursor(Query(k), [1, 2, 3, 4, 5])  # should not raise

        self.assertEqual(c.skipped, 0)
        self.assertEqual(c.returned, 0)
        self.assertEqual(c._iterable, [1, 2, 3, 4, 5])

        c.skipped = 1
        c.returned = 2
        self.assertEqual(c.skipped, 1)
        self.assertEqual(c.returned, 2)

        c._skipped_inc(None)
        c._skipped_inc(None)
        self.assertEqual(c.skipped, 3)

        c._returned_inc(None)
        c._returned_inc(None)
        c._returned_inc(None)
        self.assertEqual(c.returned, 5)

        self.subtest_cursor(Query(k), [5, 4, 3, 2, 1], [5, 4, 3, 2, 1])
        self.subtest_cursor(Query(k, limit=3), [5, 4, 3, 2, 1], [5, 4, 3])
        self.subtest_cursor(Query(k, limit=0), [5, 4, 3, 2, 1], [])
        self.subtest_cursor(Query(k, offset=2), [5, 4, 3, 2, 1], [3, 2, 1])
        self.subtest_cursor(Query(k, offset=5), [5, 4, 3, 2, 1], [])
        self.subtest_cursor(Query(k, limit=2, offset=2), [5, 4, 3, 2, 1],
                            [3, 2])

        v1, v2, v3 = version_objects()
        vs = [v1, v2, v3]

        t1 = v1['committed']
        t2 = v2['committed']
        t3 = v3['committed']

        self.subtest_cursor(Query(k), vs, vs)
        self.subtest_cursor(Query(k, limit=2), vs, [v1, v2])
        self.subtest_cursor(Query(k, offset=1), vs, [v2, v3])
        self.subtest_cursor(Query(k, offset=1, limit=1), vs, [v2])

        self.subtest_cursor(
            Query(k).filter('committed', '>=', t2), vs, [v2, v3])
        self.subtest_cursor(Query(k).filter('committed', '<=', t1), vs, [v1])

        self.subtest_cursor(Query(k).order('+committed'), vs, [v1, v2, v3])
        self.subtest_cursor(Query(k).order('-created'), vs, [v3, v2, v1])
Exemplo n.º 2
0
 def query(self, query):
     """Returns a sequence of objects matching criteria expressed in `query`"""
     cursor = Cursor(query, self.shard_query_generator(query))
     cursor.apply_order(
     )  # ordering sharded queries is expensive (no generator)
     return cursor
Exemplo n.º 3
0
    def subtest_cursor(self, query, iterable, expected_results):

        self.assertRaises(ValueError, Cursor, None, None)
        self.assertRaises(ValueError, Cursor, query, None)
        self.assertRaises(ValueError, Cursor, None, iterable)
        cursor = Cursor(query, iterable)
        self.assertEqual(cursor.skipped, 0)
        self.assertEqual(cursor.returned, 0)

        cursor._ensure_modification_is_safe()
        cursor.apply_filter()
        cursor.apply_order()
        cursor.apply_offset()
        cursor.apply_limit()

        cursor_results = []
        for i in cursor:
            self.assertRaises(AssertionError,
                              cursor._ensure_modification_is_safe)
            self.assertRaises(AssertionError, cursor.apply_filter)
            self.assertRaises(AssertionError, cursor.apply_order)
            self.assertRaises(AssertionError, cursor.apply_offset)
            self.assertRaises(AssertionError, cursor.apply_limit)
            cursor_results.append(i)

        # ensure iteration happens only once.
        self.assertRaises(RuntimeError, iter, cursor)

        self.assertEqual(cursor_results, expected_results)
        self.assertEqual(cursor.returned, len(expected_results))
        self.assertEqual(cursor.skipped, query.offset)
        if query.limit:
            self.assertTrue(cursor.returned <= query.limit)
Exemplo n.º 4
0
	def subtest_cursor(self, query, iterable, expected_results):

		with pytest.raises(ValueError):
		    Cursor(None, None)
		with pytest.raises(ValueError):
		    Cursor(query, None)
		with pytest.raises(ValueError):
		    Cursor(None, iterable)
		cursor = Cursor(query, iterable)
		assert cursor.skipped == 0
		assert cursor.returned == 0

		cursor._ensure_modification_is_safe()
		cursor.apply_filter()
		cursor.apply_order()
		cursor.apply_offset()
		cursor.apply_limit()

		cursor_results = []
		for i in cursor:
			with pytest.raises(AssertionError):
			    cursor._ensure_modification_is_safe()
			with pytest.raises(AssertionError):
			    cursor.apply_filter()
			with pytest.raises(AssertionError):
			    cursor.apply_order()
			with pytest.raises(AssertionError):
			    cursor.apply_offset()
			with pytest.raises(AssertionError):
			    cursor.apply_limit()
			cursor_results.append(i)

		# ensure iteration happens only once.
		with pytest.raises(RuntimeError):
		    iter(cursor)

		assert cursor_results == expected_results
		assert cursor.returned == len(expected_results)
		assert cursor.skipped == query.offset
		if query.limit:
			assert cursor.returned <= query.limit
Exemplo n.º 5
0
	def test_cursor(self):

		k = Key('/')

		with pytest.raises(ValueError):
		    Cursor(None, None)
		with pytest.raises(ValueError):
		    Cursor(Query(Key('/')), None)
		with pytest.raises(ValueError):
		    Cursor(None, [1])
		c = Cursor(Query(k), [1, 2, 3, 4, 5])  # should not raise

		assert c.skipped == 0
		assert c.returned == 0
		assert c._iterable == [1, 2, 3, 4, 5]

		c.skipped = 1
		c.returned = 2
		assert c.skipped == 1
		assert c.returned == 2

		c._skipped_inc(None)
		c._skipped_inc(None)
		assert c.skipped == 3

		c._returned_inc(None)
		c._returned_inc(None)
		c._returned_inc(None)
		assert c.returned == 5

		self.subtest_cursor(Query(k), [5, 4, 3, 2, 1], [5, 4, 3, 2, 1])
		self.subtest_cursor(Query(k, limit=3), [5, 4, 3, 2, 1], [5, 4, 3])
		self.subtest_cursor(Query(k, limit=0), [5, 4, 3, 2, 1], [])
		self.subtest_cursor(Query(k, offset=2), [5, 4, 3, 2, 1], [3, 2, 1])
		self.subtest_cursor(Query(k, offset=5), [5, 4, 3, 2, 1], [])
		self.subtest_cursor(Query(k, limit=2, offset=2), [5, 4, 3, 2, 1], [3, 2])

		v1, v2, v3 = version_objects()
		vs = [v1, v2, v3]

		t1 = v1['committed']
		t2 = v2['committed']

		self.subtest_cursor(Query(k), vs, vs)
		self.subtest_cursor(Query(k, limit=2), vs, [v1, v2])
		self.subtest_cursor(Query(k, offset=1), vs, [v2, v3])
		self.subtest_cursor(Query(k, offset=1, limit=1), vs, [v2])

		self.subtest_cursor(Query(k).filter('committed', '>=', t2), vs, [v2, v3])
		self.subtest_cursor(Query(k).filter('committed', '<=', t1), vs, [v1])

		self.subtest_cursor(Query(k).order('+committed'), vs, [v1, v2, v3])
		self.subtest_cursor(Query(k).order('-created'), vs, [v3, v2, v1])