Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_register_duplicate_variable_raises(self):
config = Configuration()
config.register("num_participants", int)
with pytest.raises(KeyError):
config.register("num_participants", int)
def test_setting_unknown_key_is_ignored(self):
config = Configuration()
config.ready = True
config.extend({"num_participants": 1})
config.get("num_participants", None)
def test_type_mismatch_with_cast_types(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
config.extend({"num_participants": 1.0}, cast_types=True)
assert config.get("num_participants", 1) == 1
def test_get_before_ready_is_not_possible(self):
config = Configuration()
config.register("num_participants", int)
config.extend({"num_participants": 1})
with pytest.raises(RuntimeError):
config.get("num_participants", 1)
def test_layering_of_configs(self):
config = Configuration()
config.register("num_participants", int)
config.extend({"num_participants": 1})
config.ready = True
assert config.get("num_participants", 1) == 1
config.extend({"num_participants": 2})
assert config.get("num_participants", 1) == 2
def test_setting_values_supports_synonyms(self):
config = Configuration()
config.register("num_participants", int, synonyms={"n"})
config.ready = True
config.extend({"n": 1})
assert config.get("num_participants") == 1
def test_attribute_access(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
config.extend({"num_participants": 1})
assert config.num_participants == 1
def test_loading_keys_from_environment_variables(self):
config = Configuration()
config.register("num_participants", int, synonyms={"n"})
os.environ["num_participants"] = "1"
try:
config.load_from_environment()
finally:
del os.environ["num_participants"]
config.ready = True
assert config.get("num_participants") == 1
def test_get_without_default_raises(self):
config = Configuration()
config.register("num_participants", int)
config.ready = True
with pytest.raises(KeyError):
config.get("num_participants")
def get_config():
global config
if config is None:
config = Configuration()
for registration in default_keys:
config.register(*registration)
return config