def test_meta(): response = PostResponder.build( {'id': 1, 'title': 'Yeah'}, meta={'key': 'value'}, ) assert response['meta']['key'] == 'value'
def test_one_to_one_with_missing_value(self): author = {'id': 1} p1 = {'id': 1, 'title': 'My title', 'author': author} p2 = {'id': 2, 'title': 'My other title', 'author': None} response = PostResponder.build([p1, p2], links=['author']) assert response == { 'jsonapi': JSONAPI_VERSION_DICT, 'data': [{ 'id': 1, 'title': 'My title', 'links': { 'author': 1, }, }, { 'id': 2, 'title': 'My other title', 'links': {}, }], 'links': { 'posts.author': { 'href': 'http://example.com/people/{posts.author}', 'type': 'people', } } }
def test_one_to_one_with_missing_value(self): author = {'id': 1} p1 = {'id': 1, 'title': 'My title', 'author': author} p2 = {'id': 2, 'title': 'My other title', 'author': None} response = PostResponder.build([p1, p2], links=['author']) assert response == { 'posts': [{ 'id': 1, 'title': 'My title', 'links': { 'author': 1, }, }, { 'id': 2, 'title': 'My other title', 'links': {}, }], 'links': { 'posts.author': { 'href': 'http://example.com/people/{posts.author}', 'type': 'people', } } }
def test_one_to_many(self): comments = [ {'id': 1, 'content': 'My comment'}, {'id': 2, 'content': 'Another comment'}, ] post = {'id': 1, 'title': 'My title', 'comments': comments} response = PostResponder.build(post, links=['comments']) assert response == { 'jsonapi': JSONAPI_VERSION_DICT, 'data': { 'id': 1, 'title': 'My title', 'links': { 'comments': [1, 2], } }, 'links': { 'posts.comments': { 'href': 'http://example.com/comments/{posts.comments}', 'type': 'comments', } } }
def test_one_to_one_with_collect(self): author = {'id': 1} post = {'id': 1, 'title': 'My title', 'author': author} response = PostResponder.build(post, collect=True) assert response == { 'jsonapi': JSONAPI_VERSION_DICT, 'data': { 'id': 1, 'title': 'My title', 'links': { 'author': 1, } }, 'included': { 'people': [author], }, 'links': { 'posts.author': { 'href': 'http://example.com/people/{posts.author}', 'type': 'people', }, } }
def test_one_to_many(): comments = [ {'id': 1, 'content': 'My comment'}, {'id': 2, 'content': 'Another comment'}, ] post = {'id': 1, 'title': 'My title', 'comments': comments} data = PostResponder.build(post, links=['comments']) assert data == { 'posts': [ { 'id': 1, 'title': 'My title', 'links': { 'comments': [1, 2], } }, ], 'links': { 'posts.comments': { 'href': 'http://example.com/comments/{posts.comments}', 'type': 'comments', } } }
def test_multiple(self): response = PostResponder.build([ { 'id': 1, 'title': 'A title' }, { 'id': 2, 'title': 'Another title' }, ]) assert response == { 'data': [{ 'attributes': { 'title': 'A title' }, 'id': 1, 'type': 'posts' }, { 'attributes': { 'title': 'Another title' }, 'id': 2, 'type': 'posts' }], 'jsonapi': JSONAPI_VERSION_DICT }
def test_single(self): response = PostResponder.respond( {'id': 1, 'title': 'A title'} ) assert json.loads(response) == { 'posts': {'id': 1, 'title': 'A title'} }
def test_dumps(): data = PostResponder.dumps([ {'id': 1, 'title': 'A title'} ]) assert json.loads(data) == { 'posts': [ {'id': 1, 'title': 'A title'} ] }
def test_multiple(): data = PostResponder.build([ {'id': 1, 'title': 'A title'}, {'id': 2, 'title': 'Another title'}, ]) assert data == { 'posts': [ {'id': 1, 'title': 'A title'}, {'id': 2, 'title': 'Another title'}, ] }
def test_multiple(self): response = PostResponder.build([ {'id': 1, 'title': 'A title'}, {'id': 2, 'title': 'Another title'}, ]) assert response == { 'posts': [ {'id': 1, 'title': 'A title'}, {'id': 2, 'title': 'Another title'}, ] }
def test_single(self): response = PostResponder.respond({'id': 1, 'title': 'A title'}) assert json.loads(response) == { 'data': { 'attributes': { 'title': 'A title' }, 'id': 1, 'type': 'posts' }, 'jsonapi': JSONAPI_VERSION_DICT }
def test_single(self): response = PostResponder.build({'id': 1, 'title': 'My title'}) assert response == { 'data': { 'attributes': { 'title': 'My title' }, 'id': 1, 'type': 'posts' }, 'jsonapi': JSONAPI_VERSION_DICT }
def test_nested_collection(self): author = {'id': 1} flamer = {'id': 2} comment = {'id': 1, 'content': '<redacted>', 'author': flamer} post = { 'id': 1, 'title': 'My title', 'author': author, 'comments': [comment], } response = PostResponder.build(post, collect=True) assert response == { 'posts': { 'id': 1, 'title': 'My title', 'links': { 'author': 1, 'comments': [1], } }, 'linked': { 'people': [author, flamer], 'comments': [ { 'id': 1, 'content': '<redacted>', 'links': { 'author': 2 } } ], }, 'links': { 'comments.author': { 'href': 'http://example.com/people/{comments.author}', 'type': 'people', }, 'posts.author': { 'href': 'http://example.com/people/{posts.author}', 'type': 'people', }, 'posts.comments': { 'href': 'http://example.com/comments/{posts.comments}', 'type': 'comments', }, } }
def test_nested_collection(self): author = {'id': 1} flamer = {'id': 2} comment = {'id': 1, 'content': '<redacted>', 'author': flamer} post = { 'id': 1, 'title': 'My title', 'author': author, 'comments': [comment], } response = PostResponder.build(post, collect=True) assert response == { 'jsonapi': JSONAPI_VERSION_DICT, 'data': { 'id': 1, 'title': 'My title', 'links': { 'author': 1, 'comments': [1], } }, 'included': { 'people': [author, flamer], 'comments': [{ 'id': 1, 'content': '<redacted>', 'links': { 'author': 2 } }], }, 'links': { 'comments.author': { 'href': 'http://example.com/people/{comments.author}', 'type': 'people', }, 'posts.author': { 'href': 'http://example.com/people/{posts.author}', 'type': 'people', }, 'posts.comments': { 'href': 'http://example.com/comments/{posts.comments}', 'type': 'comments', }, } }
def test_one_to_many_with_missing_values(self): comments = [None, None] post = {'id': 1, 'title': 'My title', 'comments': comments} response = PostResponder.build(post, links=['comments']) assert response == { 'posts': { 'id': 1, 'title': 'My title', 'links': {}, }, 'links': { 'posts.comments': { 'href': 'http://example.com/comments/{posts.comments}', 'type': 'comments', } } }
def test_single(self): author = {'id': 1, 'name': 'John'} comments = [ {'id': 1, 'content': 'My comment'}, {'id': 2, 'content': 'Another comment'}, ] post = {'id': 1, 'title': 'My title', 'comments': comments, 'author': author} response = PostResponder.build(post, related={ 'comments': comments, 'author': [author] }) assert response == { 'jsonapi': JSONAPI_VERSION_DICT, 'data': { 'id': 1, 'title': 'My title', 'links': { 'author': 1, 'comments': [1, 2], } }, 'links': { 'posts.author': { 'href': 'http://example.com/people/{posts.author}', 'type': 'people', }, 'posts.comments': { 'href': 'http://example.com/comments/{posts.comments}', 'type': 'comments', } }, 'included': { 'comments': [ {'id': 1, 'content': 'My comment'}, {'id': 2, 'content': 'Another comment'}, ], 'people': [ {'id': 1, 'name': 'John'}, ] } }
def test_single(self): author = {'id': 1, 'name': 'John'} comments = [ {'id': 1, 'content': 'My comment'}, {'id': 2, 'content': 'Another comment'}, ] post = {'id': 1, 'title': 'My title', 'comments': comments, 'author': author} response = PostResponder.build(post, linked={ 'comments': comments, 'author': [author] }) assert response == { 'posts': { 'id': 1, 'title': 'My title', 'links': { 'author': 1, 'comments': [1, 2], } }, 'links': { 'posts.author': { 'href': 'http://example.com/people/{posts.author}', 'type': 'people', }, 'posts.comments': { 'href': 'http://example.com/comments/{posts.comments}', 'type': 'comments', } }, 'linked': { 'comments': [ {'id': 1, 'content': 'My comment'}, {'id': 2, 'content': 'Another comment'}, ], 'people': [ {'id': 1, 'name': 'John'}, ] } }
def test_one_to_one(self): author = {'id': 1} post = {'id': 1, 'title': 'My title', 'author': author} response = PostResponder.build(post, links=['author']) assert response == { 'posts': { 'id': 1, 'title': 'My title', 'links': { 'author': 1, } }, 'links': { 'posts.author': { 'href': 'http://example.com/people/{posts.author}', 'type': 'people', } } }
def test_one_to_one_with_collect(self): author = {'id': 1} post = {'id': 1, 'title': 'My title', 'author': author} response = PostResponder.build(post, collect=True) assert response == { 'posts': { 'id': 1, 'title': 'My title', 'links': { 'author': 1, } }, 'linked': { 'people': [author], }, 'links': { 'posts.author': { 'href': 'http://example.com/people/{posts.author}', 'type': 'people', }, } }
def test_single(self): response = PostResponder.build({'id': 1, 'title': 'My title'}) assert response == {'posts': {'id': 1, 'title': 'My title'}}
def test_single_object(): data = PostResponder.build({'id': 1, 'title': 'My title'}) assert data == {'posts': [{'id': 1, 'title': 'My title'}]}