-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathtest_empty_values_autoconfig.py
More file actions
89 lines (67 loc) · 3.02 KB
/
test_empty_values_autoconfig.py
File metadata and controls
89 lines (67 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# coding: utf-8
import os
import sys
from mock import patch, MagicMock
import pytest
from decouple import AutoConfig, UndefinedValueError
# Useful for very coarse version differentiation.
PY3 = sys.version_info[0] == 3
if PY3:
from io import StringIO
else:
from io import BytesIO as StringIO
ENVFILE = '''
# Empty values
DB_PORT=
SECRET=
DEBUG=
LIST_VALUES=
# Non-empty values for comparison
DB_HOST=localhost
SECRET_KEY=abc123
'''
def test_autoconfig_empty_value_with_default_int():
"""Test that an empty DB_PORT with default and int cast works correctly with AutoConfig."""
config = AutoConfig()
# Mock the _find_file method to return a fake path
fake_path = os.path.join('fake', 'path', '.env')
config._find_file = MagicMock(return_value=fake_path)
# Mock open to return our test env content
with patch('decouple.open', return_value=StringIO(ENVFILE), create=True):
# DB_PORT= (empty) should use the default value 5432
assert 5432 == config('DB_PORT', default=5432, cast=int)
def test_autoconfig_empty_value_with_default_none():
"""Test that an empty SECRET with default=None works correctly with AutoConfig."""
config = AutoConfig()
# Mock the _find_file method to return a fake path
fake_path = os.path.join('fake', 'path', '.env')
config._find_file = MagicMock(return_value=fake_path)
# Mock open to return our test env content
with patch('decouple.open', return_value=StringIO(ENVFILE), create=True):
# SECRET= (empty) should use the default value None
assert None is config('SECRET', default=None)
def test_autoconfig_empty_value_with_default_bool():
"""Test that an empty DEBUG with default and bool cast works correctly with AutoConfig."""
config = AutoConfig()
# Mock the _find_file method to return a fake path
fake_path = os.path.join('fake', 'path', '.env')
config._find_file = MagicMock(return_value=fake_path)
# Mock open to return our test env content
with patch('decouple.open', return_value=StringIO(ENVFILE), create=True):
# DEBUG= (empty) should use the default value True
assert True is config('DEBUG', default=True, cast=bool)
# Empty value without default should be False when cast to bool
assert False is config('DEBUG', cast=bool)
def test_autoconfig_empty_value_with_csv_cast():
"""Test that an empty LIST_VALUES with Csv cast works correctly with AutoConfig."""
from decouple import Csv
config = AutoConfig()
# Mock the _find_file method to return a fake path
fake_path = os.path.join('fake', 'path', '.env')
config._find_file = MagicMock(return_value=fake_path)
# Mock open to return our test env content
with patch('decouple.open', return_value=StringIO(ENVFILE), create=True):
# LIST_VALUES= (empty) should return an empty list with Csv cast
assert [] == config('LIST_VALUES', cast=Csv())
# With default values
assert ['default'] == config('LIST_VALUES', default='default', cast=Csv())