def test_should_raise_if_format_error(): """ Test exception is raised if docstring syntax error """ docstring_error = ''' :param p1 ''' with pytest.raises(MlVToolException) as e: parse_docstring(docstring_error) assert isinstance(e.value.__cause__, ParseError)
def test_should_get_dvc_params(): """ Test dvc parameters extraction """ docstring_str = ':param str param1: Param1 description\n' \ ':param param2: input file\n' \ ':dvc-out: path/to/file.txt\n' \ ':dvc-out param1: path/to/other\n' \ ':dvc-in param2: path/to/in/file\n' \ ':dvc-in: path/to/other/infile.test' docstring = parse_docstring(docstring_str) assert len(docstring.meta) == 6 dvc_params = get_dvc_params(docstring) assert len(dvc_params.dvc_in) == 2 assert DocstringDvcIn('path/to/other/infile.test') in dvc_params.dvc_in assert DocstringDvcIn('path/to/in/file', related_param='param2') in dvc_params.dvc_in assert len(dvc_params.dvc_out) == 2 assert DocstringDvcOut('path/to/file.txt') in dvc_params.dvc_out assert DocstringDvcOut('path/to/other', related_param='param1') in dvc_params.dvc_out assert not dvc_params.dvc_extra
def test_should_raise_if_dvc_command_and_others(): """ Test dvc parameters extraction """ docstring_str = '{}' \ ':dvc-cmd: dvc run -o ./out_train.csv -o ./out_test.csv\n' \ ' ./py_cmd -m train --out ./out_train.csv && ./py_cmd -m test --out ./out_test.csv' docstring = parse_docstring(docstring_str.format(':dvc-in: ./file.csv\n')) with pytest.raises(MlVToolException): get_dvc_params(docstring) docstring = parse_docstring(docstring_str.format(':dvc-out: /file.csv\n')) with pytest.raises(MlVToolException): get_dvc_params(docstring) docstring = parse_docstring(docstring_str.format(':dvc-extra: --dry \n')) with pytest.raises(MlVToolException): get_dvc_params(docstring)
def get_docstring_data(cell_content: str) -> Tuple[Docstring, str]: """ Extract docstring and formatted parameters from a cell content """ docstring_str = extract_docstring(cell_content) if docstring_str: return parse_docstring(docstring_str), f'"""\n{docstring_str}\n"""' logging.warning("Docstring not found.") return Docstring(), ''
def test_should_parse_docstring(): """ Test parse valid docstring """ docstring_str = ''' A multiline docstring :param p1: ''' docstring = parse_docstring(docstring_str) assert docstring
def test_should_extract_arguments_as_parameters(): """ Test get argument as parameters for python method call """ docstring_str = '''""" :param str param_one: Param1 description :param int param2: """''' args_as_param = get_arguments_as_param(parse_docstring(docstring_str)) assert args_as_param == 'args.param_one, args.param2'
def test_should_convert_list_param_to_python_arg(): """ Test convert list param to python arg """ repr = ':param list param_one: Param1 description\n' arguments = get_arguments_from_docstring(parse_docstring(repr)) assert arguments == [{ 'name': 'param-one', 'type': 'str', 'help': 'Param1 description', 'is_list': True }]
def test_should_extract_parameters_as_python_params(): """ Test parameters are extracted from docstring and converted to python format """ docstring_str = '''""" :param str param_one: Param1 description :param int param2: :param param3: Param3 description :param param4: """''' parameters = get_param_as_python_method_format( parse_docstring(docstring_str)) assert parameters == 'param_one: str, param2: int, param3, param4'
def test_should_get_dvc_command(): """ Test dvc parameters extraction """ cmd = 'dvc run -o ./out_train.csv \n' \ '-o ./out_test.csv\n' \ './py_cmd -m train --out ./out_train.csv &&\n' \ './py_cmd -m test --out ./out_test.csv' docstring_str = f':dvc-cmd: {cmd}' docstring = parse_docstring(docstring_str) dvc_params = get_dvc_params(docstring) assert not dvc_params.dvc_in assert not dvc_params.dvc_out assert not dvc_params.dvc_extra assert dvc_params.dvc_cmd.cmd == cmd