def test_delete_cascade(self, class_entity_repo, monkeypatch): new_parent1 = MyEntityParent() new_ent1 = MyEntity() new_ent1.parent = new_parent1 new_child1 = MyEntityChild() new_child1.parent = new_ent1 child_rel_agg = self._make_rel_agg(class_entity_repo, new_ent1) child_rel_agg.add(new_child1) new_parent1.id = 1 new_ent1.id = 1 new_child1.id = 1 agg = class_entity_repo.get_aggregate(IMyEntity) child_agg = class_entity_repo.get_aggregate(IMyEntityChild) assert len(list(child_agg.iterator())) == 1 assert len(list(agg.iterator())) == 1 assert new_ent1.children == [new_child1] assert new_child1.parent == new_ent1 csc = DEFAULT_CASCADE | RELATION_OPERATIONS.REMOVE children_attr = get_domain_class_attribute(MyEntity, 'children') parent_attr = get_domain_class_attribute(MyEntityChild, 'parent') monkeypatch.setattr(children_attr, 'cascade', csc) monkeypatch.setattr(parent_attr, 'cascade', csc) child_rel_agg.remove(new_child1) assert new_ent1.children == [] assert new_child1.parent is None assert len(list(child_agg.iterator())) == 0 if self.__class__.__name__.startswith('TestMemory'): # FIXME: Transparent modification of RDB mapper cascades # does not work yet. assert len(list(agg.iterator())) == 0 assert len(list(child_rel_agg.iterator())) == 0
def test_nested_with_invalid_collection_type(self, class_entity_repo): session = class_entity_repo.session_factory() ent = MyEntity() child = MyEntityChild() ent.children = (child,) with pytest.raises(ValueError): session.add(MyEntity, ent) ent.id = 0 child.id = 0 with pytest.raises(ValueError) as cm: session.load(MyEntity, ent) assert cm.value.args[0].startswith('Do not know')
def _make_child(self, child_agg, child_id=0): new_parent = MyEntityParent() new_ent = MyEntity() new_ent.parent = new_parent new_child = MyEntityChild() new_ent.children.append(new_child) if new_child.parent is None: new_child.parent = new_ent child_agg.add(new_child) new_parent.id = child_id new_ent.id = child_id new_child.id = child_id return new_child
def test_update(self, class_entity_repo): session = class_entity_repo.session_factory() ent1 = MyEntity(id=0) session.add(MyEntity, ent1) ent2 = MyEntity() ent2.id = ent1.id my_attr_value = 1 ent2.number = my_attr_value session.update(MyEntity, ent2) ent3 = session.get_by_id(MyEntity, ent1.id) assert not ent3 is None assert ent3.id == ent1.id assert ent3.number == my_attr_value
def create_entity(entity_id=0, entity_text=None): my_entity = MyEntity(text=entity_text) my_entity.id = entity_id my_entity_parent = MyEntityParent() my_entity_parent.id = entity_id my_entity.parent = my_entity_parent my_entity_child = MyEntityChild() my_entity_child.id = entity_id my_entity.children.append(my_entity_child) my_entity_grandchild = MyEntityGrandchild() my_entity_grandchild.id = entity_id my_entity_child.children.append(my_entity_grandchild) # If we run with the SQLAlchemy backend, the back references are populated # automatically. if my_entity_child.parent is None: my_entity_child.parent = my_entity if my_entity_grandchild.parent is None: my_entity_grandchild.parent = my_entity_child return my_entity
def _make_test_entity_member(): parent = MyEntityParent() entity = MyEntity(parent=parent) if parent.child is None: parent.child = entity child = MyEntityChild() entity.children.append(child) if child.parent is None: child.parent = entity grandchild = MyEntityGrandchild() child.children.append(grandchild) if grandchild.parent is None: grandchild.parent = child coll = create_staging_collection(IMyEntity) mb = coll.create_member(entity) parent.id = 0 entity.id = 0 child.id = 0 grandchild.id = 0 return mb
def create_entity(entity_id=0, entity_text=None): my_entity = MyEntity(text=entity_text) my_entity.id = entity_id my_entity_parent = MyEntityParent() my_entity_parent.id = entity_id my_entity.parent = my_entity_parent my_entity_child = MyEntityChild() my_entity_child.id = entity_id my_entity_child.parent = my_entity if len(my_entity.children) == 0: # Tests that use the ORM will not need to go here. my_entity.children.append(my_entity_child) assert len(my_entity.children) == 1 my_entity_grandchild = MyEntityGrandchild() my_entity_grandchild.id = entity_id my_entity_grandchild.parent = my_entity_child # Tests that use the ORM will not need this. if len(my_entity.children) == 0: my_entity.children.append(my_entity_child) if len(my_entity_child.children) == 0: my_entity_child.children.append(my_entity_grandchild) return my_entity