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_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_hex_ordered_indexing(self): """Test that an index that uses hex-encoded keys will properly order the keys (i.e. the natural order of the keys is in numerical order). """ def to_hex(num): return "{0:#0{1}x}".format(num, 18) def age_index_key_fn(tup): return [to_hex(tup[0]).encode()] db = OrientDatabase(os.path.join(self._temp_dir, 'test_db'), _serialize_tuple, _deserialize_tuple, indexes={'age': age_index_key_fn}, flag='c', _size=1024**2) entry_count = 100 for i in range(1, entry_count): db.put(str(entry_count - i), (i, "foo" + str(i), "data")) self.assertEqual([to_hex(i) for i in range(1, entry_count)], list(db.keys(index='age')))
def test_indexing(self): """Test basic indexing around name """ 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")) print('TEST_INDEXING', db.get('1')) self.assertEqual((1, "foo", "bar"), db.get('1')) self.assertEqual((2, "alice", "Alice's data"), db.get('alice', index='name'))
def test_indexing(self): """Test basic indexing around name """ 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")) self.assertEqual((1, "foo", "bar"), db.get('1')) self.assertEqual((2, "alice", "Alice's data"), db.get('alice', index='name'))
def test_count(self): """Test that a database with three records, plus an index will return the correct count of primary key/values, using `len`. """ 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")) self.assertEqual(3, len(db)) self.assertEqual(3, db.count()) self.assertEqual(3, db.count(index="name"))
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_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])