|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import ( |
| 3 | + Any, |
| 4 | + Dict, |
| 5 | + List, |
| 6 | +) |
| 7 | + |
| 8 | +from .models import ( |
| 9 | + DataCollectionParameterModel, |
| 10 | + DataParameterModel, |
| 11 | + FloatParameterModel, |
| 12 | + IntegerParameterModel, |
| 13 | + parameters_by_name, |
| 14 | + ToolParameterBundle, |
| 15 | + ToolParameterT, |
| 16 | +) |
| 17 | +from .state import TestCaseToolState |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class TestCaseStateAndWarnings: |
| 22 | + tool_state: TestCaseToolState |
| 23 | + warnings: List[str] |
| 24 | + |
| 25 | + |
| 26 | +def legacy_from_string(parameter: ToolParameterT, value: str, warnings: List[str], profile: str) -> Any: |
| 27 | + """Convert string values in XML test cases into typed variants. |
| 28 | +
|
| 29 | + This should only be used when parsing XML test cases into a TestCaseToolState object. |
| 30 | + We have to maintain backward compatibility on these for older Galaxy tool profile versions. |
| 31 | + """ |
| 32 | + is_string = isinstance(value, str) |
| 33 | + result_value: Any = value |
| 34 | + if is_string and isinstance(parameter, (IntegerParameterModel,)): |
| 35 | + warnings.append( |
| 36 | + f"Implicitly converted {parameter.name} to an integer from a string value, please use 'value_json' to define this test input parameter value instead." |
| 37 | + ) |
| 38 | + result_value = int(value) |
| 39 | + elif is_string and isinstance(parameter, (FloatParameterModel,)): |
| 40 | + warnings.append( |
| 41 | + f"Implicitly converted {parameter.name} to a floating point number from a string value, please use 'value_json' to define this test input parameter value instead." |
| 42 | + ) |
| 43 | + result_value = float(value) |
| 44 | + return result_value |
| 45 | + |
| 46 | + |
| 47 | +def test_case_state( |
| 48 | + test_dict: Dict[str, Any], tool_parameter_bundle: ToolParameterBundle, profile: str |
| 49 | +) -> TestCaseStateAndWarnings: |
| 50 | + warnings: List[str] = [] |
| 51 | + inputs = test_dict["inputs"] |
| 52 | + state = {} |
| 53 | + by_name = parameters_by_name(tool_parameter_bundle) |
| 54 | + for input in inputs: |
| 55 | + input_name = input["name"] |
| 56 | + if input_name not in by_name: |
| 57 | + raise Exception(f"Cannot find tool parameter for {input_name}") |
| 58 | + tool_parameter_model = by_name[input_name] |
| 59 | + input_value = input["value"] |
| 60 | + input_value = legacy_from_string(tool_parameter_model, input_value, warnings, profile) |
| 61 | + if isinstance(tool_parameter_model, (DataParameterModel,)): |
| 62 | + pass |
| 63 | + elif isinstance(tool_parameter_model, (DataCollectionParameterModel,)): |
| 64 | + pass |
| 65 | + |
| 66 | + state[input_name] = input_value |
| 67 | + |
| 68 | + tool_state = TestCaseToolState(state) |
| 69 | + tool_state.validate(tool_parameter_bundle) |
| 70 | + return TestCaseStateAndWarnings(tool_state, warnings) |
0 commit comments