Skip to content

Commit

Permalink
✅ test: Use pytest for testing
Browse files Browse the repository at this point in the history
- also fix some tests that required patching settings.
- reducing time for tests running on travis by running them for lesser environments(still cover all versions)
- add support for python3.9

fix #119
fix #112
  • Loading branch information
abhiabhi94 committed Dec 13, 2020
1 parent 1c2359e commit 134ece6
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 40 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ cache: pip

python:
- 3.6
- 3.7
- 3.8
- 3.9
env:
- DJANGO=2.1
- DJANGO=2.2
- DJANGO=3.0
- DJANGO=3.1
- DJANGO=master

Expand Down
12 changes: 6 additions & 6 deletions comment/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ def test_is_edited_for_anonymous_comment(self):
comment = self.create_anonymous_comment(posted=timezone.now() - timezone.timedelta(days=1))
self.assertFalse(comment.is_edited)

@patch.object(settings, 'COMMENT_FLAGS_ALLOWED', 1)
def test_replies_method(self):
self.assertEqual(self.parent_comment_2.replies().count(), 3)

settings.COMMENT_FLAGS_ALLOWED = 1
init_count = self.parent_comment_2.replies().count()
reply = self.parent_comment_2.replies().first()
self.create_flag_instance(self.user_1, reply)
self.create_flag_instance(self.user_2, reply)
# default replies hide flagged comment
self.assertEqual(self.parent_comment_2.replies().count(), 2)
self.assertEqual(self.parent_comment_2.replies(include_flagged=True).count(), 3)
with patch.object(settings, 'COMMENT_SHOW_FLAGGED', False):
# default replies hide flagged comment
self.assertEqual(self.parent_comment_2.replies().count(), init_count - 1)
self.assertEqual(self.parent_comment_2.replies(include_flagged=True).count(), init_count)

@patch('comment.models.comments.hasattr')
def test_is_flagged_property(self, mocked_hasattr):
Expand Down
25 changes: 11 additions & 14 deletions comment/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from unittest import TestCase
from unittest.mock import patch

from django.utils import timezone
Expand Down Expand Up @@ -337,36 +336,34 @@ def test_for_drf(self):
self.assertEqual(EmailInfo.CONFIRMATION_SENT, response)


class UtilsTest(TestCase):
class TestUtils:
"""Test general purpose utilities that aren't necessarily related to a comment"""

def setUp(self):
self.len_id = 6
len_id = 6

def test_id_generator_length(self):
self.assertEqual(self.len_id, len(id_generator()))
assert self.len_id == len(id_generator())

def test_id_generator_generates_different_ids(self):
self.assertNotEqual(id_generator(), id_generator())
assert id_generator() != id_generator()

def test_id_generator_prefix(self):
prefix = 'comment'
output = id_generator(prefix=prefix)
self.assertEqual(True, output.startswith(prefix))
self.assertEqual(self.len_id + len(prefix), len(output))
assert output.startswith(prefix) is True
assert self.len_id + len(prefix) == len(output)

def test_id_generator_suffix(self):
suffix = 'comment'
output = id_generator(suffix=suffix)
self.assertEqual(True, output.endswith(suffix))
self.assertEqual(self.len_id + len(suffix), len(output))
assert output.endswith(suffix) is True
assert self.len_id + len(suffix) == len(output)

def test_id_generator_chars(self):
import string # flake8:no qa
import string
chars = string.ascii_uppercase
output = id_generator(chars=chars)
self.assertEqual(output, output.upper())
assert output == output.upper()

def test_id_generator_len(self):
len_id = 8
self.assertEqual(len_id, len(id_generator(len_id=len_id)))
assert len_id == len(id_generator(len_id=len_id))
14 changes: 7 additions & 7 deletions comment/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ def test_delete_comment_by_moderator(self):
self.assertEqual(response.status_code, 403)

# moderator can delete flagged comment
settings.COMMENT_FLAGS_ALLOWED = 1
self.create_flag_instance(self.user_1, comment)
self.create_flag_instance(self.user_2, comment)
response = self.client.post(self.get_url('comment:delete', comment.id), data=self.data)
self.assertEqual(response.status_code, 200)
self.assertRaises(Comment.DoesNotExist, Comment.objects.get, id=comment.id)
with patch.object(settings, 'COMMENT_FLAGS_ALLOWED', 1):
self.create_flag_instance(self.user_1, comment)
self.create_flag_instance(self.user_2, comment)
response = self.client.post(self.get_url('comment:delete', comment.id), data=self.data)
self.assertEqual(response.status_code, 200)
self.assertRaises(Comment.DoesNotExist, Comment.objects.get, id=comment.id)

def test_delete_comment_by_admin(self):
comment = self.create_comment(self.content_object_1)
Expand Down Expand Up @@ -344,7 +344,6 @@ def test_set_flag_for_flagging(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 @@ -455,6 +454,7 @@ def test_change_flag_state_with_wrong_state_value(self):
self.assertEqual(response.json()['state'], 0)
self.assertEqual(self.comment.flag.state, self.comment.flag.FLAGGED)

@patch.object(settings, 'COMMENT_FLAGS_ALLOWED', 1)
def test_change_flag_state_success(self):
self.assertTrue(self.comment.is_flagged)
self.client.force_login(self.moderator)
Expand Down
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[flake8]
exclude = .git,*migrations*,test
max-line-length = 120

[tool:pytest]
DJANGO_SETTINGS_MODULE = test.settings
# Standard pytest options
addopts = -p no:warnings --reuse-db
python_paths = test/example
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def get_version():
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
Expand Down
3 changes: 3 additions & 0 deletions test/example/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
django
djangorestframework
# used for testing/linting
pytest-django
pytest-cov
pytest-pythonpath
Pillow
lxml
beautifulsoup4
Expand Down
15 changes: 6 additions & 9 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
[tox]
envlist =
py{36,37,38}-{django21, django22, django30, django31, djangomaster}
py{36,39}-{django21, django31, djangomaster}

[travis]
python =
3.6: py36
3.7: py37
3.8: py38
3.9: py39

[travis:env]
DJANGO =
2.1: django21
2.2: django22
3.0: django30
3.1: django31
master: djangomaster

[testenv]
deps =
coverage
pytest-django
pytest-pythonpath
pytest-cov
flake8
pillow
djangorestframework
lxml
beautifulsoup4
cssselect
django21: Django>=2.1,<2.2
django22: Django>=2.2,<2.3
django30: Django>=3.0,<3.1
django31: Django>=3.1,<3.2
djangomaster: https://github.com/django/django/archive/master.tar.gz

Expand All @@ -40,7 +37,7 @@ commands =
pip list
flake8 comment/
python manage.py migrate
coverage run manage.py test
coverage run -m pytest
coverage report -m

setenv =
Expand Down

0 comments on commit 134ece6

Please sign in to comment.