Example #1
0
    def test_construct_relationship_embedded(self):
        """An embedded relationship"""
        class MyResource(ResourceBase):
            _relationships = Relationship('related',
                                          relation='RelatedResource',
                                          embedded=True),

        class RelatedResource(ResourceBase):
            pks = 'id',

        res = MyResource(properties=dict(related=dict(id=1, value=2)))
        adapter = JSONAPIAdapter(res, base_url='/blah')
        resp = adapter._construct_relationships(res)
        self.assertIn('related', resp)
        self.assertIn('data', resp['related'])
        data = resp['related']['data']
        self.assertEqual(len(data), 1)
        data = data[0]
        self.assertIn('attributes', data)
        self.assertDictEqual(
            dict(type='related_resource',
                 id='1',
                 links=dict(self='/blah/related_resource/1'),
                 relationships={},
                 attributes=dict(id=1, value=2)), data)
Example #2
0
    def test_construct_links_no_links(self):
        """Constructing links when there are not any"""
        class MyResource(ResourceBase):
            pks = 'id',

        res = MyResource(properties=dict(id=1, field='value'))
        adapter = JSONAPIAdapter(resource=res)
        resp = adapter._construct_links(res)
        self.assertDictEqual(dict(self='/my_resource/1'), resp)
Example #3
0
    def test_construct_data_with_base_url(self):
        """Tests when there is a base_url"""
        class MyResource(ResourceBase):
            pks = 'id',

        res = MyResource(properties=dict(id=1, field='value'))
        adapter = JSONAPIAdapter(resource=res, base_url='http://blah.com')
        resp = adapter._construct_data(res)
        self.assertEqual(resp['links']['self'],
                         'http://blah.com/my_resource/1')
Example #4
0
    def test_construct_data_not_embedded(self):
        """Only the self link should be included"""
        class MyResource(ResourceBase):
            pks = 'id',

        res = MyResource(properties=dict(id=1, field='value'))
        adapter = JSONAPIAdapter(resource=res)
        resp = adapter._construct_data(res, embedded=False)
        self.assertNotIn('relationships', resp)
        self.assertNotIn('attributes', resp)
        self.assertDictEqual(dict(self='/my_resource/1'), resp['links'])
        self.assertEqual('1', resp['id'])
        self.assertEqual('my_resource', resp['type'])
Example #5
0
    def test_construct_data_embedded(self):
        """Ensures that reltionships, links, and attributes are included"""
        class MyResource(ResourceBase):
            pks = 'id',

        res = MyResource(properties=dict(id=1, field='value'))
        adapter = JSONAPIAdapter(resource=res)
        resp = adapter._construct_data(res, embedded=True)
        self.assertDictEqual(resp['relationships'], dict())
        self.assertDictEqual(resp['attributes'], res.properties)
        self.assertDictEqual(dict(self='/my_resource/1'), resp['links'])
        self.assertEqual('1', resp['id'])
        self.assertEqual('my_resource', resp['type'])
Example #6
0
    def test_formatted_body(self):
        """Simple explosion test"""
        class MyResource(ResourceBase):
            pks = 'id',

        res = MyResource(properties=dict(id=1))
        adapter = JSONAPIAdapter(res)
        resp = adapter.formatted_body
        body = json.loads(resp)
        self.assertIn('data', body)
        body = body['data']
        self.assertEqual(body['id'], '1')
        self.assertEqual(body['type'], 'my_resource')
        self.assertDictEqual(body['links'], dict(self='/my_resource/1'))
        self.assertDictEqual(body['attributes'], dict(id=1))
Example #7
0
    def test_no_pks_resource(self):
        """
        Tests that a resource with no pks is appropriately
        constructed.
        """
        class MyResource(ResourceBase):
            pks = 'id',

        res = MyResource(properties=dict(value=1), no_pks=True)
        adapter = JSONAPIAdapter(res)
        resp = adapter.formatted_body
        data = json.loads(resp)['data']
        self.assertDictEqual(data['attributes'], dict(value=1))
        self.assertEqual(data['links'], dict(self='/my_resource'))
        self.assertEqual(data['id'], '')
        self.assertEqual(data['type'], 'my_resource')
Example #8
0
    def test_construct_links(self):
        """Expected case"""
        class MyResource(ResourceBase):
            pks = 'id',
            _links = Relationship('child',
                                  property_map=dict(child_id='pk'),
                                  relation='RelatedResource'),

        class RelatedResource(ResourceBase):
            pks = 'pk',

        res = MyResource(properties=dict(id=1),
                         meta=dict(links=dict(child_id=2)))
        adapter = JSONAPIAdapter(resource=res)
        resp = adapter._construct_links(res)
        expected = dict(self='/my_resource/1', child='/related_resource/2')
        self.assertEqual(resp, expected)
Example #9
0
    def test_construct_relationship_list(self):
        """A ListRelationship instance"""
        class MyResource(ResourceBase):
            _relationships = ListRelationship('related',
                                              relation='RelatedResource'),

        class RelatedResource(ResourceBase):
            pks = 'id',

        res = MyResource(properties=dict(related=[dict(id=2), dict(id=1)]))
        adapter = JSONAPIAdapter(res, base_url='/blah')
        resp = adapter._construct_relationships(res)
        self.assertIn('related', resp)
        self.assertIn('data', resp['related'])
        data = resp['related']['data']
        self.assertEqual(len(data), 2)
        for related in data:
            self.assertEqual(related['type'], 'related_resource')
            self.assertTrue(
                related['links']['self'].startswith('/blah/related_resource'))