def test_incorrect_class_empty_func(self) -> None: config = Configuration.get_default_configuration() result = validate_function( tests.test_class.incorrect_class.IncorrectTestClass.empty_func, config, tests.test_class.incorrect_class) assert result.result == ResultType.FAILED assert result.fail_reason == "Number of arguments differ. Expected (from signature) 0 arguments, but found (in docs) 1."
def test_func_no_summary_no_fail(self) -> None: config = Configuration.get_default_configuration() config.fail_on_missing_summary = False result = validate_function( tests.test_class.nodocstring_class.ClassNoDocString. func_no_summary, config, tests.test_class.correct_class) assert result.result == ResultType.OK
def test_func_with_incorrect_raise(self) -> None: config = Configuration.get_default_configuration() result = validate_function( tests.test_class.raises_class.RaisesClass. func_with_incorrect_raise, config, tests.test_class.incorrect_class) assert result.result == ResultType.FAILED
def test_func_with_raises(self) -> None: config = Configuration.get_default_configuration() result = validate_function( tests.test_class.raises_class.RaisesClass. func_with_raise_and_args_and_return, config, tests.test_class.incorrect_class) assert result.result == ResultType.OK
def test_incorrect_class_func_type_mismatch(self) -> None: config = Configuration.get_default_configuration() result = validate_function( tests.test_class.incorrect_class.IncorrectTestClass. func_type_mismatch, config, tests.test_class.incorrect_class) assert result.result == ResultType.FAILED assert result.fail_reason == "Argument type differ. Argument 'a' was expected (from signature) to have type '<class 'int'>', but has (in docs) type '<class 'float'>'"
def test_configuration_unknown_parser(self) -> None: config = Configuration.get_configuration_from_path( "tests/test_configuration/pydoctest.json") config.parser = "kldfgjndfgnjg" with pytest.raises(Exception): config.get_parser()
def test_incorrect_class_func_name_mismatch(self) -> None: config = Configuration.get_default_configuration() result = validate_function( tests.test_class.incorrect_class.IncorrectTestClass. func_name_mismatch, config, tests.test_class.incorrect_class) assert result.result == ResultType.FAILED assert result.fail_reason == "Argument name differ. Expected (from signature) 'a', but got (in docs) 'b'"
def test_incorrect_class_func_has_arg_returns_arg(self) -> None: config = Configuration.get_default_configuration() result = validate_function( tests.test_class.incorrect_class.IncorrectTestClass. func_has_arg_returns_arg, config, tests.test_class.incorrect_class) assert result.result == ResultType.FAILED assert result.fail_reason == "Return type differ. Expected (from signature) <class 'float'>, but got (in docs) <class 'NoneType'>."
def test_correct_class_module(self) -> None: config = Configuration.get_configuration_from_path( "tests/test_class/pydoctest_correct_class.json") ds = PyDoctestService(config) result = ds.validate() assert result.result == ResultType.OK
def test_fail_on_raises_section_dont_fail(self) -> None: config = Configuration.get_default_configuration() config.fail_on_raises_section = False result = validate_function( tests.test_class.raises_class.RaisesClass. func_with_incorrect_raise, config, tests.test_class.incorrect_class) assert result.result == ResultType.OK
def test_correct_class_class_is_ok(self) -> None: config = Configuration.get_default_configuration() result = validate_class( tests.test_class.correct_class.CorrectTestClass, config, tests.test_class.correct_class) assert result.result == ResultType.OK for res in result.function_results: assert res.result == ResultType.OK
def test_func_no_summary_do_fail(self) -> None: config = Configuration.get_default_configuration() config.fail_on_missing_summary = True result = validate_function( tests.test_class.nodocstring_class.ClassNoDocString. func_no_summary, config, tests.test_class.correct_class) assert result.result == ResultType.FAILED assert result.fail_reason == 'Function does not have a summary'
def test_text_reporter_success_verbosity_1(self) -> None: config = Configuration.get_default_configuration() config.verbosity = Verbosity.SHOW_FAILED reporter = TextReporter(config) result = get_result_object(ResultType.OK) output = reporter.get_output(result) assert output == ""
def test_incorrect_class_is_fail(self) -> None: config = Configuration.get_default_configuration() result = validate_class( tests.test_class.incorrect_class.IncorrectTestClass, config, tests.test_class.incorrect_class) assert result.result == ResultType.FAILED for res in result.function_results: assert res.result == ResultType.FAILED
def test_configuration_from_path(self) -> None: config = Configuration.get_configuration_from_path( "tests/test_configuration/pydoctest.json") assert config.parser == "parsername" assert config.include_paths == ["/path/to/module"] assert config.exclude_paths == ["/excludes/0", "excludes/1"] assert config.fail_on_missing_docstring == True
def test_invalid_python_module(self) -> None: config = Configuration.get_configuration_from_path( "tests/test_main/pydoctest.json") ds = PyDoctestService(config) result = ds.validate() assert result.module_results[0].result == ResultType.NOT_RUN assert "Failed to load file from location" in result.module_results[ 0].fail_reason
def test_text_reporter_no_doc_no_fail(self) -> None: config = Configuration.get_default_configuration() config.verbosity = Verbosity.SHOW_ALL config.fail_on_missing_docstring = False reporter = TextReporter(config) result = get_result_object(ResultType.NO_DOC) output = reporter.get_output(result) assert output == ''
def test_enum_in_module(self) -> None: # Test that enums are ignored config = Configuration.get_configuration_from_path( "tests/test_class/pydoctest_enum_in_module.json") ds = PyDoctestService(config) result = ds.validate() assert len(result.module_results[0].class_results) == 1 assert result.module_results[0].class_results[ 0].class_name == "ExampleClass"
def test_no_include_paths(self) -> None: config = Configuration.get_configuration_from_path( "tests/test_class/test_no_include_paths/pydoctest.json") ds = PyDoctestService(config) result = ds.validate() assert result.result == ResultType.OK # Assert that 1 module was found in root. We do not search recursively. assert len(result.module_results) == 1
def test_func_argument_type_differ(self) -> None: config = Configuration.get_default_configuration() result = validate_function( tests.test_ranges.example_class.ExampleClass. func_argument_type_differ, config, tests.test_ranges.example_class) assert result.result == ResultType.FAILED assert 'Argument type differ' in result.fail_reason assert result.range is not None assert result.range.start_line == 77 assert result.range.end_line == 84
def test_func_parse_exception(self) -> None: config = Configuration.get_default_configuration() result = validate_function( tests.test_ranges.example_class.ExampleClass.func_parse_exception, config, tests.test_ranges.example_class) assert result.result == ResultType.FAILED assert 'Unable to parse docstring' in result.fail_reason assert result.range is not None assert result.range.start_line == 22 assert result.range.end_line == 32
def test_func_number_of_raised_exceptions_differ(self) -> None: config = Configuration.get_default_configuration() result = validate_function( tests.test_ranges.example_class.ExampleClass. func_number_of_raised_exceptions_differ, config, tests.test_ranges.example_class) assert result.result == ResultType.FAILED assert 'Number of listed raised exceptions does not match actual' in result.fail_reason assert result.range is not None assert result.range.start_line == 89 assert result.range.end_line == 100
def test_func_no_docstring(self) -> None: config = Configuration.get_default_configuration() config.fail_on_missing_docstring = True result = validate_function( tests.test_ranges.example_class.ExampleClass.func_no_docstring, config, tests.test_ranges.example_class) assert result.result == ResultType.FAILED assert result.fail_reason == 'Function does not have a docstring' assert result.range is not None assert result.range.start_line == 16 assert result.range.end_line == 16
def test_text_reporter_success(self) -> None: config = Configuration.get_default_configuration() reporter = JSONReporter(config) result = get_result_object(ResultType.OK) output = reporter.get_output(result) d = json.loads(output) assert d['result'] == ResultType.OK assert d['module_results'][0]['function_results'][0][ 'result'] == ResultType.OK assert d['module_results'][0]['class_results'][0]['function_results'][ 0]['result'] == ResultType.OK
def get_configuration(root_dir: str, config_path: Optional[str] = None) -> Configuration: """Searches for CONFIG_FILE_NAME in root_dir, unless a path is provided. Args: root_dir (str): The directory to search in. config_path (Optional[str], optional): [description]. Defaults to None. Returns: Configuration: Either a configuration matching the specified/found one, or a default one. """ if config_path: return Configuration.get_configuration_from_path(config_path) config_paths = [p for p in os.listdir(root_dir) if p == CONFIG_FILE_NAME] if len(config_paths) == 0: # TODO: Is the interface better by returning Optional[Configuration] (None here)? # Then it is clearer that a config was not found. return Configuration.get_default_configuration(root_dir) path = os.path.join(root_dir, config_paths[0]) return Configuration.get_configuration_from_path(path)
def test_text_reporter_no_doc_no_fail(self) -> None: config = Configuration.get_default_configuration() config.fail_on_missing_docstring = False reporter = JSONReporter(config) result = get_result_object(ResultType.NO_DOC) output = reporter.get_output(result) d = json.loads(output) assert d['result'] == ResultType.NO_DOC assert d['module_results'][0]['function_results'][0][ 'result'] == ResultType.NO_DOC assert d['module_results'][0]['class_results'][0]['function_results'][ 0]['result'] == ResultType.NO_DOC
def test_text_reporter_failed(self) -> None: config = Configuration.get_default_configuration() config.verbosity = Verbosity.SHOW_ALL reporter = TextReporter(config) result = get_result_object(ResultType.FAILED, "FAIL REASON") output = reporter.get_output(result) messages = [m for m in output.split("\n") if m] assert 'FunctionName' in messages[0] assert 'FAIL | FAIL REASON' in messages[0] assert 'MethodName' in messages[1] assert 'FAIL | FAIL REASON' in messages[1]
def test_text_reporter_success_verbosity_2(self) -> None: config = Configuration.get_default_configuration() config.verbosity = Verbosity.SHOW_ALL reporter = TextReporter(config) result = get_result_object(ResultType.OK) output = reporter.get_output(result) messages = [m for m in output.split("\n") if m] assert 'FunctionName' in messages[0] assert 'OK' in messages[0] assert 'MethodName' in messages[1] assert 'OK' in messages[1]
def test_counts_class(self) -> None: config = Configuration.get_configuration_from_path( "tests/test_class/pydoctest_get_counts.json") config.fail_on_missing_docstring = False ds = PyDoctestService(config) result = ds.validate() counts = result.get_counts() assert counts.functions_failed == 1 assert counts.functions_skipped == 1 assert counts.functions_succeeded == 2 assert counts.module_count == 1 assert counts.get_total() == 4
def test_text_reporter_no_doc_do_fail(self) -> None: config = Configuration.get_default_configuration() config.verbosity = Verbosity.SHOW_ALL config.fail_on_missing_docstring = True reporter = TextReporter(config) result = get_result_object(ResultType.NO_DOC) output = reporter.get_output(result) messages = [m for m in output.split("\n") if m] assert 'FunctionName' in messages[0] assert 'is missing a docstring' in messages[0] assert 'MethodName' in messages[1] assert 'is missing a docstring' in messages[1]