Skip to content

Commit fa1490f

Browse files
authored
Attempt to fix metaschema caching (#576)
* Fix load_schema, which was incorrectly modifying the object from get_metaschema We're using custom cache to pre-load documents in schema_loader, but there are different documents with the same id, so for that to work, we can't have a shared index. * Add test for load_schema with cache
1 parent 6aa8a5a commit fa1490f

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

schema_salad/schema.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,14 @@ def load_schema(
249249

250250
metaschema_names, _metaschema_doc, metaschema_loader = get_metaschema()
251251
if cache is not None:
252-
metaschema_loader.cache.update(cache)
252+
# we want to replace some items in the cache, so we need to
253+
# make a new Loader with an empty index.
254+
for k, v in metaschema_loader.cache.items():
255+
if k not in cache:
256+
cache[k] = v
257+
metaschema_loader = Loader(
258+
ctx=metaschema_loader.ctx, cache=cache, session=metaschema_loader.session
259+
)
253260
schema_doc, schema_metadata = metaschema_loader.resolve_ref(schema_ref, "")
254261

255262
if not isinstance(schema_doc, MutableSequence):

schema_salad/tests/test_examples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class TestExp(Exception):
527527
except TestExp as e:
528528
assert str(e).endswith("frag.yml:3:3: Whoops"), e
529529
except Exception as exc:
530-
assert False, exc
530+
raise AssertionError("Unexpected exception" + str(exc))
531531

532532

533533
def test_cmap() -> None:

schema_salad/tests/test_misc.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from schema_salad.avro.schema import Names
22
from schema_salad.schema import load_schema
3+
from typing import Optional, Dict, Union
4+
from rdflib.graph import Graph
35

46
from .util import get_data
57

@@ -9,3 +11,32 @@ def test_misc() -> None:
911
assert path
1012
document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(path)
1113
assert isinstance(avsc_names, Names)
14+
15+
16+
def test_load_schema_cache() -> None:
17+
18+
schemaid = "http://commonwl.org/schema_salad/test/schema.yml"
19+
20+
path1 = get_data("tests/test_schema/misc_schema_v1.yml")
21+
assert path1
22+
23+
with open(path1, "rt") as f:
24+
cache1: Optional[Dict[str, Union[str, Graph, bool]]] = {schemaid: f.read()}
25+
26+
document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(
27+
schemaid, cache=cache1
28+
)
29+
assert isinstance(avsc_names, Names)
30+
assert "EmptyType" not in document_loader.vocab
31+
32+
path2 = get_data("tests/test_schema/misc_schema_v2.yml")
33+
assert path2
34+
35+
with open(path2, "rt") as f:
36+
cache2: Optional[Dict[str, Union[str, Graph, bool]]] = {schemaid: f.read()}
37+
38+
document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(
39+
schemaid, cache=cache2
40+
)
41+
assert isinstance(avsc_names, Names)
42+
assert "EmptyType" in document_loader.vocab
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
$base: "https://w3id.org/cwl/cwltest#"
2+
$graph:
3+
- name: TestCase
4+
type: record
5+
documentRoot: true
6+
fields:
7+
id:
8+
type: [int, string]
9+
jsonldPredicate:
10+
_type: "@id"
11+
identity: true
12+
label: string?
13+
doc: string?
14+
tags: string[]?
15+
tool:
16+
type: string
17+
jsonldPredicate:
18+
_type: "@id"
19+
job:
20+
type: string?
21+
jsonldPredicate:
22+
_type: "@id"
23+
should_fail: boolean?
24+
output:
25+
type: Any?
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
$base: "https://w3id.org/cwl/cwltest#"
2+
$graph:
3+
- name: TestCase
4+
type: record
5+
documentRoot: true
6+
fields:
7+
id:
8+
type: [int, string]
9+
jsonldPredicate:
10+
_type: "@id"
11+
identity: true
12+
label: string?
13+
doc: string?
14+
tags: string[]?
15+
tool:
16+
type: string
17+
jsonldPredicate:
18+
_type: "@id"
19+
job:
20+
type: string?
21+
jsonldPredicate:
22+
_type: "@id"
23+
should_fail: boolean?
24+
output:
25+
type: Any?
26+
27+
- name: EmptyType
28+
documentRoot: true
29+
type: record

0 commit comments

Comments
 (0)