From cb3ec1fb5dd048b06e33660b359e4363a41e1242 Mon Sep 17 00:00:00 2001 From: John Chilton Date: Sun, 4 Aug 2024 22:33:43 -0400 Subject: [PATCH] Refactor parameter unit testing to be more general. --- .../tool_util/unittest_utils/parameters.py | 23 +++++++--------- .../tool_util/test_parameter_specification.py | 27 +++++++++---------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/lib/galaxy/tool_util/unittest_utils/parameters.py b/lib/galaxy/tool_util/unittest_utils/parameters.py index 71738b5f4694..aef217b9bef7 100644 --- a/lib/galaxy/tool_util/unittest_utils/parameters.py +++ b/lib/galaxy/tool_util/unittest_utils/parameters.py @@ -2,10 +2,15 @@ from galaxy.tool_util.parameters import ( from_input_source, + input_models_for_tool_source, ToolParameterBundle, + ToolParameterBundleModel, ToolParameterT, ) -from galaxy.tool_util.parser.factory import get_tool_source +from galaxy.tool_util.parser import ( + get_tool_source, + ToolSource, +) from galaxy.util import galaxy_directory @@ -19,22 +24,12 @@ def parameter_bundle(parameter: ToolParameterT) -> ParameterBundle: return ParameterBundle(parameter) -def parameter_bundle_for_file(filename: str) -> ParameterBundle: - return parameter_bundle(tool_parameter(filename)) - - -def tool_parameter(filename: str) -> ToolParameterT: - return from_input_source(parameter_source(filename)) - - -def parameter_source(filename: str): +def parameter_bundle_for_file(filename: str) -> ToolParameterBundleModel: tool_source = parameter_tool_source(filename) - input_sources = tool_source.parse_input_pages().page_sources[0].parse_input_sources() - assert len(input_sources) == 1 - return input_sources[0] + return input_models_for_tool_source(tool_source) -def parameter_tool_source(basename: str): +def parameter_tool_source(basename: str) -> ToolSource: path_prefix = os.path.join(galaxy_directory(), "test/functional/tools/parameters", basename) if os.path.exists(f"{path_prefix}.xml"): path = f"{path_prefix}.xml" diff --git a/test/unit/tool_util/test_parameter_specification.py b/test/unit/tool_util/test_parameter_specification.py index c1d38c484f49..9e81efddbc1e 100644 --- a/test/unit/tool_util/test_parameter_specification.py +++ b/test/unit/tool_util/test_parameter_specification.py @@ -24,7 +24,6 @@ from galaxy.tool_util.unittest_utils.parameters import ( parameter_bundle, parameter_bundle_for_file, - tool_parameter, ) from galaxy.util.resources import resource_string @@ -59,7 +58,7 @@ def test_single(): def _test_file(file: str, specification=None): spec = specification or specification_object() combos = spec[file] - tool_parameter_model = tool_parameter(file) + parameter_bundle = parameter_bundle_for_file(file) assertion_functions = { "request_valid": _assert_requests_validate, @@ -74,13 +73,13 @@ def _test_file(file: str, specification=None): for valid_or_invalid, tests in combos.items(): assertion_function = assertion_functions[valid_or_invalid] - assertion_function(tool_parameter_model, tests) + assertion_function(parameter_bundle, tests) # Assume request validation will work here. if "request_internal_valid" not in combos and "request_valid" in combos: - _assert_internal_requests_validate(tool_parameter_model, combos["request_valid"]) + _assert_internal_requests_validate(parameter_bundle, combos["request_valid"]) if "request_internal_invalid" not in combos and "request_invalid" in combos: - _assert_internal_requests_invalid(tool_parameter_model, combos["request_invalid"]) + _assert_internal_requests_invalid(parameter_bundle, combos["request_invalid"]) def _for_each(test: Callable, parameter: ToolParameterT, requests: List[Dict[str, Any]]) -> None: @@ -90,7 +89,7 @@ def _for_each(test: Callable, parameter: ToolParameterT, requests: List[Dict[str def _assert_request_validates(parameter: ToolParameterT, request: Dict[str, Any]) -> None: try: - validate_request(parameter_bundle(parameter), request) + validate_request(parameter, request) except RequestParameterInvalidException as e: raise AssertionError(f"Parameter {parameter} failed to validate request {request}. {e}") @@ -98,7 +97,7 @@ def _assert_request_validates(parameter: ToolParameterT, request: Dict[str, Any] def _assert_request_invalid(parameter, request) -> None: exc = None try: - validate_request(parameter_bundle(parameter), request) + validate_request(parameter, request) except RequestParameterInvalidException as e: exc = e assert exc is not None, f"Parameter {parameter} didn't result in validation error on request {request} as expected." @@ -106,7 +105,7 @@ def _assert_request_invalid(parameter, request) -> None: def _assert_internal_request_validates(parameter, request) -> None: try: - validate_internal_request(parameter_bundle(parameter), request) + validate_internal_request(parameter, request) except RequestParameterInvalidException as e: raise AssertionError(f"Parameter {parameter} failed to validate internal request {request}. {e}") @@ -114,7 +113,7 @@ def _assert_internal_request_validates(parameter, request) -> None: def _assert_internal_request_invalid(parameter, request) -> None: exc = None try: - validate_internal_request(parameter_bundle(parameter), request) + validate_internal_request(parameter, request) except RequestParameterInvalidException as e: exc = e assert ( @@ -124,7 +123,7 @@ def _assert_internal_request_invalid(parameter, request) -> None: def _assert_internal_job_validates(parameter, request) -> None: try: - validate_internal_job(parameter_bundle(parameter), request) + validate_internal_job(parameter, request) except RequestParameterInvalidException as e: raise AssertionError(f"Parameter {parameter} failed to validate internal job description {request}. {e}") @@ -132,7 +131,7 @@ def _assert_internal_job_validates(parameter, request) -> None: def _assert_internal_job_invalid(parameter, request) -> None: exc = None try: - validate_internal_job(parameter_bundle(parameter), request) + validate_internal_job(parameter, request) except RequestParameterInvalidException as e: exc = e assert ( @@ -142,7 +141,7 @@ def _assert_internal_job_invalid(parameter, request) -> None: def _assert_test_case_validates(parameter, test_case) -> None: try: - validate_test_case(parameter_bundle(parameter), test_case) + validate_test_case(parameter, test_case) except RequestParameterInvalidException as e: raise AssertionError(f"Parameter {parameter} failed to validate test_case {test_case}. {e}") @@ -150,7 +149,7 @@ def _assert_test_case_validates(parameter, test_case) -> None: def _assert_test_case_invalid(parameter, test_case) -> None: exc = None try: - validate_test_case(parameter_bundle(parameter), test_case) + validate_test_case(parameter, test_case) except RequestParameterInvalidException as e: exc = e assert ( @@ -213,7 +212,7 @@ def encode_val(val: int) -> str: parameter_spec = specification_object() parameter_models_json = {} for file in parameter_spec.keys(): - tool_parameter_model = tool_parameter(file) + tool_parameter_model = parameter_bundle_for_file(file) parameter_models_json[file] = tool_parameter_model.dict() yaml_str = yaml.safe_dump(parameter_models_json) with open("client/src/components/Tool/parameter_models.yml", "w") as f: