feat: add decorator for writing component tests (#1008)

* feat: add decorator for writing component tests

* refactor: udpate changelog + update deps pins

* refactor: fix deps

* refactor: make cached_ref into generic and fix linter errors

* refactor: fix coverage testing

* refactor: use global var instead of env var for is_testing state
This commit is contained in:
Juro Oravec 2025-03-02 19:46:12 +01:00 committed by GitHub
parent 81ac59f7fb
commit 7dfcb447c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
62 changed files with 4428 additions and 3661 deletions

View file

@ -2,9 +2,10 @@ import re
from pathlib import Path
from django.contrib.staticfiles.management.commands.collectstatic import Command
from django.test import SimpleTestCase, override_settings
from django.test import SimpleTestCase
from .django_test_setup import setup_test_config
from django_components.testing import djc_test
from .testutils import setup_test_config
setup_test_config({"autodiscover": False})
@ -76,14 +77,16 @@ COMPONENTS = {
class StaticFilesFinderTests(SimpleTestCase):
@override_settings(
**common_settings,
COMPONENTS=COMPONENTS,
STATICFILES_FINDERS=[
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
],
@djc_test(
django_settings={
**common_settings,
"STATICFILES_FINDERS": [
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
],
},
components_settings=COMPONENTS,
)
def test_python_and_html_included(self):
collected = do_collect()
@ -97,16 +100,18 @@ class StaticFilesFinderTests(SimpleTestCase):
self.assertListEqual(collected["unmodified"], [])
self.assertListEqual(collected["post_processed"], [])
@override_settings(
**common_settings,
COMPONENTS=COMPONENTS,
STATICFILES_FINDERS=[
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
],
@djc_test(
django_settings={
**common_settings,
"STATICFILES_FINDERS": [
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
],
},
components_settings=COMPONENTS,
)
def test_python_and_html_omitted(self):
collected = do_collect()
@ -120,22 +125,24 @@ class StaticFilesFinderTests(SimpleTestCase):
self.assertListEqual(collected["unmodified"], [])
self.assertListEqual(collected["post_processed"], [])
@override_settings(
**common_settings,
COMPONENTS={
@djc_test(
django_settings={
**common_settings,
"STATICFILES_FINDERS": [
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
],
},
components_settings={
**COMPONENTS,
"static_files_allowed": [
".js",
],
"static_files_forbidden": [],
},
STATICFILES_FINDERS=[
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
],
)
def test_set_static_files_allowed(self):
collected = do_collect()
@ -149,9 +156,18 @@ class StaticFilesFinderTests(SimpleTestCase):
self.assertListEqual(collected["unmodified"], [])
self.assertListEqual(collected["post_processed"], [])
@override_settings(
**common_settings,
COMPONENTS={
@djc_test(
django_settings={
**common_settings,
"STATICFILES_FINDERS": [
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
],
},
components_settings={
**COMPONENTS,
"static_files_allowed": [
re.compile(r".*"),
@ -160,13 +176,6 @@ class StaticFilesFinderTests(SimpleTestCase):
re.compile(r"\.(?:js)$"),
],
},
STATICFILES_FINDERS=[
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
],
)
def test_set_forbidden_files(self):
collected = do_collect()
@ -180,9 +189,18 @@ class StaticFilesFinderTests(SimpleTestCase):
self.assertListEqual(collected["unmodified"], [])
self.assertListEqual(collected["post_processed"], [])
@override_settings(
**common_settings,
COMPONENTS={
@djc_test(
django_settings={
**common_settings,
"STATICFILES_FINDERS": [
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
],
},
components_settings={
**COMPONENTS,
"static_files_allowed": [
".js",
@ -192,13 +210,6 @@ class StaticFilesFinderTests(SimpleTestCase):
".js",
],
},
STATICFILES_FINDERS=[
# Default finders
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
# Django components
"django_components.finders.ComponentsFileSystemFinder",
],
)
def test_set_both_allowed_and_forbidden_files(self):
collected = do_collect()