Skip to content

Commit

Permalink
add more asset_loader tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Niicck committed Oct 23, 2023
1 parent ca2224a commit c5beb0e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion django_vite/core/asset_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 22 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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})
1 change: 0 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@
STATIC_ROOT = BASE_DIR / "data" / "staticfiles"

DJANGO_VITE_DEV_MODE = True
DJANGO_VITE_ASSETS_PATH = "/"
53 changes: 39 additions & 14 deletions tests/tests/test_asset_loader.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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)
Empty file added tests/tests/test_multi_app.py
Empty file.

0 comments on commit c5beb0e

Please sign in to comment.