Skip to content

Commit f13d30e

Browse files
committed
tests: add a Pathlib.Path test data retrieval method
1 parent 964fa75 commit f13d30e

21 files changed

+156
-281
lines changed

schema_salad/tests/test_avro_names.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
def test_avro_loading() -> None:
1010
"""Confirm conversion of SALAD style names to avro."""
1111
path = get_data("tests/test_schema/avro_naming.yml")
12-
assert path
1312
document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(path)
1413
assert isinstance(avsc_names, Names)
1514
assert avsc_names.get_name("com.example.derived_schema.ExtendedThing", None)

schema_salad/tests/test_cg.py

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import json
2-
import os
32
from typing import Any
43

54
import pytest
65

76
import schema_salad.metaschema as cg_metaschema
87
from schema_salad.exceptions import ValidationException
9-
from schema_salad.ref_resolver import file_uri
108
from schema_salad.utils import yaml_no_ts
119

1210
from .matcher import JsonDiffMatcher
13-
from .util import get_data
11+
from .util import get_data_uri, get_path
1412

1513

1614
def test_load() -> None:
@@ -45,12 +43,11 @@ def test_err() -> None:
4543

4644
def test_include() -> None:
4745
doc = {"name": "hello", "doc": [{"$include": "hello.txt"}], "type": "documentation"}
48-
path = get_data("tests/_")
49-
assert path
46+
path_uri = get_data_uri("tests") + "/_"
5047
rf = cg_metaschema.Documentation.fromDoc(
5148
doc,
5249
"http://example.com/",
53-
cg_metaschema.LoadingOptions(fileuri=file_uri(path), no_link_check=True),
50+
cg_metaschema.LoadingOptions(fileuri=path_uri, no_link_check=True),
5451
)
5552
assert "http://example.com/#hello" == rf.name
5653
assert ["hello world!\n"] == rf.doc
@@ -64,9 +61,7 @@ def test_include() -> None:
6461

6562
def test_import() -> None:
6663
doc = {"type": "record", "fields": [{"$import": "hellofield.yml"}]}
67-
tests_path = get_data("tests")
68-
assert tests_path
69-
lead = file_uri(os.path.normpath(tests_path))
64+
lead = get_data_uri("tests")
7065
rs = cg_metaschema.RecordSchema.fromDoc(
7166
doc, "http://example.com/", cg_metaschema.LoadingOptions(fileuri=lead + "/_")
7267
)
@@ -87,11 +82,9 @@ def test_import() -> None:
8782

8883

8984
def test_import2() -> None:
90-
path = get_data("tests/docimp/d1.yml")
91-
assert path
92-
rs = cg_metaschema.load_document(file_uri(path), "", cg_metaschema.LoadingOptions())
93-
path2 = get_data("tests/docimp/d1.yml")
94-
assert path2
85+
path_uri = get_data_uri("tests/docimp/d1.yml")
86+
rs = cg_metaschema.load_document(path_uri, "", cg_metaschema.LoadingOptions())
87+
path2_uri = get_data_uri("tests/docimp/d1.yml")
9588
assert [
9689
{
9790
"doc": [
@@ -104,7 +97,7 @@ def test_import2() -> None:
10497
"hello 5",
10598
],
10699
"type": "documentation",
107-
"name": file_uri(path2) + "#Semantic_Annotations_for_Linked_Avro_Data",
100+
"name": path2_uri + "#Semantic_Annotations_for_Linked_Avro_Data",
108101
}
109102
] == [r.save() for r in rs]
110103

@@ -158,10 +151,9 @@ def test_idmap2() -> None:
158151

159152

160153
def test_load_pt() -> None:
161-
path = get_data("tests/pt.yml")
162-
assert path
154+
path_uri = get_data_uri("tests/pt.yml")
163155
doc = cg_metaschema.load_document(
164-
file_uri(path), "", cg_metaschema.LoadingOptions(no_link_check=True)
156+
path_uri, "", cg_metaschema.LoadingOptions(no_link_check=True)
165157
)
166158
assert [
167159
"https://w3id.org/cwl/salad#null",
@@ -187,18 +179,15 @@ def test_shortname() -> None:
187179
@pytest.fixture
188180
def metaschema_pre() -> Any:
189181
"""Prep-parsed schema for testing."""
190-
path2 = get_data("tests/metaschema-pre.yml")
191-
assert path2
192-
with open(path2) as f:
182+
with get_path("tests/metaschema-pre.yml").open() as f:
193183
pre = json.load(f)
194184
return pre
195185

196186

197187
def test_load_metaschema(metaschema_pre: Any) -> None:
198-
path = get_data("metaschema/metaschema.yml")
199-
assert path
188+
path_uri = get_data_uri("metaschema/metaschema.yml")
200189
doc = cg_metaschema.load_document(
201-
file_uri(path),
190+
path_uri,
202191
"",
203192
cg_metaschema.LoadingOptions(no_link_check=True),
204193
)
@@ -209,33 +198,30 @@ def test_load_metaschema(metaschema_pre: Any) -> None:
209198

210199

211200
def test_load_by_yaml_metaschema(metaschema_pre: Any) -> None:
212-
path = get_data("metaschema/metaschema.yml")
213-
assert path
214-
with open(path) as path_handle:
201+
path = get_path("metaschema/metaschema.yml")
202+
with path.open() as path_handle:
215203
yaml = yaml_no_ts()
216204
yaml_doc = yaml.load(path_handle)
217205
doc = cg_metaschema.load_document_by_yaml(
218206
yaml_doc,
219-
file_uri(path),
207+
path.as_uri(),
220208
None,
221209
)
222210
saved = [d.save(relative_uris=False) for d in doc]
223211
assert saved == JsonDiffMatcher(metaschema_pre)
224212

225213

226214
def test_load_cwlschema() -> None:
227-
path = get_data("tests/test_schema/CommonWorkflowLanguage.yml")
228-
assert path
215+
path_uri = get_data_uri("tests/test_schema/CommonWorkflowLanguage.yml")
229216
doc = cg_metaschema.load_document(
230-
file_uri(path),
217+
path_uri,
231218
"",
232219
cg_metaschema.LoadingOptions(no_link_check=True),
233220
)
234-
path2 = get_data("tests/cwl-pre.yml")
235-
assert path2
221+
path2 = get_path("tests/cwl-pre.yml")
236222
saved = [d.save(relative_uris=False) for d in doc]
237-
# with open(path2, "w") as f:
223+
# with path2.open("w") as f:
238224
# json.dump(saved, f, indent=2)
239-
with open(path2) as f:
225+
with path2.open() as f:
240226
pre = json.load(f)
241227
assert saved == JsonDiffMatcher(pre)

schema_salad/tests/test_codegen_errors.py

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import importlib
44
from collections.abc import MutableSequence
55
from pathlib import Path
6-
from typing import Any, Optional, Union, cast
7-
from urllib.parse import urlparse
6+
from typing import Any, Optional, cast
87

98
import pytest
109

@@ -14,7 +13,7 @@
1413
from schema_salad.schema import load_schema
1514
from schema_salad.utils import yaml_no_ts
1615

17-
from .util import cwl_file_uri, get_data
16+
from .util import cwl_file_uri, get_path
1817

1918

2019
def test_error_message1(tmp_path: Path) -> None:
@@ -23,17 +22,17 @@ def test_error_message1(tmp_path: Path) -> None:
2322
\s+\*\s+missing\s+required\s+field\s+`inputs`
2423
\s+\*\s+missing\s+required\s+field\s+`outputs`
2524
\s+\*\s+missing\s+required\s+field\s+`steps`"""
26-
path = get_data("tests/" + t)
27-
assert path
25+
path = get_path("tests/" + t)
26+
assert path.exists()
2827
with pytest.raises(ValidationException, match=match):
2928
load_document_by_uri(tmp_path, path)
3029

3130

3231
def test_error_message2(tmp_path: Path) -> None:
3332
t = "test_schema/test2.cwl"
3433
match = r"""^.*test2\.cwl:2:1:\s+Field\s+`class`\s+contains\s+undefined\s+reference\s+to\s+`file://.+/schema_salad/tests/test_schema/xWorkflow`$"""
35-
path = get_data("tests/" + t)
36-
assert path
34+
path = get_path("tests/" + t)
35+
assert path.exists()
3736
with pytest.raises(ValidationException, match=match):
3837
load_document_by_uri(tmp_path, path)
3938

@@ -46,8 +45,8 @@ def test_error_message4(tmp_path: Path) -> None:
4645
.*test4\.cwl:7:3:\s+checking\s+object\s+`.*test4\.cwl#bar`\s+using\s+`WorkflowOutputParameter`
4746
\s+the\s+`type`\s+field\s+is\s+not\s+valid\s+because:
4847
\s+Value\s+`12`\s+is\s+a\s+int,\s+but\s+valid\s+types\s+for\s+this\s+field\s+are\s+\((str|object),\s+(str|object)\)"""
49-
path = get_data("tests/" + t)
50-
assert path
48+
path = get_path("tests/" + t)
49+
assert path.exists()
5150
with pytest.raises(ValidationException, match=match):
5251
load_document_by_uri(tmp_path, path)
5352

@@ -58,8 +57,8 @@ def test_error_message5(tmp_path: Path) -> None:
5857
.+test5\.cwl:8:1:\s+the\s+`steps`\s+field\s+is\s+not\s+valid\s+because:
5958
.+test5\.cwl:8:9:\s+array\s+item\s+is\s+invalid\s+because
6059
\s+Value\s+is\s+a\s+int,\s+but\s+valid\s+type\s+for\s+this\s+field\s+is\s+an\s+object"""
61-
path = get_data("tests/" + t)
62-
assert path
60+
path = get_path("tests/" + t)
61+
assert path.exists()
6362
with pytest.raises(ValidationException, match=match):
6463
load_document_by_uri(tmp_path, path)
6564

@@ -72,8 +71,8 @@ def test_error_message6(tmp_path: Path) -> None:
7271
\s+missing\s+required\s+field\s+`class`
7372
+\*\s+tried\s+`Workflow`\s+but
7473
\s+missing\s+required\s+field\s+`class`"""
75-
path = get_data("tests/" + t)
76-
assert path
74+
path = get_path("tests/" + t)
75+
assert path.exists()
7776
with pytest.raises(ValidationException, match=match):
7877
load_document_by_uri(tmp_path, path)
7978

@@ -86,8 +85,8 @@ def test_error_message7(tmp_path: Path) -> None:
8685
.*test7\.cwl:9:3:\s+checking\s+object\s+`.*test7.cwl#step1`\s+using\s+`WorkflowStep`
8786
\s+\*\s+missing\s+required\s+field\s+`run`
8887
.*test7\.cwl:10:5:\s+\*\s+invalid\s+field\s+`scatter_method`,\s+expected\s+one\s+of:\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*$"""
89-
path = get_data("tests/" + t)
90-
assert path
88+
path = get_path("tests/" + t)
89+
assert path.exists()
9190
with pytest.raises(ValidationException, match=match):
9291
load_document_by_uri(tmp_path, path)
9392

@@ -101,8 +100,8 @@ def test_error_message8(tmp_path: Path) -> None:
101100
\s+\*\s+missing\s+required\s+field\s+`run`
102101
.*test8\.cwl:10:5:\s+\*\s+the\s+`scatterMethod`\s+field\s+is\s+not\s+valid\s+because:
103102
\s+contains\s+undefined\s+reference\s+to\s+`file:///.*/tests/test_schema/abc`$"""
104-
path = get_data("tests/" + t)
105-
assert path
103+
path = get_path("tests/" + t)
104+
assert path.exists()
106105
with pytest.raises(ValidationException, match=match):
107106
load_document_by_uri(tmp_path, path)
108107

@@ -116,8 +115,8 @@ def test_error_message9(tmp_path: Path) -> None:
116115
\s+\*\s+missing\s+required\s+field\s+`run`
117116
.*test9\.cwl:10:5:\s+\*\s+the\s+`scatterMethod`\s+field\s+is\s+not\s+valid\s+because:
118117
\s+Value\s+`12`\s+is\s+a\s+int,\s+but\s+valid\s+values\s+for\s+this\s+field\s+are\s+\("(dotproduct|nested_crossproduct|flat_crossproduct)",\s+"(dotproduct|nested_crossproduct|flat_crossproduct)",\s+"(dotproduct|nested_crossproduct|flat_crossproduct)"\)"""
119-
path = get_data("tests/" + t)
120-
assert path
118+
path = get_path("tests/" + t)
119+
assert path.exists()
121120
with pytest.raises(ValidationException, match=match):
122121
load_document_by_uri(tmp_path, path)
123122

@@ -131,8 +130,8 @@ def test_error_message10(tmp_path: Path) -> None:
131130
\s+\*\s+missing\s+required\s+field\s+`run`
132131
.*test10\.cwl:10:5:\s+\*\s+the\s+`scatterMethod`\s+field\s+is\s+not\s+valid\s+because:
133132
\s+Value\s+is\s+a\s+array,\s+but\s+valid\s+types\s+for\s+this\s+field\s+are\s+\("dotproduct|nested_crossproduct|flat_crossproduct",\s+"dotproduct|nested_crossproduct|flat_crossproduct",\s+"dotproduct|nested_crossproduct|flat_crossproduct"\)"""
134-
path = get_data("tests/" + t)
135-
assert path
133+
path = get_path("tests/" + t)
134+
assert path.exists()
136135
with pytest.raises(ValidationException, match=match):
137136
load_document_by_uri(tmp_path, path)
138137

@@ -144,8 +143,8 @@ def test_error_message11(tmp_path: Path) -> None:
144143
\s+array\s+item\s+is\s+invalid\s+because
145144
.*test11\.cwl:9:3:\s+checking\s+object\s+`.*test11\.cwl#step1`\s+using\s+`WorkflowStep`
146145
.*test11\.cwl:10:5:\s+the\s+`run`\s+field\s+is\s+not\s+valid\s+because:\s+contains\s+undefined\s+reference\s+to\s+`file:///.*/tests/test_schema/blub\.cwl`$"""
147-
path = get_data("tests/" + t)
148-
assert path
146+
path = get_path("tests/" + t)
147+
assert path.exists()
149148
with pytest.raises(ValidationException, match=match):
150149
load_document_by_uri(tmp_path, path)
151150

@@ -161,13 +160,13 @@ def test_error_message15(tmp_path: Path) -> None:
161160
\s+tried\s+`CommandLineBinding`\s+but
162161
.*test15\.cwl:11:7:\s+\*\s+invalid\s+field\s+`invalid_field`,\s+expected\s+one\s+of:\s+`loadContents`,\s+`position`,\s+`prefix`,\s+`separate`,\s+`itemSeparator`,\s+`valueFrom`,\s+`shellQuote`
163162
.*test15\.cwl:12:7:\s+\*\s+invalid\s+field\s+`another_invalid_field`,\s+expected one\s+of:\s+`loadContents`,\s+`position`,\s+`prefix`,\s+`separate`,\s+`itemSeparator`,\s+`valueFrom`,\s+`shellQuote`$"""
164-
path = get_data("tests/" + t)
165-
assert path
163+
path = get_path("tests/" + t)
164+
assert path.exists()
166165
with pytest.raises(ValidationException, match=match):
167166
load_document_by_uri(tmp_path, path)
168167

169168

170-
def load_document_by_uri(tmp_path: Path, path: Union[str, Path]) -> Any:
169+
def load_document_by_uri(tmp_path: Path, path: Path) -> Any:
171170
src_target = tmp_path / "cwl_v1_0.py"
172171
python_codegen(cwl_file_uri, src_target)
173172
spec = importlib.util.spec_from_file_location("cwl_v1_0", src_target)
@@ -177,22 +176,15 @@ def load_document_by_uri(tmp_path: Path, path: Union[str, Path]) -> Any:
177176
spec.loader.exec_module(temp_cwl_v1_0)
178177
cwl_v1_0: Any = temp_cwl_v1_0
179178

180-
if isinstance(path, str):
181-
uri = urlparse(path)
182-
if not uri.scheme or uri.scheme == "file":
183-
real_path = Path(uri.path).resolve().as_uri()
184-
else:
185-
real_path = path
186-
else:
187-
real_path = path.resolve().as_uri()
179+
path_uri = path.resolve().as_uri()
188180

189-
baseuri = str(real_path)
181+
baseuri = path_uri
190182

191183
loadingOptions = cwl_v1_0.LoadingOptions(fileuri=baseuri)
192184

193185
with open(path) as file:
194186
doc = file.read()
195-
# doc = loadingOptions.fetcher.fetch_text(urllib.parse.unquote(str(real_path)))
187+
# doc = loadingOptions.fetcher.fetch_text(urllib.parse.unquote(path_uri))
196188
yaml = yaml_no_ts()
197189
doc = yaml.load(doc)
198190

schema_salad/tests/test_cpp_codegen.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
from schema_salad.avro.schema import Names
1212
from schema_salad.schema import load_schema
1313

14-
from .util import cwl_file_uri, get_data
14+
from .util import cwl_file_uri, get_data_uri, get_path
1515

1616

1717
def test_cwl_cpp_gen(tmp_path: Path) -> None:
1818
"""End to end test of C++ generator using the CWL v1.0 schema."""
1919
src_target = tmp_path / "cwl_v1_0.h"
2020
cpp_codegen(cwl_file_uri, src_target)
21-
source = get_data("tests/codegen/cwl.cpp")
22-
assert source
2321
assert os.path.exists(src_target)
2422

2523

@@ -36,7 +34,7 @@ def test_cwl_cpp_gen(tmp_path: Path) -> None:
3634
def test_cwl_cpp_generations(tmp_path: Path, filename: str) -> None:
3735
"""End to end test of C++ generator using small scenarios."""
3836

39-
file = Path(cast(str, get_data(f"cpp_tests/{filename}")))
37+
file = get_path(f"cpp_tests/{filename}")
4038

4139
# file with generated cpp output
4240
src_target = tmp_path / "test.h"
@@ -55,18 +53,18 @@ def test_cwl_cpp_generations_with_spdx(tmp_path: Path) -> None:
5553

5654
src_target = tmp_path / "test.h"
5755

58-
input_file = cast(str, get_data("cpp_tests/01_single_record.yml"))
56+
input_file_uri = get_data_uri("cpp_tests/01_single_record.yml")
5957

6058
"""Generating different combinations of license headers"""
6159
"""Generate License Identifier"""
62-
cpp_codegen("file://" + input_file, src_target, spdx_license_identifier="Apache-2.0")
60+
cpp_codegen(input_file_uri, src_target, spdx_license_identifier="Apache-2.0")
6361
lines = open(src_target).readlines()[0:2]
6462
assert lines[0] == "// SPDX-License-Identifier: Apache-2.0\n"
6563
assert lines[1] == "#pragma once\n"
6664

6765
"""Generate single CopyrightText"""
6866
cpp_codegen(
69-
"file://" + input_file,
67+
input_file_uri,
7068
src_target,
7169
spdx_copyright_text=["Copyright 2016 Some People <[email protected]>"],
7270
)
@@ -76,7 +74,7 @@ def test_cwl_cpp_generations_with_spdx(tmp_path: Path) -> None:
7674

7775
"""Generate two CopyrightText entries"""
7876
cpp_codegen(
79-
"file://" + input_file,
77+
input_file_uri,
8078
src_target,
8179
spdx_copyright_text=[
8280
"Copyright 2016 Person A <[email protected]>",
@@ -90,7 +88,7 @@ def test_cwl_cpp_generations_with_spdx(tmp_path: Path) -> None:
9088

9189
"""Generate CopyrightText and License Identifier"""
9290
cpp_codegen(
93-
"file://" + input_file,
91+
input_file_uri,
9492
src_target,
9593
spdx_license_identifier="Apache-2.0",
9694
spdx_copyright_text=[

0 commit comments

Comments
 (0)