def test_add_and_describe(): """ Test that a key an be added and described """ with default_config(): key = 'newkey' value = 'testvalue' value_type = 'string' description = 'A test' default = 'testdefault' cfg = Config() cfg.add(key=key, value=value, value_type=value_type, description=description, default=default) desc = cfg.describe(f'user.{key}') expected_desc = (f"{description}.\nCurrent value: {value}. " f"Type: {value_type}. Default: {default}.") assert desc == expected_desc
class TestConfig(TestCase): def setUp(self): self.conf = Config() def test_missing_config_file(self): with self.assertRaises(FileNotFoundError): self.conf.load_config("./missing.json") @patch.object(Config, 'current_schema', new_callable=PropertyMock) @patch.object(Config, 'env_file_name', new_callable=PropertyMock) @patch.object(Config, 'load_config') @patch('os.path.isfile') @unittest.skipIf(Path.cwd() == Path.home(), 'This test requires that working dir is different from' 'homedir.') def test_default_config_files(self, isfile, load_config, env, schema): # don't try to load custom schemas self.conf.schema_cwd_file_name = None self.conf.schema_home_file_name = None self.conf.schema_env_file_name = None schema.return_value = SCHEMA env.return_value = ENV_KEY isfile.return_value = True load_config.side_effect = partial(side_effect, GOOD_CONFIG_MAP) self.conf.defaults, self.defaults_schema = self.conf.load_default() config = self.conf.update_config() self.assertEqual(config, CONFIG) @patch.object(Config, 'current_schema', new_callable=PropertyMock) @patch.object(Config, 'env_file_name', new_callable=PropertyMock) @patch.object(Config, 'load_config') @patch('os.path.isfile') @unittest.skipIf(Path.cwd() == Path.home(), 'This test requires that working dir is different from' 'homedir.') def test_bad_config_files(self, isfile, load_config, env, schema): # don't try to load custom schemas self.conf.schema_cwd_file_name = None self.conf.schema_home_file_name = None self.conf.schema_env_file_name = None schema.return_value = SCHEMA env.return_value = ENV_KEY isfile.return_value = True load_config.side_effect = partial(side_effect, BAD_CONFIG_MAP) with self.assertRaises(jsonschema.exceptions.ValidationError): self.conf.defaults, self.defaults_schema = self.conf.load_default() self.conf.update_config() @patch.object(Config, 'current_schema', new_callable=PropertyMock) @patch.object(Config, 'env_file_name', new_callable=PropertyMock) @patch.object(Config, 'load_config') @patch('os.path.isfile') @patch("builtins.open", mock_open(read_data=USER_SCHEMA)) @unittest.skipIf(Path.cwd() == Path.home(), 'This test requires that working dir is different from' 'homedir.') def test_user_schema(self, isfile, load_config, env, schema): schema.return_value = copy.deepcopy(SCHEMA) env.return_value = ENV_KEY isfile.return_value = True load_config.side_effect = partial(side_effect, GOOD_CONFIG_MAP) self.conf.defaults, self.defaults_schema = self.conf.load_default() config = self.conf.update_config() self.assertEqual(config, CONFIG) @patch.object(Config, 'current_schema', new_callable=PropertyMock) @patch.object(Config, 'env_file_name', new_callable=PropertyMock) @patch.object(Config, 'load_config') @patch('os.path.isfile') @patch("builtins.open", mock_open(read_data=USER_SCHEMA)) def test_bad_user_schema(self, isfile, load_config, env, schema): schema.return_value = copy.deepcopy(SCHEMA) env.return_value = ENV_KEY isfile.return_value = True load_config.side_effect = partial(side_effect, BAD_CONFIG_MAP) with self.assertRaises(jsonschema.exceptions.ValidationError): self.conf.defaults, self.defaults_schema = self.conf.load_default() self.conf.update_config() @patch.object(Config, "current_config", new_callable=PropertyMock) def test_update_user_config(self, config): # deep copy because we mutate state config.return_value = copy.deepcopy(CONFIG) self.conf.add("foo", "bar") self.assertEqual(self.conf.current_config, UPDATED_CONFIG) @patch.object(Config, 'current_schema', new_callable=PropertyMock) @patch.object(Config, "current_config", new_callable=PropertyMock) def test_update_and_validate_user_config(self, config, schema): self.maxDiff = None schema.return_value = copy.deepcopy(SCHEMA) # deep copy because we mutate state config.return_value = copy.deepcopy(CONFIG) self.conf.add("foo", "bar", "string", "foo", "bar") self.assertEqual(self.conf.current_config, UPDATED_CONFIG) self.assertEqual(self.conf.current_schema, UPDATED_SCHEMA)