def setUp(self): self.author = Author.objects.create(name='Super powerful superhero', email='*****@*****.**') self.blog = Blog.objects.create(name='Some Blog', tagline="It's a blog") self.other_blog = Blog.objects.create(name='Other blog', tagline="It's another blog") self.first_entry = Entry.objects.create(blog=self.blog, headline='headline one', body_text='body_text two', pub_date=timezone.now(), mod_date=timezone.now(), n_comments=0, n_pingbacks=0, rating=3) self.second_entry = Entry.objects.create(blog=self.blog, headline='headline two', body_text='body_text one', pub_date=timezone.now(), mod_date=timezone.now(), n_comments=0, n_pingbacks=0, rating=1) self.comment = Comment.objects.create(entry=self.first_entry) CommentFactory.create_batch(50)
def setUp(self): self.author = Author.objects.create(name='Super powerful superhero', email='*****@*****.**') self.blog = Blog.objects.create(name='Some Blog', tagline="It's a blog") self.other_blog = Blog.objects.create(name='Other blog', tagline="It's another blog") self.first_entry = Entry.objects.create( blog=self.blog, headline='headline one', body_text='body_text two', pub_date=timezone.now(), mod_date=timezone.now(), n_comments=0, n_pingbacks=0, rating=3 ) self.second_entry = Entry.objects.create( blog=self.blog, headline='headline two', body_text='body_text one', pub_date=timezone.now(), mod_date=timezone.now(), n_comments=0, n_pingbacks=0, rating=1 ) self.comment = Comment.objects.create(entry=self.first_entry) CommentFactory.create_batch(50)
def test_retrieve_related_many_hyperlinked(self): comment = CommentFactory(author=self.author) url = reverse('author-related', kwargs={ 'pk': self.author.pk, 'related_field': 'comments' }) resp = self.client.get(url) self.assertEqual(resp.status_code, 200) self.assertTrue(isinstance(resp.json()['data'], list)) self.assertEqual(len(resp.json()['data']), 1) self.assertEqual(resp.json()['data'][0]['id'], str(comment.id))
def test_retrieve_related_many_hyperlinked(self): comment = CommentFactory(author=self.author) url = reverse("author-related", kwargs={ "pk": self.author.pk, "related_field": "comments" }) resp = self.client.get(url) self.assertEqual(resp.status_code, 200) self.assertTrue(isinstance(resp.json()["data"], list)) self.assertEqual(len(resp.json()["data"]), 1) self.assertEqual(resp.json()["data"][0]["id"], str(comment.id))
def test_new_comment_data_patch_to_many_relationship(self): entry = EntryFactory(blog=self.blog, authors=(self.author, )) comment = CommentFactory(entry=entry) url = '/authors/{}/relationships/comments'.format(self.author.id) request_data = { 'data': [ { 'type': format_resource_type('Comment'), 'id': str(comment.id) }, ] } previous_response = { 'data': [{ 'type': 'comments', 'id': str(self.second_comment.id) }], 'links': { 'self': 'http://testserver/authors/{}/relationships/comments'.format( self.author.id) } } response = self.client.get(url) assert response.status_code == 200 assert response.json() == previous_response new_patched_response = { 'data': [{ 'type': 'comments', 'id': str(comment.id) }], 'links': { 'self': 'http://testserver/authors/{}/relationships/comments'.format( self.author.id) } } response = self.client.patch(url, data=request_data) assert response.status_code == 200 assert response.json() == new_patched_response assert Comment.objects.filter(id=self.second_comment.id).exists()
def test_new_comment_data_patch_to_many_relationship(self): entry = EntryFactory(blog=self.blog, authors=(self.author, )) comment = CommentFactory(entry=entry) url = "/authors/{}/relationships/comments".format(self.author.id) request_data = { "data": [ { "type": format_resource_type("Comment"), "id": str(comment.id) }, ] } previous_response = { "data": [{ "type": "comments", "id": str(self.second_comment.id) }], "links": { "self": "http://testserver/authors/{}/relationships/comments".format( self.author.id) }, } response = self.client.get(url) assert response.status_code == 200 assert response.json() == previous_response new_patched_response = { "data": [{ "type": "comments", "id": str(comment.id) }], "links": { "self": "http://testserver/authors/{}/relationships/comments".format( self.author.id) }, } response = self.client.patch(url, data=request_data) assert response.status_code == 200 assert response.json() == new_patched_response assert Comment.objects.filter(id=self.second_comment.id).exists()