Skip to content

Commit 7e16bdb

Browse files
Added format unit tests
1 parent 59bdb5a commit 7e16bdb

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
File renamed without changes.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ cwl-upgrader >= 1.2.3
22
packaging
33
rdflib
44
requests
5+
CacheControl
56
schema-salad >= 8.2, < 9

tests/test_format.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import xml.sax
2+
from typing import Optional
3+
4+
import requests
5+
from pytest import raises
6+
from rdflib import Graph
7+
from rdflib.plugins.parsers.notation3 import BadSyntax
8+
from schema_salad.exceptions import ValidationException
9+
from schema_salad.fetcher import DefaultFetcher
10+
11+
from cwl_utils.format import check_format
12+
13+
14+
def _create_file(format_: Optional[str] = None):
15+
obj = {
16+
"class": "File",
17+
"basename": "example.txt",
18+
"size": 23,
19+
"contents": "hoopla",
20+
"nameroot": "example",
21+
"nameext": "txt",
22+
}
23+
if format_:
24+
obj["format"] = format_
25+
return obj
26+
27+
28+
def _load_format(fetchurl: str):
29+
fetcher = DefaultFetcher({}, requests.Session())
30+
content = fetcher.fetch_text(fetchurl)
31+
graph = Graph()
32+
for fmt in ["xml", "turtle", "rdfa"]:
33+
try:
34+
graph.parse(data=content, format=fmt, publicID=str(fetchurl))
35+
break
36+
except (xml.sax.SAXParseException, TypeError, BadSyntax):
37+
pass
38+
return graph
39+
40+
41+
def test_check_format():
42+
check_format(
43+
actual_file=_create_file(format_="http://edamontology.org/format_2330"),
44+
input_formats="http://edamontology.org/format_2330",
45+
ontology=_load_format("http://edamontology.org/EDAM.owl"),
46+
)
47+
48+
49+
def test_check_format_no_format():
50+
with raises(ValidationException, match=r"File has no 'format' defined: .*"):
51+
check_format(
52+
actual_file=_create_file(),
53+
input_formats="http://edamontology.org/format_2330",
54+
ontology=_load_format("http://edamontology.org/EDAM.owl"),
55+
)
56+
57+
58+
def test_check_format_no_input_formats():
59+
with raises(ValidationException, match=r"File has an incompatible format: .*"):
60+
check_format(
61+
actual_file=_create_file(format_="http://edamontology.org/format_2330"),
62+
input_formats=[],
63+
ontology=Graph(),
64+
)
65+
66+
67+
def test_check_format_no_ontology():
68+
check_format(
69+
actual_file=_create_file(format_="http://edamontology.org/format_2330"),
70+
input_formats="http://edamontology.org/format_2330",
71+
ontology=Graph(),
72+
)

0 commit comments

Comments
 (0)