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_basics(self, class_entity_repo): agg = class_entity_repo.get_aggregate(IMyEntity) child_agg = class_entity_repo.get_aggregate(IMyEntityChild) new_child0 = self._make_child(child_agg) new_parent1 = MyEntityParent() new_ent1 = MyEntity() new_ent1.parent = new_parent1 new_child1 = MyEntityChild() child_rel_agg = self._make_rel_agg(class_entity_repo, new_ent1) assert len(list(child_agg.iterator())) == 1 assert len(list(agg.iterator())) == 1 assert len(list(child_rel_agg.iterator())) == 0 # Adding to a relationship aggregate ..... child_rel_agg.add(new_child1) # ....... adds to root aggregates: assert len(list(child_agg.iterator())) == 2 # ....... adds (along the cascade) to parent root aggregate: assert len(list(agg.iterator())) == 2 # ....... appends to children: assert new_ent1.children == [new_child1] # get by ID and slug, filtering. assert child_rel_agg.get_by_id(new_child1.id).id == new_child1.id assert \ child_rel_agg.get_by_slug(new_child1.slug).slug == new_child1.slug child_rel_agg.filter = eq(id=2) assert child_rel_agg.get_by_id(new_child1.id) is None assert child_rel_agg.get_by_slug(new_child1.slug) is None # update. upd_child0 = MyEntityChild(id=0) txt = 'FROBNIC' upd_child0.text = txt child_rel_agg.update(upd_child0) assert new_child0.text == txt
def test_add_with_data_element(self): my_entity = MyEntity() my_entity_parent = MyEntityParent() my_entity.parent = my_entity_parent coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) mp = self._make_mapping() data_el = mp.map_to_data_element(member) del member root_coll = get_root_collection(IMyEntity) root_coll.add(data_el) self.assert_equal(len(coll), 1)
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_nested(self, class_entity_repo): session = class_entity_repo.session_factory() ent = MyEntity() parent = MyEntityParent() ent.parent = parent child = MyEntityChild() ent.children.append(child) session.add(MyEntity, ent) session.commit() assert len(session.query(MyEntityChild).all()) == 1 assert len(session.query(MyEntityParent).all()) == 1 fetched_ent = session.query(MyEntity).one() assert not fetched_ent.parent is None assert len(fetched_ent.children) == 1
def collection(): # FIXME: This uses a lot of the machinery we are trying to test # here. We should have some sort of pre-loading facility # like the cache loader for the entity repo. ent = MyEntity(id=0, number=1, text_ent='TEST', date_time= iso8601.parse_date('2012-06-13 11:06:47+02:00')) parent = MyEntityParent(id=0, text_ent='TEXT') ent.parent = parent child = MyEntityChild(id=0, text_ent='TEXT') ent.children.append(child) grandchild = MyEntityGrandchild(id=0, text='TEXT') child.children.append(grandchild) coll = get_root_collection(IMyEntity) coll.create_member(ent) transaction.commit() return coll
def test_state_data(self): data = dict(text='FOO', number=-1, parent=MyEntityParent(), children=[MyEntityChild()]) entity = MyEntity(**data) # We don't want to test the required unit of work here. uow = MagicMock() self.assert_raises(ValueError, EntityState.get_state, entity) entity.__everest__ = EntityState(entity, uow) state_data = EntityState.get_state(entity).data for attr, value in state_data.items(): if attr.resource_attr == 'number': number_attr = attr elif attr.resource_attr == 'parent': parent_attr = attr elif attr.resource_attr == 'parent_text': parent_text_attr = attr if attr.resource_attr in data: self.assert_equal(value, data[attr.resource_attr]) new_number = -2 state_data[number_attr] = new_number EntityState.get_state(entity).data = state_data self.assert_equal(entity.number, new_number) new_entity = MyEntity() self.assert_not_equal(new_entity.number, new_number) new_entity.__everest__ = EntityState(new_entity, uow) EntityState.transfer_state_data(entity, new_entity) self.assert_equal(new_entity.number, new_number) # Make setting invalid attribute fail. invalid_number_attr = terminal_attribute(str, 'grmbl') del state_data[number_attr] state_data[invalid_number_attr] = -2 with self.assert_raises(ValueError) as cm: EntityState.get_state(entity).data = state_data self.assert_true(cm.exception.args[0].startswith('Can not set')) # Make set nested attribute fail. entity.parent = None del state_data[invalid_number_attr] del state_data[parent_attr] state_data[parent_text_attr] = 'FOO PARENT' state = EntityState.get_state(entity) self.assert_raises(AttributeError, setattr, state, 'data', state_data)
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 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