示例#1
0
    def links(cls):
        """
        Generates links to each of the linked_resource_classes.

        :return: A list of links.
        :rtype: list
        """
        _links = []
        for klass in cls.linked_resource_classes:
            has_no_pks = False
            has_pk_endpoint = False
            for name, all_endpoints in six.iteritems(klass.endpoint_dictionary()):
                for endpoint in all_endpoints:
                    if endpoint['no_pks'] is True:
                        has_no_pks = True
                    else:
                        has_pk_endpoint = True
            if has_no_pks:
                rel = Relationship('{0}_list'.format(klass.resource_name),
                                   relation=klass.__name__, no_pks=True,
                                   templated=True)
                _links.append(rel)
            if has_pk_endpoint:
                rel = Relationship(klass.resource_name, relation=klass.__name__,
                                   no_pks=False, templated=True)
                _links.append(rel)
        return _links
示例#2
0
 def test_init(self):
     """
     Tests the initialization of a Relationship instance
     In particular checking that a property map is
     always available.
     """
     r = Relationship('related')
     self.assertEqual(r.property_map, {})
     x = dict(some='thing')
     r = Relationship('related', property_map=x)
     self.assertEqual(r.property_map, x)
示例#3
0
    def test_remove_child_resource_properties(self):
        property_map = dict(parent="child", parent2="child2")
        original_properties = dict(parent="value", parent2="value2", parent3="value3", parent4="value4")
        r = Relationship("related", property_map=property_map)
        updated_properties = r.remove_child_resource_properties(original_properties)
        self.assertNotEqual(id(updated_properties), id(original_properties))
        expected = dict(parent3="value3", parent4="value4")
        self.assertDictEqual(updated_properties, expected)

        original_properties.pop("parent2")
        updated_properties = r.remove_child_resource_properties(original_properties)
        self.assertDictEqual(updated_properties, expected)
示例#4
0
    def test_remove_child_resource_properties(self):
        property_map = dict(parent='child', parent2='child2')
        original_properties = dict(parent='value', parent2='value2',
                                   parent3='value3', parent4='value4')
        r = Relationship('related', property_map=property_map)
        updated_properties = r.remove_child_resource_properties(original_properties)
        self.assertNotEqual(id(updated_properties), id(original_properties))
        expected = dict(parent3='value3', parent4='value4')
        self.assertDictEqual(updated_properties, expected)

        original_properties.pop('parent2')
        updated_properties = r.remove_child_resource_properties(original_properties)
        self.assertDictEqual(updated_properties, expected)
示例#5
0
 def test_remove_properties(self):
     """Tests whether properties are appropriately
     kept or removed according to the remove_properties
     attribute"""
     rel = Relationship('related')
     x = dict(related=dict(name='name'))
     ret = rel._map_pks(x)
     self.assertDictEqual(ret, dict(name='name'))
     self.assertDictEqual(x, dict())
     rel.remove_properties = False
     x = dict(related=dict(name='name'))
     ret = rel._map_pks(x)
     self.assertDictEqual(ret, dict(name='name'))
     self.assertDictEqual(x, dict(related=dict(name='name')))
示例#6
0
 def test_remove_properties(self):
     """Tests whether properties are appropriately
     kept or removed according to the remove_properties
     attribute"""
     rel = Relationship('related')
     x = dict(related=dict(name='name'))
     ret = rel._map_pks(x)
     self.assertDictEqual(ret, dict(name='name'))
     self.assertDictEqual(x, dict())
     rel.remove_properties = False
     x = dict(related=dict(name='name'))
     ret = rel._map_pks(x)
     self.assertDictEqual(ret, dict(name='name'))
     self.assertDictEqual(x, dict(related=dict(name='name')))
示例#7
0
 def test_relation_property(self):
     """
     Tests whether the relation property is appropriately
     retrieved from ResourceMetaClass
     """
     r = Relationship('related')
     try:
         x = r.relation
         assert False
     except KeyError:
         assert True
     mck = mock.MagicMock(registered_names_map={'SomeClass': True})
     r = Relationship('related', relation='SomeClass')
     r._resource_meta_class = mck
     assert r.relation is True
示例#8
0
    def test_remove_properties_property_map(self):
        """Tests whether removing properties according
        to the property_map adheres to the remove_properties
        attribute"""

        rel = Relationship('related', property_map=dict(name='name'))
        x = dict(name='name')
        ret = rel._map_pks(x)
        self.assertDictEqual(ret, dict(name='name'))
        self.assertDictEqual(x, dict())
        rel.remove_properties = False
        x = dict(name='name')
        ret = rel._map_pks(x)
        self.assertDictEqual(ret, dict(name='name'))
        self.assertDictEqual(x, dict(name='name'))
示例#9
0
 def test_relation_property(self):
     """
     Tests whether the relation property is appropriately
     retrieved from ResourceMetaClass
     """
     r = Relationship('related')
     try:
         x = r.relation
         assert False
     except KeyError:
         assert True
     mck = mock.MagicMock(registered_names_map={'SomeClass': True})
     r = Relationship('related', relation='SomeClass')
     r._resource_meta_class = mck
     assert r.relation is True
示例#10
0
    def test_remove_properties_property_map(self):
        """Tests whether removing properties according
        to the property_map adheres to the remove_properties
        attribute"""

        rel = Relationship('related', property_map=dict(name='name'))
        x = dict(name='name')
        ret = rel._map_pks(x)
        self.assertDictEqual(ret, dict(name='name'))
        self.assertDictEqual(x, dict())
        rel.remove_properties = False
        x = dict(name='name')
        ret = rel._map_pks(x)
        self.assertDictEqual(ret, dict(name='name'))
        self.assertDictEqual(x, dict(name='name'))
示例#11
0
 def get_base_links(actual_class):
     """
     Gets the base links for this class.
     Necessary for properly inheriting links in
     descendant classes.
     """
     if actual_class.manager:
         fields = tuple(actual_class.manager.fields)
         fields += (actual_class.manager.pagination_pk_query_arg,
                    actual_class.manager.pagination_count_query_arg)
     else:
         fields = tuple()
     return (Relationship('next', relation=actual_class.__name__,
                          query_args=fields, no_pks=True),
             Relationship('previous', relation=actual_class.__name__,
                          query_args=fields, no_pks=True),)
示例#12
0
    def test_remove_child_resource_properties(self):
        property_map = dict(parent='child', parent2='child2')
        original_properties = dict(parent='value',
                                   parent2='value2',
                                   parent3='value3',
                                   parent4='value4')
        r = Relationship('related', property_map=property_map)
        updated_properties = r.remove_child_resource_properties(
            original_properties)
        self.assertNotEqual(id(updated_properties), id(original_properties))
        expected = dict(parent3='value3', parent4='value4')
        self.assertDictEqual(updated_properties, expected)

        original_properties.pop('parent2')
        updated_properties = r.remove_child_resource_properties(
            original_properties)
        self.assertDictEqual(updated_properties, expected)
示例#13
0
    def test_construct_resource(self):
        """
        Tests the construction of a related resource
        """
        class RelatedResource(ResourceBase):
            pass

        property_map = dict(parent='child')
        r = Relationship('related', property_map=property_map, relation='RelatedResource')
        prop_input = dict(parent='value')
        resource = r.construct_resource(prop_input)

        self.assertIsNotNone(resource)

        r.required = True
        # This should raise a key error since the field is required
        self.assertRaises(RestException, r.construct_resource, {})
示例#14
0
    def test_construct_resource(self):
        """
        Tests the construction of a related resource
        """
        class RelatedResource(ResourceBase):
            pass

        property_map = dict(parent='child')
        r = Relationship('related',
                         property_map=property_map,
                         relation='RelatedResource')
        prop_input = dict(parent='value')
        resource = r.construct_resource(prop_input)

        self.assertIsNotNone(resource)

        r.required = True
        # This should raise a key error since the field is required
        self.assertRaises(RestException, r.construct_resource, {})
示例#15
0
    class HelloWorldViewset(ResourceBase):
        namespace = name_space
        manager = MM1()
        resource_name = 'myresource'
        _relationships = [
            Relationship('related',
                         property_map={'related': 'id'},
                         relation='ComplimentaryViewset')
        ]

        @apimethod(methods=['GET'])
        @translate(fields=[StringField('content')], validate=True)
        def hello(cls, request, *args, **kwargs):
            return cls(properties=request.query_args)
示例#16
0
    def test_return_none(self):
        """Tests that the private _should_return_none appropriately
        returns according to the expected behavior"""
        class MyResource(ResourceBase):
            pks = 'id',

        res = MyResource(no_pks=True)
        rel = Relationship('related')
        self.assertFalse(rel._should_return_none(res))
        res = MyResource(properties=dict(id=1))
        self.assertFalse(rel._should_return_none(res))
        res = MyResource(properties=dict())
        self.assertTrue(rel._should_return_none(res))
        rel.templated = True
        self.assertFalse(rel._should_return_none(res))
示例#17
0
    def test_return_none(self):
        """Tests that the private _should_return_none appropriately
        returns according to the expected behavior"""
        class MyResource(ResourceBase):
            pks = 'id',

        res = MyResource(no_pks=True)
        rel = Relationship('related')
        self.assertFalse(rel._should_return_none(res))
        res = MyResource(properties=dict(id=1))
        self.assertFalse(rel._should_return_none(res))
        res = MyResource(properties=dict())
        self.assertTrue(rel._should_return_none(res))
        rel.templated = True
        self.assertFalse(rel._should_return_none(res))
示例#18
0
 def get_base_links(actual_class):
     return (Relationship('created', relation=actual_class.__name__, embedded=True), )
示例#19
0
 class MyResource2(ResourceBase):
     _relationships = [
         Relationship('related', relation='RelatedResource2')
     ]