def test_destroy_object_registered(): d = Database() first = d.create_object() d.register_object('first', first) with raises(ObjectRegisteredError): d.destroy_object(first) assert d.first is first assert d.objects == {first.id: first}
def test_register_object(): d = Database() o = d.create_object() name = 'test' d.register_object(name, o) assert d.registered_objects == {name: o} assert d.test is o o = Object(d) with raises(RuntimeError): d.register_object(name, o)
def test_unregister_object(): d = Database() o = d.create_object() name = 'test' d.register_object(name, o) d.unregister_object(name) assert not d.registered_objects with raises(AttributeError): print(d.test) with raises(KeyError): d.unregister_object(name)
def test_dump(): d = Database() o1 = d.create_object() o2 = d.create_object() name = 'test' d.register_object(name, o1) data = d.dump() assert data['registered_objects'] == {name: o1.id} assert len(data['objects']) == 2 assert data['objects'][0]['id'] == o1.id assert data['objects'][1]['id'] == o2.id
def test_load(): d = Database() grandparent_1 = d.create_object() grandparent_2 = d.create_object() parent = d.create_object() for grandparent in (grandparent_1, grandparent_2): parent.add_parent(grandparent) child = d.create_object() child.add_parent(parent) for name, obj in ( ('grandparent_1', grandparent_1), ('grandparent_2', grandparent_2), ('parent', parent), ('child', child) ): d.register_object(name, obj) data = d.dump() new = Database() new.load(data) assert d.child.id == child.id assert d.parent.id == parent.id assert d.child._parents == [d.parent] assert d.grandparent_1.id == grandparent_1.id assert d.grandparent_2.id == grandparent_2.id assert d.parent._parents == [d.grandparent_1, d.grandparent_2]