Tests for Parse From Environment

tests.parse_from.test_parse_from_environment

test_parse_from_environment.py file for the fejao_config_parser package.

This module contains unit tests for the ParseFromEnvironment class, which is responsible for parsing configuration values from environment variables based on a provided configuration schema.

TestParseFromEnvironment

Bases: TestCase

Test suite for the ParseFromEnvironment class.

Source code in tests/parse_from/test_parse_from_environment.py
 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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
class TestParseFromEnvironment(unittest.TestCase):
    """Test suite for the ParseFromEnvironment class."""

    def setUp(self):
        """Set up test fixtures and save original environment state."""
        self.original_environ = os.environ.copy()

        self.example_config = [
            {
                "name": "example_string",
                "type": "string",
                "default": "default_string_value",
                "env_name": "EXAMPLE_STRING",
            },
            {
                "name": "example_bool",
                "type": "bool",
                "default": False,
                "env_name": "EXAMPLE_BOOL",
            },
            {
                "name": "example_int",
                "type": "int",
                "default": 42,
                "env_name": "EXAMPLE_INT",
            },
            {
                "name": "example_float",
                "type": "float",
                "default": 4.2,
                "env_name": "EXAMPLE_FLOAT",
            },
            {
                "name": "example_list",
                "type": "list",
                "default": ["1", "2", "3"],
                "env_name": "EXAMPLE_LIST",
            },
            {
                "name": "example_dict",
                "type": "dict",
                "default": {"key1": "value1", "key2": "value2"},
                "env_name": "EXAMPLE_DICT",
            },
        ]

    def tearDown(self):
        """Clean up environment variables after each test."""
        os.environ.clear()
        os.environ.update(self.original_environ)

    ###
    ### PRIVATE METHODS
    ###

    def test_set_values_is_private(self):
        """Test that __set_values__ method is private."""
        os.environ["EXAMPLE_STRING"] = "value"

        parser = ParseFromEnvironment(config=self.example_config)

        with self.assertRaises(AttributeError):
            parser.__set_values()

        self.assertTrue(hasattr(parser, '_ParseFromEnvironment__set_values'))
        self.assertNotIn('__set_values', dir(parser))

    def test_check_values_is_private(self):
        """Test that __check_values__ method is private."""
        os.environ["EXAMPLE_STRING"] = "value"

        parser = ParseFromEnvironment(config=self.example_config)

        with self.assertRaises(AttributeError):
            parser.__check_values()

        self.assertTrue(hasattr(parser, '_ParseFromEnvironment__check_values'))
        self.assertNotIn('__check_values', dir(parser))

    ###
    ### TEST VALID VALUE TYPES
    ###

    def test_parse_string_from_environment(self):
        """Test that string values are parsed correctly from environment variables."""
        os.environ["EXAMPLE_STRING"] = "env_string_value"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertIn("example_string", values)
        self.assertEqual(values["example_string"], "env_string_value")
        self.assertIsInstance(values["example_string"], str)

    def test_parse_bool_true_from_environment(self):
        """Test that boolean true values are parsed correctly from environment."""
        os.environ["EXAMPLE_BOOL"] = "True"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertIn("example_bool", values)
        self.assertEqual(values["example_bool"], True)
        self.assertIsInstance(values["example_bool"], bool)

    def test_parse_bool_false_from_environment(self):
        """Test that boolean false values are parsed correctly from environment."""
        os.environ["EXAMPLE_BOOL"] = "False"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertIn("example_bool", values)
        self.assertEqual(values["example_bool"], False)
        self.assertIsInstance(values["example_bool"], bool)

    def test_parse_bool_yes_from_environment(self):
        """Test that boolean 'yes' is parsed as True."""
        os.environ["EXAMPLE_BOOL"] = "yes"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertEqual(values["example_bool"], True)

    def test_parse_bool_no_from_environment(self):
        """Test that boolean 'no' is parsed as False."""
        os.environ["EXAMPLE_BOOL"] = "no"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertEqual(values["example_bool"], False)

    def test_parse_int_from_environment(self):
        """Test that integer values are parsed correctly from environment."""
        os.environ["EXAMPLE_INT"] = "100"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertIn("example_int", values)
        self.assertEqual(values["example_int"], 100)
        self.assertIsInstance(values["example_int"], int)

    def test_parse_negative_int_from_environment(self):
        """Test that negative integer values are parsed correctly."""
        os.environ["EXAMPLE_INT"] = "-50"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertEqual(values["example_int"], -50)

    def test_parse_float_from_environment(self):
        """Test that float values are parsed correctly from environment."""
        os.environ["EXAMPLE_FLOAT"] = "10.5"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertIn("example_float", values)
        self.assertEqual(values["example_float"], 10.5)
        self.assertIsInstance(values["example_float"], float)

    def test_parse_negative_float_from_environment(self):
        """Test that negative float values are parsed correctly."""
        os.environ["EXAMPLE_FLOAT"] = "-3.14"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertEqual(values["example_float"], -3.14)

    def test_parse_list_from_environment(self):
        """Test that list values are parsed correctly from environment."""
        os.environ["EXAMPLE_LIST"] = "[1, 2, 3, 4, 5]"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertIn("example_list", values)
        self.assertEqual(values["example_list"], [1, 2, 3, 4, 5])
        self.assertIsInstance(values["example_list"], list)

    def test_parse_list_of_strings_from_environment(self):
        """Test that list of strings is parsed correctly."""
        os.environ["EXAMPLE_LIST"] = "['a', 'b', 'c']"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertEqual(values["example_list"], ['a', 'b', 'c'])

    def test_parse_dict_from_environment(self):
        """Test that dictionary values are parsed correctly from environment."""
        os.environ["EXAMPLE_DICT"] = "{'key1': 'value1', 'key2': 'value2'}"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertIn("example_dict", values)
        self.assertEqual(values["example_dict"], {'key1': 'value1', 'key2': 'value2'})
        self.assertIsInstance(values["example_dict"], dict)

    def test_missing_environment_variable(self):
        """Test that missing environment variables result in empty values dict."""
        # Don't set any environment variables

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        # Should return empty dict since no env vars are set
        self.assertEqual(values, {})

    def test_partial_environment_variables(self):
        """Test that only set environment variables are included in values."""
        os.environ["EXAMPLE_STRING"] = "only_string"
        os.environ["EXAMPLE_INT"] = "42"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertEqual(len(values), 2)
        self.assertIn("example_string", values)
        self.assertIn("example_int", values)
        self.assertNotIn("example_bool", values)
        self.assertNotIn("example_float", values)
        self.assertNotIn("example_list", values)
        self.assertNotIn("example_dict", values)

    def test_get_values_returns_dict(self):
        """Test that get_values returns a dictionary."""
        os.environ["EXAMPLE_STRING"] = "test"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertIsInstance(values, dict)

    def test_multiple_env_vars_simultaneously(self):
        """Test parsing multiple environment variables at the same time."""
        os.environ["EXAMPLE_STRING"] = "multi_test"
        os.environ["EXAMPLE_BOOL"] = "True"
        os.environ["EXAMPLE_INT"] = "99"
        os.environ["EXAMPLE_FLOAT"] = "9.9"
        os.environ["EXAMPLE_LIST"] = "[1, 2, 3]"
        os.environ["EXAMPLE_DICT"] = "{'a': 'b'}"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertEqual(len(values), 6)
        self.assertEqual(values["example_string"], "multi_test")
        self.assertEqual(values["example_bool"], True)
        self.assertEqual(values["example_int"], 99)
        self.assertEqual(values["example_float"], 9.9)
        self.assertEqual(values["example_list"], [1, 2, 3])
        self.assertEqual(values["example_dict"], {'a': 'b'})

    def test_empty_string_environment_variable(self):
        """Test that empty string environment variables are parsed correctly."""
        os.environ["EXAMPLE_STRING"] = ""

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertIn("example_string", values)
        self.assertEqual(values["example_string"], "")

    def test_env_var_with_spaces(self):
        """Test that environment variables with spaces are parsed correctly."""
        os.environ["EXAMPLE_STRING"] = "string with spaces"

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        self.assertEqual(values["example_string"], "string with spaces")

    def test_case_sensitive_env_var_names(self):
        """Test that environment variable names are case-sensitive."""
        os.environ["example_string"] = "lowercase"  # lowercase

        parser = ParseFromEnvironment(config=self.example_config)
        values = parser.get_values()

        # Should not find the variable because env names are uppercase
        self.assertNotIn("example_string", values)
        self.assertEqual(values, {})

    def test_none_env_var_set(self):
        """Test that when none environment variables are set, an empty dict is returned."""
        # Don't set any environment variables
        test_config = [
            {
                "name": "example_string",
                "type": "string",
                "default": "default_string_value"
            }
        ]

        parser = ParseFromEnvironment(config=test_config)
        values = parser.get_values()

        # The values should be an empty dict since no environment variables are set.
        self.assertIsInstance(values, dict)
        self.assertEqual(len(values), 0)
        self.assertNotIn("example_string", values)
        self.assertEqual(values, {})

    ###
    ### TEST INVALID VALUE TYPES
    ###

    def test_invalid_bool_value_raises_error(self):
        """Test that invalid boolean values raise an error."""
        os.environ["EXAMPLE_BOOL"] = "invalid_bool"

        with self.assertRaises(ValueError) as context:
            ParseFromEnvironment(config=self.example_config)
        self.assertIn("Error parsing the 'environment' variable for the type 'boolean'", str(context.exception))

    def test_invalid_int_value_raises_error(self):
        """Test that non-integer values raise an error when int is expected."""
        os.environ["EXAMPLE_INT"] = "not_an_integer"

        with self.assertRaises(ValueError) as context:
            ParseFromEnvironment(config=self.example_config)
        self.assertIn("invalid literal for int() with base 10", str(context.exception))

    def test_invalid_float_value_raises_error(self):
        """Test that non-float values raise an error when float is expected."""
        os.environ["EXAMPLE_FLOAT"] = "not_a_float"

        with self.assertRaises(ValueError) as context:
            ParseFromEnvironment(config=self.example_config)
        self.assertIn("could not convert string to float", str(context.exception))

    def test_invalid_list_value_raises_error(self):
        """Test that invalid list syntax raises an error."""
        os.environ["EXAMPLE_LIST"] = "not a valid list"

        with self.assertRaises(ValueError) as context:
            ParseFromEnvironment(config=self.example_config)
        self.assertIn("Invalid list format", str(context.exception))

    def test_invalid_dict_value_raises_error(self):
        """Test that invalid dict syntax raises an error."""
        os.environ["EXAMPLE_DICT"] = "not a valid dict"

        with self.assertRaises(SyntaxError) as context:
            ParseFromEnvironment(config=self.example_config)
        self.assertIn("invalid syntax", str(context.exception))
Methods:
setUp
setUp()

Set up test fixtures and save original environment state.

Source code in tests/parse_from/test_parse_from_environment.py
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
def setUp(self):
    """Set up test fixtures and save original environment state."""
    self.original_environ = os.environ.copy()

    self.example_config = [
        {
            "name": "example_string",
            "type": "string",
            "default": "default_string_value",
            "env_name": "EXAMPLE_STRING",
        },
        {
            "name": "example_bool",
            "type": "bool",
            "default": False,
            "env_name": "EXAMPLE_BOOL",
        },
        {
            "name": "example_int",
            "type": "int",
            "default": 42,
            "env_name": "EXAMPLE_INT",
        },
        {
            "name": "example_float",
            "type": "float",
            "default": 4.2,
            "env_name": "EXAMPLE_FLOAT",
        },
        {
            "name": "example_list",
            "type": "list",
            "default": ["1", "2", "3"],
            "env_name": "EXAMPLE_LIST",
        },
        {
            "name": "example_dict",
            "type": "dict",
            "default": {"key1": "value1", "key2": "value2"},
            "env_name": "EXAMPLE_DICT",
        },
    ]
tearDown
tearDown()

Clean up environment variables after each test.

Source code in tests/parse_from/test_parse_from_environment.py
60
61
62
63
def tearDown(self):
    """Clean up environment variables after each test."""
    os.environ.clear()
    os.environ.update(self.original_environ)
test_case_sensitive_env_var_names
test_case_sensitive_env_var_names()

Test that environment variable names are case-sensitive.

Source code in tests/parse_from/test_parse_from_environment.py
293
294
295
296
297
298
299
300
301
302
def test_case_sensitive_env_var_names(self):
    """Test that environment variable names are case-sensitive."""
    os.environ["example_string"] = "lowercase"  # lowercase

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    # Should not find the variable because env names are uppercase
    self.assertNotIn("example_string", values)
    self.assertEqual(values, {})
test_check_values_is_private
test_check_values_is_private()

Test that check_values method is private.

Source code in tests/parse_from/test_parse_from_environment.py
81
82
83
84
85
86
87
88
89
90
91
def test_check_values_is_private(self):
    """Test that __check_values__ method is private."""
    os.environ["EXAMPLE_STRING"] = "value"

    parser = ParseFromEnvironment(config=self.example_config)

    with self.assertRaises(AttributeError):
        parser.__check_values()

    self.assertTrue(hasattr(parser, '_ParseFromEnvironment__check_values'))
    self.assertNotIn('__check_values', dir(parser))
test_empty_string_environment_variable
test_empty_string_environment_variable()

Test that empty string environment variables are parsed correctly.

Source code in tests/parse_from/test_parse_from_environment.py
274
275
276
277
278
279
280
281
282
def test_empty_string_environment_variable(self):
    """Test that empty string environment variables are parsed correctly."""
    os.environ["EXAMPLE_STRING"] = ""

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertIn("example_string", values)
    self.assertEqual(values["example_string"], "")
test_env_var_with_spaces
test_env_var_with_spaces()

Test that environment variables with spaces are parsed correctly.

Source code in tests/parse_from/test_parse_from_environment.py
284
285
286
287
288
289
290
291
def test_env_var_with_spaces(self):
    """Test that environment variables with spaces are parsed correctly."""
    os.environ["EXAMPLE_STRING"] = "string with spaces"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertEqual(values["example_string"], "string with spaces")
test_get_values_returns_dict
test_get_values_returns_dict()

Test that get_values returns a dictionary.

Source code in tests/parse_from/test_parse_from_environment.py
245
246
247
248
249
250
251
252
def test_get_values_returns_dict(self):
    """Test that get_values returns a dictionary."""
    os.environ["EXAMPLE_STRING"] = "test"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertIsInstance(values, dict)
test_invalid_bool_value_raises_error
test_invalid_bool_value_raises_error()

Test that invalid boolean values raise an error.

Source code in tests/parse_from/test_parse_from_environment.py
328
329
330
331
332
333
334
def test_invalid_bool_value_raises_error(self):
    """Test that invalid boolean values raise an error."""
    os.environ["EXAMPLE_BOOL"] = "invalid_bool"

    with self.assertRaises(ValueError) as context:
        ParseFromEnvironment(config=self.example_config)
    self.assertIn("Error parsing the 'environment' variable for the type 'boolean'", str(context.exception))
test_invalid_dict_value_raises_error
test_invalid_dict_value_raises_error()

Test that invalid dict syntax raises an error.

Source code in tests/parse_from/test_parse_from_environment.py
360
361
362
363
364
365
366
def test_invalid_dict_value_raises_error(self):
    """Test that invalid dict syntax raises an error."""
    os.environ["EXAMPLE_DICT"] = "not a valid dict"

    with self.assertRaises(SyntaxError) as context:
        ParseFromEnvironment(config=self.example_config)
    self.assertIn("invalid syntax", str(context.exception))
test_invalid_float_value_raises_error
test_invalid_float_value_raises_error()

Test that non-float values raise an error when float is expected.

Source code in tests/parse_from/test_parse_from_environment.py
344
345
346
347
348
349
350
def test_invalid_float_value_raises_error(self):
    """Test that non-float values raise an error when float is expected."""
    os.environ["EXAMPLE_FLOAT"] = "not_a_float"

    with self.assertRaises(ValueError) as context:
        ParseFromEnvironment(config=self.example_config)
    self.assertIn("could not convert string to float", str(context.exception))
test_invalid_int_value_raises_error
test_invalid_int_value_raises_error()

Test that non-integer values raise an error when int is expected.

Source code in tests/parse_from/test_parse_from_environment.py
336
337
338
339
340
341
342
def test_invalid_int_value_raises_error(self):
    """Test that non-integer values raise an error when int is expected."""
    os.environ["EXAMPLE_INT"] = "not_an_integer"

    with self.assertRaises(ValueError) as context:
        ParseFromEnvironment(config=self.example_config)
    self.assertIn("invalid literal for int() with base 10", str(context.exception))
test_invalid_list_value_raises_error
test_invalid_list_value_raises_error()

Test that invalid list syntax raises an error.

Source code in tests/parse_from/test_parse_from_environment.py
352
353
354
355
356
357
358
def test_invalid_list_value_raises_error(self):
    """Test that invalid list syntax raises an error."""
    os.environ["EXAMPLE_LIST"] = "not a valid list"

    with self.assertRaises(ValueError) as context:
        ParseFromEnvironment(config=self.example_config)
    self.assertIn("Invalid list format", str(context.exception))
test_missing_environment_variable
test_missing_environment_variable()

Test that missing environment variables result in empty values dict.

Source code in tests/parse_from/test_parse_from_environment.py
219
220
221
222
223
224
225
226
227
def test_missing_environment_variable(self):
    """Test that missing environment variables result in empty values dict."""
    # Don't set any environment variables

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    # Should return empty dict since no env vars are set
    self.assertEqual(values, {})
test_multiple_env_vars_simultaneously
test_multiple_env_vars_simultaneously()

Test parsing multiple environment variables at the same time.

Source code in tests/parse_from/test_parse_from_environment.py
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
def test_multiple_env_vars_simultaneously(self):
    """Test parsing multiple environment variables at the same time."""
    os.environ["EXAMPLE_STRING"] = "multi_test"
    os.environ["EXAMPLE_BOOL"] = "True"
    os.environ["EXAMPLE_INT"] = "99"
    os.environ["EXAMPLE_FLOAT"] = "9.9"
    os.environ["EXAMPLE_LIST"] = "[1, 2, 3]"
    os.environ["EXAMPLE_DICT"] = "{'a': 'b'}"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertEqual(len(values), 6)
    self.assertEqual(values["example_string"], "multi_test")
    self.assertEqual(values["example_bool"], True)
    self.assertEqual(values["example_int"], 99)
    self.assertEqual(values["example_float"], 9.9)
    self.assertEqual(values["example_list"], [1, 2, 3])
    self.assertEqual(values["example_dict"], {'a': 'b'})
test_none_env_var_set
test_none_env_var_set()

Test that when none environment variables are set, an empty dict is returned.

Source code in tests/parse_from/test_parse_from_environment.py
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
def test_none_env_var_set(self):
    """Test that when none environment variables are set, an empty dict is returned."""
    # Don't set any environment variables
    test_config = [
        {
            "name": "example_string",
            "type": "string",
            "default": "default_string_value"
        }
    ]

    parser = ParseFromEnvironment(config=test_config)
    values = parser.get_values()

    # The values should be an empty dict since no environment variables are set.
    self.assertIsInstance(values, dict)
    self.assertEqual(len(values), 0)
    self.assertNotIn("example_string", values)
    self.assertEqual(values, {})
test_parse_bool_false_from_environment
test_parse_bool_false_from_environment()

Test that boolean false values are parsed correctly from environment.

Source code in tests/parse_from/test_parse_from_environment.py
119
120
121
122
123
124
125
126
127
128
def test_parse_bool_false_from_environment(self):
    """Test that boolean false values are parsed correctly from environment."""
    os.environ["EXAMPLE_BOOL"] = "False"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertIn("example_bool", values)
    self.assertEqual(values["example_bool"], False)
    self.assertIsInstance(values["example_bool"], bool)
test_parse_bool_no_from_environment
test_parse_bool_no_from_environment()

Test that boolean 'no' is parsed as False.

Source code in tests/parse_from/test_parse_from_environment.py
139
140
141
142
143
144
145
146
def test_parse_bool_no_from_environment(self):
    """Test that boolean 'no' is parsed as False."""
    os.environ["EXAMPLE_BOOL"] = "no"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertEqual(values["example_bool"], False)
test_parse_bool_true_from_environment
test_parse_bool_true_from_environment()

Test that boolean true values are parsed correctly from environment.

Source code in tests/parse_from/test_parse_from_environment.py
108
109
110
111
112
113
114
115
116
117
def test_parse_bool_true_from_environment(self):
    """Test that boolean true values are parsed correctly from environment."""
    os.environ["EXAMPLE_BOOL"] = "True"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertIn("example_bool", values)
    self.assertEqual(values["example_bool"], True)
    self.assertIsInstance(values["example_bool"], bool)
test_parse_bool_yes_from_environment
test_parse_bool_yes_from_environment()

Test that boolean 'yes' is parsed as True.

Source code in tests/parse_from/test_parse_from_environment.py
130
131
132
133
134
135
136
137
def test_parse_bool_yes_from_environment(self):
    """Test that boolean 'yes' is parsed as True."""
    os.environ["EXAMPLE_BOOL"] = "yes"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertEqual(values["example_bool"], True)
test_parse_dict_from_environment
test_parse_dict_from_environment()

Test that dictionary values are parsed correctly from environment.

Source code in tests/parse_from/test_parse_from_environment.py
208
209
210
211
212
213
214
215
216
217
def test_parse_dict_from_environment(self):
    """Test that dictionary values are parsed correctly from environment."""
    os.environ["EXAMPLE_DICT"] = "{'key1': 'value1', 'key2': 'value2'}"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertIn("example_dict", values)
    self.assertEqual(values["example_dict"], {'key1': 'value1', 'key2': 'value2'})
    self.assertIsInstance(values["example_dict"], dict)
test_parse_float_from_environment
test_parse_float_from_environment()

Test that float values are parsed correctly from environment.

Source code in tests/parse_from/test_parse_from_environment.py
168
169
170
171
172
173
174
175
176
177
def test_parse_float_from_environment(self):
    """Test that float values are parsed correctly from environment."""
    os.environ["EXAMPLE_FLOAT"] = "10.5"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertIn("example_float", values)
    self.assertEqual(values["example_float"], 10.5)
    self.assertIsInstance(values["example_float"], float)
test_parse_int_from_environment
test_parse_int_from_environment()

Test that integer values are parsed correctly from environment.

Source code in tests/parse_from/test_parse_from_environment.py
148
149
150
151
152
153
154
155
156
157
def test_parse_int_from_environment(self):
    """Test that integer values are parsed correctly from environment."""
    os.environ["EXAMPLE_INT"] = "100"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertIn("example_int", values)
    self.assertEqual(values["example_int"], 100)
    self.assertIsInstance(values["example_int"], int)
test_parse_list_from_environment
test_parse_list_from_environment()

Test that list values are parsed correctly from environment.

Source code in tests/parse_from/test_parse_from_environment.py
188
189
190
191
192
193
194
195
196
197
def test_parse_list_from_environment(self):
    """Test that list values are parsed correctly from environment."""
    os.environ["EXAMPLE_LIST"] = "[1, 2, 3, 4, 5]"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertIn("example_list", values)
    self.assertEqual(values["example_list"], [1, 2, 3, 4, 5])
    self.assertIsInstance(values["example_list"], list)
test_parse_list_of_strings_from_environment
test_parse_list_of_strings_from_environment()

Test that list of strings is parsed correctly.

Source code in tests/parse_from/test_parse_from_environment.py
199
200
201
202
203
204
205
206
def test_parse_list_of_strings_from_environment(self):
    """Test that list of strings is parsed correctly."""
    os.environ["EXAMPLE_LIST"] = "['a', 'b', 'c']"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertEqual(values["example_list"], ['a', 'b', 'c'])
test_parse_negative_float_from_environment
test_parse_negative_float_from_environment()

Test that negative float values are parsed correctly.

Source code in tests/parse_from/test_parse_from_environment.py
179
180
181
182
183
184
185
186
def test_parse_negative_float_from_environment(self):
    """Test that negative float values are parsed correctly."""
    os.environ["EXAMPLE_FLOAT"] = "-3.14"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertEqual(values["example_float"], -3.14)
test_parse_negative_int_from_environment
test_parse_negative_int_from_environment()

Test that negative integer values are parsed correctly.

Source code in tests/parse_from/test_parse_from_environment.py
159
160
161
162
163
164
165
166
def test_parse_negative_int_from_environment(self):
    """Test that negative integer values are parsed correctly."""
    os.environ["EXAMPLE_INT"] = "-50"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertEqual(values["example_int"], -50)
test_parse_string_from_environment
test_parse_string_from_environment()

Test that string values are parsed correctly from environment variables.

Source code in tests/parse_from/test_parse_from_environment.py
 97
 98
 99
100
101
102
103
104
105
106
def test_parse_string_from_environment(self):
    """Test that string values are parsed correctly from environment variables."""
    os.environ["EXAMPLE_STRING"] = "env_string_value"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertIn("example_string", values)
    self.assertEqual(values["example_string"], "env_string_value")
    self.assertIsInstance(values["example_string"], str)
test_partial_environment_variables
test_partial_environment_variables()

Test that only set environment variables are included in values.

Source code in tests/parse_from/test_parse_from_environment.py
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
def test_partial_environment_variables(self):
    """Test that only set environment variables are included in values."""
    os.environ["EXAMPLE_STRING"] = "only_string"
    os.environ["EXAMPLE_INT"] = "42"

    parser = ParseFromEnvironment(config=self.example_config)
    values = parser.get_values()

    self.assertEqual(len(values), 2)
    self.assertIn("example_string", values)
    self.assertIn("example_int", values)
    self.assertNotIn("example_bool", values)
    self.assertNotIn("example_float", values)
    self.assertNotIn("example_list", values)
    self.assertNotIn("example_dict", values)
test_set_values_is_private
test_set_values_is_private()

Test that set_values method is private.

Source code in tests/parse_from/test_parse_from_environment.py
69
70
71
72
73
74
75
76
77
78
79
def test_set_values_is_private(self):
    """Test that __set_values__ method is private."""
    os.environ["EXAMPLE_STRING"] = "value"

    parser = ParseFromEnvironment(config=self.example_config)

    with self.assertRaises(AttributeError):
        parser.__set_values()

    self.assertTrue(hasattr(parser, '_ParseFromEnvironment__set_values'))
    self.assertNotIn('__set_values', dir(parser))