Skip to content

Commit a62cb46

Browse files
committed
test jsonschema with bad inputs
1 parent 46387d6 commit a62cb46

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

testdata/echo-tool.cwl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env cwl-runner
2+
3+
class: CommandLineTool
4+
cwlVersion: v1.2
5+
inputs:
6+
in:
7+
type: Any
8+
inputBinding: {}
9+
outputs:
10+
out:
11+
type: string
12+
outputBinding:
13+
glob: out.txt
14+
loadContents: true
15+
outputEval: $(self[0].contents)
16+
baseCommand: echo
17+
stdout: out.txt

testdata/empty.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"in": null
3+
}

testdata/null-expression1-job.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"i1": null
3+
}

testdata/null-expression2-tool.cwl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env cwl-runner
2+
3+
class: ExpressionTool
4+
requirements:
5+
- class: InlineJavascriptRequirement
6+
cwlVersion: v1.2
7+
8+
inputs:
9+
i1: Any
10+
11+
outputs:
12+
output: int
13+
14+
expression: "$({'output': (inputs.i1 == 'the-default' ? 1 : 2)})"

tests/test_inputs_schema_gen.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_cwl_inputs_to_jsonschema(tool_path: Path, inputs_path: Path) -> None:
7373
raise SchemaError(f"{inputs_path.name} failed schema validation") from err
7474

7575

76-
def test_cwl_inputs_to_jsonschema_fails() -> None:
76+
def test_cwl_inputs_to_jsonschema_single_fail() -> None:
7777
"""Compare tool schema of param 1 against input schema of param 2."""
7878
tool_path: Path = TEST_PARAMS[0][0]
7979
inputs_path: Path = TEST_PARAMS[3][1]
@@ -92,3 +92,54 @@ def test_cwl_inputs_to_jsonschema_fails() -> None:
9292
# We expect this to fail
9393
with pytest.raises(ValidationError):
9494
validate(input_obj, json_schema)
95+
96+
97+
BAD_TEST_PARAMS = [
98+
# Any without defaults cannot be unspecified
99+
(
100+
get_path("testdata/null-expression2-tool.cwl"),
101+
get_path("testdata/empty.json"),
102+
"'i1' is a required property",
103+
),
104+
# null to Any type without a default value.
105+
(
106+
get_path("testdata/null-expression2-tool.cwl"),
107+
get_path("testdata/null-expression1-job.json"),
108+
"None is not valid under any of the given schemas",
109+
),
110+
# Any without defaults, unspecified
111+
(
112+
get_path("testdata/echo-tool.cwl"),
113+
get_path("testdata/null-expression-echo-job.json"),
114+
"None is not valid under any of the given schemas",
115+
),
116+
# JSON null provided for required input
117+
(
118+
get_path("testdata/echo-tool.cwl"),
119+
get_path("testdata/null-expression1-job.json"),
120+
"'in' is a required property",
121+
),
122+
]
123+
124+
125+
@pytest.mark.parametrize("tool_path,inputs_path,exception_message", BAD_TEST_PARAMS)
126+
def test_cwl_inputs_to_jsonschema_fails(
127+
tool_path: Path,
128+
inputs_path: Path,
129+
exception_message: str,
130+
) -> None:
131+
cwl_obj = load_document_by_uri(tool_path.as_uri())
132+
133+
logger.info(f"Generating schema for {tool_path.name}")
134+
json_schema = cwl_to_jsonschema(cwl_obj)
135+
136+
logger.info(
137+
f"Testing {inputs_path.name} against schema generated for input {tool_path.name}"
138+
)
139+
140+
yaml = YAML()
141+
142+
input_obj = yaml.load(inputs_path)
143+
144+
with pytest.raises(ValidationError, match=exception_message):
145+
validate(input_obj, json_schema)

0 commit comments

Comments
 (0)