def test_paginated_included_is_list(self): """ Checks that a list GET for resources returns a list for expected 'included' fields """ response = self.client.get('/planets_with_included') jsonapi_document = api_utils.list_to_jsonapi_document(response.data) self.assertTrue(isinstance(jsonapi_document['included'], list))
def test_get_is_list(self): """ Checks a list is paired with the ``data`` member """ response = self.client.get('/planets') jsonapi_document = api_utils.list_to_jsonapi_document(response.data) document_data = jsonapi_document['data'] self.assertTrue(isinstance(document_data, list))
def test_unpaginated_get_resource_count(self): """ Checks unpaginated list GET resource count matches database count for query """ db_count = Planet.objects.all().count() response = self.client.get('/planets_unpaginated') jsonapi_document = api_utils.list_to_jsonapi_document(response.data, paginated=False) data = jsonapi_document['data'] self.assertEqual(db_count, len(data))
def test_unpaginated_top_level_members(self): """ Checks that an unpaginated list GET for resources returns 'data' member but not a 'meta' member. """ response = self.client.get('/planets_unpaginated') print(response.content.decode()) jsonapi_document = api_utils.list_to_jsonapi_document(response.data, paginated=False) self.assertTrue('data' in jsonapi_document) self.assertFalse('meta' in jsonapi_document)
def test_paginated_top_level_members(self): """ Checks that a list GET for resources returns 'data', 'meta', and expected 'included' fields """ response = self.client.get('/planets_with_included') jsonapi_document = api_utils.list_to_jsonapi_document(response.data) self.assertTrue('data' in jsonapi_document) self.assertTrue('meta' in jsonapi_document) self.assertTrue('included' in jsonapi_document)
def test_paginated_get_one_to_one_pk(self): """ Ensures JSON API compliant build of one-to-one primary key relationship fields within a paginated list view """ response = self.client.get('/yellow_dwarfs') jsonapi_document = api_utils.list_to_jsonapi_document(response.data) data = jsonapi_document['data'] for yellow_dwarf in data: relationships = yellow_dwarf['relationships'] star = relationships['star'] one_to_one_star_data = {'data': {'type': 'stars', 'id': '1'}} self.assertEqual(one_to_one_star_data, star)
def test_unpaginated_get_many_to_one_pk(self): """ Ensures JSON API compliant build of many-to-one primary key fields within an unpaginated list view. In Django, this type of model relationship is represented by the ``ForeignKey`` field that has its ``many`` argument set to ``False``, which is the default. """ response = self.client.get('/planets') jsonapi_document = api_utils.list_to_jsonapi_document(response.data) data = jsonapi_document['data'] for planet in data: relationships = planet['relationships'] many_to_one_star_data = {'data': {'type': 'stars', 'id': '1'}} self.assertEqual(many_to_one_star_data, relationships['system-star'])
def test_paginated_get_one_to_one_with_included(self): """ Ensures JSON API compliant build of one-to-one serialized relationship fields within a paginated list view """ response = self.client.get('/yellow_dwarfs_with_included') jsonapi_document = api_utils.list_to_jsonapi_document(response.data) data = jsonapi_document['data'] for yellow_dwarf in data: relationships = yellow_dwarf['relationships'] star = relationships['star'] one_to_one_star_data = {'data': {'type': 'stars', 'id': '1'}} self.assertEqual(one_to_one_star_data, star) included_star = [{"type": "stars", "id": "1", "attributes": {"galaxy-name": "Via Lactea", "name": "Sun"}}] included = jsonapi_document['included'] self.assertEqual(included_star, included)
def test_unpaginated_get_many_to_many_pk(self): """ Ensures JSON API compliant build of many-to-many primary key fields within an unpaginated list view. In Django, this type of model relationship is represented by the ``ForeignKey`` field that has its ``many`` argument set to ``True``. """ response = self.client.get('/planets') jsonapi_document = api_utils.list_to_jsonapi_document(response.data) data = jsonapi_document['data'] for planet in data: relationships = planet['relationships'] planet_neighbors = relationships['neighbors']['data'] for neighbor in planet_neighbors: self.assertTrue('id' in neighbor) self.assertTrue('type' in neighbor) self.assertTrue(isinstance(neighbor['id'], str)) self.assertTrue(isinstance(neighbor['type'], str))
def test_unpaginated_get_many_to_many_with_included(self): """ Ensures JSON API compliant build of many-to-many serialized relationship fields within an unpaginated list view. In Django, many-to-many model relationship are represented by a ``ForeignKey`` field that has its ``many`` argument set to ``True``. """ response = self.client.get('/planets_with_included') print(response.content.decode()) jsonapi_document = api_utils.list_to_jsonapi_document(response.data) data = jsonapi_document['data'] for planet in data: relationships = planet['relationships'] planet_neighbors = relationships['neighbors']['data'] for neighbor in planet_neighbors: self.assertTrue('id' in neighbor) self.assertTrue('type' in neighbor) self.assertTrue(isinstance(neighbor['id'], str)) self.assertTrue(isinstance(neighbor['type'], str)) included = jsonapi_document['included'] self.assertEqual(included, PLANETS_INCLUDED)
def test_unpaginated_get_many_to_one_with_included(self): """ Ensures JSON API compliant build of many-to-one serialized relationship fields with an unpaginated list view. In Django, this type of model relationship is represented by the ``ForeignKey`` field that has its ``many`` argument set to ``False``, which is the default. """ response = self.client.get('/planets_with_included') jsonapi_document = api_utils.list_to_jsonapi_document(response.data) data = jsonapi_document['data'] for planet in data: relationships = planet['relationships'] many_to_one_star_data = {'data': {'type': 'stars', 'id': '1'}} self.assertEqual(many_to_one_star_data, relationships['system-star']) sun_elements = [("id", "1"), ("type", "stars"), ("attributes", {"galaxy-name": "Via Lactea", "name": "Sun"})] sun = OrderedDict(sun_elements) included = jsonapi_document['included'] included_star = None for model in included: if model['type'] == 'stars': included_star = model self.assertEqual(included_star, sun)