|
| 1 | +import os |
| 2 | +import importlib |
| 3 | + |
| 4 | +from django.core.exceptions import ImproperlyConfigured |
| 5 | +from django.test import TestCase |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +from configurations import environ_prefix |
| 9 | +from tests.settings import prefix_decorator |
| 10 | + |
| 11 | + |
| 12 | +class EnvironPrefixDecoratorTests(TestCase): |
| 13 | + @patch.dict(os.environ, clear=True, |
| 14 | + DJANGO_CONFIGURATION="PrefixDecoratorConf1", |
| 15 | + DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", |
| 16 | + ACME_FOO="bar") |
| 17 | + def test_prefix_decorator_with_value(self): |
| 18 | + importlib.reload(prefix_decorator) |
| 19 | + self.assertEqual(prefix_decorator.FOO, "bar") |
| 20 | + |
| 21 | + @patch.dict(os.environ, clear=True, |
| 22 | + DJANGO_CONFIGURATION="PrefixDecoratorConf2", |
| 23 | + DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", |
| 24 | + ACME_FOO="True") |
| 25 | + def test_prefix_decorator_for_value_subclasses(self): |
| 26 | + importlib.reload(prefix_decorator) |
| 27 | + self.assertIs(prefix_decorator.FOO, True) |
| 28 | + |
| 29 | + @patch.dict(os.environ, clear=True, |
| 30 | + DJANGO_CONFIGURATION="PrefixDecoratorConf3", |
| 31 | + DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", |
| 32 | + ZEUS_FOO="bar") |
| 33 | + def test_value_prefix_takes_precedence(self): |
| 34 | + importlib.reload(prefix_decorator) |
| 35 | + self.assertEqual(prefix_decorator.FOO, "bar") |
| 36 | + |
| 37 | + @patch.dict(os.environ, clear=True, |
| 38 | + DJANGO_CONFIGURATION="PrefixDecoratorConf4", |
| 39 | + DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", |
| 40 | + FOO="bar") |
| 41 | + def test_prefix_decorator_empty_string_value(self): |
| 42 | + importlib.reload(prefix_decorator) |
| 43 | + self.assertEqual(prefix_decorator.FOO, "bar") |
| 44 | + |
| 45 | + @patch.dict(os.environ, clear=True, |
| 46 | + DJANGO_CONFIGURATION="PrefixDecoratorConf5", |
| 47 | + DJANGO_SETTINGS_MODULE="tests.settings.prefix_decorator", |
| 48 | + FOO="bar") |
| 49 | + def test_prefix_decorator_none_value(self): |
| 50 | + importlib.reload(prefix_decorator) |
| 51 | + self.assertEqual(prefix_decorator.FOO, "bar") |
| 52 | + |
| 53 | + def test_prefix_value_must_be_none_or_str(self): |
| 54 | + class Conf: |
| 55 | + pass |
| 56 | + |
| 57 | + self.assertRaises(ImproperlyConfigured, lambda: environ_prefix(1)(Conf)) |
0 commit comments