def test_Smoke(self): writer = sparkey.LogWriter(self.logfile) for i in range(0, 10): writer.put('key%d' % i, 'value%d' % i) writer.close() reader = sparkey.LogReader(self.logfile) for i, (key, value, type) in enumerate(reader): self.assertEqual('key%d' % i, key) self.assertEqual('value%d' % i, value) self.assertEqual(sparkey.IterType.PUT, type) self.assertEqual(9, i) reader.close() sparkey.writehash(self.hashfile, self.logfile) hashreader = sparkey.HashReader(self.hashfile, self.logfile) self.assertEqual(10, len(hashreader)) for i in range(0, 10): self.assertTrue('key%d' % i in hashreader) self.assertFalse('key_miss' in hashreader) for i, (key, value) in enumerate(hashreader): self.assertEqual('key%d' % i, key) self.assertEqual('value%d' % i, value) self.assertEqual(9, i) self.assertEqual('value0', hashreader.get('key0')) self.assertEqual('value9', hashreader.get('key9')) self.assertEqual(None, hashreader.get('key10')) hashreader.close()
def test_binary(self): writer = sparkey.HashWriter(self.hashfile, self.logfile) for key in keys: writer.put(binascii.unhexlify(key), 'value') writer.close() reader = sparkey.HashReader(self.hashfile, self.logfile) for key in keys: self.assertEqual('value', reader[binascii.unhexlify(key)]) reader.close()
def test_append(self): keys = ('a', 'b', 'c') writer = sparkey.HashWriter(self.hashfile, self.logfile) for key in keys: writer.put(key, 'value') writer.close() reader = sparkey.HashReader(self.hashfile, self.logfile) for key in keys: self.assertEqual(b'value', reader[key]) reader.close() for i in range(0, 10): writer = sparkey.HashWriter(self.hashfile, self.logfile, mode='APPEND') for key in keys: writer.put(key, 'value%d' % i) writer.close() reader = sparkey.HashReader(self.hashfile, self.logfile) for key in keys: self.assertEqual(b'value%d' % i, reader[key]) reader.close()
def test_recreate(self): keys = ("a", "b", "c") for i in range(0, 10): writer = sparkey.HashWriter(self.hashfile, self.logfile) for key in keys: writer.put(key, "value") writer.close() reader = sparkey.HashReader(self.hashfile, self.logfile) self.assertEqual(3, len(reader)) for key in keys: self.assertEqual(b'value', reader[key]) reader.close()
def test_binary_key_and_value(self): writer = sparkey.HashWriter(self.hashfile, self.logfile) for key in keys: writer.put(binascii.unhexlify(key), binascii.unhexlify(key)) writer.close() reader = sparkey.HashReader(self.hashfile, self.logfile) for key in keys: self.assertEqual(binascii.unhexlify(key), reader[binascii.unhexlify(key)]) self.assertEqual(binascii.unhexlify(key), reader.get(binascii.unhexlify(key))) self.assertEqual(binascii.unhexlify(key), reader.getAsString(binascii.unhexlify(key))) reader.close()
def _random_access(self, num_entries, num_lookups): reader = sparkey.HashReader(self.hashfile, self.logfile) for i in range(0, num_lookups): r = str(randint(0, num_entries - 1)) self.assertEqual("value_" + r, reader.getAsString('key_' + r)) reader.close()