def test_001_load_single_config_file(self): self.logger.debug("TestConfig: test_001_load_single_config_file") # Always use an explicit new conf object conf = JsonConfig.new_conf(logger=self.logger, package_name='tests') test_conf_file_name = "config.json" test_data = {"key1": "value1"} test_config_file_contents = json.dumps(test_data) test_conf_file_path = self.write_file_to_tmp_dir( test_conf_file_name, test_config_file_contents) # Set up conf object conf.use_default_sym_names = False conf.set_conf_name(test_conf_file_name) conf.add_search_path(test_conf_file_path) conf.expand_search_paths() full_search_paths = conf.full_search_paths # Only search path should be the one we added self.assertEqual(test_conf_file_path, full_search_paths[0]) loaded_data = conf.get_data() # Clean up os.remove(test_conf_file_path) self.assertEqual(loaded_data, test_data)
def test_002_load_multiple_config_files(self): self.logger.debug("TestConfig: test_002_load_multiple_config_files") # Always use an explicit new conf object conf = JsonConfig.new_conf(logger=self.logger, package_name='tests') test_conf_file_name = "conf.json" test_data = {"key1": "val2.1", "key5": "val2.5"} test_config_file_contents = json.dumps(test_data) test_conf_file_path = self.write_file_to_tmp_dir( test_conf_file_name, test_config_file_contents) # Set up conf object - search paths are ordered conf.use_default_sym_names = False conf.add_sym_name('package') conf.add_sym_name('unknown_sym_name') conf.add_search_path(test_conf_file_path) conf.add_search_path( paths.get_data_path(file_name='conf.json', package_name='tests')) conf.expand_search_paths() full_search_paths = conf.full_search_paths self.assertEqual(len(full_search_paths), 4) # Only search path that can be tested here is the test_conf_file_path self.assertEqual(test_conf_file_path, full_search_paths[2]) loaded_data = conf.get_data() # Clean up os.remove(test_conf_file_path) self.assertEqual(loaded_data["key1"], "val3.1") self.assertEqual(loaded_data["key2"], "val3.2") self.assertEqual(loaded_data["key4"], "val1.4") self.assertEqual(loaded_data["key5"], "val2.5") self.assertEqual(loaded_data["key6"], "val3.6")
def test_003_load_config_file_from_env(self): self.logger.debug("TestConfig: test_003_load_config_file_from_env") # Always use an explicit new conf object conf = JsonConfig.new_conf(logger=self.logger, package_name='tests') test_conf_file_name = "conf.json" test_data = {"key1": "val2.1", "key5": "val2.5"} test_config_file_contents = json.dumps(test_data) test_conf_file_path = self.write_file_to_tmp_dir( test_conf_file_name, test_config_file_contents) # Set up conf object - search paths are ordered conf.use_default_sym_names = False os.environ['JSON_CONF_DIR'] = path.dirname(test_conf_file_path) os.environ['JSON_CONF'] = test_conf_file_name conf.add_sym_name('env') conf.expand_search_paths() full_search_paths = conf.full_search_paths self.assertEqual(len(full_search_paths), 1) # Only search path that can be tested here is the test_conf_file_path self.assertEqual(test_conf_file_path, full_search_paths[0]) loaded_data = conf.get_data() # Clean up del os.environ['JSON_CONF_DIR'] del os.environ['JSON_CONF'] os.remove(test_conf_file_path) self.assertEqual(loaded_data["key1"], "val2.1") self.assertEqual(loaded_data["key5"], "val2.5")