def end_protocol_error(self): s1 = ClientStorage(address=self.address) c1 = Connection(s1) r1 = c1.get_root() s1.s = FakeSocket('\0\0\0\0?') r1._p_note_change() raises(ProtocolError, c1.commit)
def update(self): pd = PersistentDict() pd.update() raises(TypeError, pd.update, {}, {}) assert not list(pd.items()) pd.update(a=1) assert list(pd.items()) == [('a', 1)] pd = PersistentDict() pd.update(dict(b=2), a=1) assert len(list(pd.items())) == 2 assert pd['b'] == 2 assert pd['a'] == 1 pd = PersistentDict() pd.update([('b', 2)], a=1) assert len(pd.items()) == 2 assert pd['b'] == 2 assert pd['a'] == 1 pd2 = PersistentDict((x, True) for x in range(10)) pd.update(pd2) class keyed(object): data = dict(a=3) keys = data.keys __setitem__ = data.__setitem__ __getitem__ = data.__getitem__ pd.update(keyed()) assert pd['a'] == 3
def check_write_conflict(self): s1 = ClientStorage(address=self.address) c1 = Connection(s1) r1 = c1.get_root() s1.s = FakeSocket('\0\0\0\0', STATUS_INVALID) r1._p_note_change() raises(WriteConflictError, c1.commit)
def test_remove(self): s1 = set_type() raises(KeyError, s1.remove, 1) assert s1 == set_type() s1 = set_type('asdf') s1.remove('a') assert s1 == set_type('sdf')
def check_object_writer(self): class FakeConnection(ConnectionBase): def new_oid(self): return ROOT_OID def note_access(self, obj): pass connection = FakeConnection() self.s=s=ObjectWriter(connection) x = Persistent() assert x._p_connection == None x._p_oid = ROOT_OID x._p_connection = connection assert s._persistent_id(x) == (ROOT_OID, Persistent) x._p_connection = FakeConnection() # connection of x no longer matches connection of s. raises(ValueError, s._persistent_id, x) x.a = Persistent() assert s.get_state(x), ( '\x80\x02cdurus.persistent\nPersistent\nq\x01.\x80\x02}q\x02U' '\x01aU\x08\x00\x00\x00\x00\x00\x00\x00\x00q\x03h\x01\x86Qs.', '\x00\x00\x00\x00\x00\x00\x00\x00') assert list(s.gen_new_objects(x)) == [x, x.a] # gen_new_objects() can only be called once. raises(RuntimeError, s.gen_new_objects, 3) s.close()
def check_wrong_magic(self): name = mktemp() f = open(name, 'w') f.write('bogusbogus') f.close() raises(AssertionError, FileStorage, name) unlink(name)
def a(self): s = BytesIO() for sample in ([], [0], [2, 1], range(7)): int_array = IntArray(file=s, number_of_ints=10, maximum_int=10) for j, x in enumerate(sample): int_array[j] = x non_blanks = set(int_array) non_blanks.discard(int_array.get_blank_value()) assert set(sample) == non_blanks, (list(int_array), sample) assert raises(IndexError, int_array.__getitem__, 10) int_array2 = IntArray(file=BytesIO(s.getvalue())) int_array3 = IntArray(number_of_ints=10, maximum_int=300) for x in range(10): assert int_array3.get(x) == None assert int_array3[1] == int_array3.get_blank_value() int_array3[1] = 42 assert int_array3.get(1)== 42 assert len(int_array3) == 10 raises(ValueError, int_array3.__setitem__, 2, 100000) int_array4 = IntArray(number_of_ints=10) assert int_array4.get(1, default=42) == 42 assert int_array4.get(100, default=42) == 42 assert list(iteritems(int_array4)) == [] int_array4[3] = 4 int_array4[8] = 9 assert list(iteritems(int_array4)) == [(3, 4), (8, 9)]
def test_pop(self): raises(KeyError, set_type().pop) s1 = set_type('asdf') x = s1.pop() assert x not in s1 assert len(s1) == 3 assert (s1 | set_type(x)) == set_type('asdf')
def find_extremes(self): bt = BTree() raises(AssertionError, bt.get_min_item) raises(AssertionError, bt.get_max_item) for j in range(100): bt.add(j) assert bt.get_min_item() == (0, True) assert bt.get_max_item() == (99, True)
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()
def f(self): class FakeSocket(object): def recv(self, n): if n > 10: return as_bytes('') return as_bytes('x') def send(self, s): return len(s) s = FakeSocket() write(s, 'x' * 2000000) read(s, 8) raises(ShortRead, read, s, 11)
def check_client_storage(self): b = ClientStorage(address=self.address) c = ClientStorage(address=self.address) oid = b.new_oid() assert oid == int8_to_str(0), repr(oid) oid = b.new_oid() assert oid == int8_to_str(1), repr(oid) oid = b.new_oid() assert oid == int8_to_str(2), repr(oid) 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) assert b.end() is None b.load(int8_to_str(0)) assert 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())) == 1 records = b.bulk_load([int8_to_str(0), int8_to_str(1)]) assert len(list(records)) == 2 records = b.bulk_load([int8_to_str(0), int8_to_str(1), int8_to_str(2)]) raises(DurusKeyError, list, records) b.pack() assert len(list(b.gen_oid_record())) == 1 raises(ReadConflictError, c.load, int8_to_str(0)) raises(ReadConflictError, c.load, int8_to_str(0)) assert set(c.sync()) == set([int8_to_str(0), int8_to_str(1)]) assert record == c.load(int8_to_str(0)) b.close() c.close()
def lowlevelops(self): from durus.persistent import _getattribute, _setattribute from durus.persistent import _delattribute, _hasattribute storage = TempFileStorage() connection = Connection(storage) root = connection.get_root() root._p_set_status_ghost() assert not _hasattribute(root, 'data') root._p_set_status_ghost() raises(AttributeError, _getattribute, root, 'data') assert root._p_is_ghost() _setattribute(root, 'data', 'bogus') assert root._p_is_ghost() _delattribute(root, 'data') assert root._p_is_ghost()
def a(self): for sample in (["a"], ["a", "b"], ["ab", "cd", "ef"]): sample = [as_bytes(x) for x in sample] s = BytesIO() number_of_words = len(sample) bytes_per_word = 0 if sample: bytes_per_word = len(sample[0]) word_array = WordArray(file=s, bytes_per_word=bytes_per_word, number_of_words=number_of_words) for j, word in enumerate(sample): word_array[j] = word assert list(word_array) == sample, (list(word_array), sample) assert raises(ValueError, word_array.__setitem__, 1, "sdf") assert raises(IndexError, word_array.__setitem__, 10, "sf") assert raises(IndexError, word_array.__getitem__, -10)
def a(self): b = Byte(0) b[2] = 1 b[-1] = 1 assert b[-1] == 1 assert [b[j] for j in range(8)] == [0,0,1,0,0,0,0,1] b[-1] = 0 assert [b[j] for j in range(8)] == [0,0,1,0,0,0,0,0] assert int(b) == 32 assert str(b) == chr(32) raises(TypeError, Byte, 300) raises(IndexError, b.__getitem__, -9) raises(IndexError, b.__getitem__, 8) raises(IndexError, b.__setitem__, -9, 1) raises(IndexError, b.__setitem__, 8, 1)
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)
def check_conflict(self): b = Connection(self._get_storage()) c = Connection(self._get_storage()) rootb = b.get(int8_to_str(0)) rootb['b'] = Persistent() rootc = c.get(int8_to_str(0)) rootc['c'] = Persistent() c.commit() raises(ConflictError, b.commit) raises(KeyError, rootb.__getitem__, 'c') transaction_serial = b.transaction_serial b.abort() assert b.get_transaction_serial() > transaction_serial assert rootb._p_is_ghost() rootc['d'] = Persistent() c.commit() rootb['d']
def check_more(self): storage = TempFileStorage() connection = Connection(storage) root=connection.get_root() assert not root._p_is_ghost() root['a'] = 1 assert root._p_is_unsaved() del root['a'] connection.abort() assert root._p_is_ghost() raises(AttributeError, getattr, root, 'a') root._p_set_status_saved() assert root._p_is_saved() root._p_set_status_unsaved() assert root._p_is_unsaved() root._p_set_status_ghost() assert root._p_is_ghost() root._p_set_status_unsaved()
def a(self): for sample in (['a'], ['a', 'b'], ['ab', 'cd', 'ef']): sample = [as_bytes(x) for x in sample] s = BytesIO() number_of_words = len(sample) bytes_per_word = 0 if sample: bytes_per_word = len(sample[0]) word_array = WordArray( file=s, bytes_per_word=bytes_per_word, number_of_words=number_of_words) for j, word in enumerate(sample): word_array[j] = word assert list(word_array) == sample, (list(word_array), sample) assert raises(ValueError, word_array.__setitem__, 1, 'sdf') assert raises(IndexError, word_array.__setitem__, 10, 'sf') assert raises(IndexError, word_array.__getitem__, -10)
def a(self): s = BytesIO() b = ByteArray(size=10000, file=s) assert list(b) == [as_bytes('\x00') for j in xrange(10000)], list(b) for j in xrange(10000): assert as_bytes('\x00') == b[j] for j in xrange(10000): b[j] = as_bytes('!') for j in xrange(10000): assert as_bytes('!') == b[j] assert b[0:3] == as_bytes('!!!') assert b[47:50] == as_bytes('!!!'), repr(b[47:50]) s = BytesIO() b2 = ByteArray(file=s) b2.set_size(10000, init_byte=as_bytes('\xff')) for j in xrange(10000): assert as_bytes('\xff') == b2[j], (j, b2[j]) s.seek(0) raises(AssertionError, ByteArray, size=20000, file=s)
def update(self): bt = BTree() bt.update() assert not list(bt.items()) bt.update(a=1) assert list(bt.items()) == [('a', 1)] bt = BTree() bt.update(dict(b=2), a=1) assert len(list(bt.items())) == 2 assert bt['b'] == 2 assert bt['a'] == 1 bt = BTree() bt.update([('b', 2)], a=1) assert len(list(bt.items())) == 2 assert bt['b'] == 2 assert bt['a'] == 1 class Fake(object): def items(self): return [('1',2)] bt.update(Fake()) raises(TypeError, bt.update, 1, 2)
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_memory_storage(self): b = MemoryStorage() 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())) == 1 assert record == b.load(int8_to_str(0)) records = b.bulk_load([int8_to_str(0), int8_to_str(1)]) assert len(list(records)) == 2 records = b.bulk_load([int8_to_str(0), int8_to_str(1), int8_to_str(2)]) raises(KeyError, list, records)
def a(self): f = File(prefix='shelftest') name = f.get_name() f.close() s = FileStorage(name) c = Connection(s) r = c.get_root() for x in range(10): r["a%s" % x] = Persistent() c.commit() deleted_oids = [ r['a0']._p_oid, r['a2']._p_oid, r['a7']._p_oid, r['a8']._p_oid ] del r['a0'] del r['a2'] del r['a7'] del r['a8'] c.commit() c.pack() c.abort() assert c.get(deleted_oids[0])._p_is_ghost() assert c.get(deleted_oids[1])._p_is_ghost() raises(KeyError, getattr, c.get(deleted_oids[0]), 'a') assert len([repr(oid) for oid, record in s.gen_oid_record()]) == 7 c.commit() c.pack() new_oid = s.new_oid() assert new_oid == deleted_oids[-1], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-2], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-3], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-4], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == int8_to_str(11), repr(new_oid) new_oid = s.new_oid() assert new_oid == int8_to_str(12), repr(new_oid)
def conflict_from_invalid_removable_previously_accessed(self): c1, c2 = self._scenario() A = c1.get_root()['A'] A.a # access A in c1. This will lead to conflict. A_oid = A._p_oid A = None # forget about it # Lose the reference to A. c1.get_root()._p_set_status_ghost() # Commit a new A in c2. c2.get_root()['A'].a = 2 c2.commit() c1.get_root()['B'].b = 1 # touch B in c1 # Conflict because A has been accessed in c1, but it is invalid. assert raises(ConflictError, c1.commit)
def check_fine_conflict(self): c1 = Connection(self._get_storage()) c2 = Connection(self._get_storage()) c1.get_root()['A'] = Persistent() c1.get_root()['A'].a = 1 c1.get_root()['B'] = Persistent() c1.commit() c2.abort() # c1 has A loaded. assert not c1.get_root()['A']._p_is_ghost() c1.get_root()['B'].b = 1 c2.get_root()['A'].a = 2 c2.commit() # Even though A has been changed by c2, # c1 has not accessed an attribute of A since # the last c1.commit(), so we don't want a ConflictError. c1.commit() assert c1.get_root()['A']._p_is_ghost() c1.get_root()['A'].a # accessed! c1.get_root()['B'].b = 1 c2.get_root()['A'].a = 2 c2.commit() raises(WriteConflictError, c1.commit)
def update(self): bt = BTree() bt.update() assert not list(bt.items()) bt.update(a=1) assert list(bt.items()) == [('a', 1)] bt = BTree() bt.update(dict(b=2), a=1) assert len(list(bt.items())) == 2 assert bt['b'] == 2 assert bt['a'] == 1 bt = BTree() bt.update([('b', 2)], a=1) assert len(list(bt.items())) == 2 assert bt['b'] == 2 assert bt['a'] == 1 class Fake(object): def items(self): return [('1', 2)] bt.update(Fake()) raises(TypeError, bt.update, 1, 2)
def a(self): f = File(prefix='shelftest') name = f.get_name() f.close() s = FileStorage(name) c = Connection(s) r = c.get_root() for x in range(10): r["a%s" % x] = Persistent() c.commit() deleted_oids = [ r['a0']._p_oid, r['a2']._p_oid, r['a7']._p_oid, r['a8']._p_oid] del r['a0'] del r['a2'] del r['a7'] del r['a8'] c.commit() c.pack() c.abort() assert c.get(deleted_oids[0])._p_is_ghost() assert c.get(deleted_oids[1])._p_is_ghost() raises(ReadConflictError, getattr, c.get(deleted_oids[0]), 'a') assert len([repr(oid) for oid, record in s.gen_oid_record()]) == 7 c.commit() c.pack() new_oid = s.new_oid() assert new_oid == deleted_oids[-1], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-2], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-3], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == deleted_oids[-4], (new_oid, deleted_oids) new_oid = s.new_oid() assert new_oid == int8_to_str(11), repr(new_oid) new_oid = s.new_oid() assert new_oid == int8_to_str(12), repr(new_oid)
def test__lt__(self): s1 = set_type() s2 = other_type() if set_type != other_type: assert raises(TypeError, s1.__lt__, s2) return assert not (s1 < s2) s2 = other_type(['k']) assert not (s2 < s1) assert s1 < s2 s1 = set_type('k') assert not (s1 < s2) s2 = other_type('jk') assert not (s2 < s1) assert s1 < s2
def test__le__(self): s1 = set_type() s2 = other_type() if set_type != other_type: assert raises(TypeError, s1.__le__, s2) return assert s1 <= s2 s2 = other_type(['k']) assert not (s2 <= s1) assert s1 <= s2 s1 = set_type(['k']) assert s1 <= s2 assert s2 <= s1 s2 = other_type(['j', 'k']) assert not (s2 <= s1) assert s1 <= s2
def b(self): name = mktemp() raises(OSError, File, name, readonly=True) g = File(name, readonly=False) g.close() f = File(name, readonly=True) assert f.is_readonly() raises(AssertionError, f.write, 'ok') raises(IOError, f.file.write, 'ok') # readonly file f.close() unlink(name)
def test__gt__(self): s1 = set_type() s2 = other_type() if set_type != other_type: assert raises(TypeError, s1.__gt__, s2) return assert not (s1 > s2) s2 = other_type(['k']) assert not (s1 > s2) assert s2 > s1 assert s2.__gt__(s1) s1 = set_type(['k']) assert not (s1 > s2) assert not (s2 > s1) s2 = other_type(['j', 'k']) assert not (s1 > s2) assert s2 > s1
def a(self): f = File() f.rename(f.get_name()) assert f.is_temporary() raises(AssertionError, f.rename, f.get_name() + '.renamed') test_name = f.get_name() + '.test' assert not exists(test_name) tmp = open(test_name, 'w+b') tmp.close() g = File(test_name) assert not g.is_temporary() g.rename(g.get_name() + '.renamed') assert g.get_name() == test_name + '.renamed' f.write(as_bytes('abc')) f.seek(0) assert len(f) == 3 assert as_bytes('a') == f.read(1) assert as_bytes('bc') == f.read() f.close() assert not exists(f.get_name()) raises(OSError, f.__len__) # tmpfile removed on close h = File(g.get_name()) g.write(as_bytes('a')) g.seek(0) assert g.tell() == 0 g.seek_end() assert g.tell() == 1 assert g.has_lock assert not h.has_lock raises(IOError, h.write, as_bytes('b')) g.flush() g.fsync() g.seek(0) g.truncate() g.close() h.close() unlink(g.get_name())
def d(self): s = BytesIO(as_bytes('nope')) assert raises(AssertionError, Shelf, s, readonly=True) s = BytesIO(as_bytes("SHELF-1\nbogus")) assert raises(ShortRead, Shelf, s, readonly=True)
def no_arbitrary_attributes(self): p = PersistentList() raises(AttributeError, setattr, p, 'bogus', 1)
def check_storage_tools(self): connection = Connection(self._get_storage()) root = connection.get_root() root['a'] = Persistent() root['b'] = Persistent() connection.commit() index = get_reference_index(connection.get_storage()) assert index == { int8_to_str(1): [int8_to_str(0)], int8_to_str(2): [int8_to_str(0)] } census = get_census(connection.get_storage()) assert census == { as_bytes('PersistentDict'): 1, as_bytes('Persistent'): 2 } references = list( gen_referring_oid_record(connection.get_storage(), int8_to_str(1))) assert references == [(int8_to_str(0), connection.get_storage().load(int8_to_str(0)))] class Fake(object): pass s = Fake() s.__class__ = Storage raises(RuntimeError, s.__init__) raises(NotImplementedError, s.load, None) raises(NotImplementedError, s.begin) raises(NotImplementedError, s.store, None, None) raises(NotImplementedError, s.end) raises(NotImplementedError, s.sync) g = s.gen_oid_record() raises(NotImplementedError, next, g)
def a(self): p = SlottedPersistentObject() assert p.__getstate__() == {} p.a = 1 assert p.__getstate__() == dict(a=1) raises(AttributeError, setattr, p, 'c', 2)
def check_getstate(self): p = PersistentObject() assert p.__getstate__() == {} raises(AttributeError, setattr, p, 'a', 1)
def no_arbitrary_attributes(self): pd = PersistentDict() raises(AttributeError, setattr, pd, 'bogus', 1)
def check_setstate(self): p = PersistentObject() p.__setstate__({}) raises(AttributeError, p.__setstate__, {'a':1})
def b(self): s = Shelf() assert s.get_value(as_bytes('okokokok')) is None assert raises(ValueError, s.get_value, as_bytes('okok'))