| 79 | |
| 80 | |
| 81 | class EnvironmentVariableTest(unittest.TestCase): |
| 82 | """ |
| 83 | Ensures proper settings file is used. |
| 84 | """ |
| 85 | |
| 86 | def test_env_var_used(self): |
| 87 | """ |
| 88 | If the environment variable is set, do |
| 89 | not ignore it. However, the kwarg |
| 90 | original_settings_path takes precedence. |
| 91 | |
| 92 | This tests both plus the default (neither set). |
| 93 | """ |
| 94 | import os |
| 95 | from django.core.management import setup_environ |
| 96 | |
| 97 | #whatever was already there |
| 98 | original_module = os.environ.get( |
| 99 | 'DJANGO_SETTINGS_MODULE', |
| 100 | 'the default' |
| 101 | ) |
| 102 | |
| 103 | #environment variable set by user |
| 104 | user_override = 'custom.settings' |
| 105 | |
| 106 | #optional argument to setup_environ |
| 107 | orig_path = 'original.path' |
| 108 | |
| 109 | #expect default |
| 110 | setup_environ(global_settings) |
| 111 | self.assertEquals( |
| 112 | os.environ.get('DJANGO_SETTINGS_MODULE'), |
| 113 | original_module |
| 114 | ) |
| 115 | |
| 116 | #override with environment variable |
| 117 | os.environ['DJANGO_SETTINGS_MODULE'] = user_override |
| 118 | setup_environ(global_settings) |
| 119 | |
| 120 | self.assertEquals( |
| 121 | os.environ.get('DJANGO_SETTINGS_MODULE'), |
| 122 | user_override |
| 123 | ) |
| 124 | |
| 125 | #pass in original_settings_path (takes precedence) |
| 126 | os.environ['DJANGO_SETTINGS_MODULE'] = user_override |
| 127 | setup_environ(global_settings, original_settings_path = orig_path) |
| 128 | |
| 129 | self.assertEquals( |
| 130 | os.environ.get('DJANGO_SETTINGS_MODULE'), |
| 131 | orig_path |
| 132 | ) |
| 133 | |
| 134 | #return to factory defaults, since this changes |
| 135 | #things beyond just in this test |
| 136 | os.environ['DJANGO_SETTINGS_MODULE'] = original_module |
| 137 | |