From c5beb0ea83b990dafc6506991c23f9d61a52fbe1 Mon Sep 17 00:00:00 2001 From: Niicck Date: Mon, 23 Oct 2023 13:36:41 -0700 Subject: [PATCH] add more asset_loader tests --- django_vite/core/asset_loader.py | 2 +- tests/conftest.py | 25 +++++++++++++-- tests/settings.py | 1 - tests/tests/test_asset_loader.py | 53 +++++++++++++++++++++++--------- tests/tests/test_multi_app.py | 0 5 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 tests/tests/test_multi_app.py diff --git a/django_vite/core/asset_loader.py b/django_vite/core/asset_loader.py index b36d3de..397b82b 100644 --- a/django_vite/core/asset_loader.py +++ b/django_vite/core/asset_loader.py @@ -705,7 +705,7 @@ def _apply_legacy_django_vite_settings(cls): if cls.DJANGO_VITE in applied_settings: warnings.warn( f"You're mixing the new {cls.DJANGO_VITE} setting with these " - f"legacy settings: [{applied_legacy_settings.join(',')}]. Those legacy " + f"legacy settings: [{', '.join(applied_legacy_settings)}]. Those legacy " f"settings will be ignored since you have a {cls.DJANGO_VITE}" " setting configured. Please remove those legacy django-vite settings.", DeprecationWarning, diff --git a/tests/conftest.py b/tests/conftest.py index df10a20..11b10c9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,11 @@ -from typing import Dict, Any +from typing import Dict, Any, List import pytest from django_vite.core.asset_loader import DjangoViteAssetLoader +__PYTEST_EMPTY__ = "__PYTEST_EMPTY__" +__PYTEST_DELETE__ = "__PYTEST_DELETE__" + def reload_django_vite(): DjangoViteAssetLoader._instance = None @@ -16,13 +19,16 @@ def patch_settings(settings): 2. Recreate DjangoViteAssetLoader._instance with new settings applied. 3. Restore the original settings once the test is over. """ - __PYTEST_EMPTY__ = "__PYTEST_EMPTY__" + original_settings_cache = {} def _patch_settings(new_settings: Dict[str, Any]): for key, value in new_settings.items(): original_settings_cache[key] = getattr(settings, key, __PYTEST_EMPTY__) - setattr(settings, key, value) + if value == __PYTEST_DELETE__: + delattr(settings, key) + else: + setattr(settings, key, value) reload_django_vite() yield _patch_settings @@ -35,6 +41,19 @@ def _patch_settings(new_settings: Dict[str, Any]): reload_django_vite() +@pytest.fixture() +def delete_settings(patch_settings): + """ + Unset settings that are part of the default test settings.py + """ + + def _delete_settings(*settings_to_delete: str): + new_settings = {key: __PYTEST_DELETE__ for key in settings_to_delete} + patch_settings(new_settings) + + return _delete_settings + + @pytest.fixture() def dev_mode_off(patch_settings): patch_settings({"DJANGO_VITE_DEV_MODE": False}) diff --git a/tests/settings.py b/tests/settings.py index 541df85..4165305 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -28,4 +28,3 @@ STATIC_ROOT = BASE_DIR / "data" / "staticfiles" DJANGO_VITE_DEV_MODE = True -DJANGO_VITE_ASSETS_PATH = "/" diff --git a/tests/tests/test_asset_loader.py b/tests/tests/test_asset_loader.py index 1b5085d..1fa9ccc 100644 --- a/tests/tests/test_asset_loader.py +++ b/tests/tests/test_asset_loader.py @@ -1,9 +1,7 @@ import pytest -from contextlib import suppress -from django_vite.core.exceptions import DjangoViteManifestError +from django_vite.core.asset_loader import DjangoViteConfig from django_vite.templatetags.django_vite import DjangoViteAssetLoader -from django_vite.apps import check_loader_instance def test_django_vite_asset_loader_cannot_be_instantiated(): @@ -17,20 +15,47 @@ def test_check_loader_instance_happy(patch_settings): "DJANGO_VITE_DEV_MODE": False, } ) - DjangoViteAssetLoader._instance = None - warnings = check_loader_instance() + warnings = DjangoViteAssetLoader.instance().check() assert len(warnings) == 0 def test_check_loader_instance_warnings(patch_settings): - with suppress(DjangoViteManifestError): - patch_settings( - { - "DJANGO_VITE_DEV_MODE": False, - "DJANGO_VITE_MANIFEST_PATH": "fake.json", - } - ) - DjangoViteAssetLoader._instance = None - warnings = check_loader_instance() + patch_settings( + { + "DJANGO_VITE_DEV_MODE": False, + "DJANGO_VITE_MANIFEST_PATH": "fake.json", + } + ) + warnings = DjangoViteAssetLoader.instance().check() assert len(warnings) == 1 assert "Make sure you have generated a manifest file" in warnings[0].hint + + +def test_apply_fallback(delete_settings): + """ + Test that a fallback "default" app is made even when there are no DJANGO_VITE + settings defined. + """ + delete_settings("DJANGO_VITE_DEV_MODE") + default_app = DjangoViteAssetLoader.instance()._apps["default"] + assert default_app + assert default_app._config == DjangoViteConfig() + + +def test_combined_settings(patch_settings): + patch_settings( + { + "DJANGO_VITE": {"default": {}}, + "DJANGO_VITE_DEV_MODE": True, + "DJANGO_VITE_ASSETS_PATH": "/", + } + ) + DjangoViteAssetLoader._instance = None + + with pytest.warns(DeprecationWarning) as record: + DjangoViteAssetLoader.instance() + + assert ( + "You're mixing the new DJANGO_VITE setting with these legacy settings: " + "[DJANGO_VITE_DEV_MODE, DJANGO_VITE_ASSETS_PATH]" + ) in str(record[0].message) diff --git a/tests/tests/test_multi_app.py b/tests/tests/test_multi_app.py new file mode 100644 index 0000000..e69de29