django-components/tests/test_settings.py
Juro Oravec f100cc1836
Some checks are pending
Docs - build & deploy / docs (push) Waiting to run
Run tests / build (ubuntu-latest, 3.10) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.11) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.12) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.13) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.8) (push) Waiting to run
Run tests / test_sampleproject (3.13) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.9) (push) Waiting to run
Run tests / build (windows-latest, 3.10) (push) Waiting to run
Run tests / build (windows-latest, 3.11) (push) Waiting to run
Run tests / build (windows-latest, 3.12) (push) Waiting to run
Run tests / build (windows-latest, 3.13) (push) Waiting to run
Run tests / build (windows-latest, 3.8) (push) Waiting to run
Run tests / build (windows-latest, 3.9) (push) Waiting to run
Run tests / test_docs (3.13) (push) Waiting to run
refactor: replace isort, black and flake8 with ruff (#1346)
2025-09-10 14:06:53 +02:00

66 lines
2.2 KiB
Python

import re
from pathlib import Path
import pytest
from django.test import override_settings
from django_components.app_settings import ComponentsSettings, app_settings
from django_components.testing import djc_test
from .testutils import setup_test_config
setup_test_config(components={"autodiscover": False})
@djc_test
class TestSettings:
@djc_test(
components_settings={
"context_behavior": "isolated",
},
)
def test_valid_context_behavior(self):
assert app_settings.CONTEXT_BEHAVIOR == "isolated"
# NOTE: Since the part that we want to test here is otherwise part of the test setup
# this test places the `override_settings` and `_load_settings` (which is called by `djc_test`)
# inside the test.
def test_raises_on_invalid_context_behavior(self):
with override_settings(COMPONENTS={"context_behavior": "invalid_value"}):
with pytest.raises(
ValueError,
match=re.escape("Invalid context behavior: invalid_value. Valid options are ['django', 'isolated']"),
):
app_settings._load_settings()
@djc_test(
django_settings={
"BASE_DIR": "base_dir",
},
)
def test_works_when_base_dir_is_string(self):
assert [Path("base_dir/components")] == app_settings.DIRS
@djc_test(
django_settings={
"BASE_DIR": Path("base_dir"),
},
)
def test_works_when_base_dir_is_path(self):
assert [Path("base_dir/components")] == app_settings.DIRS
@djc_test(
components_settings={
"context_behavior": "isolated",
},
)
def test_settings_as_dict(self):
assert app_settings.CONTEXT_BEHAVIOR == "isolated"
# NOTE: Since the part that we want to test here is otherwise part of the test setup
# this test places the `override_settings` and `_load_settings` (which is called by `djc_test`)
# inside the test.
def test_settings_as_instance(self):
with override_settings(COMPONENTS=ComponentsSettings(context_behavior="isolated")):
app_settings._load_settings()
assert app_settings.CONTEXT_BEHAVIOR == "isolated"