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(')'))
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))
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))
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(')'))
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)
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)
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__))
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)