Пример #1
0
 def set_up(self):
     EntityTestCase.set_up(self)
     self._ent0 = create_entity(entity_id=0, entity_text='foo0')
     self._ent1 = create_entity(entity_id=1, entity_text='foo1')
     self._aggregate = get_root_aggregate(IMyEntity)
     self._aggregate.add(self._ent0)
     self._aggregate.add(self._ent1)
Пример #2
0
 def test_update_member_with_link(self):
     my_entity = create_entity()
     new_parent = MyEntityParent()
     new_parent.text = self.UPDATED_TEXT
     new_parent.id = 2
     my_entity.parent = new_parent
     coll = create_staging_collection(IMyEntity)
     member = coll.create_member(my_entity)
     mp = self._make_mapping()
     attribute_options = {('parent',):{WRITE_AS_LINK_OPTION:True},
                        ('nested_parent',):{IGNORE_OPTION:True}}
     mp_cloned = mp.clone(attribute_options=attribute_options)
     data_el = mp_cloned.map_to_data_element(member)
     # The linked-to parent needs to be in the root collection.
     my_entity.parent = None
     del member
     del my_entity
     parent_coll = get_root_collection(IMyEntityParent)
     parent_coll.create_member(new_parent)
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT)
     context.update_from_data(data_el)
     self.assert_equal(context.parent.text, self.UPDATED_TEXT)
Пример #3
0
 def set_up(self):
     EntityTestCase.set_up(self)
     self._ent0 = create_entity(entity_id=0, entity_text='foo0')
     self._ent1 = create_entity(entity_id=1, entity_text='foo1')
     self._aggregate = get_root_aggregate(IMyEntity)
     self._aggregate.add(self._ent0)
     self._aggregate.add(self._ent1)
Пример #4
0
 def test_update_member_with_link(self):
     my_entity = create_entity()
     new_parent = MyEntityParent()
     new_parent.text = self.UPDATED_TEXT
     new_parent.id = 2
     my_entity.parent = new_parent
     coll = create_staging_collection(IMyEntity)
     member = coll.create_member(my_entity)
     mp = self._make_mapping()
     attribute_options = {
         ('parent', ): {
             WRITE_AS_LINK_OPTION: True
         },
         ('nested_parent', ): {
             IGNORE_OPTION: True
         }
     }
     mp_cloned = mp.clone(attribute_options=attribute_options)
     data_el = mp_cloned.map_to_data_element(member)
     # The linked-to parent needs to be in the root collection.
     my_entity.parent = None
     del member
     del my_entity
     parent_coll = get_root_collection(IMyEntityParent)
     parent_coll.create_member(new_parent)
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT)
     context.update_from_data(data_el)
     self.assert_equal(context.parent.text, self.UPDATED_TEXT)
Пример #5
0
 def test_order_by(self):
     ent2 = create_entity(entity_id=2, entity_text='foo3')
     ent3 = create_entity(entity_id=3, entity_text='foo3')
     self._aggregate.add(ent2)
     self._aggregate.add(ent3)
     q = self._query.order_by(('text', ASCENDING), ('id', DESCENDING))
     last_ent = q.all()[-1]
     self.assert_true(last_ent is ent2)
     self.assert_raises(ValueError, self._query.order_by, ('text', None))
Пример #6
0
 def test_order_by(self):
     ent2 = create_entity(entity_id=2, entity_text='foo3')
     ent3 = create_entity(entity_id=3, entity_text='foo3')
     self._aggregate.add(ent2)
     self._aggregate.add(ent3)
     q = self._query.order_by(('text', ASCENDING), ('id', DESCENDING))
     last_ent = q.all()[-1]
     self.assert_true(last_ent is ent2)
     self.assert_raises(ValueError, self._query.order_by, ('text', None))
Пример #7
0
 def _test_order(self, txt_expr, id_expr):
     ent2 = create_entity(entity_id=2, entity_text='foo3')
     ent3 = create_entity(entity_id=3, entity_text='foo3')
     self._aggregate.add(ent2)
     self._aggregate.add(ent3)
     q = self._query.order(txt_expr)
     self.assert_true(next(iter(q)) is self._ent0)
     q = q.order(None)
     q = q.order(txt_expr).order(id_expr)
     last_ent = q.all()[-1]
     self.assert_true(last_ent is ent2)
Пример #8
0
 def _test_order(self, txt_expr, id_expr):
     ent2 = create_entity(entity_id=2, entity_text='foo3')
     ent3 = create_entity(entity_id=3, entity_text='foo3')
     self._aggregate.add(ent2)
     self._aggregate.add(ent3)
     q = self._query.order(txt_expr)
     self.assert_true(next(iter(q)) is self._ent0)
     q = q.order(None)
     q = q.order(txt_expr).order(id_expr)
     last_ent = q.all()[-1]
     self.assert_true(last_ent is ent2)
Пример #9
0
 def test_traverse_with_remove_sequence(self):
     ent0 = create_entity(entity_id=0)
     ent1 = create_entity(entity_id=None)
     cache = EntityCacheMap()
     agg = StagingAggregate(MyEntity, cache=cache)
     agg.add(ent0)
     agg.add(ent1)
     trv = SourceTargetDataTreeTraverser.make_traverser(
         None, [ent0, ent1], RELATION_OPERATIONS.REMOVE)
     vst = AruVisitor(MyEntity, remove_callback=agg.remove)
     trv.run(vst)
     self.assert_equal(len(list(iter(agg))), 0)
Пример #10
0
 def test_traverse_with_remove_sequence(self):
     ent0 = create_entity(entity_id=0)
     ent1 = create_entity(entity_id=None)
     cache = EntityCacheMap()
     agg = StagingAggregate(MyEntity, cache=cache)
     agg.add(ent0)
     agg.add(ent1)
     trv = SourceTargetDataTreeTraverser.make_traverser(
                                             None, [ent0, ent1],
                                             RELATION_OPERATIONS.REMOVE)
     vst = AruVisitor(MyEntity, remove_callback=agg.remove)
     trv.run(vst)
     self.assert_equal(len(list(iter(agg))), 0)
Пример #11
0
 def test_update_terminal_in_parent(self):
     my_entity = create_entity()
     my_entity.parent.text = self.UPDATED_TEXT
     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
     del my_entity
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT)
     context.update_from_data(data_el)
     self.assert_equal(context.parent.text, self.UPDATED_TEXT)
Пример #12
0
 def test_delete_grandchild(self):
     my_entity = create_entity()
     del my_entity.children[0].children[0]
     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
     del my_entity
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(len(iter(context.children).next().children), 1)
     context.update_from_data(data_el)
     self.assert_equal(len(iter(context.children).next().children), 0)
Пример #13
0
 def test_delete_child(self):
     my_entity = create_entity()
     del my_entity.children[0]
     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
     del my_entity
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(len(context.children), 1)
     context.update_from_data(data_el)
     self.assert_equal(len(context.children), 0)
Пример #14
0
 def test_make_traverser_invalid_params(self):
     ent0 = create_entity(entity_id=None)
     ent1 = create_entity(entity_id=None)
     self.assert_raises(ValueError,
                        SourceTargetDataTreeTraverser.make_traverser, ent0,
                        ent1, RELATION_OPERATIONS.ADD)
     self.assert_raises(ValueError,
                        SourceTargetDataTreeTraverser.make_traverser, ent1,
                        ent0, RELATION_OPERATIONS.REMOVE)
     self.assert_raises(ValueError,
                        SourceTargetDataTreeTraverser.make_traverser,
                        ent0,
                        None,
                        RELATION_OPERATIONS.UPDATE,
                        accessor=None)
Пример #15
0
 def test_update_terminal_in_parent(self):
     my_entity = create_entity()
     my_entity.parent.text = self.UPDATED_TEXT
     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
     del my_entity
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT)
     context.update_from_data(data_el)
     self.assert_equal(context.parent.text, self.UPDATED_TEXT)
Пример #16
0
 def _test(cnt_type, parent_repr_name, children_repr_name):
     entity = create_entity()
     mb = MyEntityMember.create_from_entity(entity)
     mp_reg = get_mapping_registry(cnt_type)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     de = mp.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     self.assert_true(prx.get_data_element() is de)
     self.assert_equal(prx.id, 0)
     self.assert_equal(prx.text, 'TEXT')
     self.assert_equal(prx.number, 1)
     # The parent and children attributes are links.
     self.assert_true(isinstance(getattr(prx, parent_repr_name),
                                 LinkedDataElement))
     children_el = getattr(prx, children_repr_name)
     if cnt_type is XmlMime:
         self.assert_is_none(children_el)
     else:
         self.assert_true(isinstance(children_el, LinkedDataElement))
     # Nonexisting attribute raises error.
     self.assert_raises(AttributeError, getattr, prx, 'foo')
     self.assert_raises(AttributeError, setattr, prx, 'foo', 'murks')
     # Set terminal attribute.
     prx.id = 1
     self.assert_equal(prx.id, 1)
     # Set nested attribute.
     setattr(prx, parent_repr_name, None)
     self.assert_is_none(getattr(prx, parent_repr_name))
     self.assert_raises(ValueError, setattr, prx, parent_repr_name,
                        1)
Пример #17
0
 def test_map_to_data_element_with_collection(self, resource_repo):
     entity = create_entity()
     coll = resource_repo.get_collection(IMyEntity)
     mb = coll.create_member(entity)
     assert len(entity.children) == 1
     assert len(mb.children) == 1
     mb_child = next(iter(mb.children))
     assert len(mb_child.children) == 1
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(
         attribute_options={
             ('children', ): {
                 IGNORE_OPTION: False,
                 WRITE_AS_LINK_OPTION: False
             },
             ('children', 'children'): {
                 IGNORE_OPTION: False,
                 WRITE_AS_LINK_OPTION: False
             }
         })
     de = mp1.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     assert len(prx.children) == 1
     assert len(prx.children[0].children) == 1
Пример #18
0
 def test_make_traverser_invalid_params(self):
     ent0 = create_entity(entity_id=None)
     ent1 = create_entity(entity_id=None)
     self.assert_raises(ValueError,
                        SourceTargetDataTreeTraverser.make_traverser,
                        ent0, ent1,
                        RELATION_OPERATIONS.ADD)
     self.assert_raises(ValueError,
                        SourceTargetDataTreeTraverser.make_traverser,
                        ent1, ent0,
                        RELATION_OPERATIONS.REMOVE)
     self.assert_raises(ValueError,
                        SourceTargetDataTreeTraverser.make_traverser,
                        ent0, None,
                        RELATION_OPERATIONS.UPDATE,
                        accessor=None)
Пример #19
0
 def test_urls(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     mb = coll.create_member(my_entity)
     exp_url = '/my-entities/0/'
     url = resource_to_url(mb)
     self.assert_true(url.endswith(exp_url))
     exp_url = '/my-entity-parents/0/'
     url = resource_to_url(mb.parent)
     self.assert_true(url.endswith(exp_url))
     exp_url = 'my-entities/0/nested-parent/'
     url = resource_to_url(mb.nested_parent)
     self.assert_true(url.endswith(exp_url))
     exp_url = '/my-entities/0/children/'
     url = resource_to_url(mb.children)
     self.assert_true(url.endswith(exp_url))
     mb_child = mb.children['0']
     self.assert_equal(mb_child.id, 0)
     exp_url = '/my-entity-grandchildren/?q=parent:equal-to:' \
               'http://0.0.0.0:6543/my-entities/0/children/0/'
     url = resource_to_url(mb_child.children)
     self.assert_true(url.endswith(exp_url))
     exp_url = 'my-entity-grandchildren/?q=id:contained:0'
     url = resource_to_url(mb_child.no_backref_children)
     self.assert_true(url.endswith(exp_url))
Пример #20
0
 def test_resource_to_url_floating_member(self):
     ent = create_entity(entity_id=2)
     mb = MyEntityMember.create_from_entity(ent)
     with self.assert_raises(ValueError) as cm:
         resource_to_url(mb)
     exc_msg = 'Can not generate URL for floating member'
     self.assert_true(cm.exception.message.startswith(exc_msg))
Пример #21
0
 def test_new_stage_collection(self):
     ent = create_entity(entity_id=2, entity_text='too2')
     mb = MyEntityMember.create_from_entity(ent)
     nscoll = create_staging_collection(IMyEntity)
     self.assert_equal(len(nscoll), 0)
     nscoll.add(mb)
     self.assert_equal(len(nscoll), 1)
Пример #22
0
 def test_new_stage_collection(self):
     ent = create_entity(entity_id=2, entity_text='too2')
     mb = MyEntityMember.create_from_entity(ent)
     nscoll = create_staging_collection(IMyEntity)
     self.assert_equal(len(nscoll), 0)
     nscoll.add(mb)
     self.assert_equal(len(nscoll), 1)
Пример #23
0
 def _test(cnt_type, parent_repr_name, children_repr_name):
     entity = create_entity()
     mb = MyEntityMember.create_from_entity(entity)
     mp_reg = get_mapping_registry(cnt_type)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     de = mp.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     self.assert_true(prx.get_data_element() is de)
     self.assert_equal(prx.id, 0)
     self.assert_equal(prx.text, 'TEXT')
     self.assert_equal(prx.number, 1)
     # The parent and children attributes are links.
     self.assert_true(
         isinstance(getattr(prx, parent_repr_name), LinkedDataElement))
     children_el = getattr(prx, children_repr_name)
     if cnt_type is XmlMime:
         self.assert_is_none(children_el)
     else:
         self.assert_true(isinstance(children_el, LinkedDataElement))
     # Nonexisting attribute raises error.
     self.assert_raises(AttributeError, getattr, prx, 'foo')
     self.assert_raises(AttributeError, setattr, prx, 'foo', 'murks')
     # Set terminal attribute.
     prx.id = 1
     self.assert_equal(prx.id, 1)
     # Set nested attribute.
     setattr(prx, parent_repr_name, None)
     self.assert_is_none(getattr(prx, parent_repr_name))
     self.assert_raises(ValueError, setattr, prx, parent_repr_name, 1)
Пример #24
0
 def test_urls(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     mb = coll.create_member(my_entity)
     exp_url = '/my-entities/0/'
     url = resource_to_url(mb)
     self.assert_true(url.endswith(exp_url))
     exp_url = '/my-entity-parents/0/'
     url = resource_to_url(mb.parent)
     self.assert_true(url.endswith(exp_url))
     exp_url = 'my-entities/0/nested-parent/'
     url = resource_to_url(mb.nested_parent)
     self.assert_true(url.endswith(exp_url))
     exp_url = '/my-entities/0/children/'
     url = resource_to_url(mb.children)
     self.assert_true(url.endswith(exp_url))
     mb_child = mb.children['0']
     self.assert_equal(mb_child.id, 0)
     exp_url = '/my-entity-grandchildren/?q=parent:equal-to:' \
               'http://0.0.0.0:6543/my-entities/0/children/0/'
     url = resource_to_url(mb_child.children)
     self.assert_true(url.endswith(exp_url))
     exp_url = 'my-entity-grandchildren/?q=id:contained:0'
     url = resource_to_url(mb_child.no_backref_children)
     self.assert_true(url.endswith(exp_url))
Пример #25
0
 def test_add_child(self):
     my_entity = create_entity()
     new_child = MyEntityChild()
     my_entity.children.append(new_child)
     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
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     context = coll.create_member(my_entity)
     self.assert_equal(len(context.children), 1)
     context.update_from_data(data_el)
     self.assert_equal(len(context.children), 2)
Пример #26
0
 def test_no_backref_collection_url(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     mb = coll.create_member(my_entity)
     mb_child = mb.children['0']
     exp_url = 'my-entity-grandchildren/?q=id:contained:0'
     url = resource_to_url(mb_child.children)
     self.assert_true(url.endswith(exp_url))
Пример #27
0
 def test_no_backref_collection_url(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     mb = coll.create_member(my_entity)
     mb_child = mb.children['0']
     exp_url = 'my-entity-grandchildren/?q=id:contained:0'
     url = resource_to_url(mb_child.children)
     self.assert_true(url.endswith(exp_url))
Пример #28
0
 def test_remove_with_data_element(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     member = coll.create_member(my_entity)
     mp = self._make_mapping()
     data_el = mp.map_to_data_element(member)
     del member
     coll.remove(data_el)
     self.assert_equal(len(coll), 0)
Пример #29
0
 def _make_one(self):
     rc_repo = self._get_repo()
     ent = create_entity()
     agg = rc_repo.get_aggregate(IMyEntity)
     agg.add(ent)
     agg_children = rc_repo.get_aggregate(IMyEntityChild)
     for child in ent.children:
         agg_children.add(child)
     return ent, agg_children
Пример #30
0
 def test_remove_with_data_element(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     member = coll.create_member(my_entity)
     mp = self._make_mapping()
     data_el = mp.map_to_data_element(member)
     del member
     coll.remove(data_el)
     self.assert_equal(len(coll), 0)
Пример #31
0
 def _make_one(self):
     rc_repo = self._get_repo()
     ent = create_entity()
     agg = rc_repo.get_aggregate(IMyEntity)
     agg.add(ent)
     agg_children = rc_repo.get_aggregate(IMyEntityChild)
     for child in ent.children:
         agg_children.add(child)
     return ent, agg_children
Пример #32
0
 def test_make_traverser_update(self):
     ent0 = create_entity(entity_id=0)
     ent1 = create_entity(entity_id=None)
     agg = create_staging_collection(IMyEntity).get_aggregate()
     agg.add(ent0)
     agg.add(ent1)
     ent01 = create_entity(entity_id=0)
     ent11 = create_entity(entity_id=None)
     # With many as source and one as target.
     with self.assert_raises(ValueError) as cm:
         SourceTargetDataTreeTraverser.make_traverser(
             [ent01, ent1], ent0, RELATION_OPERATIONS.UPDATE, accessor=agg)
     self.assert_true(
         cm.exception.args[0].endswith('or both not be sequences.'))
     # Without target.
     trv = SourceTargetDataTreeTraverser.make_traverser(
         [ent01, ent11], None, RELATION_OPERATIONS.UPDATE, accessor=agg)
     self.assert_is_not_none(getattr(trv, '_tgt_prx'))
Пример #33
0
 def test_set_member(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     mb = coll.create_member(my_entity)
     txt = 'FROBNIC'
     new_parent = MyEntityParent(text=txt)
     parent_coll = get_root_collection(IMyEntityParent)
     parent_mb = parent_coll.create_member(new_parent)
     self.assert_not_equal(mb.parent.text, txt)
     mb.parent = parent_mb
     self.assert_equal(mb.parent.text, txt)
Пример #34
0
 def test_set_member(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     mb = coll.create_member(my_entity)
     txt = 'FROBNIC'
     new_parent = MyEntityParent(text=txt)
     parent_coll = get_root_collection(IMyEntityParent)
     parent_mb = parent_coll.create_member(new_parent)
     self.assert_not_equal(mb.parent.text, txt)
     mb.parent = parent_mb
     self.assert_equal(mb.parent.text, txt)
Пример #35
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)
Пример #36
0
 def test_make_traverser_update(self):
     ent0 = create_entity(entity_id=0)
     ent1 = create_entity(entity_id=None)
     agg = create_staging_collection(IMyEntity).get_aggregate()
     agg.add(ent0)
     agg.add(ent1)
     ent01 = create_entity(entity_id=0)
     ent11 = create_entity(entity_id=None)
     # With many as source and one as target.
     with self.assert_raises(ValueError) as cm:
         SourceTargetDataTreeTraverser.make_traverser(
                                             [ent01, ent1], ent0,
                                             RELATION_OPERATIONS.UPDATE,
                                             accessor=agg)
     self.assert_true(
             cm.exception.args[0].endswith('or both not be sequences.'))
     # Without target.
     trv = SourceTargetDataTreeTraverser.make_traverser(
                                             [ent01, ent11], None,
                                             RELATION_OPERATIONS.UPDATE,
                                             accessor=agg)
     self.assert_is_not_none(getattr(trv, '_tgt_prx'))
Пример #37
0
 def test_filter_specification_visitor(self):
     coll = get_root_collection(IMyEntity)
     mb_cls = get_member_class(coll)
     my_entity = create_entity()
     member = coll.create_member(my_entity)
     spec_fac = FilterSpecificationFactory()
     specs = [
             # Terminal access.
             spec_fac.create_equal_to('text', self.TEST_TEXT),
             # Terminal access with different name in entity.
             spec_fac.create_equal_to('text_rc', self.TEST_TEXT),
             # Nested member access with different name in entity.
             spec_fac.create_equal_to('parent.text_rc', self.TEST_TEXT),
             # Nested collection access with different name in entity.
             spec_fac.create_equal_to('children.text_rc', self.TEST_TEXT),
             # Access with dotted entity name in rc attr declaration.
             spec_fac.create_equal_to('parent_text', self.TEST_TEXT),
             # Access to member.
             spec_fac.create_equal_to('parent',
                                      member.parent.get_entity()),
             ]
     expecteds = [('text', MyEntity.text.__eq__(self.TEST_TEXT)),
                  ('text_ent', MyEntity.text_ent.__eq__(self.TEST_TEXT)),
                  ('parent.text_ent',
                   MyEntity.parent.has(
                                 MyEntityParent.text_ent.__eq__(
                                                     self.TEST_TEXT))),
                  ('children.text_ent',
                   MyEntity.children.any(
                                 MyEntityChild.text_ent.__eq__(
                                                     self.TEST_TEXT))),
                  ('parent.text_ent',
                   MyEntity.parent.has(
                                 MyEntityParent.text_ent.__eq__(
                                                     self.TEST_TEXT))),
                  ('parent',
                   MyEntity.parent.__eq__(member.parent.get_entity())),
                  ]
     for spec, expected in zip(specs, expecteds):
         new_attr_name, expr = expected
         visitor = ResourceToEntityFilterSpecificationVisitor(mb_cls)
         spec.accept(visitor)
         new_spec = visitor.expression
         self.assert_equal(new_spec.attr_name, new_attr_name)
         visitor = SqlFilterSpecificationVisitor(MyEntity)
         new_spec.accept(visitor)
         self.assert_equal(str(visitor.expression), str(expr))
     invalid_spec = spec_fac.create_equal_to('foo', self.TEST_TEXT)
     vst = ResourceToEntityFilterSpecificationVisitor(mb_cls)
     self.assert_raises(AttributeError, invalid_spec.accept, vst)
Пример #38
0
 def test_map_to_data_element_with_collection(self):
     entity = create_entity()
     mb = MyEntityMember.create_from_entity(entity)
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(
         attribute_options={('children',):{IGNORE_ON_WRITE_OPTION:False,
                                         WRITE_AS_LINK_OPTION:False},
                          ('children', 'children'):
                                     {IGNORE_ON_WRITE_OPTION:False,
                                      WRITE_AS_LINK_OPTION:False}
                          })
     de = mp1.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     self.assert_equal(len(prx.children), 1)
     self.assert_equal(len(prx.children[0].children), 1)
Пример #39
0
 def test_map_to_data_element_with_member(self):
     entity = create_entity()
     mb = MyEntityMember.create_from_entity(entity)
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(
         attribute_options={('parent',):{WRITE_AS_LINK_OPTION:False},
                          })
     de = mp1.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     self.assert_is_not_none(prx.parent)
     #
     entity.parent = None
     de1 = mp1.map_to_data_element(mb)
     prx1 = DataElementAttributeProxy(de1)
     self.assert_is_none(prx1.parent)
Пример #40
0
 def test_filter_specification_visitor(self):
     coll = get_root_collection(IMyEntity)
     mb_cls = get_member_class(coll)
     my_entity = create_entity()
     member = coll.create_member(my_entity)
     spec_fac = FilterSpecificationFactory()
     specs = [
         # Terminal access.
         spec_fac.create_equal_to('text', self.TEST_TEXT),
         # Terminal access with different name in entity.
         spec_fac.create_equal_to('text_rc', self.TEST_TEXT),
         # Nested member access with different name in entity.
         spec_fac.create_equal_to('parent.text_rc', self.TEST_TEXT),
         # Nested collection access with different name in entity.
         spec_fac.create_equal_to('children.text_rc', self.TEST_TEXT),
         # Access with dotted entity name in rc attr declaration.
         spec_fac.create_equal_to('parent_text', self.TEST_TEXT),
         # Access to member.
         spec_fac.create_equal_to('parent', member.parent.get_entity()),
     ]
     expecteds = [
         ('text', MyEntity.text.__eq__(self.TEST_TEXT)),
         ('text_ent', MyEntity.text_ent.__eq__(self.TEST_TEXT)),
         ('parent.text_ent',
          MyEntity.parent.has(MyEntityParent.text_ent.__eq__(
              self.TEST_TEXT))),
         ('children.text_ent',
          MyEntity.children.any(
              MyEntityChild.text_ent.__eq__(self.TEST_TEXT))),
         ('parent.text_ent',
          MyEntity.parent.has(MyEntityParent.text_ent.__eq__(
              self.TEST_TEXT))),
         ('parent', MyEntity.parent.__eq__(member.parent.get_entity())),
     ]
     for spec, expected in zip(specs, expecteds):
         new_attr_name, expr = expected
         visitor = ResourceToEntityFilterSpecificationVisitor(mb_cls)
         spec.accept(visitor)
         new_spec = visitor.expression
         self.assert_equal(new_spec.attr_name, new_attr_name)
         visitor = SqlFilterSpecificationVisitor(MyEntity)
         new_spec.accept(visitor)
         self.assert_equal(str(visitor.expression), str(expr))
     invalid_spec = spec_fac.create_equal_to('foo', self.TEST_TEXT)
     vst = ResourceToEntityFilterSpecificationVisitor(mb_cls)
     self.assert_raises(AttributeError, invalid_spec.accept, vst)
Пример #41
0
 def test_map_to_data_element_with_member(self, resource_repo):
     entity = create_entity()
     coll = resource_repo.get_collection(IMyEntity)
     mb = coll.create_member(entity)
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(
         attribute_options={('parent',):{WRITE_AS_LINK_OPTION:False},
                          })
     de = mp1.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     assert not prx.parent is None
     #
     entity.parent = None
     de1 = mp1.map_to_data_element(mb)
     prx1 = DataElementAttributeProxy(de1)
     with pytest.raises(AttributeError):
         dummy = getattr(prx1, 'parent')
Пример #42
0
 def test_map_to_data_element_with_member(self):
     entity = create_entity()
     mb = MyEntityMember.create_from_entity(entity)
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(attribute_options={
         ('parent', ): {
             WRITE_AS_LINK_OPTION: False
         },
     })
     de = mp1.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     self.assert_is_not_none(prx.parent)
     #
     entity.parent = None
     de1 = mp1.map_to_data_element(mb)
     prx1 = DataElementAttributeProxy(de1)
     self.assert_is_none(prx1.parent)
Пример #43
0
 def test_basic_urls(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     mb = coll.create_member(my_entity)
     exp_url = '/my-entities/0/'
     url = resource_to_url(mb)
     self.assert_true(url.endswith(exp_url))
     exp_url = '/my-entity-parents/0/'
     url = resource_to_url(mb.parent)
     self.assert_true(url.endswith(exp_url))
     exp_url = '/my-entity-children/?q=parent:equal-to:' \
               '"http://0.0.0.0:6543/my-entities/0/"'
     url = resource_to_url(mb.children)
     self.assert_true(url.endswith(exp_url))
     mb_child = mb.children['0']
     self.assert_equal(mb_child.id, 0)
     exp_url = "/my-entity-grandchildren/?q=parent:equal-to:" \
               "'http://0.0.0.0:6543/my-entity-children/0/'"
     url = resource_to_url(mb_child.children)
Пример #44
0
 def test_basic_urls(self):
     my_entity = create_entity()
     coll = get_root_collection(IMyEntity)
     mb = coll.create_member(my_entity)
     exp_url = '/my-entities/0/'
     url = resource_to_url(mb)
     self.assert_true(url.endswith(exp_url))
     exp_url = '/my-entity-parents/0/'
     url = resource_to_url(mb.parent)
     self.assert_true(url.endswith(exp_url))
     exp_url = '/my-entity-children/?q=parent:equal-to:' \
               '"http://0.0.0.0:6543/my-entities/0/"'
     url = resource_to_url(mb.children)
     self.assert_true(url.endswith(exp_url))
     mb_child = mb.children['0']
     self.assert_equal(mb_child.id, 0)
     exp_url = "/my-entity-grandchildren/?q=parent:equal-to:" \
               "'http://0.0.0.0:6543/my-entity-children/0/'"
     url = resource_to_url(mb_child.children)
Пример #45
0
 def test_map_to_data_element_with_collection(self):
     entity = create_entity()
     mb = MyEntityMember.create_from_entity(entity)
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(
         attribute_options={
             ('children', ): {
                 IGNORE_ON_WRITE_OPTION: False,
                 WRITE_AS_LINK_OPTION: False
             },
             ('children', 'children'): {
                 IGNORE_ON_WRITE_OPTION: False,
                 WRITE_AS_LINK_OPTION: False
             }
         })
     de = mp1.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     self.assert_equal(len(prx.children), 1)
     self.assert_equal(len(prx.children[0].children), 1)
Пример #46
0
 def test_map_to_data_element_with_member(self, resource_repo):
     entity = create_entity()
     coll = resource_repo.get_collection(IMyEntity)
     mb = coll.create_member(entity)
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(attribute_options={
         ('parent', ): {
             WRITE_AS_LINK_OPTION: False
         },
     })
     de = mp1.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     assert not prx.parent is None
     #
     entity.parent = None
     de1 = mp1.map_to_data_element(mb)
     prx1 = DataElementAttributeProxy(de1)
     with pytest.raises(AttributeError):
         dummy = getattr(prx1, 'parent')
Пример #47
0
 def test_map_to_data_element_with_collection(self, resource_repo):
     entity = create_entity()
     coll = resource_repo.get_collection(IMyEntity)
     mb = coll.create_member(entity)
     assert len(entity.children) == 1
     assert len(mb.children) == 1
     mb_child = next(iter(mb.children))
     assert len(mb_child.children) == 1
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(
         attribute_options={('children',):{IGNORE_OPTION:False,
                                         WRITE_AS_LINK_OPTION:False},
                            ('children', 'children'):
                                          {IGNORE_OPTION:False,
                                           WRITE_AS_LINK_OPTION:False}
                             })
     de = mp1.map_to_data_element(mb)
     prx = DataElementAttributeProxy(de)
     assert len(prx.children) == 1
     assert len(prx.children[0].children) == 1
Пример #48
0
    def test_map_to_data_element(self, resource_repo):
        def _test(mb, cnt_type, parent_repr_name, children_repr_name):
            mp_reg = get_mapping_registry(cnt_type)
            mp = mp_reg.find_or_create_mapping(MyEntityMember)
            de = mp.map_to_data_element(mb)
            prx = DataElementAttributeProxy(de)
            assert prx.get_data_element() is de
            assert prx.id == 0
            assert prx.text == 'TEXT'
            assert prx.number == 1
            if cnt_type is XmlMime:
                # The parent attribute is a link.
                assert isinstance(getattr(prx, parent_repr_name),
                                  LinkedDataElement)
                with pytest.raises(AttributeError):
                    dummy = getattr(prx, children_repr_name)
            else:
                assert isinstance(getattr(prx, parent_repr_name),
                                  DataElementAttributeProxy)
                children_el = getattr(prx, children_repr_name)
                assert isinstance(children_el, LinkedDataElement)
            # Nonexisting attribute raises error.
            with pytest.raises(AttributeError):
                dummy = getattr(prx, 'foo')
            with pytest.raises(AttributeError):
                setattr(prx, 'foo', 'murks')
            # Set terminal attribute.
            prx.id = 1
            assert prx.id == 1
            # Set nested attribute.
            setattr(prx, parent_repr_name, None)
            assert getattr(prx, parent_repr_name) is None
            with pytest.raises(ValueError):
                setattr(prx, parent_repr_name, 1)

        entity = create_entity()
        coll = resource_repo.get_collection(IMyEntity)
        mb = coll.create_member(entity)
        _test(mb, XmlMime, 'myentityparent', 'myentitychildren')
        _test(mb, CsvMime, 'parent', 'children')
Пример #49
0
 def test_map_to_data_element(self, resource_repo):
     def _test(mb, cnt_type, parent_repr_name, children_repr_name):
         mp_reg = get_mapping_registry(cnt_type)
         mp = mp_reg.find_or_create_mapping(MyEntityMember)
         de = mp.map_to_data_element(mb)
         prx = DataElementAttributeProxy(de)
         assert prx.get_data_element() is de
         assert prx.id == 0
         assert prx.text == 'TEXT'
         assert prx.number == 1
         if cnt_type is XmlMime:
             # The parent attribute is a link.
             assert isinstance(getattr(prx, parent_repr_name),
                               LinkedDataElement)
             with pytest.raises(AttributeError):
                 dummy = getattr(prx, children_repr_name)
         else:
             assert isinstance(getattr(prx, parent_repr_name),
                               DataElementAttributeProxy)
             children_el = getattr(prx, children_repr_name)
             assert isinstance(children_el, LinkedDataElement)
         # Nonexisting attribute raises error.
         with pytest.raises(AttributeError):
             dummy = getattr(prx, 'foo')
         with pytest.raises(AttributeError):
             setattr(prx, 'foo', 'murks')
         # Set terminal attribute.
         prx.id = 1
         assert prx.id == 1
         # Set nested attribute.
         setattr(prx, parent_repr_name, None)
         assert getattr(prx, parent_repr_name) is None
         with pytest.raises(ValueError):
             setattr(prx, parent_repr_name, 1)
     entity = create_entity()
     coll = resource_repo.get_collection(IMyEntity)
     mb = coll.create_member(entity)
     _test(mb, XmlMime, 'myentityparent', 'myentitychildren')
     _test(mb, CsvMime, 'parent', 'children')
Пример #50
0
 def test_traverse_with_add(self):
     mock_vst = MagicMock()
     ent = create_entity(entity_id=None)
     trv = SourceTargetDataTreeTraverser.make_traverser(
         ent, None, RELATION_OPERATIONS.ADD)
     trv.run(mock_vst)
     parent_attr = get_domain_class_attribute(MyEntity, 'parent')
     children_attr = get_domain_class_attribute(MyEntity, 'children')
     grandchildren_attr = get_domain_class_attribute(
         MyEntityChild, 'children')
     # Check the visiting sequence and args (depth first).
     for idx, (meth_name, attr) \
         in enumerate([('visit', parent_attr),
                       ('visit', grandchildren_attr),
                       ('visit', children_attr),
                       ]):
         meth_call = mock_vst.method_calls[idx + 1]
         self.assert_equal(meth_call[0], meth_name)
         self.assert_equal(meth_call[1][1], attr)
         prx = meth_call[1][2]
         self.assert_true(isinstance(prx, DomainDataTraversalProxy))
         self.assert_true(isinstance(prx.get_entity(), Entity))
         self.assert_is_none(meth_call[1][3])
Пример #51
0
 def test_traverse_with_add(self):
     mock_vst = MagicMock()
     ent = create_entity(entity_id=None)
     trv = SourceTargetDataTreeTraverser.make_traverser(
                                                 ent, None,
                                                 RELATION_OPERATIONS.ADD)
     trv.run(mock_vst)
     parent_attr = get_domain_class_attribute(MyEntity, 'parent')
     children_attr = get_domain_class_attribute(MyEntity, 'children')
     grandchildren_attr = get_domain_class_attribute(MyEntityChild,
                                                     'children')
     # Check the visiting sequence and args (depth first).
     for idx, (meth_name, attr) \
         in enumerate([('visit', parent_attr),
                       ('visit', grandchildren_attr),
                       ('visit', children_attr),
                       ]):
         meth_call = mock_vst.method_calls[idx + 1]
         self.assert_equal(meth_call[0], meth_name)
         self.assert_equal(meth_call[1][1], attr)
         prx = meth_call[1][2]
         self.assert_true(isinstance(prx, DomainDataTraversalProxy))
         self.assert_true(isinstance(prx.get_entity(), Entity))
         self.assert_is_none(meth_call[1][3])
Пример #52
0
 def test_nested_get(self):
     my_entity = create_entity()
     coll = create_staging_collection(IMyEntity)
     member = coll.create_member(my_entity)
     self.assert_equal(member.parent_text, MyEntityParent.DEFAULT_TEXT)
Пример #53
0
 def test_nested_get(self):
     my_entity = create_entity()
     coll = create_staging_collection(IMyEntity)
     member = coll.create_member(my_entity)
     self.assert_equal(member.parent_text, MyEntityParent.DEFAULT_TEXT)
Пример #54
0
 def test_resource_to_url_non_resource_object(self):
     ent = create_entity(entity_id=2)
     with self.assert_raises(ValueError) as cm:
         resource_to_url(ent)
     exc_msg = 'Can not convert non-resource object'
     self.assert_true(cm.exception.message.startswith(exc_msg))