def test_data(self): coll = create_collection() mb = iter(coll).next() mp = self.__get_member_mapping_and_representer()[0] de = mp.map_to_data_element(mb) self.assert_equal(de.data.keys(), ['text', 'date_time', 'myentityparent', 'number'])
def test_backref_only_collection(self): coll = create_collection() child_mb = iter(iter(coll).next().children).next() self.assert_equal(len(child_mb.backref_only_children), 1) grandchild_mb = iter(child_mb.children).next() grandchild_mb.parent = None self.assert_equal(len(child_mb.backref_only_children), 0)
def test_get_collection_with_refs_options(self): # The links options are not processed by the renderers, so we need # a native everest view with a defined response MIME type. self.config.add_resource_view(IMyEntity, default_response_content_type=CsvMime, request_method=RequestMethods.GET) create_collection() res1 = self.app.get(self.path, params=dict(refs='parent:OFF'), status=200) self.assert_is_not_none(res1) self.assert_equal(native_(res1.body).find(',"parent",'), -1) self.assert_equal(native_(res1.body).find(',"parent.id",'), -1) res2 = self.app.get(self.path, params=dict(refs='parent:INLINE'), status=200) self.assert_is_not_none(res2) self.assert_equal(native_(res2.body).find(',"parent",'), -1) self.assert_not_equal(native_(res2.body).find(',"parent.id",'), -1)
def test_filter_nested(self): coll = create_collection() children = iter(coll).next().children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 1) children.filter = spec self.assert_true(isinstance(children.filter, ValueEqualToFilterSpecification)) self.assert_equal(len(children), 0)
def test_filter_nested(self): coll = create_collection() children = coll['1'].children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 0) children.filter = spec self.assert_true( isinstance(children.filter, ConjunctionFilterSpecification)) self.assert_equal(len(children), 0)
def test_filter_nested(self): coll = create_collection() children = coll['1'].children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 0) children.filter = spec self.assert_true(isinstance(children.filter, ConjunctionFilterSpecification)) self.assert_equal(len(children), 0)
def test_terminal_attr(self): coll = create_collection() mb = iter(coll).next() mp = self.__get_member_mapping_and_representer()[0] text_attr = mp.get_attribute_map()['text'] de = mp.map_to_data_element(mb) self.assert_equal(de.get_terminal(text_attr), mb.text) mb.text = None de1 = mp.map_to_data_element(mb) self.assert_true(de1.get_terminal(text_attr) is None)
def test_configure_rpr_with_zcml(self): coll = create_collection() rpr = as_representer(coll, CsvMime) rpr_str = rpr.to_string(coll) lines = rpr_str.split(os.linesep) chld_field_idx = lines[0].split(',').index('"children"') row_data = lines[1].split(',') # Collection should be a link. self.assert_not_equal( row_data[chld_field_idx].find('my-entities/0/children/'), -1)
def test_mapping_linked_xml_data_element_with_string_id(self): mp_reg = get_mapping_registry(XmlMime) mb_mp = mp_reg.find_or_create_mapping(MyEntityMember) coll = create_collection() mb = next(iter(coll)) mb_id = 'unique' mb.id = mb_id data_el = mb_mp.create_linked_data_element_from_resource(mb) link_el = next(data_el.iterchildren()) self.assert_equal(link_el.get_id(), mb.id)
def test_backref_only_collection(self): coll = create_collection() mb = next(iter(coll)) child_mb = next(iter(mb.children)) with patch('%s.resources.MyEntityChildMember.children.entity_attr' % self.package_name, None): self.assert_equal(len(child_mb.children), 1) grandchild_mb = next(iter(child_mb.children)) grandchild_mb.parent = None self.assert_equal(len(child_mb.children), 0)
def test_create_link(self): coll = create_collection() rpr_reg = self.config.get_registered_utility(IRepresenterRegistry) mp_reg = rpr_reg.get_mapping_registry(XmlMime) mp = mp_reg.find_mapping(Link) de = mp.data_element_class.create_from_resource(coll) link_el = de.iterchildren().next() self.assert_equal(link_el.get_kind(), ResourceKinds.COLLECTION) self.assert_not_equal(link_el.get_relation().find('myentity'), -1) self.assert_true(link_el.get_title().startswith('Collection of')) self.assert_true(link_el.get_id() is None)
def test_backref_only_collection(self): coll = create_collection() mb = next(iter(coll)) child_mb = next(iter(mb.children)) with patch( '%s.resources.MyEntityChildMember.children.entity_attr' % self.package_name, None): self.assert_equal(len(child_mb.children), 1) grandchild_mb = next(iter(child_mb.children)) grandchild_mb.parent = None self.assert_equal(len(child_mb.children), 0)
def test_update_collection_from_data_with_id_raises_error(self): coll = create_collection() rpr = as_representer(coll, CsvMime) upd_coll = create_staging_collection(IMyEntity) ent = MyEntity(id=2) upd_coll.create_member(ent) de = rpr.data_from_resource(upd_coll) with self.assert_raises(ValueError) as cm: coll.update_from_data(de) exc_msg = 'New member data should not provide an ID attribute.' self.assert_equal(cm.exception.message, exc_msg)
def test_mapping_linked_xml_data_element_with_string_id( self, mapping_registry_factory, resource_repo): # pylint: disable=W0613 # FIXME: Using resource_repo only for its side effect here. mp_reg = mapping_registry_factory(XmlMime) mb_mp = mp_reg.find_or_create_mapping(MyEntityMember) coll = create_collection() mb = next(iter(coll)) mb_id = 'unique' mb.id = mb_id data_el = mb_mp.create_linked_data_element_from_resource(mb) link_el = next(data_el.iterchildren()) assert link_el.get_id() == mb.id
def test_renderer_factory(self): def _test(rc, rnd_name, exp_str): inf = self._Info(rnd_name) fac = RendererFactory(inf) rpr_str = fac(dict(), dict(request=self._request, context=rc)) self.assert_true(isinstance(rpr_str, binary_type)) self.assert_not_equal(rpr_str.find(exp_str), -1) resource = create_collection() _test(resource, 'csv', b'"id","parent.id"') _test(resource, 'atom', b'www.w3.org/2005/Atom') self.assert_raises(ValueError, _test, resource, 'foo', '') self.assert_raises(ValueError, _test, None, 'csv', '"id","parent"')
def test_renderer_factory(self): def _test(rc, rnd_name, exp_str): inf = self._Info(rnd_name) fac = RendererFactory(inf) rpr_str = fac(dict(), dict(request=self._request, context=rc)) self.assert_true(isinstance(rpr_str, binary_type)) self.assert_not_equal(rpr_str.find(exp_str), -1) resource = create_collection() _test(resource, 'csv', b'"id","parent"') _test(resource, 'atom', b'www.w3.org/2005/Atom') self.assert_raises(ValueError, _test, resource, 'foo', '') self.assert_raises(ValueError, _test, None, 'csv', '"id","parent"')
def test_mapping_linked_xml_data_element_with_string_id(self, mapping_registry_factory, resource_repo): # pylint: disable=W0613 # FIXME: Using resource_repo only for its side effect here. mp_reg = mapping_registry_factory(XmlMime) mb_mp = mp_reg.find_or_create_mapping(MyEntityMember) coll = create_collection() mb = next(iter(coll)) mb_id = 'unique' mb.id = mb_id data_el = mb_mp.create_linked_data_element_from_resource(mb) link_el = next(data_el.iterchildren()) assert link_el.get_id() == mb.id
def test_filter_backref_only_raises_error(self): # We can not use resource attributes that do not have a corresponding # entity attribute (such as backref only collections) for filtering. coll = create_collection() children = iter(coll).next().children grandchildren = iter(children).next().children grandchild = iter(grandchildren).next() spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('backref_only_children', grandchild) with self.assert_raises(ValueError) as cm: children.filter = spec exc_msg = 'does not have a corresponding entity attribute.' self.assert_true(cm.exception.message.endswith(exc_msg))
def test_nonvalidating_renderer(self): class FooRenderer(ResourceRenderer): def __init__(self): ResourceRenderer.__init__(self, CsvMime) def _validate(self, resource): # pylint: disable=W0613 return False def _prepare_response(self, system): pass rc = create_collection() rnd = FooRenderer() self.assert_raises(ValueError, rnd, dict(), dict(request=self._request, context=rc))
def test_create_with_attr_namespace(self): coll = create_collection() mb = iter(coll).next() ns = 'foo' mp = self.__get_member_mapping_and_representer()[0] mp.configuration.set_attribute_option(('parent', ), NAMESPACE_MAPPING_OPTION, ns) mp.configuration.set_attribute_option(('parent', ), WRITE_AS_LINK_OPTION, False) attr = mp.get_attribute_map()['parent'] self.assert_equal(attr.namespace, ns) de = mp.map_to_data_element(mb) parent_de = de.get_nested(attr) self.assert_true(parent_de.tag.startswith('{%s}' % ns))
def test_linked_data_element(self): rc = create_collection() self.assert_raises(ValueError, CsvLinkedDataElement.create_from_resource, rc.get_aggregate()) data_el = CsvLinkedDataElement.create_from_resource(rc) self.assert_equal(data_el.get_title(), 'Collection of MyEntityMember') self.assert_not_equal(data_el.get_url().find('/my-entities/'), -1) self.assert_equal(data_el.get_kind(), ResourceKinds.COLLECTION) self.assert_equal(data_el.get_relation(), 'http://test.org/myentity-collection') # Can not use data element attribute proxy with a link. self.assert_raises(ValueError, DataElementAttributeProxy, data_el) self.assert_true(str(data_el).startswith(data_el.__class__.__name__))
def test_provides_resource(self): coll = create_collection() mb = next(iter(coll)) self.assert_true(provides_member_resource(type(mb))) self.assert_true(provides_member_resource(mb)) self.assert_false(provides_member_resource(coll)) self.assert_true(provides_resource(type(mb))) self.assert_true(provides_resource(mb)) self.assert_true(provides_collection_resource(type(coll))) self.assert_true(provides_collection_resource(coll)) self.assert_false(provides_collection_resource(mb)) self.assert_true(provides_resource(type(coll))) self.assert_true(provides_resource(coll)) self.assert_false(provides_resource(mb.get_entity()))
def test_provides_resource(self): coll = create_collection() mb = iter(coll).next() self.assert_true(provides_member_resource(type(mb))) self.assert_true(provides_member_resource(mb)) self.assert_false(provides_member_resource(coll)) self.assert_true(provides_resource(type(mb))) self.assert_true(provides_resource(mb)) self.assert_true(provides_collection_resource(type(coll))) self.assert_true(provides_collection_resource(coll)) self.assert_false(provides_collection_resource(mb)) self.assert_true(provides_resource(type(coll))) self.assert_true(provides_resource(coll)) self.assert_false(provides_resource(mb.get_entity()))
def test_create_with_attr_namespace(self): coll = create_collection() mb = iter(coll).next() ns = 'foo' mp = self.__get_member_mapping_and_representer()[0] mp.configuration.set_attribute_option(('parent',), NAMESPACE_MAPPING_OPTION, ns) mp.configuration.set_attribute_option(('parent',), WRITE_AS_LINK_OPTION, False) attr = mp.get_attribute_map()['parent'] self.assert_equal(attr.namespace, ns) de = mp.map_to_data_element(mb) parent_de = de.get_nested(attr) self.assert_true(parent_de.tag.startswith('{%s}' % ns))
def test_atom_collection(self): def _test(rc): rpr = as_representer(rc, AtomMime) rpr_str = rpr.to_string(rc) self.assert_not_equal(rpr_str.find('<feed xmlns:'), -1) coll = create_collection() _test(coll) coll.slice = slice(0, 1) filter_spec_fac = get_filter_specification_factory() filter_spec = filter_spec_fac.create_equal_to('id', 0) coll.filter = filter_spec order_spec_fac = get_order_specification_factory() order_spec = order_spec_fac.create_ascending('id') coll.order = order_spec _test(coll)
def test_xml_roundtrip(self): coll = create_collection() rpr = as_representer(coll, XmlMime) attribute_options = \ {('text_rc',):{IGNORE_OPTION:True}, ('parent_text',):{IGNORE_OPTION:True}, ('children',):{IGNORE_OPTION:False, WRITE_AS_LINK_OPTION:True}, } rpr.configure(attribute_options=attribute_options) data = rpr.data_from_resource(coll) self.assert_equal(len(data), 2) rpr_str = rpr.string_from_data(data) reloaded_coll = rpr.from_string(rpr_str) self.assert_equal(len(reloaded_coll), 2)
def test_filter_not_nested(self): # The grand children are not nested, so the filter spec has to be # a ConjunctionFilterSpecification. coll = create_collection() grand_children = coll['0'].children['0'].children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 1) grand_children.filter = spec self.assert_equal(len(grand_children), 0) self.assert_true( isinstance(grand_children.filter, ConjunctionFilterSpecification)) grand_children.filter = None self.assert_equal(len(grand_children), 1) self.assert_true( isinstance(grand_children.filter, ValueEqualToFilterSpecification))
def test_filter_backref_only_raises_error(self): # We can not use resource attributes that do not have a corresponding # entity attribute (such as backref only collections) for filtering. coll = create_collection() children = next(iter(coll)).children grandchildren = next(iter(children)).children grandchild = next(iter(grandchildren)) spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('children', grandchild) with patch('%s.resources.MyEntityChildMember.children.entity_attr' % self.package_name, None): with self.assert_raises(ValueError) as cm: children.filter = spec exc_msg = 'does not have a corresponding entity attribute.' self.assert_true(str(cm.exception).endswith(exc_msg))
def test_filter_not_nested(self): # The grand children are not nested, so the filter spec has to be # a ConjunctionFilterSpecification. coll = create_collection() grand_children = iter(iter(coll).next().children).next().children spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('id', 1) grand_children.filter = spec self.assert_equal(len(grand_children), 0) self.assert_true(isinstance(grand_children.filter, ConjunctionFilterSpecification)) grand_children.filter = None self.assert_equal(len(grand_children), 1) self.assert_true(isinstance(grand_children.filter, ValueEqualToFilterSpecification))
def test_autocreate_mapping(self): coll = create_collection() # This registers a representer (factory) and creates a mapping for # the collection. coll_rpr = as_representer(coll, CsvMime) mb = iter(coll).next() # This auto-creates a mapping for the member. coll_rpr.data_from_resource(mb) rpr_reg = self.config.get_registered_utility(IRepresenterRegistry) mp_reg = rpr_reg.get_mapping_registry(CsvMime) mp_before = mp_reg.find_mapping(type(mb)) # This registers a representer (factory) for the member and finds # the previously created mapping for the member. as_representer(mb, CsvMime) mp_after = mp_reg.find_mapping(type(mb)) self.assert_true(mp_before is mp_after)
def test_xml_roundtrip(self): coll = create_collection() rpr = as_representer(coll, XmlMime) attribute_options = \ {('nested_parent',):{IGNORE_OPTION:True}, ('text_rc',):{IGNORE_OPTION:True}, ('parent_text',):{IGNORE_OPTION:True}, ('children',):{IGNORE_OPTION:False, WRITE_AS_LINK_OPTION:True}, } rpr.configure(attribute_options=attribute_options) data = rpr.data_from_resource(coll) self.assert_equal(len(data), 2) rpr_str = rpr.representation_from_data(data) reloaded_coll = rpr.from_string(rpr_str) self.assert_equal(len(reloaded_coll), 2)
def test_filter_backref_only_raises_error(self): # We can not use resource attributes that do not have a corresponding # entity attribute (such as backref only collections) for filtering. coll = create_collection() children = next(iter(coll)).children grandchildren = next(iter(children)).children grandchild = next(iter(grandchildren)) spec_fac = get_filter_specification_factory() spec = spec_fac.create_equal_to('children', grandchild) with patch( '%s.resources.MyEntityChildMember.children.entity_attr' % self.package_name, None): with self.assert_raises(ValueError) as cm: children.filter = spec exc_msg = 'does not have a corresponding entity attribute.' self.assert_true(str(cm.exception).endswith(exc_msg))
def test_data(self): coll = create_collection() mb = next(iter(coll)) mp = self.__get_member_mapping_and_representer()[0] mp.configuration.set_attribute_option(('parent',), WRITE_AS_LINK_OPTION, False) de1 = mp.map_to_data_element(mb) self.assert_equal(list(de1.data.keys()), ['id', 'myentityparent', 'text', 'number', 'date_time']) self.assert_equal(de1.data['number'], 1) self.assert_true( IMemberDataElement.providedBy(de1.data['myentityparent'])) # pylint:disable=E1101 mp.configuration.set_attribute_option(('parent',), WRITE_AS_LINK_OPTION, True) de2 = mp.map_to_data_element(mb) self.assert_true( ILinkedDataElement.providedBy(de2.data['myentityparent'])) # pylint:disable=E1101
def test_atom_collection(self): def _test(rc): rpr = as_representer(rc, AtomMime) rpr_str = rpr.to_string(rc) self.assert_not_equal( rpr_str.find('<feed xmlns:ent="http://xml.test.org/tests"'), -1) coll = create_collection() _test(coll) coll.slice = slice(0, 1) filter_spec_fac = get_filter_specification_factory() filter_spec = filter_spec_fac.create_equal_to('id', 0) coll.filter = filter_spec order_spec_fac = get_order_specification_factory() order_spec = order_spec_fac.create_ascending('id') coll.order = order_spec _test(coll)
def test_create_with_attr_no_namespace(self): coll = create_collection() mb = iter(coll).next() ns = None mp = self.__get_member_mapping_and_representer()[0] parent_mp = mp.mapping_registry.find_mapping(MyEntityParentMember) parent_mp.configuration.set_option(XML_NAMESPACE_OPTION, ns) mp.configuration.set_attribute_option(('parent',), WRITE_AS_LINK_OPTION, False) # This is a hack: the parent's text attribute would cause objectify # to complain that the "text" attribute is not writable (because the # test strips its usual namespace), so we ignore it alltogether. mp.configuration.set_attribute_option(('parent', 'text',), IGNORE_OPTION, True) attr = mp.get_attribute_map()['parent'] self.assert_equal(attr.namespace, ns) de = mp.map_to_data_element(mb) self.assert_equal(de.data.keys(), ['text', 'date_time', 'myentityparent', 'number']) parent_de = de.get_nested(attr) self.assert_equal(parent_de.tag.find('{'), -1)
def test_create_with_attr_no_namespace(self): coll = create_collection() mb = iter(coll).next() ns = None mp = self.__get_member_mapping_and_representer()[0] parent_mp = mp.mapping_registry.find_mapping(MyEntityParentMember) parent_mp.configuration.set_option(XML_NAMESPACE_OPTION, ns) mp.configuration.set_attribute_option(('parent', ), WRITE_AS_LINK_OPTION, False) # This is a hack: the parent's text attribute would cause objectify # to complain that the "text" attribute is not writable (because the # test strips its usual namespace), so we ignore it alltogether. mp.configuration.set_attribute_option(( 'parent', 'text', ), IGNORE_OPTION, True) attr = mp.get_attribute_map()['parent'] self.assert_equal(attr.namespace, ns) de = mp.map_to_data_element(mb) self.assert_equal(de.data.keys(), ['text', 'date_time', 'myentityparent', 'number']) parent_de = de.get_nested(attr) self.assert_equal(parent_de.tag.find('{'), -1)
def test_delete_member(self): coll = create_collection() self.assert_equal(len(coll), 2) res = self.app.delete("%s/0" % self.path, content_type=CsvMime.mime_type_string, status=200) self.assert_is_not_none(res) self.assert_equal(len(coll), 1) # Second delete triggers 404. self.app.delete("%s/0" % self.path, content_type=CsvMime.mime_type_string, status=404) coll_cls = get_collection_class(IMyEntity) old_remove = coll_cls.__dict__.get('remove') def remove_with_exception(self): # pylint: disable=W0613 raise RuntimeError() coll_cls.remove = remove_with_exception try: self.app.delete("%s/1" % self.path, content_type=CsvMime.mime_type_string, status=500) finally: if not old_remove is None: coll_cls.remove = old_remove
def test_atom_member(self): mb = iter(create_collection()).next() rpr = as_representer(mb, AtomMime) rpr_str = rpr.to_string(mb) self.assert_not_equal( rpr_str.find('<entry xmlns:ent="http://xml.test.org/tests"'), -1)
def test_get_collection_with_slice_larger_max_size(self): create_collection() res = self.app.get(self.path, params=dict(size=10000), status=200) self.assert_is_not_none(res)
def test_get_resource_url(self): coll = create_collection() url = get_resource_url(coll) self.assert_not_equal(url.find('/my-entities'), -1)
def set_up(self): ResourceTestCase.set_up(self) self._collection = create_collection() self._representer = as_representer(self._collection, self.content_type)
def test_get_collection_with_order_and_size(self): create_collection() res = self.app.get(self.path, params=dict(sort='id:asc', size=1), status=200) self.assert_is_not_none(res)
def test_get_collection_with_slice_start(self): create_collection() res = self.app.get(self.path, params=dict(start=1, size=1), status=200) self.assert_is_not_none(res)
def test_get_collection_with_filter(self): create_collection() res = self.app.get(self.path, params=dict(q='id:equal-to:0'), status=200) self.assert_is_not_none(res)
def test_get_collection_with_invalid_slice_raises_error(self): create_collection() res = self.app.get(self.path, params=dict(size='foo'), status=500) self.assert_is_not_none(res)
def test_xml_with_defaults(self): coll = create_collection() rpr = as_representer(coll, XmlMime) rpr_str = rpr.to_string(coll) self.assert_not_equal(rpr_str.find('<ent:myentityparent id="0">'), -1)