From c6bfff80791a4eeed5d2290e2e21599f6e1943fa Mon Sep 17 00:00:00 2001 From: Wolfgang Fehr <24782511+wfehr@users.noreply.github.com> Date: Mon, 16 Jan 2023 12:06:39 +0100 Subject: [PATCH] implemented tests --- .coveragerc | 18 ++++ Dockerfile.tests | 2 + djangocms_time_wizard/cms_plugins.py | 2 +- tests/__init__.py | 0 tests/requirements.txt | 4 + tests/settings.py | 26 +++++ tests/test_models.py | 139 +++++++++++++++++++++++++++ tox.ini | 22 +++++ 8 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 .coveragerc create mode 100644 tests/__init__.py create mode 100644 tests/requirements.txt create mode 100644 tests/settings.py create mode 100644 tests/test_models.py diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..ea1e17a --- /dev/null +++ b/.coveragerc @@ -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 diff --git a/Dockerfile.tests b/Dockerfile.tests index 37b30de..b2189ff 100644 --- a/Dockerfile.tests +++ b/Dockerfile.tests @@ -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 diff --git a/djangocms_time_wizard/cms_plugins.py b/djangocms_time_wizard/cms_plugins.py index 0e35f3a..623cf36 100644 --- a/djangocms_time_wizard/cms_plugins.py +++ b/djangocms_time_wizard/cms_plugins.py @@ -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 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/requirements.txt b/tests/requirements.txt new file mode 100644 index 0000000..19129c8 --- /dev/null +++ b/tests/requirements.txt @@ -0,0 +1,4 @@ +coverage +django-app-helper +djangocms-text-ckeditor +tox diff --git a/tests/settings.py b/tests/settings.py new file mode 100644 index 0000000..9243e4c --- /dev/null +++ b/tests/settings.py @@ -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() diff --git a/tests/test_models.py b/tests/test_models.py new file mode 100644 index 0000000..56de1fc --- /dev/null +++ b/tests/test_models.py @@ -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") diff --git a/tox.ini b/tox.ini index 246aa9b..4d2b1d9 100644 --- a/tox.ini +++ b/tox.ini @@ -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 @@ -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