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 2a7e8e1 commit cb3ec1f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
23 changes: 9 additions & 14 deletions lib/galaxy/tool_util/unittest_utils/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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"
Expand Down
27 changes: 13 additions & 14 deletions test/unit/tool_util/test_parameter_specification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -90,31 +89,31 @@ 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}")


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."


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}")


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 (
Expand All @@ -124,15 +123,15 @@ 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}")


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 (
Expand All @@ -142,15 +141,15 @@ 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}")


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 (
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit cb3ec1f

Please sign in to comment.