Skip to content

Commit

Permalink
Add function to get a list of all settings in a module
Browse files Browse the repository at this point in the history
  • Loading branch information
timsavage committed Oct 10, 2024
1 parent 7d51056 commit 190c8bc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions HISTORY
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Features
- Fully implement split logging configuration. Settings now contains `LOG_HANDLERS`
and `LOG_LOGGERS` that are merged into the logging configuration before it is
applied.
- Add a function `settings_in_module` in the testing package to fetch a list of
setting names from a settings module.


4.15.1
Expand Down
26 changes: 24 additions & 2 deletions src/pyapp/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
"""Helper methods for testing.
Includes a plugin for Pytest.
"""
Includes a plugin for Pytest.
"""

from types import ModuleType

from ..conf.loaders import settings_iterator


def settings_in_module(
*modules: ModuleType,
exclude: list[str] = ("INCLUDE",),
):
"""Generate a list of settings defined in a module (or modules).
Used for ensuring that a second settings module only contains specified settings.
"""
settings = set()

for mod in modules:
settings.update(
name for name, _ in settings_iterator(mod) if name not in exclude
)

return settings
17 changes: 17 additions & 0 deletions tests/unit/testing/test_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pyapp import testing


def test_settings_in_module():
from pyapp.conf import base_settings

actual = testing.settings_in_module(base_settings)

assert actual == {
"DEBUG",
"LOG_LOGGERS",
"LOG_HANDLERS",
"LOGGING",
"CHECK_LOCATIONS",
"FEATURE_FLAGS",
"FEATURE_FLAG_PREFIX",
}

0 comments on commit 190c8bc

Please sign in to comment.