Пример #1
0
 def test_member_data_element(self):
     data_el = SimpleMemberDataElement.create()
     # Test nesteds.
     self.assert_equal(data_el.nesteds.keys(), [])
     parent_data_el = SimpleMemberDataElement.create()
     rc_nested_attr = MyEntityMember.get_attributes()['parent']
     mp_nested_attr = MappedAttribute(rc_nested_attr)
     data_el.set_nested(mp_nested_attr, parent_data_el)
     self.assert_true(data_el.get_nested(mp_nested_attr) is parent_data_el)
     self.assert_equal(data_el.nesteds.keys(), ['parent'])
     # Test terminals.
     self.assert_equal(data_el.terminals.keys(), [])
     utc = timezone('UTC')
     ldt = datetime.datetime(2012, 8, 29, 16, 20, 0, tzinfo=utc)
     term_attr_data = OrderedDict(text='foo', number=0, date_time=ldt)
     for term_attr_name, term_attr_value in term_attr_data.items():
         rc_attr = MyEntityMember.get_attributes()[term_attr_name]
         mp_attr = MappedAttribute(rc_attr)
         # Check setting to None value.
         data_el.set_terminal(mp_attr, None)
         self.assert_true(data_el.get_terminal(mp_attr) is None)
         data_el.set_terminal_converted(mp_attr, None)
         self.assert_true(data_el.get_terminal(mp_attr) is None)
         # Check setting to value.
         data_el.set_terminal(mp_attr, term_attr_value)
         self.assert_equal(data_el.get_terminal(mp_attr), term_attr_value)
         rpr_val = data_el.get_terminal_converted(mp_attr)
         data_el.set_terminal_converted(mp_attr, rpr_val)
         self.assert_equal(data_el.get_terminal(mp_attr), term_attr_value)
     self.assert_equal(data_el.terminals.keys(), term_attr_data.keys())
     # Printing.
     prt_str = str(data_el)
     self.assert_true(prt_str.startswith(data_el.__class__.__name__))
     self.assert_true(prt_str.endswith(')'))
Пример #2
0
 def test_types(self):
     attrs = MyEntityMember.get_attributes().values()
     self.assert_equal(attrs[0].name, ATTRIBUTE_NAMES[0])
     self.assert_equal(attrs[0].kind, ResourceAttributeKinds.TERMINAL)
     self.assert_equal(attrs[0].entity_name, 'id')
     self.assert_equal(attrs[0].value_type, int)
     self.assert_equal(attrs[1].name, ATTRIBUTE_NAMES[1])
     self.assert_equal(attrs[1].kind, ResourceAttributeKinds.MEMBER)
     self.assert_equal(attrs[1].entity_name, 'parent')
     self.assert_equal(attrs[1].value_type, IMyEntityParent)
     self.assert_equal(attrs[3].name, ATTRIBUTE_NAMES[3])
     self.assert_equal(attrs[3].kind, ResourceAttributeKinds.COLLECTION)
     self.assert_equal(attrs[3].entity_name, 'children')
     self.assert_equal(attrs[3].value_type, IMyEntityChild)
     self.assert_equal(attrs[4].name, ATTRIBUTE_NAMES[4])
     self.assert_equal(attrs[4].kind, ResourceAttributeKinds.TERMINAL)
     self.assert_equal(attrs[4].entity_name, 'text')
     self.assert_equal(attrs[4].value_type, str)
     self.assert_equal(attrs[6].name, ATTRIBUTE_NAMES[6])
     self.assert_equal(attrs[6].kind, ResourceAttributeKinds.TERMINAL)
     self.assert_equal(attrs[6].entity_name, 'number')
     self.assert_equal(attrs[6].value_type, int)
     self.assert_true(MyEntityMember.is_member('parent'))
     self.assert_true(MyEntityMember.is_collection('children'))
     self.assert_true(MyEntityMember.is_resource('parent'))
     self.assert_true(MyEntityMember.is_resource('children'))
     self.assert_true(
         isinstance(getattr(MyEntityMember, 'id'), terminal_attribute))
Пример #3
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
Пример #4
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)
Пример #5
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)
Пример #6
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)
Пример #7
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))
Пример #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
Пример #9
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)
Пример #10
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)
Пример #11
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
Пример #12
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)
Пример #13
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)
Пример #14
0
 def test_printing_with_none_value(self):
     data_el = SimpleMemberDataElement.create()
     self.assert_equal(data_el.terminals.keys(), [])
     rc_attr = MyEntityMember.get_attributes()['text']
     mp_attr = MappedAttribute(rc_attr)
     data_el.set_terminal(mp_attr, None)  # Need one None attr value.
     prt_str = str(data_el)
     self.assert_true(prt_str.startswith(data_el.__class__.__name__))
     self.assert_true(prt_str.endswith(')'))
Пример #15
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)
Пример #16
0
 def test_clone(self):
     rc_attr = MyEntityMember.get_attributes()['number']
     mp_attr = MappedAttribute(rc_attr)
     mp_attr_clone = mp_attr.clone()
     self.assert_equal(mp_attr.options, mp_attr_clone.options)
     self.assert_equal(mp_attr.name, mp_attr_clone.name)
     self.assert_equal(mp_attr.kind, mp_attr_clone.kind)
     self.assert_equal(mp_attr.value_type, mp_attr_clone.value_type)
     self.assert_equal(mp_attr.entity_name, mp_attr_clone.entity_name)
     self.assert_equal(mp_attr.cardinality, mp_attr_clone.cardinality)
Пример #17
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
Пример #18
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)
Пример #19
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
Пример #20
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)
Пример #21
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)
Пример #22
0
 def test_names(self):
     self.assert_equal(MyEntityMember.get_attribute_names(),
                       ATTRIBUTE_NAMES)
Пример #23
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')
Пример #24
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)
Пример #25
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))
Пример #26
0
 def test_defaults(self):
     rc_attr = MyEntityMember.get_attributes()['number']
     mp_attr = MappedAttribute(rc_attr)
     self.assert_equal(mp_attr.repr_name, rc_attr.name)
     self.assert_raises(AttributeError, getattr, mp_attr, 'foo')
     self.assert_true(str(mp_attr).startswith(mp_attr.__class__.__name__))
Пример #27
0
 def test_ignore(self):
     rc_attr = MyEntityMember.get_attributes()['number']
     mp_attr = MappedAttribute(rc_attr, options={IGNORE_OPTION: False})
     self.assert_true(mp_attr.ignore_on_read is False)
     self.assert_true(mp_attr.ignore_on_write is False)