Skip to content

Commit

Permalink
Env tests (#254)
Browse files Browse the repository at this point in the history
* Add tests for environment substitution

* Add a few more cases

* remove debug fail
  • Loading branch information
mathialo authored Oct 30, 2023
1 parent 7718749 commit 14b31ae
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/tests_unit/test_configtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class CastingClass:
yet_another_string_field: str


@dataclass
class SimpleStringConfig:
string_field: str


class TestConfigtoolsMethods(unittest.TestCase):
def test_ensure_snake_case(self):
snake_dict = {
Expand Down Expand Up @@ -379,3 +384,31 @@ def test_dump_and_reload_config(self):
yaml.dump(dataclasses.asdict(config), config_file)
with open("test_dump_config.yml", "r") as config_file:
load_yaml(config_file, BaseConfig)

def test_env_substitution(self):
os.environ["STRING_VALUE"] = "heyo"

config_file1 = "string-field: ${STRING_VALUE}"
config1: SimpleStringConfig = load_yaml(config_file1, SimpleStringConfig)

self.assertEqual(config1.string_field, "heyo")

config_file2 = "string-field: ${STRING_VALUE} in context"
config2 = load_yaml(config_file2, SimpleStringConfig)

self.assertEqual(config2.string_field, "heyo in context")

config_file3 = 'string-field: !env "${STRING_VALUE} in context"'
config3 = load_yaml(config_file3, SimpleStringConfig)

self.assertEqual(config3.string_field, "heyo in context")

config_file4 = "string-field: ${STRING_VALUE}without space"
config4 = load_yaml(config_file4, SimpleStringConfig)

self.assertEqual(config4.string_field, "heyowithout space")

config_file5 = "string-field: !env very${STRING_VALUE}crowded"
config5 = load_yaml(config_file5, SimpleStringConfig)

self.assertEqual(config5.string_field, "veryheyocrowded")

0 comments on commit 14b31ae

Please sign in to comment.