示例#1
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_update_nested_collection_from_data(self):
     # Set up member that has one child.
     root_coll = get_root_collection(IMyEntity)
     ent = MyEntity(id=1)
     child0 = MyEntityChild(id=0)
     ent.children.append(child0)
     mb = root_coll.create_member(ent)
     # Set up another member with two children with different IDs.
     stg_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 = stg_coll.create_member(upd_ent)
     rpr = as_representer(mb, CsvMime)
     attribute_options = {('children',):{IGNORE_OPTION:False,
                                         WRITE_AS_LINK_OPTION:False}, }
     rpr.configure(attribute_options=attribute_options)
     de = rpr.data_from_resource(upd_mb)
     #
     mb.update(de)
     self.assert_equal(set([mb.id for mb in mb.children]), set([1, 2]))
示例#3
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
示例#4
0
 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
示例#5
0
def create_entity_tree(id=0, text=None):  # pylint: disable=W0622
    my_entity_grandchild = MyEntityGrandchild(id=id, text=text)
    my_entity_child = MyEntityChild(id=id, text=text, children=[my_entity_grandchild])
    my_entity_parent = MyEntityParent(id=id, text=text)
    my_entity = MyEntity(id=id, text=text, children=[my_entity_child], parent=my_entity_parent)
    # 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
示例#6
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')
示例#7
0
 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
示例#8
0
 def test_update_cascade(self, class_entity_repo, monkeypatch):
     csc = DEFAULT_CASCADE & ~RELATION_OPERATIONS.UPDATE
     child_agg = class_entity_repo.get_aggregate(IMyEntityChild)
     new_child = self._make_child(child_agg)
     child_rel_agg = self._make_rel_agg(class_entity_repo,
                                        new_child.parent)
     children_attr = get_domain_class_attribute(MyEntity, 'children')
     monkeypatch.setattr(children_attr, 'cascade', csc)
     upd_child = MyEntityChild(id=0)
     txt = 'FROBNIC'
     upd_child.text = txt
     child_rel_agg.update(upd_child)
     assert new_child.text != txt
     assert not new_child.parent is None
示例#9
0
 def test_add_with_relationship(self):
     ent, agg_children = self._make_one()
     rel = Relationship(ent, ent.children)
     agg_children.set_relationship(rel)
     self.assert_raises(ValueError, agg_children.add, ent.children[0])
     self.assert_equal(len(list(agg_children.iterator())), 1)
     # children.append adds to agg.
     new_child = MyEntityChild()
     ent.children.append(new_child)
     self.assert_equal(len(list(agg_children.iterator())), 2)
     self.assert_equal(len(ent.children), 2)
     # agg.add appends to children.
     new_child1 = MyEntityChild()
     agg_children.add(new_child1)
     self.assert_equal(len(list(agg_children.iterator())), 3)
     self.assert_equal(len(ent.children), 3)
示例#10
0
def create_entity(entity_id=0, entity_text=None):
    my_entity_parent = MyEntityParent(id=entity_id, text=entity_text)
    my_entity_grandchild = MyEntityGrandchild(id=entity_id, text=entity_text)
    my_entity_child = MyEntityChild(id=entity_id,
                                    text=entity_text,
                                    children=[my_entity_grandchild])
    my_entity = MyEntity(id=entity_id,
                         text=entity_text,
                         parent=my_entity_parent,
                         children=[my_entity_child])
    # 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
示例#11
0
def create_entity(entity_id=0, entity_text=None):
    my_entity_parent = MyEntityParent(id=entity_id, text=entity_text)
    my_entity_grandchild = MyEntityGrandchild(id=entity_id, text=entity_text)
    my_entity_child = MyEntityChild(id=entity_id,
                                    text=entity_text,
                                    children=[my_entity_grandchild])
    my_entity = MyEntity(id=entity_id,
                         text=entity_text,
                         parent=my_entity_parent,
                         children=[my_entity_child])
    # 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
示例#12
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])
示例#13
0
 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)
示例#14
0
 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)
示例#15
0
文件: testing.py 项目: b8va/everest
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
示例#16
0
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)
示例#17
0
 def test_add(self):
     ent, agg_children = self._make_one()
     self.assert_raises(ValueError, agg_children.add, None)
     self.assert_raises(ValueError, agg_children.add, ent.children[0])
     new_child = MyEntityChild()
     self.assert_equal(len(list(agg_children.iterator())), 1)
     # agg.add does not append to children.
     agg_children.add(new_child)
     self.assert_equal(len(list(agg_children.iterator())), 2)
     self.assert_equal(len(ent.children), 1)
示例#18
0
 def test_update_from_data_add_child(self):
     my_entity = create_entity()
     new_child = MyEntityChild()
     my_entity.children.append(new_child)
     if new_child.parent is None:
         new_child.parent = my_entity
     coll = create_staging_collection(IMyEntity)
     member = coll.create_member(my_entity)
     self.assert_equal(len(member.children), 2)
     mp = self._make_mapping()
     data_el = mp.map_to_data_element(member)
     del member
     del my_entity
     #        import gc; gc.collect()
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(len(context.children), 1)
     context.update(data_el)
     self.assert_equal(len(context.children), 2)
示例#19
0
    def test_update_from_data_add_child(self):
        my_entity = create_entity()
        new_child = MyEntityChild()
        my_entity.children.append(new_child)
        if new_child.parent is None:
            new_child.parent = my_entity
        coll = create_staging_collection(IMyEntity)
        member = coll.create_member(my_entity)
        self.assert_equal(len(member.children), 2)
        mp = self._make_mapping()
        data_el = mp.map_to_data_element(member)
        del member
        del my_entity
#        import gc; gc.collect()
        my_entity = create_entity()
        coll = get_root_collection(IMyEntity)
        context = coll.create_member(my_entity)
        self.assert_equal(len(context.children), 1)
        context.update(data_el)
        self.assert_equal(len(context.children), 2)
示例#20
0
def create_entity_tree(id=0, text=None):  # pylint: disable=W0622
    my_entity_grandchild = MyEntityGrandchild(id=id, text=text)
    my_entity_child = MyEntityChild(id=id,
                                    text=text,
                                    children=[my_entity_grandchild])
    my_entity_parent = MyEntityParent(
        id=id,
        text=text,
    )
    my_entity = MyEntity(id=id,
                         text=text,
                         children=[my_entity_child],
                         parent=my_entity_parent)
    # 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
示例#21
0
 def test_entity_attr(self):
     attr = get_domain_class_attribute(IMyEntity, 'parent')
     my_parent = MyEntityParent(id=0)
     self._run_test(attr, my_parent, 'child',
                    ValueEqualToFilterSpecification,
                    ValueEqualToFilterSpecification, 0)
     attr = get_domain_class_attribute(IMyEntityChild, 'parent')
     my_child = MyEntityChild(id=1)
     self._run_test(attr, my_child, 'children',
                    ValueContainsFilterSpecification,
                    ValueEqualToFilterSpecification, 1)
示例#22
0
文件: test_io.py 项目: b8va/everest
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
示例#23
0
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
示例#24
0
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
示例#25
0
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
示例#26
0
 def test_collection_access(self):
     parent = MyEntityParent()
     entity = MyEntity(parent=parent)
     coll = get_root_collection(IMyEntity)
     member = coll.create_member(entity)
     self.assert_true(isinstance(member.children, Collection))
     i = 0
     n = 5
     while i < n:
         child_entity = MyEntityChild()
         member.children.create_member(child_entity)
         i += 1
     self.assert_equal(len(member.children), n)
示例#27
0
 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
示例#28
0
 def test_sync_with_repository(self, resource_repo_with_data):
     coll = resource_repo_with_data.get_collection(IMyEntity)
     ent = MyEntity()
     mb_add = MyEntityMember.create_from_entity(ent)
     coll.add(mb_add)
     assert mb_add.id is None
     coll.get_aggregate().sync_with_repository()
     assert not mb_add.id is None
     rel_coll = mb_add.children
     ent_child = MyEntityChild()
     mb_add_child = MyEntityChildMember.create_from_entity(ent_child)
     rel_coll.add(mb_add_child)
     assert mb_add_child.id is None
     rel_coll.get_aggregate().sync_with_repository()
     assert not mb_add_child.id is None
示例#29
0
 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)
示例#30
0
 def test_find_connected_with_custom_deps(self):
     member = _make_test_entity_member()
     ent = member.get_entity()
     # Point grandchild's parent to new child.
     new_child = MyEntityChild(id=1, parent=ent)
     ent.children[0].children[0].parent = new_child
     # When backrefs are excluded, we should not pick up the new parent
     # of the grandchild; when backrefs are included, we should.
     dep_grph = build_resource_dependency_graph(self._interfaces)
     self.assert_false(dep_grph.has_edge((MyEntityGrandchildMember,
                                          MyEntityChildMember)))
     coll_map = find_connected_resources(member)
     self.assert_equal(len(coll_map[MyEntityChildMember]), 1)
     dep_grph = \
         build_resource_dependency_graph(self._interfaces,
                                         include_backrefs=True)
     self.assert_true(dep_grph.has_edge((MyEntityGrandchildMember,
                                         MyEntityChildMember)))
     coll_map = find_connected_resources(member,
                                         dependency_graph=dep_grph)
     self.assert_equal(len(coll_map[MyEntityChildMember]), 2)
示例#31
0
 def test_string_representation(self):
     attr = get_domain_class_attribute(IMyEntity, 'children')
     my_child = MyEntityChild(id=0)
     rel = attr.make_relationship(my_child)
     self.assert_true('<->' in str(rel))
示例#32
0
 def test_aggregate_attr(self):
     attr = get_domain_class_attribute(IMyEntity, 'children')
     my_child = MyEntityChild(id=0)
     self._run_test(attr, my_child, 'parent',
                    ValueEqualToFilterSpecification,
                    ValueContainedFilterSpecification, [0])