示例#1
0
 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
示例#2
0
 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
示例#3
0
 def test_type_casts_follow_file_pointers(self):
     config = Configuration()
     config.register("data", six.text_type)
     config.ready = True
     with NamedTemporaryFile() as data_file:
         data_file.write("hello".encode("utf-8"))
         data_file.flush()
         config.extend({"data": "file:" + data_file.name}, cast_types=True)
     assert config.get("data") == "hello"
示例#4
0
 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
示例#5
0
 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
示例#6
0
    def test_loading_keys_from_config_file(self):
        config = Configuration()
        config.register("mode", six.text_type)
        config.register("num_participants", int, synonyms={"n"})
        config.register("deploy_worldwide", bool, synonyms={"worldwide"})
        mode_with_trailing_whitespace = "live    "
        contents = """
[Example Section]
mode = {}
num_participants = 10
worldwide = false
""".format(mode_with_trailing_whitespace)

        with NamedTemporaryFile() as configfile:
            configfile.write(contents.encode("utf-8"))
            configfile.flush()
            config.load_from_file(configfile.name)

        config.ready = True
        assert config.get("mode") == "live"  # whitespace stripped
        assert config.get("num_participants") == 10
        assert config.get("deploy_worldwide") is False
示例#7
0
    def test_loading_keys_from_config_file(self):
        config = Configuration()
        config.register('num_participants', int, synonyms={'n', })
        config.register('deploy_worldwide', bool, synonyms={'worldwide', })
        with NamedTemporaryFile() as configfile:
            configfile.write("""
[Example Section]
num_participants = 10
worldwide = false
""")
            configfile.flush()
            config.load_from_config_file(configfile.name)
        config.ready = True
        assert config.get('num_participants') == 10
        assert config.get('deploy_worldwide') is False
示例#8
0
 def test_get_has_default_value(self):
     config = Configuration()
     config.register('num_participants', int)
     config.ready = True
     assert config.get('num_participants', 10) == 10
示例#9
0
 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')
示例#10
0
 def test_setting_unknown_key_is_ignored(self):
     config = Configuration()
     config.ready = True
     config.extend({'num_participants': 1})
     config.get('num_participants', None)
示例#11
0
 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)
示例#12
0
 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
示例#13
0
 def test_register_new_variable(self):
     config = Configuration()
     config.register('num_participants', int)
     config.extend({'num_participants': 1})
     config.ready = True
     assert config.get('num_participants', 1)
示例#14
0
 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
示例#15
0
 def test_get_strips_strings(self):
     config = Configuration()
     config.register("test_string", six.text_type)
     config.ready = True
     config.extend({"test_string": " something "})
     assert config.get("test_string") == "something"