Skip to content

Commit

Permalink
implemented tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wfehr committed Jan 16, 2023
1 parent 961e3b1 commit c6bfff8
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[run]
branch = True
source = djangocms_time_wizard
omit =
djangocms_time_wizard/migrations/*
djangocms_time_wizard/workarounds.py

[report]
exclude_lines =
pragma: no cover
def __repr__
if self.debug:
if settings.DEBUG
raise AssertionError
raise NotImplementedError
if 0:
if __name__ == .__main__.:
ignore_errors = True
2 changes: 2 additions & 0 deletions Dockerfile.tests
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ RUN pip install tox
WORKDIR /home/app

COPY djangocms_time_wizard/ /home/app/djangocms_time_wizard/
COPY tests/ /home/app/tests/
COPY setup.py /home/app/setup.py
COPY tox.ini /home/app/tox.ini
COPY README.rst /home/app/README.rst
COPY .coveragerc /home/app/.coveragerc
2 changes: 1 addition & 1 deletion djangocms_time_wizard/cms_plugins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import gettext_lazy as _
from polymorphic.admin import PolymorphicInlineSupportMixin
from time_wizard.admin import PeriodModelInline

Expand Down
Empty file added tests/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage
django-app-helper
djangocms-text-ckeditor
tox
26 changes: 26 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
HELPER_SETTINGS = {
'DATABASES': {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
},
},
'INSTALLED_APPS': (
'polymorphic',
'time_wizard',
'djangocms_time_wizard',
'djangocms_text_ckeditor',
),
'SECRET_KEY': 'foobar1337',
'TIME_WIZARD_COUNTRIES': ['US'],
'TIME_WIZARD_COUNTRY_PROVINCES': {'US': ['AL']},
}


def run():
from app_helper import runner
runner.run('djangocms_time_wizard')


if __name__ == '__main__':
run()
139 changes: 139 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
from app_helper.base_test import BaseTestCase
from cms.api import add_plugin, create_page
from django.contrib.contenttypes.models import ContentType
from django.utils.timezone import now
from time_wizard.models import AbsolutePeriodModel, TimeWizardModel


class TestTimeWizardModel(BaseTestCase):
def setUp(self):
self.page = self._create_page()
self.page.set_as_homepage()

def test_plugin(self):
time_wizard = TimeWizardModel.objects.create(
name="dummy",
)
plugin = self._add_plugin(time_wizard)
self._add_text_to_plugin(plugin, body="Hello World!")

with self.subTest("no periods -> no output"):
html = self._render_page().strip()
self.assertNotIn("Hello World!", html)

period = AbsolutePeriodModel.objects.create(
content_type=ContentType.objects.get(
app_label="time_wizard",
model="timewizardmodel",
),
object_id=time_wizard.pk,
start=now(),
)

with self.subTest("content shown - start given"):
html = self._render_page().strip()
self.assertIn("Hello World!", html)

with self.subTest("content not shown - end given"):
period.end = now()
period.save()
html = self._render_page().strip()
self.assertNotIn("Hello World!", html)

with self.subTest("content shown - no start/end given"):
period.start = None
period.end = None
period.save()
html = self._render_page().strip()
self.assertIn("Hello World!", html)

def test_inline_plugin(self):
plugin = self._add_inline_plugin()
self._add_text_to_plugin(plugin, body="Hello Inline World!")

with self.subTest("no periods -> no output"):
html = self._render_page().strip()
self.assertNotIn("Hello Inline World!", html)

period = AbsolutePeriodModel.objects.create(
content_type=ContentType.objects.get(
app_label="djangocms_time_wizard",
model="timewizardinlinemodel",
),
object_id=plugin.pk,
start=now(),
)

with self.subTest("content shown - start given"):
html = self._render_page().strip()
self.assertIn("Hello Inline World!", html)

with self.subTest("content not shown - end given"):
period.end = now()
period.save()
html = self._render_page().strip()
self.assertNotIn("Hello Inline World!", html)

with self.subTest("content shown - no start/end given"):
period.start = None
period.end = None
period.save()
html = self._render_page().strip()
self.assertIn("Hello Inline World!", html)

def _add_plugin(self, time_wizard, *args, **kwargs):
return self._add_plugin_to_page(
"TimeWizardPlugin",
*args,
time_wizard=time_wizard,
**kwargs,
)

def _add_inline_plugin(self, *args, **kwargs):
return self._add_plugin_to_page(
"TimeWizardInlinePlugin",
*args,
**kwargs,
)

def _create_page(self, **kwargs):
if "template" not in kwargs:
kwargs["template"] = "page.html"
if "title" not in kwargs:
kwargs["title"] = "Home"
if "language" not in kwargs:
kwargs["language"] = "en"
return create_page(**kwargs)

def _add_text_to_plugin(
self, parent, *args, page=None, **kwargs
):
if page is None:
page = self.page
return add_plugin(
page.placeholders.get(slot="content"),
"TextPlugin",
"en",
"last-child",
parent,
*args,
**kwargs,
)

def _add_plugin_to_page(self, plugin_publisher, *args, page=None, **kwargs):
if page is None:
page = self.page
return add_plugin(
page.placeholders.get(slot="content"),
plugin_publisher,
"en",
*args,
**kwargs,
)

def _render_page(self, page=None):
if page is None:
page = self.page
page.publish("en")
response = self.client.get(page.get_absolute_url())
return response.content.decode("utf-8")
22 changes: 22 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
envlist =
flake8
isort
py{37,38,39}-django22-cms{37,38,39,310,311,master}
py{37,38,39}-django32-cms{38,39,310,311,master}
py310-django{32,40,41}-cms{310,311,master}

skip_missing_interpreters=True

[flake8]
exlude = migrations
Expand All @@ -16,6 +21,23 @@ line_length = 79
multi_line_output = 5
skip = migrations

[testenv]
deps =
cms37: django-cms~=3.7
cms38: django-cms~=3.8
cms39: django-cms~=3.9
cms310: django-cms~=3.10
cms311: django-cms~=3.11
cmsmaster: https://github.com/django-cms/django-cms/archive/master.zip
django32: django~=3.2
django40: django~=4.0
django41: django~=4.1
-r{toxinidir}/tests/requirements.txt
commands =
{env:COMMAND:coverage} erase
{env:COMMAND:coverage} run tests/settings.py test --extra-settings=tests/settings.py --cms
{env:COMMAND:coverage} report

[testenv:flake8]
deps = flake8
commands = flake8 djangocms_time_wizard
Expand Down

0 comments on commit c6bfff8

Please sign in to comment.