Skip to content

Commit

Permalink
test: Make tests run order independent
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiabhi94 committed May 3, 2021
1 parent bdfdea1 commit 8f55488
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
11 changes: 6 additions & 5 deletions comment/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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)

Expand Down
10 changes: 6 additions & 4 deletions comment/tests/test_api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion comment/tests/test_models/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
8 changes: 7 additions & 1 deletion comment/tests/test_models/test_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion comment/tests/test_template_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions comment/tests/test_views/test_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions comment/tests/test_views/test_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8f55488

Please sign in to comment.