Beispiel #1
0
 def create_from_resource(cls, resource):
     # Create the wrapping element.
     mp_reg = get_mapping_registry(XmlMime)
     mp = mp_reg.find_or_create_mapping(type(resource))
     xml_ns = mp.configuration.get_option(XML_NAMESPACE_OPTION)
     options = {XML_NAMESPACE_OPTION:xml_ns}
     rc_data_el = mp.create_data_element_from_resource(resource)
     if provides_member_resource(resource):
         link_el = cls.create(resource_to_url(resource),
                              RESOURCE_KINDS.MEMBER,
                              id=str(resource.id),
                              relation=resource.relation,
                              title=resource.title,
                              **options)
         rc_data_el.set('id', str(resource.id))
         rc_data_el.append(link_el)
     else: # collection resource.
         # Collection links only get an actual link element if they
         # contain any members.
         link_el = cls.create(resource_to_url(resource),
                              RESOURCE_KINDS.COLLECTION,
                              relation=resource.relation,
                              title=resource.title,
                              **options)
         rc_data_el.append(link_el)
     return rc_data_el
Beispiel #2
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
Beispiel #3
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)
Beispiel #4
0
 def test_mapping_polymorhpic(self):
     # pylint: disable=W0232
     class IMyDerivedEntity(IMyEntity):
         pass
     class MyDerivedEntity(MyEntity):
         pass
     class MyDerivedEntityMember(MyEntityMember):
         pass
     class MyDerivedEntityCollection(get_collection_class(IMyEntity)):
         pass
     # pylint: enable=W0232
     self.config.add_resource(IMyDerivedEntity, MyDerivedEntityMember,
                              MyDerivedEntity, MyDerivedEntityCollection,
                              expose=False)
     self.config.add_resource_representer(
                                 IMyDerivedEntity,
                                 XmlMime,
                                 attribute_options=
                                         {('parent',):dict(ignore=True)})
     mp_reg = get_mapping_registry(XmlMime)
     mp = mp_reg.find_or_create_mapping(get_collection_class(IMyEntity))
     for rc in (MyDerivedEntityMember, MyDerivedEntityCollection):
         attr = None
         for attr in mp.attribute_iterator(rc):
             if attr.name == 'parent':
                 break
         self.assert_is_not_none(attr)
         self.assert_equal(attr.ignore_on_write, True)
         self.assert_equal(attr.ignore_on_read, True)
Beispiel #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)
Beispiel #6
0
 def create_from_resource(cls, resource):
     # Create the wrapping element.
     mp_reg = get_mapping_registry(XmlMime)
     mp = mp_reg.find_or_create_mapping(type(resource))
     xml_ns = mp.configuration.get_option(XML_NAMESPACE_OPTION)
     options = {XML_NAMESPACE_OPTION:xml_ns}
     rc_data_el = mp.create_data_element_from_resource(resource)
     if provides_member_resource(resource):
         link_el = cls.create(resource_to_url(resource),
                              RESOURCE_KINDS.MEMBER,
                              id=str(resource.id),
                              relation=resource.relation,
                              title=resource.title,
                              **options)
         rc_data_el.set('id', str(resource.id))
         rc_data_el.append(link_el)
     else: # collection resource.
         # Collection links only get an actual link element if they
         # contain any members.
         link_el = cls.create(resource_to_url(resource),
                              RESOURCE_KINDS.COLLECTION,
                              relation=resource.relation,
                              title=resource.title,
                              **options)
         rc_data_el.append(link_el)
     return rc_data_el
Beispiel #7
0
 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)
Beispiel #8
0
 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)
Beispiel #9
0
 def test_mapping_duplicate_prefix(self):
     mp_reg = get_mapping_registry(XmlMime)
     mp = mp_reg.find_or_create_mapping(get_collection_class(IMyEntity))
     ns = 'foo'
     mp.configuration.set_option(XML_NAMESPACE_OPTION, ns)
     with self.assert_raises(ValueError) as cm:
         mp.mapping_registry.set_mapping(mp)
     exc_msg = 'is already registered for namespace'
     self.assert_not_equal(cm.exception.message.find(exc_msg), -1)
Beispiel #10
0
 def test_clone_with_options(self):
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(
             attribute_options={('parent', 'text'):{IGNORE_OPTION:True}})
     key = ('parent',)
     parent_attrs = mp1.get_attribute_map(key=key)
     self.assert_true(
         parent_attrs['text'].options.get(IGNORE_OPTION) is True)
Beispiel #11
0
 def test_mapping_duplicate_prefix(self):
     mp_reg = get_mapping_registry(XmlMime)
     mp = mp_reg.find_or_create_mapping(get_collection_class(IMyEntity))
     ns = 'foo'
     mp.configuration.set_option(XML_NAMESPACE_OPTION, ns)
     with self.assert_raises(ValueError) as cm:
         mp.mapping_registry.set_mapping(mp)
     exc_msg = 'is already registered for namespace'
     self.assert_not_equal(cm.exception.message.find(exc_msg), -1)
Beispiel #12
0
 def test_mapping_duplicate_tag(self):
     mp_reg = get_mapping_registry(XmlMime)
     mp = mp_reg.find_or_create_mapping(get_collection_class(IMyEntity))
     mb_mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mb_tag = mb_mp.configuration.get_option(XML_TAG_OPTION)
     mp.configuration.set_option(XML_TAG_OPTION, mb_tag)
     mp.mapping_registry.set_mapping(mp)
     with self.assert_raises(ValueError) as cm:
         getattr(mp.mapping_registry, 'parsing_lookup')
     self.assert_true(str(cm.exception).startswith('Duplicate tag'))
Beispiel #13
0
    def create_from_resource_class(cls, resource_class):
        """
        Creates a new representer for the given resource class.

        The representer obtains a reference to the (freshly created or looked
        up) mapping for the resource class.
        """
        mp_reg = get_mapping_registry(cls.content_type)
        mp = mp_reg.find_or_create_mapping(resource_class)
        return cls(resource_class, mp)
Beispiel #14
0
 def test_defaults(self):
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     attrs = mp.get_attribute_map()
     self.assert_true(attrs['text'].options.get(IGNORE_OPTION) is None)
     self.assert_true(attrs['parent'].options.get(IGNORE_OPTION) is None)
     key = ('parent',)
     parent_attrs = mp.get_attribute_map(key=key)
     self.assert_true(
         parent_attrs['text'].options.get(IGNORE_OPTION) is None)
Beispiel #15
0
 def create(cls, schema_location=None):
     if not schema_location is None:
         schema = cls.__get_xml_schema(schema_location)
         parser = objectify.makeparser(schema=schema)
     else:
         parser = objectify.makeparser()
     # Get the class lookup from the mapping registry.
     mp_reg = get_mapping_registry(XmlMime)
     parser.set_element_class_lookup(mp_reg.parsing_lookup)
     return parser
Beispiel #16
0
 def namespace_map(self):
     atom_ns_map = \
         getattr(XmlMappingRegistry, 'namespace_map').__get__(self)
     xml_mp_reg = get_mapping_registry(XmlMime)
     xml_ns_map = xml_mp_reg.namespace_map
     atom_ns_map.update(xml_ns_map)
     # Make ATOM namespace the default.
     del atom_ns_map[XML_PREFIX_ATOM]
     atom_ns_map[None] = XML_NS_ATOM
     return atom_ns_map
Beispiel #17
0
    def create_from_resource_class(cls, resource_class):
        """
        Creates a new representer for the given resource class.

        The representer obtains a reference to the (freshly created or looked
        up) mapping for the resource class.
        """
        mp_reg = get_mapping_registry(cls.content_type)
        mp = mp_reg.find_or_create_mapping(resource_class)
        return cls(resource_class, mp)
Beispiel #18
0
 def create(cls, schema_location=None):
     if not schema_location is None:
         schema = cls.__get_xml_schema(schema_location)
         parser = objectify.makeparser(schema=schema)
     else:
         parser = objectify.makeparser()
     # Get the class lookup from the mapping registry.
     mp_reg = get_mapping_registry(XmlMime)
     parser.set_element_class_lookup(mp_reg.parsing_lookup)
     return parser
Beispiel #19
0
 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)
Beispiel #20
0
 def namespace_map(self):
     atom_ns_map = \
         getattr(XmlMappingRegistry, 'namespace_map').__get__(self)
     xml_mp_reg = get_mapping_registry(XmlMime)
     xml_ns_map = xml_mp_reg.namespace_map
     atom_ns_map.update(xml_ns_map)
     # Make ATOM namespace the default.
     del atom_ns_map[XML_PREFIX_ATOM]
     atom_ns_map[None] = XML_NS_ATOM
     return atom_ns_map
Beispiel #21
0
 def test_mapping_duplicate_tag(self):
     mp_reg = get_mapping_registry(XmlMime)
     mp = mp_reg.find_or_create_mapping(get_collection_class(IMyEntity))
     mb_mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mb_tag = mb_mp.configuration.get_option(XML_TAG_OPTION)
     mp.configuration.set_option(XML_TAG_OPTION, mb_tag)
     mp.mapping_registry.set_mapping(mp)
     with self.assert_raises(ValueError) as cm:
         getattr(mp.mapping_registry, 'parsing_lookup')
     exc_msg = 'Duplicate tag "%s" ' % mb_tag
     self.assert_not_equal(cm.exception.message.find(exc_msg), -1)
Beispiel #22
0
 def test_mapping_duplicate_tag(self):
     mp_reg = get_mapping_registry(XmlMime)
     mp = mp_reg.find_or_create_mapping(get_collection_class(IMyEntity))
     mb_mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mb_tag = mb_mp.configuration.get_option(XML_TAG_OPTION)
     mp.configuration.set_option(XML_TAG_OPTION, mb_tag)
     mp.mapping_registry.set_mapping(mp)
     with self.assert_raises(ValueError) as cm:
         getattr(mp.mapping_registry, 'parsing_lookup')
     exc_msg = 'Duplicate tag "%s" ' % mb_tag
     self.assert_not_equal(cm.exception.message.find(exc_msg), -1)
Beispiel #23
0
 def test_clone_with_options(self):
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     mp1 = mp.clone(attribute_options={
         ('parent', 'text'): {
             IGNORE_ON_READ_OPTION: True
         }
     })
     key = ('parent', )
     parent_attrs = mp1.get_attribute_map(key=key)
     self.assert_true(
         parent_attrs['text'].options.get(IGNORE_ON_READ_OPTION) is True)
Beispiel #24
0
 def test_mapping_reset_lookup(self):
     mp_reg = get_mapping_registry(XmlMime)
     old_lookup = mp_reg.parsing_lookup
     mp = mp_reg.find_or_create_mapping(get_collection_class(IMyEntity))
     new_tag = 'my-new-entities'
     mp.configuration.set_option(XML_TAG_OPTION, new_tag)
     mp_reg.set_mapping(mp)
     new_lookup = mp_reg.parsing_lookup
     self.assert_false(old_lookup is new_lookup)
     ns = mp.configuration.get_option(XML_NAMESPACE_OPTION)
     cls_map = new_lookup.get_namespace(ns)
     self.assert_equal(cls_map[new_tag], mp.data_element_class)
Beispiel #25
0
 def test_mapping_reset_lookup(self):
     mp_reg = get_mapping_registry(XmlMime)
     old_lookup = mp_reg.parsing_lookup
     mp = mp_reg.find_or_create_mapping(get_collection_class(IMyEntity))
     new_tag = 'my-new-entities'
     mp.configuration.set_option(XML_TAG_OPTION, new_tag)
     mp_reg.set_mapping(mp)
     new_lookup = mp_reg.parsing_lookup
     self.assert_false(old_lookup is new_lookup)
     ns = mp.configuration.get_option(XML_NAMESPACE_OPTION)
     cls_map = new_lookup.get_namespace(ns)
     self.assert_equal(cls_map[new_tag], mp.data_element_class)
Beispiel #26
0
 def map_to_data_element(self, resource):
     # We use the XML mapping for the content serialization.
     xml_mp_reg = get_mapping_registry(XmlMime)
     xml_mp = xml_mp_reg.find_or_create_mapping(type(resource))
     ns_map = self.mapping_registry.namespace_map
     atom_mp = self.mapping_registry.find_or_create_mapping(type(resource))
     data_el = \
         atom_mp.data_element_class.create_from_resource(resource,
                                                         ns_map=ns_map)
     if provides_member_resource(resource):
         self.__map_member_to_data_element(data_el, resource, xml_mp)
     else:
         self.__map_collection_to_data_element(data_el, resource, xml_mp)
     return data_el
Beispiel #27
0
 def map_to_data_element(self, resource):
     # We use the XML mapping for the content serialization.
     xml_mp_reg = get_mapping_registry(XmlMime)
     xml_mp = xml_mp_reg.find_or_create_mapping(type(resource))
     ns_map = self.mapping_registry.namespace_map
     atom_mp = self.mapping_registry.find_or_create_mapping(type(resource))
     data_el = \
         atom_mp.data_element_class.create_from_resource(resource,
                                                         ns_map=ns_map)
     if provides_member_resource(resource):
         self.__map_member_to_data_element(data_el, resource, xml_mp)
     else:
         self.__map_collection_to_data_element(data_el, resource, xml_mp)
     return data_el
 def test_json_data_tree_traverser(self):
     mp_reg = get_mapping_registry(JsonMime)
     default_mp = mp_reg.find_or_create_mapping(MyEntityMember)
     attr_opts = {('parent',):{WRITE_AS_LINK_OPTION:False}}
     mp = default_mp.clone(attribute_options=attr_opts)
     vst = DataElementBuilderRepresentationDataVisitor(mp)
     for json_data, exc_msg in ((object, 'Need dict (member),'),
                                ({'parent':
                                     {'__jsonclass__':'http://foo.org'}},
                                 'Expected data for'),):
         trv = JsonDataTreeTraverser(json_data, mp)
         with self.assert_raises(ValueError) as cm:
             trv.run(vst)
         self.assert_true(cm.exception.message.startswith(exc_msg))
Beispiel #29
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)
Beispiel #30
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)
Beispiel #31
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)
Beispiel #32
0
 def test_defaults(self):
     mp_reg = get_mapping_registry(CsvMime)
     mp = mp_reg.find_or_create_mapping(MyEntityMember)
     attrs = mp.get_attribute_map()
     self.assert_true(
         attrs['text'].options.get(IGNORE_ON_READ_OPTION) is None)
     self.assert_true(
         attrs['text'].options.get(IGNORE_ON_WRITE_OPTION) is None)
     self.assert_true(
         attrs['parent'].options.get(IGNORE_ON_READ_OPTION) is None)
     self.assert_true(
         attrs['parent'].options.get(IGNORE_ON_WRITE_OPTION) is None)
     key = ('parent', )
     parent_attrs = mp.get_attribute_map(key=key)
     self.assert_true(
         parent_attrs['text'].options.get(IGNORE_ON_READ_OPTION) is None)
     self.assert_true(
         parent_attrs['text'].options.get(IGNORE_ON_WRITE_OPTION) is None)
Beispiel #33
0
 def _create(cls, ns_map):
     if ns_map is None:
         mp_reg = get_mapping_registry(XmlMime)
         ns_map = mp_reg.namespace_map
     cls_xml_tag = cls.mapping.configuration.get_option(XML_TAG_OPTION)
     if cls_xml_tag is None:
         raise ValueError('No XML tag registered for mapped class '
                          '%s.' % cls.mapping.mapped_class)
     cls_xml_ns = \
             cls.mapping.configuration.get_option(XML_NAMESPACE_OPTION)
     if not cls_xml_ns is None:
         tag = "{%s}%s" % (cls_xml_ns, cls_xml_tag)
         # FIXME: is this really necessary?
         ns_map[None] = cls_xml_ns
     else:
         tag = cls_xml_tag
     el_fac = XmlParserFactory.create().makeelement
     return el_fac(tag, nsmap=ns_map)
 def test_json_data_tree_traverser(self):
     mp_reg = get_mapping_registry(JsonMime)
     default_mp = mp_reg.find_or_create_mapping(MyEntityMember)
     attr_opts = {('parent', ): {WRITE_AS_LINK_OPTION: False}}
     mp = default_mp.clone(attribute_options=attr_opts)
     vst = DataElementBuilderRepresentationDataVisitor(mp)
     for json_data, exc_msg in (
         (object, 'Need dict (member),'),
         ({
             'parent': {
                 '__jsonclass__': 'http://foo.org'
             }
         }, 'Expected data for'),
     ):
         trv = JsonDataTreeTraverser(json_data, mp)
         with self.assert_raises(ValueError) as cm:
             trv.run(vst)
         self.assert_true(cm.exception.message.startswith(exc_msg))
Beispiel #35
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')
Beispiel #36
0
 def _create(cls, ns_map):
     if ns_map is None:
         mp_reg = get_mapping_registry(XmlMime)
         ns_map = mp_reg.namespace_map
     cls_xml_tag = cls.mapping.configuration.get_option(XML_TAG_OPTION)
     if cls_xml_tag is None:
         raise ValueError('No XML tag registered for mapped class '
                          '%s.' % cls.mapping.mapped_class)
     cls_xml_ns = \
             cls.mapping.configuration.get_option(XML_NAMESPACE_OPTION)
     if not cls_xml_ns is None:
         tag = "{%s}%s" % (cls_xml_ns, cls_xml_tag)
         # FIXME: is this really necessary?
         ns_map[None] = cls_xml_ns
     else:
         tag = cls_xml_tag
     el_fac = XmlParserFactory.create().makeelement
     return el_fac(tag, nsmap=ns_map)
Beispiel #37
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)
Beispiel #38
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')
Beispiel #39
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
Beispiel #40
0
    def test_mapping_polymorhpic(self):
        # pylint: disable=W0232
        class IMyDerivedEntity(IMyEntity):
            pass

        class MyDerivedEntity(MyEntity):
            pass

        class MyDerivedEntityMember(MyEntityMember):
            pass

        class MyDerivedEntityCollection(get_collection_class(IMyEntity)):
            pass

        # pylint: enable=W0232
        self.config.add_resource(IMyDerivedEntity,
                                 MyDerivedEntityMember,
                                 MyDerivedEntity,
                                 MyDerivedEntityCollection,
                                 expose=False)
        self.config.add_resource_representer(IMyDerivedEntity,
                                             XmlMime,
                                             attribute_options={
                                                 ('parent', ):
                                                 dict(ignore=True)
                                             })
        mp_reg = get_mapping_registry(XmlMime)
        mp = mp_reg.find_or_create_mapping(get_collection_class(IMyEntity))
        for rc in (MyDerivedEntityMember, MyDerivedEntityCollection):
            attr = None
            for attr in mp.attribute_iterator(rc):
                if attr.name == 'parent':
                    break
            self.assert_is_not_none(attr)
            self.assert_equal(attr.ignore_on_write, True)
            self.assert_equal(attr.ignore_on_read, True)
Beispiel #41
0
 def create_from_resource(cls, rc):
     mp_reg = get_mapping_registry(cls.content_type)
     rc_cls = type(rc)
     mp = mp_reg.find_or_create_mapping(rc_cls)
     return cls(rc_cls, mp)
Beispiel #42
0
 def __get_ns_map(self):
     ns_map = get_mapping_registry(AtomMime).namespace_map
     ns_map[XML_PREFIX_ATOM] = XML_NS_ATOM
     if None in ns_map:
         del ns_map[None]
     return ns_map
Beispiel #43
0
 def create_from_resource(cls, rc):
     mp_reg = get_mapping_registry(cls.content_type)
     rc_cls = type(rc)
     mp = mp_reg.find_or_create_mapping(rc_cls)
     return cls(rc_cls, mp)
Beispiel #44
0
 def __get_ns_map(self):
     ns_map = get_mapping_registry(AtomMime).namespace_map
     ns_map[XML_PREFIX_ATOM] = XML_NS_ATOM
     if None in ns_map:
         del ns_map[None]
     return ns_map