Skip to content

Commit 74eb5f2

Browse files
committed
Almost 100% test coverage
1 parent ee8e688 commit 74eb5f2

4 files changed

Lines changed: 31 additions & 69 deletions

File tree

.github/workflows/tests.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ on:
1010

1111
jobs:
1212
tests:
13-
name:
14-
Python ${{ matrix.python-version }}, USE_CUSTOM_SITE = ${{ matrix.use-custom-site
15-
}}
13+
name: Python ${{ matrix.python-version }}
1614
runs-on: ubuntu-latest
1715
strategy:
1816
fail-fast: false
@@ -21,22 +19,18 @@ jobs:
2119
- "3.9"
2220
- "3.10"
2321
- "3.11"
24-
use-custom-site:
25-
- false
26-
- true
2722

2823
steps:
29-
- uses: actions/checkout@v3
24+
- uses: actions/checkout@v4
3025
- name: Set up Python ${{ matrix.python-version }}
3126
uses: actions/setup-python@v4
3227
with:
3328
python-version: ${{ matrix.python-version }}
29+
cache: pip
3430
- name: Install dependencies
3531
run: |
3632
python -m pip install --upgrade pip tox
37-
- name:
38-
Run tox targets for ${{ matrix.python-version }} and custom site model
39-
= ${{ matrix.use-custom-site }}
33+
- name: Run tox targets for ${{ matrix.python-version }}
4034
run: |
4135
ENV_PREFIX=$(tr -C -d "0-9" <<< "${{ matrix.python-version }}")
42-
USE_CUSTOM_SITE='${{ matrix.use-custom-site}}' TOXENV=$(tox --listenvs | grep "^py$ENV_PREFIX" | tr '\n' ',') python -m tox
36+
TOXENV=$(tox --listenvs | grep "^py$ENV_PREFIX" | tr '\n' ',') python -m tox

tests/testapp/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from content_editor.models import Region, create_plugin_base
2+
from django.conf import settings
23
from django.db import models
34
from django.utils.translation import gettext_lazy as _
45
from feincms3 import plugins
@@ -71,7 +72,9 @@ class Article(models.Model):
7172
max_length=20,
7273
choices=(("publications", "publications"), ("blog", "blog")),
7374
)
74-
site = models.ForeignKey("feincms3_sites.Site", on_delete=models.CASCADE)
75+
site = models.ForeignKey(
76+
settings.FEINCMS3_SITES_SITE_MODEL, on_delete=models.CASCADE
77+
)
7578

7679
class Meta:
7780
ordering = ["-pk"]

tests/testapp/settings.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,3 @@
6161
*MIDDLEWARE_BASE,
6262
"testapp.middleware.page_if_404_middleware",
6363
]
64-
65-
USE_CUSTOM_SITE = bool(os.environ.get("USE_CUSTOM_SITE", False))
66-
if USE_CUSTOM_SITE:
67-
FEINCMS3_SITES_SITE_MODEL = "testapp.CustomSite"

tests/testapp/test_feincms3.py

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.contrib.auth.models import User
44
from django.core.exceptions import ImproperlyConfigured, ValidationError
55
from django.test import Client, TestCase
6-
from django.test.utils import override_settings
6+
from django.test.utils import isolate_apps, override_settings
77
from django.urls import set_urlconf
88
from django.utils.translation import deactivate_all, override
99
from feincms3.applications import NoReverseMatch, _del_apps_urlconf_cache, apps_urlconf
@@ -15,7 +15,7 @@
1515
set_sites,
1616
site_for_host,
1717
)
18-
from feincms3_sites.models import Site, validate_language_codes
18+
from feincms3_sites.models import AbstractPage, Site, validate_language_codes
1919
from feincms3_sites.utils import get_site_model, import_callable
2020
from testapp.models import Article, CustomSite, Page
2121

@@ -722,26 +722,7 @@ def test_default_language_list_filter(self):
722722
)
723723

724724

725-
class SiteModelDeclaredTest(TestCase):
726-
def setUp(self):
727-
self.user = User.objects.create_superuser("admin", "admin@test.ch", "blabla")
728-
deactivate_all()
729-
730-
if settings.USE_CUSTOM_SITE:
731-
self.test_site = CustomSite.objects.create(
732-
host="testserver", is_default=True, title="Test Site"
733-
)
734-
else:
735-
self.test_site = Site.objects.create(host="testserver", is_default=True)
736-
737-
def test_get_site_model(self):
738-
if settings.USE_CUSTOM_SITE:
739-
self.assertEqual(get_site_model(), CustomSite)
740-
else:
741-
self.assertEqual(get_site_model(), Site)
742-
743-
744-
class InvalidSiteModels(TestCase):
725+
class SiteModelTest(TestCase):
745726
@override_settings(FEINCMS3_SITES_SITE_MODEL="bla")
746727
def test_invalid_site_model(self):
747728
with self.assertRaisesRegex(ImproperlyConfigured, "must be of the form"):
@@ -754,42 +735,30 @@ def test_uninstalled_site_model(self):
754735
):
755736
get_site_model()
756737

738+
@override_settings(FEINCMS3_SITES_SITE_MODEL="testapp.CustomSite")
739+
def test_get_site_model(self):
740+
self.assertEqual(get_site_model(), CustomSite)
741+
# self.assertEqual(CustomSite().get_host(), "return value")
742+
743+
@override_settings(FEINCMS3_SITES_SITE_MODEL="missing.Model")
744+
def test_swapped_out_model(self):
745+
with self.assertRaisesRegex(AttributeError, "Manager isn't available"):
746+
Site.objects.create(host="testserver", is_default=True)
757747

758-
class UtilsTest(TestCase):
759748
def test_import_callable(self):
760749
from math import ceil
761750

762751
self.assertEqual(import_callable(ceil), ceil)
763752
self.assertEqual(import_callable("math.ceil"), ceil)
764753

754+
@isolate_apps("testapp")
755+
def test_page_with_missing_unique_together(self):
756+
"""Page subclass without unique_together fails validation"""
757+
758+
class Page(AbstractPage):
759+
class Meta:
760+
unique_together = []
765761

766-
# The following test cases require a separate testapp each, since changing the
767-
# settings afterwards will result in AttributeError: Manager isn't available;
768-
# 'feincms3_sites.Site' has been swapped for 'missing.Model'
769-
#
770-
# @override_settings(
771-
# FEINCMS3_SITES_SITE_MODEL=''
772-
# )
773-
# class SiteModelUndeclaredTest(TestCase):
774-
# def setUp(self):
775-
# self.user = User.objects.create_superuser("admin", "admin@test.ch", "blabla")
776-
# deactivate_all()
777-
#
778-
# self.test_site = Site.objects.create(host="testserver", is_default=True)
779-
#
780-
# def test_get_site_model(self):
781-
# self.assertRaises(ImproperlyConfigured, get_site_model)
782-
#
783-
#
784-
# @override_settings(
785-
# FEINCMS3_SITES_SITE_MODEL='missing.Model'
786-
# )
787-
# class SiteModelUndeclaredTest(TestCase):
788-
# def setUp(self):
789-
# self.user = User.objects.create_superuser("admin", "admin@test.ch", "blabla")
790-
# deactivate_all()
791-
#
792-
# self.test_site = Site.objects.create(host="testserver", is_default=True)
793-
#
794-
# def test_get_site_model(self):
795-
# self.assertRaises(ImproperlyConfigured, get_site_model)
762+
errors = Page.check()
763+
error_ids = [error.id for error in errors]
764+
self.assertIn("feincms3_sites.E001", error_ids)

0 commit comments

Comments
 (0)