def check_open_empty(self): name = mktemp() f = open(name, 'w') f.close() s = FileStorage(name) s.close() unlink(name)
def check_file_storage(self): name = mktemp() b = FileStorage(name) assert b.new_oid() == int8_to_str(0) assert b.new_oid() == int8_to_str(1) assert b.new_oid() == int8_to_str(2) raises(KeyError, b.load, int8_to_str(0)) record = pack_record(int8_to_str(0), as_bytes('ok'), as_bytes('')) b.begin() b.store(int8_to_str(0), record) b.end() b.sync() b.begin() b.store(int8_to_str(1), pack_record( int8_to_str(1), as_bytes('no'), as_bytes(''))) b.end() assert len(list(b.gen_oid_record(start_oid=int8_to_str(0)))) == 1 assert len(list(b.gen_oid_record())) == 2 b.pack() b.close() unlink(name + '.prepack') raises(ValueError, b.pack) # storage closed unlink(name + '.pack') raises(ValueError, b.load, int8_to_str(0)) # storage closed unlink(name)
def check_file_storage(self): name = mktemp() b = FileStorage(name) assert b.new_oid() == int8_to_str(0) assert b.new_oid() == int8_to_str(1) assert b.new_oid() == int8_to_str(2) raises(KeyError, b.load, int8_to_str(0)) record = pack_record(int8_to_str(0), as_bytes('ok'), as_bytes('')) b.begin() b.store(int8_to_str(0), record) b.end() b.sync() b.begin() b.store(int8_to_str(1), pack_record(int8_to_str(1), as_bytes('no'), as_bytes(''))) b.end() assert len(list(b.gen_oid_record(start_oid=int8_to_str(0)))) == 1 assert len(list(b.gen_oid_record())) == 2 b.pack() b.close() unlink(name + '.prepack') raises(ValueError, b.pack) # storage closed unlink(name + '.pack') raises(ValueError, b.load, int8_to_str(0)) # storage closed unlink(name)
def check_bad_record_size(self): name = mktemp() f = open(name, 'wb') g = FileStorage(name) f.seek(0, 2) write_int4_str(f, 'ok') g.close() f.close() raises(ShortRead, FileStorage, name) unlink(name)
def check_reopen(self): f = TempFileStorage() filename = f.get_filename() if os.name == 'nt': f.close() # don't try to re-open an open file on windows return g = FileStorage(filename, readonly=True) raises(IOError, FileStorage, filename) f.close() g.close()
class TestBackend: def __init__(self, filename, mode): self.mode = mode if mode == "w": self.storage = FileStorage(filename) self.connection = Connection(self.storage) self.test_db_items = self.connection.get_root() elif mode == "r": self.storage = FileStorage(filename) self.connection = Connection(self.storage) self.test_db_items = self.connection.get_root() self.next_rec_num = 0 # Initialise next record counter self.num_records = len(self.test_db_items) def __setitem__(self, key, value): self.test_db_items[key] = value def __getitem__(self, key): return self.test_db_items[str(key)] def __len__(self): return len(self.test_db_items) def first(self): return self.test_db_items[0] def iteritems(self): while(self.next_rec_num < self.num_records): value = self.test_db_items[self.next_rec_num] self.next_rec_num += 1 yield value def close(self): self.connection.commit() self.storage.close() def getTestDBItems(self): return self.test_db_items.values()
def copy_to_new_format(from_file, to_file, format): tmp_file_name = "%s.%s.tmp" % (to_file, datetime.now()) if format == 1: to_storage = FileStorage1(tmp_file_name) elif format == 2: to_storage = FileStorage2(tmp_file_name) from_storage = FileStorage(from_file, readonly=True) duplicate_file_storage(from_storage, to_storage) old_num_records = len(from_storage.index) assert len(to_storage.index) == old_num_records from_storage.close() repickle_storage(to_storage) assert len(to_storage.index) > max(1, old_num_records/2) move_to_backup(to_file) rename(tmp_file_name, to_file)
def check_repair(self): name = mktemp() g = FileStorage(name) g.close() f = open(name, 'r+b') f.seek(0, 2) p = f.tell() f.write(as_bytes('b')) f.flush() raises(ShortRead, FileStorage, name, readonly=True) h = FileStorage(name, repair=True) f.seek(0, 2) assert p == f.tell() f.close() h.close() unlink(name)
class DurusStore(SyncStore): '''Class for Durus object database frontend.''' init = 'durus://' def __init__(self, engine, **kw): super(DurusStore, self).__init__(engine, **kw) self._db = FileStorage(self._engine) self._connection = Connection(self._db) self.sync = self._connection.commit self._store = self._connection.get_root() def close(self): '''Closes all open storage and connections.''' self.sync() self._db.close() super(DurusStore, self).close()