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')
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")
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'])
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'])
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"])
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"])