Просмотр исходного кода

Extend test setup with tests in CI

Co-authored-by: Lupyana <lupyanambembati@gmail.com>
Thibaud Colas 3 недель назад
Родитель
Сommit
4f26e6219b

+ 16 - 1
.github/workflows/ci.yml

@@ -1,4 +1,4 @@
-name: Bakerydemo CI
+name: CI
 
 on:
   push:
@@ -29,3 +29,18 @@ jobs:
           node-version-file: '.nvmrc'
       - run: npm ci
       - run: make lint-client
+  test-server:
+    runs-on: ubuntu-latest
+    env:
+      DJANGO_SETTINGS_MODULE: bakerydemo.settings.test
+    steps:
+      - uses: actions/checkout@v6
+      - uses: actions/setup-python@v4
+        with:
+          python-version: '3.12'
+          cache: 'pip'
+          cache-dependency-path: 'requirements/*.txt'
+      - run: pip install -r requirements/development.txt
+      - run: ./manage.py check
+      - run: ./manage.py makemigrations --check --noinput
+      - run: ./manage.py test

+ 0 - 0
bakerydemo/base/tests/__init__.py


+ 2 - 34
bakerydemo/base/tests/test_home_page.py

@@ -1,5 +1,4 @@
 from django.contrib.auth.models import User
-from django.test import override_settings
 from wagtail.models import Page, Site
 from wagtail.test.utils import WagtailPageTestCase
 from wagtail.test.utils.form_data import nested_form_data, streamfield
@@ -7,37 +6,23 @@ from wagtail.test.utils.form_data import nested_form_data, streamfield
 from bakerydemo.base.models import HomePage
 
 
-@override_settings(
-    STORAGES={
-        "staticfiles": {
-            "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
-        }
-    }
-)
 class HomePageRenderTest(WagtailPageTestCase):
-    """
-    Tests that the HomePage
-    """
-
     @classmethod
     def setUpTestData(cls):
         """
         Create the page tree and site once for all tests in this class.
 
         This runs ONCE before all tests, not before each test.
-        Both tests will share this setup, making tests faster.
+        All tests will share this setup, making tests faster.
         """
-        # Get root page
         cls.root = Page.get_first_root_node()
 
-        # Create site
         cls.site = Site.objects.create(
             hostname="testserver",
             root_page=cls.root,
             is_default_site=True,
         )
 
-        # Create and publish a HomePage for the render test
         cls.home = HomePage(
             title="Home",
             slug="test-home",
@@ -49,40 +34,23 @@ class HomePageRenderTest(WagtailPageTestCase):
 
     def setUp(self):
         super().setUp()
-        # Create and log in a superuser for each test
+
         self.user = User.objects.create_superuser(
             username="testadmin", email="test@example.com", password="password"
         )
         self.client.login(username="testadmin", password="password")
 
     def test_homepage_renders(self):
-        """
-        Test that a published HomePage created renders correctly
-        """
-
-        # Make request to the page created in setUpTestData()
         response = self.client.get(self.home.url)
 
-        # Verify HTTP response
         self.assertEqual(response.status_code, 200)
 
-        # Verify correct template
         self.assertTemplateUsed(response, "base/home_page.html")
 
-        # Verify content appears on the page
         self.assertContains(response, "Welcome to our site")
         self.assertContains(response, "Get Started")
 
-        # Alternative: Use Wagtail's built-in assertion
-        self.assertPageIsRoutable(self.home)
-
     def test_can_create_another_homepage(self):
-        """
-        Test that a another Page  can be created under the root page.
-        This test uses assertCanCreate()
-        """
-
-        # Create and publish the HomePage
         home_page_data = nested_form_data(
             {
                 "title": "Home",

+ 41 - 0
bakerydemo/settings/test.py

@@ -0,0 +1,41 @@
+from .base import *  # noqa
+from .base import STORAGES
+# #############
+# General
+
+ALLOWED_HOSTS = ["*"]
+
+# Don't redirect to HTTPS in tests or send the HSTS header
+SECURE_SSL_REDIRECT = False
+SECURE_HSTS_SECONDS = 0
+
+STORAGES["staticfiles"]["BACKEND"] = (
+    "django.contrib.staticfiles.storage.StaticFilesStorage"
+)
+
+# Use local memory caching instead of redis when testing locally
+# to prevent caching shenanigans
+CACHES = {
+    "default": {
+        "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
+    }
+}
+
+# Wagtail
+WAGTAILADMIN_BASE_URL = "http://localhost:8000"
+
+# Task queue configuration to ensure tasks run immediately in the test environment
+# https://docs.wagtail.org/en/stable/releases/6.4.html#background-tasks-run-at-end-of-current-transaction
+TASKS = {
+    "default": {
+        "BACKEND": "django_tasks.backends.immediate.ImmediateBackend",
+        "ENQUEUE_ON_COMMIT": False,
+    }
+}
+
+# #############
+# Performance
+
+# By default, Django uses a computationally difficult algorithm for passwords hashing.
+# We don't need such a strong algorithm in tests, so use MD5
+PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]