From 8f554883855c1751953c8133b601449f5f349a75 Mon Sep 17 00:00:00 2001 From: abhiabhi94 <13880786+abhiabhi94@users.noreply.github.com> Date: Sun, 2 May 2021 20:47:29 +0530 Subject: [PATCH] test: Make tests run order independent --- comment/tests/base.py | 11 ++++++----- comment/tests/test_api/test_views.py | 10 ++++++---- comment/tests/test_models/test_comments.py | 2 +- comment/tests/test_models/test_flags.py | 8 +++++++- comment/tests/test_template_tags.py | 5 ++++- comment/tests/test_views/test_comments.py | 16 ++++++++-------- comment/tests/test_views/test_flags.py | 3 +-- 7 files changed, 33 insertions(+), 22 deletions(-) diff --git a/comment/tests/base.py b/comment/tests/base.py index 676649e..8209b47 100644 --- a/comment/tests/base.py +++ b/comment/tests/base.py @@ -112,6 +112,7 @@ def setUp(self): self.client.force_login(self.user_1) translation.activate("test") self.addCleanup(patch.stopall) + self.addCleanup(translation.deactivate) @classmethod def increase_comment_count(cls): @@ -264,8 +265,8 @@ def setUp(self): self.client_non_ajax.force_login(self.user_2) self.data = { 'content': 'parent comment was edited', - 'app_name': 'post', - 'model_name': 'post', + 'app_name': self.post_1._meta.app_label, + 'model_name': self.post_1.__class__.__name__.lower(), 'model_id': self.post_1.id, } @@ -405,9 +406,9 @@ def setUpTestData(cls): cls.factory = RequestFactory() cls.data = { 'content': 'test', - 'model_name': 'post', - 'app_name': 'post', - 'model_id': 1 + 'model_name': cls.post_1.__class__.__name__.lower(), + 'app_name': cls.post_1._meta.app_label, + 'model_id': cls.post_1.id, } cls.comment = cls.create_comment(cls.post_1, cls.user_1) diff --git a/comment/tests/test_api/test_views.py b/comment/tests/test_api/test_views.py index 00147e4..f81d8b8 100644 --- a/comment/tests/test_api/test_views.py +++ b/comment/tests/test_api/test_views.py @@ -9,6 +9,7 @@ from comment.models import Comment, FlagInstanceManager, ReactionInstance from comment.messages import ContentTypeError, EmailError, ReactionError from comment.api.serializers import CommentSerializer +from comment.utils import get_model_obj from comment.tests.base import BaseAPITest, timezone from comment.tests.test_utils import BaseAnonymousCommentTest @@ -17,9 +18,9 @@ class BaseAPIViewTest(BaseAPITest): def setUp(self): super().setUp() self.url_data = { - 'model_name': 'post', - 'app_name': 'post', - 'model_id': 1 + 'model_name': self.post_1.__class__.__name__.lower(), + 'app_name': self.post_1._meta.app_label, + 'model_id': self.post_1.id, } self.parents = Comment.objects.filter_parents_by_object(self.post_1).count() self.all_comments = Comment.objects.all().count() @@ -163,7 +164,8 @@ def test_create_parent_comment(self): def test_create_child_comment(self): url_data = self.url_data.copy() - url_data['parent_id'] = 1 + model_obj = get_model_obj(**url_data) + url_data['parent_id'] = Comment.objects.all_comments_by_object(model_obj).filter(parent=None).first().id data = {'content': 'new child comment from api'} response = self.client.post(self.get_url(self.get_base_url(), **url_data), data=data) diff --git a/comment/tests/test_models/test_comments.py b/comment/tests/test_models/test_comments.py index 0cb6e95..a98d4be 100644 --- a/comment/tests/test_models/test_comments.py +++ b/comment/tests/test_models/test_comments.py @@ -243,7 +243,7 @@ def test_get_parent_comment(self): # no parent_id doesn't exist passed -> although this is highly unlikely as this will be handled by the mixin # but is useful for admin interface if required self.assertIsNone(Comment.objects.get_parent_comment(100)) - parent_comment = Comment.objects.get_parent_comment(1) + parent_comment = Comment.objects.get_parent_comment(self.parent_comment_1.id) self.assertIsNotNone(parent_comment) self.assertEqual(parent_comment, self.parent_comment_1) diff --git a/comment/tests/test_models/test_flags.py b/comment/tests/test_models/test_flags.py index 2409b4a..39c0832 100644 --- a/comment/tests/test_models/test_flags.py +++ b/comment/tests/test_models/test_flags.py @@ -110,7 +110,13 @@ def setUpTestData(cls): cls.flag.refresh_from_db() @patch.object(settings, 'COMMENT_FLAGS_ALLOWED', 0) - def test_flag_disabled(self): + def test_flag_disabled_with_flag_count_greater_than_allowed_count(self): + self.flag.state = self.flag.UNFLAGGED + self.flag.save() + self.flag.refresh_from_db() + + # verify that number of flags on the object is greater than the allowed flags + assert self.flag.count > settings.COMMENT_FLAGS_ALLOWED self.flag.toggle_flagged_state() self.assertEqual(self.flag.state, self.flag.UNFLAGGED) diff --git a/comment/tests/test_template_tags.py b/comment/tests/test_template_tags.py index ba9ce5e..fdb1c67 100644 --- a/comment/tests/test_template_tags.py +++ b/comment/tests/test_template_tags.py @@ -147,7 +147,10 @@ def setUpTestData(cls): cls.content = "Any long text just for testing render content function" cls.comment.content = cls.content cls.comment.save() - cls.comment.refresh_from_db() + + def setUp(self): + super().setUp() + self.comment.refresh_from_db() def test_urlhash(self): result = render_content(self.comment) diff --git a/comment/tests/test_views/test_comments.py b/comment/tests/test_views/test_comments.py index b444c55..0f6f6af 100644 --- a/comment/tests/test_views/test_comments.py +++ b/comment/tests/test_views/test_comments.py @@ -202,9 +202,11 @@ def test_cannot_edit_comment_by_different_user(self): class TestDeleteComment(BaseCommentViewTest): - def response_fails_test(self, response): + def response_fails_test(self, response, comment): self.assertEqual(response.status_code, 403) self.assertEqual(response.reason_phrase, 'Forbidden') + # comment has not been deleted + self.assertEqual(comment, Comment.objects.get(id=comment.id)) def test_delete_comment(self): comment = self.create_comment(self.content_object_1) @@ -265,21 +267,19 @@ def test_delete_comment_by_admin(self): self.assertEqual(Comment.objects.count(), init_count - 1) def test_cannot_delete_comment_by_different_user(self): + user = self.user_2 comment = self.create_comment(self.content_object_1) - self.client.force_login(self.user_2) - self.assertEqual(comment.content, 'comment 1') - self.assertEqual(comment.user.username, self.user_1.username) + self.client.force_login(user) - init_count = Comment.objects.all().count() - self.assertEqual(init_count, 1) + assert comment.user.username != user # test GET request response = self.client.get(self.get_url('comment:delete', comment.id), data=self.data) - self.response_fails_test(response) + self.response_fails_test(response, comment) # test POST request response = self.client.post(self.get_url('comment:delete', comment.id), data=self.data) - self.response_fails_test(response) + self.response_fails_test(response, comment) class ConfirmCommentViewTest(BaseAnonymousCommentTest): diff --git a/comment/tests/test_views/test_flags.py b/comment/tests/test_views/test_flags.py index 78c1d22..70e7af0 100644 --- a/comment/tests/test_views/test_flags.py +++ b/comment/tests/test_views/test_flags.py @@ -39,7 +39,6 @@ def test_set_flag(self): @patch.object(settings, 'COMMENT_FLAGS_ALLOWED', 0) def test_set_flag_when_flagging_not_enabled(self): - settings.COMMENT_FLAGS_ALLOWED = 0 _url = self.get_url('comment:flag', self.comment.id) self.flag_data['reason'] = 1 response = self.client.post(_url, data=self.flag_data) @@ -120,7 +119,7 @@ def test_incorrect_comment_id(self): def test_incorrect_reason(self): """Test response when incorrect reason is passed""" url = self.get_url('comment:flag', self.comment.id) - data = self.flag_data + data = self.flag_data.copy() reason = -1 data.update({'reason': reason}) response = self.client.post(url, data=data)