Repository: digitalfabrik/integreat-cms

Issue:
Fix tests and add them to the Jenkins pipeline
We already have a test file here: [`backend/cms/tests.py`.](https://github.com/Integreat/cms-django/blob/develop/backend/cms/tests.py) However, it is not up-to-date and sometimes an old syntax for the forms is used. We have to fix this and include the tests in our Jenkins build, so that we immediately notice it if anything breaks.
Fix tests and add them to the Jenkins pipeline
We already have a test file here: [`backend/cms/tests.py`.](https://github.com/Integreat/cms-django/blob/develop/backend/cms/tests.py) However, it is not up-to-date and sometimes an old syntax for the forms is used. We have to fix this and include the tests in our Jenkins build, so that we immediately notice it if anything breaks.


Discussion:

Resolving patch:
diff --git a/backend/backend/jenkins_settings.py b/backend/backend/jenkins_settings.py
new file mode 100644
--- /dev/null
+++ b/backend/backend/jenkins_settings.py
@@ -0,0 +1,28 @@
+# pylint: disable=wildcard-import
+# pylint: disable=unused-wildcard-import
+from django.core.exceptions import ImproperlyConfigured
+
+from .settings import *
+
+
+def get_env_value(env_variable):
+    try:
+        return os.environ[env_variable]
+    except KeyError:
+        error_msg = 'Set the {} environment variable'.format(env_variable)
+        raise ImproperlyConfigured(error_msg)
+
+
+DATABASES = {
+    'default': {
+        'ENGINE': 'django.db.backends.postgresql_psycopg2',
+        'NAME': 'djangojenkins',
+        'USER': get_env_value('CMS_DJANGO_DATABASE_USER'),
+        'PASSWORD': get_env_value('CMS_DJANGO_DATABASE_PASSWORD'),
+        'HOST': 'localhost',
+        'PORT': '3333',
+        'TEST': {
+            'NAME': 'djangojenkins_test',
+        },
+    }
+}


Test patch:
diff --git a/backend/cms/tests.py b/backend/cms/_tests.py
similarity index 100%
rename from backend/cms/tests.py
rename to backend/cms/_tests.py
diff --git a/backend/cms/tests/__init__.py b/backend/cms/tests/__init__.py
new file mode 100644
--- /dev/null
+++ b/backend/cms/tests/__init__.py
@@ -0,0 +1 @@
+from .pages import *
diff --git a/backend/cms/tests/pages/__init__.py b/backend/cms/tests/pages/__init__.py
new file mode 100644
--- /dev/null
+++ b/backend/cms/tests/pages/__init__.py
@@ -0,0 +1 @@
+from .models import *
diff --git a/backend/cms/tests/pages/models.py b/backend/cms/tests/pages/models.py
new file mode 100644
--- /dev/null
+++ b/backend/cms/tests/pages/models.py
@@ -0,0 +1,43 @@
+from django.test import TestCase
+
+from cms.models import Page, PageTranslation, Region
+
+
+class PageTest(TestCase):
+    def setUp(self):
+        # Setup run before every test method.
+        self.region = Region.objects.create(
+            aliases=[],
+            push_notification_channels=[],
+            slug='testregion'
+        )
+        self.page1 = Page.objects.create(region=self.region)
+        self.page2 = Page.objects.create(parent=self.page1, region=self.region)
+        self.page3 = Page.objects.create(parent=self.page2, region=self.region)
+
+    def test_depth_no_parent(self):
+        # Depth is correctly determined for page on first level.
+        self.assertTrue(self.page1.depth == 0)
+
+    def test_depth_third_level(self):
+        # Depth is correctly determined for page on third level.
+        self.assertTrue(self.page3.depth == 2)
+
+    def test_get_archived(self):
+        self.page1.archived = True
+        self.page1.save()
+        self.page2.archived = True
+        self.page2.save()
+        archived = Page.get_archived(self.region.slug)
+        self.assertTrue(len(archived) == 2)
+
+
+class PageTranslationTest(TestCase):
+    def setUp(self):
+        # Setup run before every test method.
+        self.region = Region.objects.create(
+            aliases=[],
+            push_notification_channels=[],
+            slug='testregion'
+        )
+        self.pageTranslation = PageTranslation.objects.create()
diff --git a/dev-tools/test.sh b/dev-tools/test.sh
new file mode 100755
--- /dev/null
+++ b/dev-tools/test.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+# TODO: make it possible to use without docker database
+
+source .venv/bin/activate
+
+export DJANGO_SETTINGS_MODULE=backend.docker_settings
+
+integreat-cms test cms
\ No newline at end of file