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_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_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_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_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_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_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_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_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_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(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_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_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_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_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_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 test_find_added_by_slug(self, class_entity_repo, monkeypatch): monkeypatch.setattr(MyEntity, 'slug', 'slug') session = class_entity_repo.session_factory() ent1 = MyEntity() session.add(MyEntity, ent1) ents = session.get_by_slug(MyEntity, ent1.slug) assert not ents is None assert ent1.id == ents[0].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_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): coll = get_root_collection(IMyEntity) mb_rm = iter(coll).next() coll.remove(mb_rm) ent = MyEntity(id=1) mb_add = MyEntityMember.create_from_entity(ent) coll.add(mb_add) transaction.commit() self.assert_equal(len(coll), 1)
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_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_collection_access(self): parent = MyEntityParent(id=0) entity = MyEntity(id=0, parent=parent) coll = get_root_collection(IMyEntity) member = coll.create_member(entity) self.assert_true(isinstance(member.children, Collection)) child_entity = MyEntityChild() member.children.create_member(child_entity) self.assert_equal(len(member.children), 1)
def _make_test_entity_member(): parent = MyEntityParent(id=0) entity = MyEntity(id=0, parent=parent) parent.child = entity child = MyEntityChild(id=0, parent=entity) entity.children.append(child) grandchild = MyEntityGrandchild(id=0, parent=child) child.children.append(grandchild) coll = create_staging_collection(IMyEntity) return coll.create_member(entity)