Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
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))
Ejemplo n.º 4
0
 def test_resource_to_url_floating_member(self, member):
     ent = member.get_entity()
     mb = MyEntityMember.create_from_entity(ent)
     with pytest.raises(ValueError) as cm:
         resource_to_url(mb)
     exc_msg = 'Can not generate URL for floating resource'
     assert str(cm.value).startswith(exc_msg)
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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
Ejemplo n.º 9
0
 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)
     coll.remove(mb)
     self.assert_equal(len(coll), 1)
Ejemplo n.º 10
0
 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
Ejemplo n.º 11
0
 def test_resource_to_url_floating_member(self, member):
     ent = member.get_entity()
     mb = MyEntityMember.create_from_entity(ent)
     with pytest.raises(ValueError) as cm:
         resource_to_url(mb)
     exc_msg = 'Can not generate URL for floating resource'
     assert str(cm.value).startswith(exc_msg)
Ejemplo n.º 12
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)
Ejemplo n.º 13
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)
Ejemplo n.º 14
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)
Ejemplo n.º 15
0
 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
Ejemplo n.º 16
0
 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)
Ejemplo n.º 17
0
 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
Ejemplo n.º 18
0
 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)
Ejemplo n.º 19
0
 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)
Ejemplo n.º 20
0
 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
Ejemplo n.º 21
0
 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)
Ejemplo n.º 22
0
 def test_repeated_add_remove_same_member_no_id(self):
     coll = get_root_collection(IMyEntity)
     ent1 = MyEntity()
     mb1 = MyEntityMember.create_from_entity(ent1)
     coll.add(mb1)
     transaction.commit()
     self.assert_equal(len(coll), 2)
     coll.remove(mb1)
     transaction.commit()
     self.assert_equal(len(coll), 1)
     ent2 = MyEntity()
     mb2 = MyEntityMember.create_from_entity(ent2)
     coll.add(mb2)
     transaction.commit()
     self.assert_equal(len(coll), 2)
     coll.remove(mb2)
     transaction.commit()
     self.assert_equal(len(coll), 1)
     self.assert_not_equal(mb1.id, mb2.id)
Ejemplo n.º 23
0
 def test_repeated_add_remove_same_member_no_id(self):
     coll = get_root_collection(IMyEntity)
     ent1 = MyEntity()
     mb1 = MyEntityMember.create_from_entity(ent1)
     coll.add(mb1)
     transaction.commit()
     self.assert_equal(len(coll), 2)
     coll.remove(mb1)
     transaction.commit()
     self.assert_equal(len(coll), 1)
     ent2 = MyEntity()
     mb2 = MyEntityMember.create_from_entity(ent2)
     coll.add(mb2)
     transaction.commit()
     self.assert_equal(len(coll), 2)
     coll.remove(mb2)
     transaction.commit()
     self.assert_equal(len(coll), 1)
     self.assert_not_equal(mb1.id, mb2.id)
Ejemplo n.º 24
0
 def test_add_commit_remove_same_member(self, resource_repo_with_data):
     coll = resource_repo_with_data.get_collection(IMyEntity)
     ent1 = MyEntity()
     mb1 = MyEntityMember.create_from_entity(ent1)
     coll.add(mb1)
     transaction.commit()
     assert len(coll) == 2
     #
     mb2 = coll[mb1.id]
     coll.remove(mb2)
     transaction.commit()
     assert len(coll) == 1
Ejemplo n.º 25
0
 def test_add_commit_remove_same_member(self, resource_repo_with_data):
     coll = resource_repo_with_data.get_collection(IMyEntity)
     ent1 = MyEntity()
     mb1 = MyEntityMember.create_from_entity(ent1)
     coll.add(mb1)
     transaction.commit()
     assert len(coll) == 2
     #
     mb2 = coll[mb1.id]
     coll.remove(mb2)
     transaction.commit()
     assert len(coll) == 1
Ejemplo n.º 26
0
 def test_add_commit_remove_same_member(self):
     coll = get_root_collection(IMyEntity)
     ent1 = MyEntity()
     mb1 = MyEntityMember.create_from_entity(ent1)
     coll.add(mb1)
     transaction.commit()
     self.assert_equal(len(coll), 2)
     #
     mb2 = coll[mb1.id]
     coll.remove(mb2)
     transaction.commit()
     self.assert_equal(len(coll), 1)
Ejemplo n.º 27
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
Ejemplo n.º 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
Ejemplo n.º 29
0
 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)
Ejemplo n.º 30
0
 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)
Ejemplo n.º 31
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)
Ejemplo n.º 32
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)
Ejemplo n.º 33
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)
Ejemplo n.º 34
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)
Ejemplo n.º 35
0
 def test_nested_set(self):
     ent = MyEntity()
     mb = MyEntityMember.create_from_entity(ent)
     self.assert_true(mb.parent is None)
     self.assert_raises(AttributeError, setattr, mb, 'parent_text', 'foo')
Ejemplo n.º 36
0
 def test_member_access(self):
     parent = MyEntityParent()
     entity = MyEntity(parent=parent)
     member = MyEntityMember.create_from_entity(entity)
     self.assert_true(isinstance(member.parent, MyEntityParentMember))
     self.assert_true(member.parent.get_entity() is parent)
Ejemplo n.º 37
0
 def test_terminal_access(self):
     entity = MyEntity()
     member = MyEntityMember.create_from_entity(entity)
     self.assert_true(isinstance(member.text, str))
     self.assert_true(isinstance(member.number, int))
     self.assert_true(isinstance(member.date_time, datetime.datetime))
Ejemplo n.º 38
0
 def test_member_access(self):
     parent = MyEntityParent()
     entity = MyEntity(parent=parent)
     member = MyEntityMember.create_from_entity(entity)
     self.assert_true(isinstance(member.parent, MyEntityParentMember))
     self.assert_true(member.parent.get_entity() is parent)
Ejemplo n.º 39
0
 def test_terminal_access(self):
     entity = MyEntity()
     member = MyEntityMember.create_from_entity(entity)
     self.assert_true(isinstance(member.text, str))
     self.assert_true(isinstance(member.number, int))
     self.assert_true(isinstance(member.date_time, datetime.datetime))
Ejemplo n.º 40
0
 def test_nested_set(self):
     ent = MyEntity()
     mb = MyEntityMember.create_from_entity(ent)
     self.assert_true(mb.parent is None)
     self.assert_raises(AttributeError, setattr, mb, 'parent_text', 'foo')