def test_load_file_no_file(): # test trying to load a config file which does not exist with mock.patch("os.path.exists") as mock_path_exists: mock_path_exists.return_value = False with pytest.raises(config_manager.InvalidConfigException): config_manager.load_file("blabla")
def test_load_file_no_validator(): # tests loading a existing file with no validator with mock.patch("os.path.exists") as mock_path_exists: mock_path_exists.return_value = True with mock.patch(BUILTIN_SOPEN) as mock_file: with mock.patch("yaml.load") as mock_yml_load: mock_yml_load.return_value = {"db": "path"} data = config_manager.load_file("blabla") assert data == mock_yml_load.return_value
def test_load_file_validator_invalid(): # tests loading a existing file with validator and data is invalid with mock.patch("os.path.exists") as mock_path_exists: mock_path_exists.return_value = True with mock.patch(BUILTIN_SOPEN) as mock_file: with mock.patch("yaml.load") as mock_yml_load: mock_yml_load.return_value = {"db": "path"} mock_validator = MagicMock() mock_validator.return_value = False, "reason" with pytest.raises(config_manager.InvalidConfigException): data = config_manager.load_file("blabla", mock_validator)
def test_load_file_validator_valid(): # tests loading a existing file with validator and data is valid with mock.patch("os.path.exists") as mock_path_exists: mock_path_exists.return_value = True with mock.patch(BUILTIN_SOPEN) as mock_file: with mock.patch("yaml.load") as mock_yml_load: mock_yml_load.return_value = {"db": "path"} mock_validator = MagicMock() mock_validator.return_value = True, "reason" data = config_manager.load_file("blabla", mock_validator) assert data == mock_yml_load.return_value assert mock_validator.called
value_schema=valideer.String) FOLDER_TEMPLATES_YML = "folder_templates.yml" class RouteNotExists(KeyError): def __init__(self, route_name): super(RouteNotExists, self).__init__( "Route with name {} does not exist! Please head over to {} and create your route!" .format( route_name, os.path.join(config_manager.get_config_folder(), ROUTES_YML))) # load data for folders _data_folders = config_manager.load_file(FOLDER_TEMPLATES_YML, None) # todo add validator # load data for routes _data_routes = config_manager.load_file( ROUTES_YML, lambda data: config_manager.validate_schema(data, route_schema)) def get_file_templates(entity_type): # type: (str) -> list[dict[str, str]] """ Returns a list of file templates for one entity. :param entity_type: entity to get file templates for :return: a list of file templates """ return get_file_and_folder_templates(entity_type)[0]
import valideer from kttk.config import config_manager TASK_PRESETS_YML = "task_presets.yml" task_preset_schema = valideer.Mapping( key_schema=valideer.String, value_schema=valideer.HomogeneousSequence(item_schema=valideer.Object( required={ "name": valideer.String, "step": valideer.String })), ) _data_presets = config_manager.load_file( TASK_PRESETS_YML, lambda data: config_manager.validate_schema(data, task_preset_schema), ) def get_task_presets(entity_type): # type: (str) -> list[dict] """ Returns list of all task templates for given type. Task preset example, everything will be lowercase { 'step': 'anim', 'name': 'anim' } :param entity_type: type to get all task templates for :return: """ raw_preset = _data_presets[entity_type]