def test_delete(self): """Test that items are deleted, including their index references. """ db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'), _serialize_tuple, _deserialize_tuple, indexes={'name': lambda tup: [tup[1].encode()]}, flag='c', _size=1024**2) db.put('1', (1, "foo", "bar")) db.put('2', (2, "alice", "Alice's data")) db.put('3', (3, "bob", "Bob's data")) with db.cursor(index='name') as curs: ordered_values = list(curs.iter()) self.assertEqual([(2, "alice", "Alice's data"), (3, "bob", "Bob's data"), (1, "foo", "bar")], ordered_values) db.delete('3') with db.cursor(index='name') as curs: ordered_values = list(curs.iter()) self.assertEqual([(2, "alice", "Alice's data"), (1, "foo", "bar")], ordered_values)
def test_delete(self): """Test that items are deleted, including their index references. """ db = OrientDatabase(DB_URI, _serialize_tuple, _deserialize_tuple, indexes={'name': lambda tup: [tup[1].encode()]}, flag='c', _size=1024**2) print('TEST_DELETE ....') db.put('1', (1, "foo", "bar")) db.put('2', (2, "alice", "Alice's data")) db.put('3', (3, "bob", "Bob's data")) with db.cursor(index='name') as curs: ordered_values = list(curs.iter()) print('test_delete.ORDERED_VALUES', ordered_values) self.assertEqual([(2, "alice", "Alice's data"), (3, "bob", "Bob's data"), (1, "foo", "bar")], ordered_values) db.delete('3') with db.cursor(index='name') as curs: ordered_values = list(curs.iter()) print('test_delete.ORDERED_VALUES 1', ordered_values) self.assertEqual([(2, "alice", "Alice's data"), (1, "foo", "bar")], ordered_values)
def test_index_iteration_with_concurrent_mods(self): """Given a database with three items, and a cursor on the index keys, test that a concurrent update will: - not change the results - not interrupt the iteration with an error """ db = OrientDatabase(DB_URI, _serialize_tuple, _deserialize_tuple, indexes={'name': lambda tup: [tup[1].encode()]}, flag='c', _size=1024**2) db.put('1', (1, "foo", "bar")) db.put('2', (2, "alice", "Alice's data")) db.put('3', (3, "bob", "Bob's data")) with db.cursor(index='name') as curs: iterator = curs.iter() self.assertEqual(2, next(iterator)[0]) db.put('4', (4, 'charlie', "Charlie's data")) self.assertEqual(3, next(iterator)[0]) self.assertEqual(1, next(iterator)[0]) with self.assertRaises(StopIteration): next(iterator)
def test_seek(self): """Given a database with three items and a cursor on the primary keys, test that the cursor can be properly position to an arbitrary element in the natural order of the keys """ db = OrientDatabase(DB_URI, _serialize_tuple, _deserialize_tuple, indexes={'name': lambda tup: [tup[1].encode()]}, flag='c', _size=1024**2) print('TEST_SEEK ...') db.put('1', (1, "alice", "Alice's data")) db.put('2', (2, "bob", "Bob's data")) db.put('3', (3, 'charlie', "Charlie's data")) with db.cursor() as curs: curs.seek('2') print('TEST_SEEK', curs.key()) self.assertEqual(2, curs.value()[0]) self.assertEqual('2', curs.key()) iterator = curs.iter() print('TEST_SEEK 1', curs.key()) self.assertEqual(2, next(iterator)[0])
def test_integer_indexing(self): """Test that a database can be indexed using integer keys. """ int_index_config = { 'key_fn': lambda tup: [struct.pack('I', tup[0])], 'integerkey': True } db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'), _serialize_tuple, _deserialize_tuple, indexes={'age': int_index_config}, flag='c', _size=1024**2) db.put('1', (1, "foo", "bar")) db.put('2', (30, "alice", "Alice's data")) db.put('3', (20, "bob", "Bob's data")) db.put('4', (12, "charlie", "Charlie's data")) self.assertEqual( [1, 12, 20, 30], [struct.unpack('I', k.encode())[0] for k in db.keys(index='age')]) with db.cursor(index='age') as curs: self.assertEqual([(1, "foo", "bar"), (12, "charlie", "Charlie's data"), (20, "bob", "Bob's data"), (30, "alice", "Alice's data")], list(curs.iter()))
def test_index_empty_db(self): """Given an empty database, show that the cursor will return a value of None for the first position. """ db = OrientDatabase(DB_URI, _serialize_tuple, _deserialize_tuple, indexes={'name': lambda tup: [tup[1].encode()]}, flag='c', _size=1024**2) with db.cursor(index='name') as curs: curs.first() self.assertIsNone(curs.value()) with db.cursor() as curs: curs.first() self.assertIsNone(curs.value())
def test_index_reverse_iteration(self): """Test reverse iteration over the items in a database, using the reverse natural order of the index keys. """ db = OrientDatabase(DB_URI, _serialize_tuple, _deserialize_tuple, indexes={'name': lambda tup: [tup[1].encode()]}, flag='c', _size=1024**2) db.put('1', (1, "foo", "bar")) db.put('2', (2, "alice", "Alice's data")) db.put('3', (3, "bob", "Bob's data")) with db.cursor(index='name') as curs: ordered_values = list(curs.iter_rev()) self.assertEqual([(1, "foo", "bar"), (3, "bob", "Bob's data"), (2, "alice", "Alice's data")], ordered_values)
def test_iteration(self): """Test iteration on over the items in a database, using the natural order of the primary keys. """ db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'), _serialize_tuple, _deserialize_tuple, indexes={'name': lambda tup: [tup[1].encode()]}, flag='c', _size=1024**2) db.put('1', (1, "foo", "bar")) db.put('2', (2, "alice", "Alice's data")) db.put('3', (3, "bob", "Bob's data")) with db.cursor() as curs: ordered_values = list(curs.iter()) self.assertEqual([(1, "foo", "bar"), (2, "alice", "Alice's data"), (3, "bob", "Bob's data")], ordered_values)
def test_integer_indexing(self): """Test that a database can be indexed using integer keys. """ int_index_config = { 'key_fn': lambda tup: [struct.pack('I', tup[0])], 'integerkey': True } db = OrientDatabase(DB_URI, _serialize_tuple, _deserialize_tuple, indexes={'age': int_index_config}, flag='c', _size=1024**2) db.put('1', (1, "foo", "bar")) db.put('2', (30, "alice", "Alice's data")) db.put('3', (20, "bob", "Bob's data")) db.put('4', (12, "charlie", "Charlie's data")) print('TEST_INTEGER_INDEXING', db.keys(index='age')) """ self.assertEqual( [1, 12, 20, 30], [struct.unpack('I', k.encode())[0] for k in db.keys(index='age')]) print('TEST_INTEGER_INDEXING 1') """ with db.cursor(index='age') as curs: print('TEST_INTEGER_INDEXING LIST', list(curs.iter())) """ self.assertEqual([ (1, "foo", "bar"), (12, "charlie", "Charlie's data"), (20, "bob", "Bob's data"), (30, "alice", "Alice's data")], list(curs.iter())) """ print('TEST_INTEGER_INDEXING 2')
def test_last(self): """Given a database with three items and a cursor on the primary keys, test that the cursor can be properly position to the last element in the natural order of the keys """ db = OrientDatabase(DB_URI, _serialize_tuple, _deserialize_tuple, indexes={'name': lambda tup: [tup[1].encode()]}, flag='c', _size=1024**2) db.put('1', (1, "alice", "Alice's data")) db.put('2', (2, "bob", "Bob's data")) db.put('3', (3, 'charlie', "Charlie's data")) with db.cursor() as curs: # Start from the beginning first = next(curs.iter()) # Read backward again iterator = curs.iter_rev() backward = next(iterator) self.assertEqual(first, backward) # Check the iterator is exhausted from there with self.assertRaises(StopIteration): next(iterator) # reset to first element curs.last() new_iter = curs.iter_rev() # verify that we'll see the first element again last = next(new_iter) self.assertEqual(3, last[0])
def test_first(self): """Given a database with three items and a cursor on the primary keys, test that the cursor can be properly position to the first element in the natural order of the keys """ db = OrientDatabase(DB_URI, _serialize_tuple, _deserialize_tuple, indexes={'name': lambda tup: [tup[1].encode()]}, flag='c', _size=1024**2) db.put('1', (1, "alice", "Alice's data")) db.put('2', (2, "bob", "Bob's data")) db.put('3', (3, 'charlie', "Charlie's data")) print('TEST_FIRST ...') with db.cursor() as curs: # Start from the end last = next(curs.iter_rev()) # Read forward again iterator = curs.iter() forward = next(iterator) self.assertEqual(last, forward) """ print("ITER ....") iter =curs.iter() k = curs.key() v = next(iter) k1 = curs.key() v1 = next(iter) k2 = curs.key() v2 = next(iter) k3 = curs.key() print("ITER:[{}]={},[{}]={},[{}]={}=>{}".format(k,v,k1,v1,k2,v2,k3)) print("POS={}={}".format(curs.key(),curs.value())) print('LIST', list(curs.iter())) print("POS={}".format(curs.key())) iter = curs.iter_rev() print("POS={}".format(curs.key())) print('LIST REV',list(iter)) print("POS={}".format(curs.key())) last = next(curs.iter_rev()) # Read forward again iterator = curs.iter() print('ITERATOR',iterator,'LAST',last) forward = next(iterator) print('LAST',last,'forward',forward) self.assertEqual(last, forward) """ # Check the iterator is exhausted from there with self.assertRaises(StopIteration): next(iterator) print("reset to first element ....") # reset to first element curs.first() new_iter = curs.iter() # verify that we'll see the first element again first = next(new_iter) print('FIRST', first) self.assertEqual(1, first[0])