|
| 1 | +"""Test the get_bool_env_var function""" |
| 2 | + |
| 3 | +import os |
| 4 | +import unittest |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from env import get_bool_env_var |
| 8 | + |
| 9 | + |
| 10 | +class TestEnv(unittest.TestCase): |
| 11 | + """Test the get_bool_env_var function""" |
| 12 | + |
| 13 | + @patch.dict( |
| 14 | + os.environ, |
| 15 | + { |
| 16 | + "TEST_BOOL": "true", |
| 17 | + }, |
| 18 | + clear=True, |
| 19 | + ) |
| 20 | + def test_get_bool_env_var_that_exists_and_is_true(self): |
| 21 | + """Test that gets a boolean environment variable that exists and is true""" |
| 22 | + result = get_bool_env_var("TEST_BOOL", False) |
| 23 | + self.assertTrue(result) |
| 24 | + |
| 25 | + @patch.dict( |
| 26 | + os.environ, |
| 27 | + { |
| 28 | + "TEST_BOOL": "false", |
| 29 | + }, |
| 30 | + clear=True, |
| 31 | + ) |
| 32 | + def test_get_bool_env_var_that_exists_and_is_false(self): |
| 33 | + """Test that gets a boolean environment variable that exists and is false""" |
| 34 | + result = get_bool_env_var("TEST_BOOL", False) |
| 35 | + self.assertFalse(result) |
| 36 | + |
| 37 | + @patch.dict( |
| 38 | + os.environ, |
| 39 | + { |
| 40 | + "TEST_BOOL": "nope", |
| 41 | + }, |
| 42 | + clear=True, |
| 43 | + ) |
| 44 | + def test_get_bool_env_var_that_exists_and_is_false_due_to_invalid_value(self): |
| 45 | + """Test that gets a boolean environment variable that exists and is false |
| 46 | + due to an invalid value |
| 47 | + """ |
| 48 | + result = get_bool_env_var("TEST_BOOL", False) |
| 49 | + self.assertFalse(result) |
| 50 | + |
| 51 | + @patch.dict( |
| 52 | + os.environ, |
| 53 | + { |
| 54 | + "TEST_BOOL": "false", |
| 55 | + }, |
| 56 | + clear=True, |
| 57 | + ) |
| 58 | + def test_get_bool_env_var_that_does_not_exist_and_default_value_returns_true(self): |
| 59 | + """Test that gets a boolean environment variable that does not exist |
| 60 | + and default value returns: true |
| 61 | + """ |
| 62 | + result = get_bool_env_var("DOES_NOT_EXIST", True) |
| 63 | + self.assertTrue(result) |
| 64 | + |
| 65 | + @patch.dict( |
| 66 | + os.environ, |
| 67 | + { |
| 68 | + "TEST_BOOL": "true", |
| 69 | + }, |
| 70 | + clear=True, |
| 71 | + ) |
| 72 | + def test_get_bool_env_var_that_does_not_exist_and_default_value_returns_false(self): |
| 73 | + """Test that gets a boolean environment variable that does not exist |
| 74 | + and default value returns: false |
| 75 | + """ |
| 76 | + result = get_bool_env_var("DOES_NOT_EXIST", False) |
| 77 | + self.assertFalse(result) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + unittest.main() |
0 commit comments