Skip to content

Commit

Permalink
Refactor parameter unit testing to be more general.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmchilton committed Aug 5, 2024
1 parent 58de6ef commit cde98e7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 51 deletions.
24 changes: 9 additions & 15 deletions lib/galaxy/tool_util/unittest_utils/parameters.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import os

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


Expand All @@ -19,22 +23,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"
Expand Down
72 changes: 36 additions & 36 deletions test/unit/tool_util/test_parameter_specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@
encode,
RequestInternalToolState,
RequestToolState,
ToolParameterBundleModel,
validate_internal_job,
validate_internal_request,
validate_request,
validate_test_case,
)
from galaxy.tool_util.parameters.json import to_json_schema_string
from galaxy.tool_util.parameters.models import ToolParameterT
from galaxy.tool_util.unittest_utils.parameters import (
parameter_bundle,
parameter_bundle_for_file,
tool_parameter,
)
from galaxy.tool_util.unittest_utils.parameters import parameter_bundle_for_file
from galaxy.util.resources import resource_string


RawStateDict = Dict[str, Any]


def specification_object():
try:
yaml_str = resource_string(__package__, "parameter_specification.yml")
Expand Down Expand Up @@ -59,7 +59,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: ToolParameterBundleModel = parameter_bundle_for_file(file)

assertion_functions = {
"request_valid": _assert_requests_validate,
Expand All @@ -74,88 +74,88 @@ 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:
def _for_each(test: Callable, parameters: ToolParameterBundleModel, requests: List[RawStateDict]) -> None:
for request in requests:
test(parameter, request)
test(parameters, request)


def _assert_request_validates(parameter: ToolParameterT, request: Dict[str, Any]) -> None:
def _assert_request_validates(parameters: ToolParameterBundleModel, request: RawStateDict) -> None:
try:
validate_request(parameter_bundle(parameter), request)
validate_request(parameters, request)
except RequestParameterInvalidException as e:
raise AssertionError(f"Parameter {parameter} failed to validate request {request}. {e}")
raise AssertionError(f"Parameters {parameters} failed to validate request {request}. {e}")


def _assert_request_invalid(parameter, request) -> None:
def _assert_request_invalid(parameters: ToolParameterBundleModel, request: RawStateDict) -> None:
exc = None
try:
validate_request(parameter_bundle(parameter), request)
validate_request(parameters, 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."
assert exc is not None, f"Parameters {parameters} didn't result in validation error on request {request} as expected."


def _assert_internal_request_validates(parameter, request) -> None:
def _assert_internal_request_validates(parameters: ToolParameterBundleModel, request: RawStateDict) -> None:
try:
validate_internal_request(parameter_bundle(parameter), request)
validate_internal_request(parameters, request)
except RequestParameterInvalidException as e:
raise AssertionError(f"Parameter {parameter} failed to validate internal request {request}. {e}")
raise AssertionError(f"Parameters {parameters} failed to validate internal request {request}. {e}")


def _assert_internal_request_invalid(parameter, request) -> None:
def _assert_internal_request_invalid(parameters: ToolParameterBundleModel, request: RawStateDict) -> None:
exc = None
try:
validate_internal_request(parameter_bundle(parameter), request)
validate_internal_request(parameters, request)
except RequestParameterInvalidException as e:
exc = e
assert (
exc is not None
), f"Parameter {parameter} didn't result in validation error on internal request {request} as expected."
), f"Parameters {parameters} didn't result in validation error on internal request {request} as expected."


def _assert_internal_job_validates(parameter, request) -> None:
def _assert_internal_job_validates(parameters: ToolParameterBundleModel, request: RawStateDict) -> None:
try:
validate_internal_job(parameter_bundle(parameter), request)
validate_internal_job(parameters, request)
except RequestParameterInvalidException as e:
raise AssertionError(f"Parameter {parameter} failed to validate internal job description {request}. {e}")
raise AssertionError(f"Parameters {parameters} failed to validate internal job description {request}. {e}")


def _assert_internal_job_invalid(parameter, request) -> None:
def _assert_internal_job_invalid(parameters: ToolParameterBundleModel, request: RawStateDict) -> None:
exc = None
try:
validate_internal_job(parameter_bundle(parameter), request)
validate_internal_job(parameters, request)
except RequestParameterInvalidException as e:
exc = e
assert (
exc is not None
), f"Parameter {parameter} didn't result in validation error on internal job description {request} as expected."
), f"Parameters {parameters} didn't result in validation error on internal job description {request} as expected."


def _assert_test_case_validates(parameter, test_case) -> None:
def _assert_test_case_validates(parameters: ToolParameterBundleModel, test_case: RawStateDict) -> None:
try:
validate_test_case(parameter_bundle(parameter), test_case)
validate_test_case(parameters, test_case)
except RequestParameterInvalidException as e:
raise AssertionError(f"Parameter {parameter} failed to validate test_case {test_case}. {e}")
raise AssertionError(f"Parameters {parameters} failed to validate test_case {test_case}. {e}")


def _assert_test_case_invalid(parameter, test_case) -> None:
def _assert_test_case_invalid(parameters: ToolParameterBundleModel, test_case: RawStateDict) -> None:
exc = None
try:
validate_test_case(parameter_bundle(parameter), test_case)
validate_test_case(parameters, test_case)
except RequestParameterInvalidException as e:
exc = e
assert (
exc is not None
), f"Parameter {parameter} didn't result in validation error on test_case {test_case} as expected."
), f"Parameters {parameters} didn't result in validation error on test_case {test_case} as expected."


_assert_requests_validate = partial(_for_each, _assert_request_validates)
Expand Down Expand Up @@ -213,7 +213,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:
Expand Down

0 comments on commit cde98e7

Please sign in to comment.