def test_get_collection_given_invalid_limit_for_pagination(self): """Get collection given invalid limit for pagination returns 400. A BadRequestError is raised. """ user = models.User(first='Sally', last='Smith', password='******', username='******') self.session.add(user) blog_post = models.Post(title='This Is A Title', content='This is the content', author_id=user.id, author=user) self.session.add(blog_post) for x in range(10): comment = models.Comment(content='This is comment {0}'.format(x + 1), author_id=user.id, post_id=blog_post.id, author=user, post=blog_post) self.session.add(comment) self.session.commit() with self.assertRaises(errors.BadRequestError) as error: models.serializer.get_collection(self.session, { 'page[offset]': u'5', 'page[limit]': u'foo' }, 'comments') expected_detail = 'Page query parameters must be integers' self.assertEqual(error.exception.detail, expected_detail) self.assertEqual(error.exception.status_code, 400)
def test_get_collection_response_with_no_query_args(self): """Get collection with no query params returns 200.""" user = models.User(first='Sally', last='Smith', password='******', username='******') self.session.add(user) blog_post = models.Post(title='This Is A Title', content='This is the content', author_id=user.id, author=user) self.session.add(blog_post) comment = models.Comment(content='This is a comment', author_id=user.id, post_id=blog_post.id, author=user, post=blog_post) self.session.add(comment) self.session.commit() response = models.serializer.get_collection(self.session, {}, 'comments') expected = { 'data': [{ 'attributes': { 'content': 'This is a comment' }, 'type': 'comments', 'relationships': { 'author': { 'links': { 'related': '/comments/1/author', 'self': '/comments/1/relationships/author' } }, 'post': { 'links': { 'related': '/comments/1/post', 'self': '/comments/1/relationships/post' } } }, 'id': 1 }], 'jsonapi': { 'version': '1.0' }, 'meta': { 'sqlalchemy_jsonapi_version': __version__ }, 'included': [] } actual = response.data self.assertEqual(expected, actual) self.assertEqual(200, response.status_code)
def test_get_collection_given_pagination_with_offset(self): """Get collection given pagination with offset 200.""" user = models.User(first='Sally', last='Smith', password='******', username='******') self.session.add(user) blog_post = models.Post(title='This Is A Title', content='This is the content', author_id=user.id, author=user) self.session.add(blog_post) for x in range(10): comment = models.Comment(content='This is comment {0}'.format(x + 1), author_id=user.id, post_id=blog_post.id, author=user, post=blog_post) self.session.add(comment) self.session.commit() response = models.serializer.get_collection(self.session, { 'page[offset]': u'5', 'page[limit]': u'2' }, 'comments') expected = { 'jsonapi': { 'version': '1.0' }, 'meta': { 'sqlalchemy_jsonapi_version': __version__ }, 'included': [], 'data': [{ 'relationships': { 'author': { 'links': { 'related': '/comments/6/author', 'self': '/comments/6/relationships/author' } }, 'post': { 'links': { 'related': '/comments/6/post', 'self': '/comments/6/relationships/post' } } }, 'attributes': { 'content': u'This is comment 6' }, 'id': 6, 'type': 'comments' }, { 'relationships': { 'author': { 'links': { 'related': '/comments/7/author', 'self': '/comments/7/relationships/author' } }, 'post': { 'links': { 'related': '/comments/7/post', 'self': '/comments/7/relationships/post' } } }, 'attributes': { 'content': u'This is comment 7' }, 'id': 7, 'type': 'comments' }] } actual = response.data self.assertEqual(expected, actual) self.assertEqual(200, response.status_code)
def test_get_collection_when_including_model_and_its_attribute(self): """Get collection when including the model and its attribute returns 200.""" user = models.User(first='Sally', last='Smith', password='******', username='******') self.session.add(user) blog_post = models.Post(title='This Is A Title', content='This is the content', author_id=user.id, author=user) self.session.add(blog_post) comment = models.Comment(content='This is comment 1', author_id=user.id, post_id=blog_post.id, author=user, post=blog_post) self.session.add(comment) self.session.commit() response = models.serializer.get_collection(self.session, {'include': 'post.author'}, 'comments') expected = { 'included': [{ 'id': 1, 'type': 'users', 'relationships': { 'posts': { 'links': { 'self': '/users/1/relationships/posts', 'related': '/users/1/posts' } }, 'comments': { 'links': { 'self': '/users/1/relationships/comments', 'related': '/users/1/comments' } }, 'logs': { 'links': { 'self': '/users/1/relationships/logs', 'related': '/users/1/logs' } } }, 'attributes': { 'username': u'SallySmith1', 'first': u'Sally', 'last': u'Smith' } }, { 'id': 1, 'type': 'posts', 'relationships': { 'author': { 'data': { 'id': 1, 'type': 'users' }, 'links': { 'self': '/posts/1/relationships/author', 'related': '/posts/1/author' } }, 'comments': { 'links': { 'self': '/posts/1/relationships/comments', 'related': '/posts/1/comments' } } }, 'attributes': { 'content': u'This is the content', 'title': u'This Is A Title' } }], 'meta': { 'sqlalchemy_jsonapi_version': __version__ }, 'data': [{ 'id': 1, 'type': 'comments', 'relationships': { 'post': { 'data': { 'id': 1, 'type': 'posts' }, 'links': { 'self': '/comments/1/relationships/post', 'related': '/comments/1/post' } }, 'author': { 'links': { 'self': '/comments/1/relationships/author', 'related': '/comments/1/author' } } }, 'attributes': { 'content': u'This is comment 1' } }], 'jsonapi': { 'version': '1.0' } } actual = response.data self.assertEqual(expected, actual) self.assertEqual(200, response.status_code)
def test_get_collection_paginated_response_by_page(self): """Get collection with pagination by page returns 200.""" user = models.User(first='Sally', last='Smith', password='******', username='******') self.session.add(user) blog_post = models.Post(title='This Is A Title', content='This is the content', author_id=user.id, author=user) self.session.add(blog_post) for x in range(20): comment = models.Comment(content='This is comment {0}'.format(x + 1), author_id=user.id, post_id=blog_post.id, author=user, post=blog_post) self.session.add(comment) self.session.commit() response = models.serializer.get_collection(self.session, { 'page[number]': u'1', 'page[size]': u'2' }, 'comments') expected = { 'data': [{ 'id': 3, 'attributes': { 'content': u'This is comment 3' }, 'type': 'comments', 'relationships': { 'author': { 'links': { 'self': '/comments/3/relationships/author', 'related': '/comments/3/author' } }, 'post': { 'links': { 'self': '/comments/3/relationships/post', 'related': '/comments/3/post' } } } }, { 'id': 4, 'attributes': { 'content': u'This is comment 4' }, 'type': 'comments', 'relationships': { 'author': { 'links': { 'self': '/comments/4/relationships/author', 'related': '/comments/4/author' } }, 'post': { 'links': { 'self': '/comments/4/relationships/post', 'related': '/comments/4/post' } } } }], 'included': [], 'meta': { 'sqlalchemy_jsonapi_version': __version__ }, 'jsonapi': { 'version': '1.0' } } actual = response.data self.assertEquals(expected, actual) self.assertEquals(200, response.status_code)