def test_delete_comment_on_customuser_detail(self): """ ユーザ詳細画面から書籍に紐付いたコメントを削除するテスト """ comment = factory.CommentFactory(book=self.book, user=self.user) comment = get_object_or_404(Comment, uuid=comment.uuid) kwargs = { 'book_pk': self.book.uuid, 'comment_pk': comment.uuid } data = { 'template_name': 'customuser_detail', 'user_uuid': self.user.uuid } response = self.client.post(reverse('book:delete_comment', kwargs=kwargs), data, follow=True) is_comment_exist = Comment.objects.filter(book=self.book, user=self.user).exists() expected_message = 'コメントを削除しました。' message = get_response_message(response) self.assertFalse(is_comment_exist) self.assertEqual(message, expected_message) self.assertRedirects(response, reverse('accounts:detail', kwargs={'pk': self.user.uuid}))
def test_delete_comment_on_book_detail(self): """ 書籍詳細画面から書籍に紐付いたコメントを削除するテスト """ book = factory.BookFactory() comment = factory.CommentFactory(book=book, user=self.user) comment = get_object_or_404(Comment, uuid=comment.uuid) kwargs = { 'book_pk': book.uuid, 'comment_pk': comment.uuid } response = self.client.post(reverse('book:delete_comment', kwargs=kwargs), follow=True) is_comment_exist = Comment.objects.filter(book=self.book, user=self.user).exists() expected_message = 'コメントを削除しました。' message = get_response_message(response) self.assertFalse(is_comment_exist) self.assertEqual(message, expected_message) self.assertRedirects(response, reverse('book:detail', kwargs={'pk': book.uuid}))
def test_update_comment(self): """ コメント編集ページにPOSTリクエストを送信時のテスト """ book = factory.BookFactory() comment = factory.CommentFactory(book=book, user=self.user) comment = get_object_or_404(Comment, uuid=comment.uuid) expected_comment_title = 'テストタイトル' expected_comment_score = 5.0 expected_comment_content = 'テストコンテンツ' kwargs = { 'book_pk': book.uuid, 'comment_pk': comment.uuid } data = { 'title': expected_comment_title, 'score': expected_comment_score, 'content': expected_comment_content } response = self.client.post(reverse('book:update_comment', kwargs=kwargs), data, follow=True) comment = get_object_or_404(Comment, uuid=comment.uuid) # TODO フラッシュメッセージの内容をテスト self.assertEqual(comment.title, expected_comment_title) self.assertEqual(comment.score, expected_comment_score) self.assertEqual(comment.content, expected_comment_content) self.assertRedirects(response, reverse('book:detail', kwargs={'pk': book.uuid}))
def test_context_data(self): """ 対象ユーザの詳細ページのHTMLに 渡されるcontextの内容のテスト """ book = factory.BookFactory() favorite = factory.FavoriteFactory(user=self.user, book=book) comment = factory.CommentFactory(user=self.user, book=book) another = factory.CustomUserFactory(email='*****@*****.**') following_relation = Relation(user=self.user, followed=another) following_relation.save() followed_relation = Relation(user=another, followed=self.user) followed_relation.save() response = self.client.get(reverse('accounts:detail', kwargs={'pk': self.user.uuid}), follow=True) self.assertEqual(response.context.get('customuser'), self.user) self.assertIn(favorite, response.context.get('favorite_list')) self.assertIn(comment, response.context.get('comment_list')) self.assertIn(following_relation, response.context.get('following_list')) self.assertIn(followed_relation, response.context.get('followed_list'))
def test_str(self): """ __str__のテスト """ expected_title = 'テストタイトル' comment = factory.CommentFactory(title=expected_title) self.assertEqual(str(comment), expected_title)
def test_update_form_is_valid(self): """ コメント編集時のフォームのバリデーションのテスト(正常系) """ comment = factory.CommentFactory() data = { 'title': 'テストタイトル', 'score': 1.5, 'content': 'テストコンテンツ' } form = self.form(data, instance=comment) self.assertTrue(form.is_valid())
def test_update_comment_get(self): """ コメント編集ページにGETリクエストを送信時のテスト """ comment = factory.CommentFactory(user=self.user, book=self.book) kwargs = { 'book_pk': self.book.uuid, 'comment_pk': comment.uuid } response = self.client.get(reverse('book:update_comment', kwargs=kwargs), follow=True) self.assertTrue(response.context.get('form')) self.assertTemplateUsed('book/comment_form.html')
def test_context_data(self): """ context_dataの中身のテスト """ comment = factory.CommentFactory(user=self.user, book=self.book) favorite = factory.FavoriteFactory(user=self.user, book=self.book) response = self.client.get(reverse('book:detail', args=[self.book.uuid])) context_data = response.context self.assertEqual(self.book, context_data.get('book')) self.assertIn(comment, context_data.get('comment_list')) self.assertTrue(context_data.get('form')) self.assertEqual(favorite, context_data.get('favorite'))
def test_update_form_is_invalid(self): """ コメント投稿時のフォームのバリデーションのテスト(異常系) """ comment = factory.CommentFactory() data = { 'title': '', 'score': 1.5, 'content': 'テストコンテンツ' } form = self.form(data, instance=comment) self.assertFalse(form.is_valid()) self.assertIn('このフィールドは必須です。', form.errors.get('title'))
def test_book_order(self): """ コメントのscoreの平均点の降順で辞書が生成されるかテスト """ for i in range(5): book_name = 'book' + str(i) book = factory.BookFactory(title=book_name) score = 5 - i factory.CommentFactory(book=book, score=score, user=self.user) response = self.client.get(reverse('book:comment_rating_lanking')) sorted_book_average_dict = response.context['sorted_book_average_dict'] for i in range(5): score = 5 - i averages = list(sorted_book_average_dict.values()) self.assertTrue(averages[i], score)