Skip to content

Commit fd0c0df

Browse files
Added format unit tests
1 parent 59bdb5a commit fd0c0df

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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(
35+
data=content, format=fmt, publicID=str(fetchurl)
36+
)
37+
break
38+
except (xml.sax.SAXParseException, TypeError, BadSyntax):
39+
pass
40+
return graph
41+
42+
43+
def test_check_format():
44+
check_format(
45+
actual_file=_create_file(
46+
format_='http://edamontology.org/format_2330'),
47+
input_formats="http://edamontology.org/format_2330",
48+
ontology=_load_format('http://edamontology.org/EDAM.owl'))
49+
50+
51+
def test_check_format_no_format():
52+
with raises(ValidationException, match=r"File has no 'format' defined: .*"):
53+
check_format(
54+
actual_file=_create_file(),
55+
input_formats="http://edamontology.org/format_2330",
56+
ontology=_load_format('http://edamontology.org/EDAM.owl'))
57+
58+
59+
def test_check_format_no_input_formats():
60+
with raises(ValidationException, match=r"File has an incompatible format: .*"):
61+
check_format(
62+
actual_file=_create_file(
63+
format_='http://edamontology.org/format_2330'),
64+
input_formats=[],
65+
ontology=Graph())
66+
67+
68+
def test_check_format_no_ontology():
69+
check_format(
70+
actual_file=_create_file(
71+
format_='http://edamontology.org/format_2330'),
72+
input_formats="http://edamontology.org/format_2330",
73+
ontology=Graph())

0 commit comments

Comments
 (0)