-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add environ_prefix class decorator (#240)
- allows to configure environ_prefix on a per-class basis
- Loading branch information
Showing
7 changed files
with
143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from configurations import Configuration, environ_prefix, values | ||
|
||
|
||
@environ_prefix("ACME") | ||
class PrefixDecoratorConf1(Configuration): | ||
FOO = values.Value() | ||
|
||
|
||
@environ_prefix("ACME") | ||
class PrefixDecoratorConf2(Configuration): | ||
FOO = values.BooleanValue(False) | ||
|
||
|
||
@environ_prefix("ACME") | ||
class PrefixDecoratorConf3(Configuration): | ||
FOO = values.Value(environ_prefix="ZEUS") | ||
|
||
|
||
@environ_prefix("") | ||
class PrefixDecoratorConf4(Configuration): | ||
FOO = values.Value() | ||
|
||
|
||
@environ_prefix(None) | ||
class PrefixDecoratorConf5(Configuration): | ||
FOO = values.Value() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os | ||
import importlib | ||
|
||
from django.core.exceptions import ImproperlyConfigured | ||
from django.test import TestCase | ||
from unittest.mock import patch | ||
|
||
from configurations import environ_prefix | ||
from tests.settings import prefix_decorator | ||
|
||
|
||
class EnvironPrefixDecoratorTests(TestCase): | ||
@patch.dict(os.environ, clear=True, | ||
DJANGO_CONFIGURATION="PrefixDecoratorConf1", | ||
DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", | ||
ACME_FOO="bar") | ||
def test_prefix_decorator_with_value(self): | ||
importlib.reload(prefix_decorator) | ||
self.assertEqual(prefix_decorator.FOO, "bar") | ||
|
||
@patch.dict(os.environ, clear=True, | ||
DJANGO_CONFIGURATION="PrefixDecoratorConf2", | ||
DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", | ||
ACME_FOO="True") | ||
def test_prefix_decorator_for_value_subclasses(self): | ||
importlib.reload(prefix_decorator) | ||
self.assertIs(prefix_decorator.FOO, True) | ||
|
||
@patch.dict(os.environ, clear=True, | ||
DJANGO_CONFIGURATION="PrefixDecoratorConf3", | ||
DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", | ||
ZEUS_FOO="bar") | ||
def test_value_prefix_takes_precedence(self): | ||
importlib.reload(prefix_decorator) | ||
self.assertEqual(prefix_decorator.FOO, "bar") | ||
|
||
@patch.dict(os.environ, clear=True, | ||
DJANGO_CONFIGURATION="PrefixDecoratorConf4", | ||
DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", | ||
FOO="bar") | ||
def test_prefix_decorator_empty_string_value(self): | ||
importlib.reload(prefix_decorator) | ||
self.assertEqual(prefix_decorator.FOO, "bar") | ||
|
||
@patch.dict(os.environ, clear=True, | ||
DJANGO_CONFIGURATION="PrefixDecoratorConf5", | ||
DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", | ||
FOO="bar") | ||
def test_prefix_decorator_none_value(self): | ||
importlib.reload(prefix_decorator) | ||
self.assertEqual(prefix_decorator.FOO, "bar") | ||
|
||
def test_prefix_value_must_be_none_or_str(self): | ||
class Conf: | ||
pass | ||
|
||
self.assertRaises(ImproperlyConfigured, lambda: environ_prefix(1)(Conf)) |