def test_get_root_address(): f = tempfile.NamedTemporaryFile() p = Storage(f) f.seek(0) f.write(b'\x00\x00\x00\x00\x00\x00\x02\x02') root_address = p.get_root_address() assert root_address == 514
class DBDB(object): def __init__(self, f): self._storage = Storage(f) self._tree = RedBlackTree(self._storage) def _assert_not_closed(self): if self._storage.closed: raise ValueError('Database closed.') def close(self): self._storage.close() def commit(self): self._assert_not_closed() self._tree.commit() def get(self, key): self._assert_not_closed() return self._tree.get(key) def set(self, key, value): self._assert_not_closed() return self._tree.set(key, value) def get_smaller_than(self, key): self._assert_not_closed() return self._tree.get_smaller_than(key)
def test_read(): f = tempfile.NamedTemporaryFile() p = Storage(f) f.seek(Storage.SUPERBLOCK_SIZE) f.write(b'\x00\x00\x00\x00\x00\x00\x00\x0801234567') value = p.read(Storage.SUPERBLOCK_SIZE) assert value == b'01234567'
def test_write(): f = tempfile.NamedTemporaryFile() p = Storage(f) p.write(b'ABCDE') value = _get_f_contents(f) superblock, data = _get_superblock_and_data(value) assert data == b'\x00\x00\x00\x00\x00\x00\x00\x05ABCDE'
def test_init_ensures_superblock(): f = tempfile.NamedTemporaryFile() p = Storage(f) EMPTY_SUPERBLOCK = (b'\x00' * Storage.SUPERBLOCK_SIZE) f.seek(0, os.SEEK_END) value = _get_f_contents(f) assert value == EMPTY_SUPERBLOCK
def __init__(self, f): self._storage = Storage(f) self._tree = RedBlackTree(self._storage)
def test_workflow(): f = tempfile.NamedTemporaryFile() p = Storage(f) a1 = p.write(b'one') a2 = p.write(b'two') p.commit_root_address(a2) a3 = p.write(b'three') assert p.get_root_address() == a2 a4 = p.write(b'four') p.commit_root_address(a4) assert p.read(a1) == b'one' assert p.read(a2) == b'two' assert p.read(a3) == b'three' assert p.read(a4) == b'four' assert p.get_root_address() == a4
def test_commit_root_address(): f = tempfile.NamedTemporaryFile() p = Storage(f) p.commit_root_address(257) root_bytes = _get_f_contents(f)[:8] assert root_bytes == b'\x00\x00\x00\x00\x00\x00\x01\x01'