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_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_update_nested_collection_from_data(self, resource_repo): # Set up member that has one child. coll = resource_repo.get_collection(IMyEntity) ent = MyEntity(id=1) child0 = MyEntityChild(id=0) ent.children.append(child0) mb = coll.create_member(ent) # Set up another member with two children with different IDs. tmp_coll = create_staging_collection(IMyEntity) upd_ent = MyEntity(id=1) child1 = MyEntityChild(id=1) child1.parent = upd_ent child2 = MyEntityChild(id=2) child2.parent = upd_ent upd_ent.children.append(child1) upd_ent.children.append(child2) upd_mb = tmp_coll.create_member(upd_ent) rpr = as_representer(mb, CsvMime) attribute_options = { ('children', ): { IGNORE_OPTION: False, WRITE_AS_LINK_OPTION: False }, } with rpr.with_updated_configuration( attribute_options=attribute_options): de = rpr.resource_to_data(upd_mb) mb.update(de) assert set([mb.id for mb in mb.children]) == set([1, 2])
def test_duplicate_id_raises_error(self, class_entity_repo): session = class_entity_repo.session_factory() ent_id = new_entity_id() ent1 = MyEntity(id=ent_id) session.add(MyEntity, ent1) ent2 = MyEntity(id=ent_id) with pytest.raises(ValueError): session.add(MyEntity, ent2)
def test_nested_with_set_collection_type(self, class_entity_repo): session = class_entity_repo.session_factory() ent = MyEntity() child = MyEntityChild() ent.children = set([child]) session.add(MyEntity, ent) session.commit() fetched_ent = session.query(MyEntity).one() assert isinstance(fetched_ent.children, set)
def test_find_added_with_none_slug_by_slug(self, class_entity_repo, monkeypatch): monkeypatch.setattr(MyEntity, 'slug', None) session = class_entity_repo.session_factory() ent1 = MyEntity() session.add(MyEntity, ent1) ent1.slug = 'testslug' ents = session.get_by_slug(MyEntity, ent1.slug) assert not ents is None assert ent1.id == ents[0].id
def test_add_same_slug(self, class_entity_repo, monkeypatch): monkeypatch.setattr(MyEntity, 'slug', None) session = class_entity_repo.session_factory() ent0 = MyEntity(id=0) ent0.slug = str(ent0.id) ent1 = MyEntity(id=1) ent1.slug = ent0.slug session.add(MyEntity, ent0) session.add(MyEntity , ent1) ents = session.get_by_slug(MyEntity, '0') assert len(ents) == 2
def test_failing_commit_duplicate_id(self, class_entity_repo): session = class_entity_repo.session_factory() ent1 = MyEntity() session.add(MyEntity, ent1) ent2 = MyEntity() session.add(MyEntity, ent2) assert ent1.id is None assert ent2.id is None ent2.id = ent1.id = 0 with pytest.raises(ValueError): session.commit()
def test_id_generation(self, class_entity_repo): session = class_entity_repo.session_factory() ent1 = MyEntity() session.add(MyEntity, ent1) session.commit() assert not ent1.id is None ent2 = MyEntity() session.add(MyEntity, ent2) session.commit() assert not ent2.id is None # entity IDs can be sorted by creation time. assert ent2.id > ent1.id
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 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_repeated_add_remove(self, class_entity_repo, monkeypatch): monkeypatch.setattr(MyEntity, 'slug', 'slug') session = class_entity_repo.session_factory() ent1 = MyEntity() session.add(MyEntity, ent1) assert session.get_by_slug(MyEntity, ent1.slug)[0] is ent1 session.remove(MyEntity, ent1) assert session.get_by_slug(MyEntity, ent1.slug) is None ent2 = MyEntity() session.add(MyEntity, ent2) assert session.get_by_slug(MyEntity, ent2.slug)[0] is ent2 session.remove(MyEntity, ent2) assert session.get_by_slug(MyEntity, ent2.slug) is None
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 test_filter_expr(self): expr0 = EvalFilterExpression(ValueEqualToFilterSpecification('id', 0)) expr1 = \ EvalFilterExpression(ValueEqualToFilterSpecification('text', 'text')) and_expr = expr0 & expr1 or_expr = expr0 | expr1 not_expr = ~expr0 ent0 = MyEntity(id=0, text='text') ent1 = MyEntity(id=1, text='text') ents = [ent0, ent1] self.assert_equal(list(and_expr(ents)), [ent0]) self.assert_equal(set(or_expr(ents)), set([ent0, ent1])) self.assert_equal(list(not_expr(ents)), [ent1])
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 test_basics(self): foo = MyEntity(id=0) foo_mb = MyEntityMember.create_from_entity(foo) self.coll.add(foo_mb) agg = self.coll.get_aggregate() self.assert_true(agg.get_by_id(foo.id) is foo) self.assert_true(agg.get_by_slug(foo.slug) is foo) foo1 = MyEntity(id=0) txt = 'FROBNIC' foo1.text = txt agg.update(foo1) self.assert_equal(agg.get_by_id(foo.id).text, txt) self.assert_equal(len(list(agg.iterator())), 1) agg.remove(foo) self.assert_equal(len(list(agg.iterator())), 0)
def test_basics(self): agg = StagingAggregate(MyEntity) ent0 = MyEntity(id=0, text='text0') ent1 = MyEntity(id=1, text='text1') agg.add(ent0) agg.add(ent1) q = agg.query() filter_expr = \ EvalFilterExpression(ValueEqualToFilterSpecification('id', 0)) self.assert_equal(q.filter(filter_expr).all(), [ent0]) self.assert_equal(len(q.slice(1, 2).all()), 1) self.assert_equal(q.slice(1, 2).count(), 2) order_expr = EvalOrderExpression(AscendingOrderSpecification('text')) q = q.order(order_expr) self.assert_equal(q.all()[0].text, 'text0')
def test_put_member_raises_error(self, exc_vw_app_creator): coll = get_root_collection(IMyEntity) ent = MyEntity(id=0) coll.create_member(ent) exc_vw_app_creator.put("%s/0" % self.path, params='dummy body', status=500)
def test_add(self): coll = get_root_collection(IMyEntity) ent = MyEntity(id=2) mb_add = MyEntityMember.create_from_entity(ent) coll.add(mb_add) transaction.commit() self.assert_equal(len(coll), 2)
def test_add_deleted(self, class_entity_repo): session = class_entity_repo.session_factory() ent = MyEntity() session.add(MyEntity, ent) session.commit() session.remove(MyEntity, ent) session.add(MyEntity, ent)
def test_add_immediate_remove(self, class_entity_repo): session = class_entity_repo.session_factory() ent1 = MyEntity() session.add(MyEntity, ent1) session.remove(MyEntity, ent1) assert not ent1 in session assert len(session.query(MyEntity).all()) == 0
def test_basics(self, class_entity_repo): session = class_entity_repo.session_factory() ent = MyEntity() assert ent.id is None assert ent.slug is None # Load without ID fails. with pytest.raises(ValueError) as cm: session.load(MyEntity, ent) assert cm.value.args[0].startswith('Can not load') session.add(MyEntity, ent) assert ent in session assert ent in session.query(MyEntity) assert list(session.new) == [ent] # Commit triggers ID generation. session.commit() assert not ent.id is None assert not ent.slug is None # After commit, the session is empty. assert not ent in session assert len(session.query(MyEntity).all()) == 1 # Test loading by ID and slug. fetched_ent0 = session.get_by_id(MyEntity, ent.id) assert fetched_ent0.slug == ent.slug fetched_ent1 = session.get_by_slug(MyEntity, ent.slug)[0] assert fetched_ent1.id == ent.id # We get a clone when we load an entity from the session. assert not fetched_ent0 is ent # Once loaded, we always get the same entity. assert fetched_ent0 is fetched_ent1 session.remove(MyEntity, fetched_ent0) assert len(session.query(MyEntity).all()) == 0 assert session.get_by_id(MyEntity, ent.id) is None assert session.get_by_slug(MyEntity, ent.slug) is None assert list(session.deleted) == [fetched_ent0]
def test_add_remove_same_member(self, resource_repo_with_data): coll = resource_repo_with_data.get_collection(IMyEntity) ent = MyEntity(id=1) mb = MyEntityMember.create_from_entity(ent) coll.add(mb) coll.remove(mb) assert len(coll) == 1
def test_add(self, resource_repo_with_data): coll = resource_repo_with_data.get_collection(IMyEntity) ent = MyEntity(id=2) mb_add = MyEntityMember.create_from_entity(ent) coll.add(mb_add) transaction.commit() assert len(coll) == 2
def test_update_nested_member_from_data(self, resource_repo): # Set up member that does not have a parent. coll = resource_repo.get_collection(IMyEntity) ent = MyEntity(id=1) mb = coll.create_member(ent) # Set up second member with same ID that does have a parent. tmp_coll = create_staging_collection(IMyEntity) parent = MyEntityParent(id=0) upd_ent = MyEntity(id=1, parent=parent) upd_mb = tmp_coll.create_member(upd_ent) rpr = as_representer(mb, CsvMime) attribute_options = {('parent', ): {WRITE_AS_LINK_OPTION: False}} with rpr.with_updated_configuration( attribute_options=attribute_options): de = rpr.resource_to_data(upd_mb) mb.update(de) assert mb.parent.id == parent.id
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_add_one_to_one(self, class_entity_repo): new_parent1 = MyEntityParent(id=1) new_ent1 = MyEntity(id=1) parent_rel_agg = self._make_rel_agg(class_entity_repo, new_ent1, 'parent') assert new_ent1.parent is None parent_rel_agg.add(new_parent1) assert new_ent1.parent == new_parent1
def test_find_added_by_id(self, class_entity_repo, monkeypatch): monkeypatch.setattr(MyEntity, 'slug', 'slug') session = class_entity_repo.session_factory() ent1 = MyEntity(id=0) session.add(MyEntity, ent1) ent2 = session.get_by_id(MyEntity, ent1.id) assert not ent2 is None assert ent1.id == ent2.id
def test_add_remove_same_member(self): coll = get_root_collection(IMyEntity) ent = MyEntity(id=1) mb = MyEntityMember.create_from_entity(ent) coll.add(mb) transaction.commit() coll.remove(mb) transaction.commit() self.assert_equal(len(coll), 1)
def test_update_nested_member_from_data(self): # Set up member that does not have a parent. ent = MyEntity(id=1) mb = MyEntityMember.create_from_entity(ent) # Set up second member with same ID that does have a parent. parent = MyEntityParent(id=0) upd_ent = MyEntity(id=1, parent=parent) upd_mb = MyEntityMember.create_from_entity(upd_ent) rpr = as_representer(mb, CsvMime) attribute_options = { ('parent', ): { WRITE_AS_LINK_OPTION: False }, } rpr.configure(attribute_options=attribute_options) de = rpr.data_from_resource(upd_mb) mb.update_from_data(de) self.assert_equal(mb.parent.id, parent.id)
def test_add_remove(self, resource_repo_with_data): coll = resource_repo_with_data.get_collection(IMyEntity) mb_rm = next(iter(coll)) coll.remove(mb_rm) ent = MyEntity(id=1) mb_add = MyEntityMember.create_from_entity(ent) coll.add(mb_add) transaction.commit() assert len(coll) == 1
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 _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
def test_nested_with_invalid_collection_data(self, class_entity_repo): session = class_entity_repo.session_factory() ent = MyEntity() ent.children = [None] with pytest.raises(ValueError): session.add(MyEntity, ent)