Skip to content

Commit 8c5b6e9

Browse files
authored
Merge branch 'main' into fix_yamlutils_as_json_object_inject_type
2 parents 2772b8c + 771ff41 commit 8c5b6e9

File tree

11 files changed

+537
-589
lines changed

11 files changed

+537
-589
lines changed
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import unittest
2-
31
from linkml_runtime.linkml_model.meta import SchemaDefinition
42
from linkml_runtime.loaders import yaml_loader
53
from tests.test_issues.environment import env
64

75
# https://github.com/linkml/linkml/issues/3
86

97

10-
class IncludeSchemaTestCase(unittest.TestCase):
11-
"""include_schema.yaml produces a Python exception on an uncaught error"""
12-
13-
# "Awaiting fix for issue #3"
14-
def test_include_schema(self):
15-
inp = yaml_loader.load(env.input_path("include_schema.yaml"), SchemaDefinition)
8+
def test_include_schema():
9+
"""
10+
Test for GitHub issue #3: include_schema.yaml produces a Python exception on an uncaught error
1611
12+
This is a regression test to ensure schema inclusion works without throwing exceptions.
13+
"""
14+
# Load schema file - this should not raise an exception
15+
inp = yaml_loader.load(env.input_path("include_schema.yaml"), SchemaDefinition)
1716

18-
if __name__ == "__main__":
19-
unittest.main()
17+
# Verify the schema was loaded successfully
18+
assert inp is not None
19+
assert isinstance(inp, SchemaDefinition)
Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import unittest
32

43
import yaml
54

@@ -8,31 +7,34 @@
87
from tests.test_loaders_dumpers.models.enum_model import Organism, StateEnum
98

109

11-
class EnumTestCase(unittest.TestCase):
12-
def test_enum(self):
13-
"""
14-
Tests that enums are encoded as json correctly
15-
16-
* https://github.com/linkml/linkml/issues/337
17-
* https://github.com/linkml/linkml/issues/119
18-
"""
19-
i = Organism(state="LIVING")
20-
print(i)
21-
print(i.state)
22-
print(i.state.code)
23-
print(i.state.code.text)
24-
print(type(i.state))
25-
print(StateEnum.LIVING)
26-
assert str(i.state) == "LIVING"
27-
assert i.state.code == StateEnum.LIVING
28-
obj = json.loads(json_dumper.dumps(i))
29-
assert obj["state"] == "LIVING"
30-
obj = yaml.safe_load(yaml_dumper.dumps(i))
31-
assert obj["state"] == "LIVING"
32-
reconstituted = json_loader.loads(json_dumper.dumps(i), target_class=Organism)
33-
print(f"RECONSTITUTED = {reconstituted}")
34-
assert reconstituted.state.code == StateEnum.LIVING
35-
36-
37-
if __name__ == "__main__":
38-
unittest.main()
10+
def test_enum():
11+
"""
12+
Tests that enums are encoded as json correctly
13+
14+
* https://github.com/linkml/linkml/issues/337
15+
* https://github.com/linkml/linkml/issues/119
16+
"""
17+
i = Organism(state="LIVING")
18+
print(i)
19+
print(i.state)
20+
print(i.state.code)
21+
print(i.state.code.text)
22+
print(type(i.state))
23+
print(StateEnum.LIVING)
24+
25+
# Test string representation and enum code
26+
assert str(i.state) == "LIVING"
27+
assert i.state.code == StateEnum.LIVING
28+
29+
# Test JSON dumping/loading
30+
obj = json.loads(json_dumper.dumps(i))
31+
assert obj["state"] == "LIVING"
32+
33+
# Test YAML dumping
34+
obj = yaml.safe_load(yaml_dumper.dumps(i))
35+
assert obj["state"] == "LIVING"
36+
37+
# Test round-trip JSON serialization
38+
reconstituted = json_loader.loads(json_dumper.dumps(i), target_class=Organism)
39+
print(f"RECONSTITUTED = {reconstituted}")
40+
assert reconstituted.state.code == StateEnum.LIVING
Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
import unittest
2-
1+
import pytest
32
from rdflib import URIRef
43

54
from linkml_runtime.utils.curienamespace import CurieNamespace
65

76

8-
class CurieNamespaceTestCase(unittest.TestCase):
9-
def test_basics(self):
10-
BFO = CurieNamespace("bfo", "http://purl.obolibrary.org/obo/BFO_")
11-
self.assertEqual(URIRef("http://purl.obolibrary.org/obo/BFO_test"), BFO.test)
12-
self.assertEqual("http://purl.obolibrary.org/obo/BFO_", BFO)
13-
self.assertEqual("bfo:test", BFO.curie("test"))
14-
self.assertEqual("bfo:", BFO.curie())
15-
16-
@unittest.expectedFailure
17-
def test_curie_as_curie(self):
18-
""" "curie can't be a local name at the moment" """
19-
BFO = CurieNamespace("bfo", "http://purl.obolibrary.org/obo/BFO_")
20-
self.assertEqual("bfo:curie", BFO.curie)
7+
def test_basics():
8+
BFO = CurieNamespace("bfo", "http://purl.obolibrary.org/obo/BFO_")
9+
assert URIRef("http://purl.obolibrary.org/obo/BFO_test") == BFO.test
10+
assert "http://purl.obolibrary.org/obo/BFO_" == BFO
11+
assert "bfo:test" == BFO.curie("test")
12+
assert "bfo:" == BFO.curie()
2113

2214

23-
if __name__ == "__main__":
24-
unittest.main()
15+
@pytest.mark.xfail(reason="curie can't be a local name at the moment")
16+
def test_curie_as_curie():
17+
"""curie can't be a local name at the moment"""
18+
BFO = CurieNamespace("bfo", "http://purl.obolibrary.org/obo/BFO_")
19+
assert "bfo:curie" == BFO.curie
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
import unittest
2-
31
from linkml_runtime.utils.distroutils import get_jsonschema_string, get_schema_string
42

53

6-
class DistroUtilsTestCase(unittest.TestCase):
7-
def test_distroutils(self):
8-
p = "linkml_runtime.linkml_model.meta"
9-
js = get_jsonschema_string(p)
10-
assert "ClassDefinition" in js
11-
ys = get_schema_string(p)
12-
assert "class_definition" in ys
13-
14-
15-
if __name__ == "__main__":
16-
unittest.main()
4+
def test_distroutils():
5+
p = "linkml_runtime.linkml_model.meta"
6+
js = get_jsonschema_string(p)
7+
assert "ClassDefinition" in js
8+
ys = get_schema_string(p)
9+
assert "class_definition" in ys
Lines changed: 82 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import unittest
32
from typing import Any
43

54
from jsonasobj2 import JsonObj, as_json
@@ -99,105 +98,89 @@
9998
"""
10099

101100

102-
class FormatUtilsTestCase(unittest.TestCase):
103-
def test_formats(self):
104-
self.assertEqual("ThisIsIt", camelcase("this is it"))
105-
self.assertEqual("ThisIsIT", camelcase(" this is iT "))
106-
self.assertEqual("un camelcased", uncamelcase("UnCamelcased"))
107-
self.assertEqual("oneword", uncamelcase("Oneword"))
108-
self.assertEqual("one_word", uncamelcase("one_word"))
109-
self.assertEqual("another word", uncamelcase("anotherWord"))
110-
self.assertEqual("IBeY", camelcase("i be y "))
111-
self.assertEqual("ThisIsIt", camelcase("This__is_it"))
112-
113-
self.assertEqual("this_is_it", underscore(" this is it "))
114-
self.assertEqual("this_is_it", underscore("this is it"))
115-
116-
self.assertEqual("thisIsIt", lcamelcase(" this is\t it\n"))
117-
118-
self.assertEqual("abc", be(" abc\n"))
119-
self.assertEqual("", be(None))
120-
self.assertEqual("", be(" "))
121-
122-
def test_linestuff(self):
123-
text = (
124-
"This is a mess'o test that goes on for a long way. It has some carriage\n returns embedded in it "
125-
"but otherwise it drags on and on and on until the cows come home. Splitline covers this we hope."
126-
)
127-
self.assertEqual(
128-
[
129-
"This is a mess'o test that goes on for a long way. It has some carriage returns embedded"
130-
" in it but otherwise it ",
131-
"drags on and on and on until the cows come home. Splitline covers this we hope. ",
132-
],
133-
split_line(text),
134-
)
135-
self.assertEqual(
136-
[
137-
"This is a mess'o ",
138-
"test that goes on ",
139-
"for a long way. It ",
140-
"has some carriage ",
141-
"returns embedded in ",
142-
"it but otherwise it ",
143-
"drags on and on and ",
144-
"on until the cows ",
145-
"come home. ",
146-
"Splitline covers ",
147-
"this we hope. ",
148-
],
149-
split_line(text, 20),
150-
)
151-
self.assertEqual(["X" * 100 + " "], split_line("X" * 100, 20))
152-
self.assertEqual(
153-
(
154-
"This is a mess'o test that goes on for a long way. It has some carriage\n"
155-
"\treturns embedded in it but otherwise it drags on and on and on until the "
156-
"cows come home. Splitline covers this we \n"
157-
"\thope. "
158-
),
159-
wrapped_annotation(text),
160-
)
161-
162-
def test_empty_functions(self):
163-
"""Test the various forms of is_empty"""
164-
for thing in empty_things:
165-
self.assertTrue(is_empty(thing), msg=f"{thing} should clock in as empty")
166-
for thing in non_empty_things:
167-
self.assertFalse(is_empty(thing))
168-
obj = JsonObj([])
169-
assert is_empty(obj)
170-
171-
def test_remove_empty_items(self):
172-
"""Test the various remove empty items paths"""
173-
seen = set()
174-
save = list() # Keep garbage collection from reusing ids
175-
for thing, expected in things_removed:
176-
actual = remove_empty_items(thing)
177-
self.assertEqual(expected, actual, msg=f"Input = {thing}")
178-
self.assertFalse(isinstance(actual, JsonObj), msg="JSON objects are never returned")
179-
if isinstance(expected, (dict, list)):
180-
self.assertNotEqual(id(expected), id(actual), msg=f"Copy of {thing} was not returned")
181-
self.assertNotIn(id(actual), seen, msg="remove_empty_items should always return a new thing")
182-
save.append(actual)
183-
seen.add(id(actual))
184-
185-
def test_enumerations_case(self):
186-
self.assertEqual(
187-
"""[
101+
def test_formats():
102+
assert "ThisIsIt" == camelcase("this is it")
103+
assert "ThisIsIT" == camelcase(" this is iT ")
104+
assert "un camelcased" == uncamelcase("UnCamelcased")
105+
assert "oneword" == uncamelcase("Oneword")
106+
assert "one_word" == uncamelcase("one_word")
107+
assert "another word" == uncamelcase("anotherWord")
108+
assert "IBeY" == camelcase("i be y ")
109+
assert "ThisIsIt" == camelcase("This__is_it")
110+
111+
assert "this_is_it" == underscore(" this is it ")
112+
assert "this_is_it" == underscore("this is it")
113+
114+
assert "thisIsIt" == lcamelcase(" this is\t it\n")
115+
116+
assert "abc" == be(" abc\n")
117+
assert "" == be(None)
118+
assert "" == be(" ")
119+
120+
121+
def test_linestuff():
122+
text = (
123+
"This is a mess'o test that goes on for a long way. It has some carriage\n returns embedded in it "
124+
"but otherwise it drags on and on and on until the cows come home. Splitline covers this we hope."
125+
)
126+
assert [
127+
"This is a mess'o test that goes on for a long way. It has some carriage returns embedded"
128+
" in it but otherwise it ",
129+
"drags on and on and on until the cows come home. Splitline covers this we hope. ",
130+
] == split_line(text)
131+
assert [
132+
"This is a mess'o ",
133+
"test that goes on ",
134+
"for a long way. It ",
135+
"has some carriage ",
136+
"returns embedded in ",
137+
"it but otherwise it ",
138+
"drags on and on and ",
139+
"on until the cows ",
140+
"come home. ",
141+
"Splitline covers ",
142+
"this we hope. ",
143+
] == split_line(text, 20)
144+
assert ["X" * 100 + " "] == split_line("X" * 100, 20)
145+
assert (
146+
"This is a mess'o test that goes on for a long way. It has some carriage\n"
147+
"\treturns embedded in it but otherwise it drags on and on and on until the "
148+
"cows come home. Splitline covers this we \n"
149+
"\thope. "
150+
) == wrapped_annotation(text)
151+
152+
153+
def test_empty_functions():
154+
"""Test the various forms of is_empty"""
155+
for thing in empty_things:
156+
assert is_empty(thing), f"{thing} should clock in as empty"
157+
for thing in non_empty_things:
158+
assert not is_empty(thing)
159+
obj = JsonObj([])
160+
assert is_empty(obj)
161+
162+
163+
def test_remove_empty_items():
164+
"""Test the various remove empty items paths"""
165+
seen = set()
166+
save = list() # Keep garbage collection from reusing ids
167+
for thing, expected in things_removed:
168+
actual = remove_empty_items(thing)
169+
assert expected == actual, f"Input = {thing}"
170+
assert not isinstance(actual, JsonObj), "JSON objects are never returned"
171+
if isinstance(expected, (dict, list)):
172+
assert id(expected) != id(actual), f"Copy of {thing} was not returned"
173+
assert id(actual) not in seen, "remove_empty_items should always return a new thing"
174+
save.append(actual)
175+
seen.add(id(actual))
176+
177+
178+
def test_enumerations_case():
179+
assert """[
188180
"state",
189181
"1"
190-
]""",
191-
as_json(remove_empty_items(json.loads(issue_157_1), hide_protected_keys=True)),
192-
)
193-
self.assertEqual(
194-
"""[
182+
]""" == as_json(remove_empty_items(json.loads(issue_157_1), hide_protected_keys=True))
183+
assert """[
195184
"namedstate",
196185
"production"
197-
]""",
198-
as_json(remove_empty_items(json.loads(issue_157_2), hide_protected_keys=True)),
199-
)
200-
201-
202-
if __name__ == "__main__":
203-
unittest.main()
186+
]""" == as_json(remove_empty_items(json.loads(issue_157_2), hide_protected_keys=True))

tests/test_utils/test_list_strings.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import unittest
2-
31
from linkml_runtime.linkml_model.meta import SchemaDefinition
42
from linkml_runtime.loaders import yaml_loader
53

@@ -36,14 +34,9 @@
3634
"""
3735

3836

39-
class ListStringsTestCase(unittest.TestCase):
40-
def test_strings_in_list_slot(self):
41-
rslt = yaml_loader.loads(sample, SchemaDefinition)
42-
self.assertEqual(1, len(rslt.classes["AClass"].slots))
43-
self.assertEqual("a_slot", rslt.classes["AClass"].slots[0])
44-
self.assertEqual(1, len(rslt.classes["BClass"].mixins))
45-
self.assertEqual("AClass", rslt.classes["BClass"].mixins[0])
46-
47-
48-
if __name__ == "__main__":
49-
unittest.main()
37+
def test_strings_in_list_slot():
38+
rslt = yaml_loader.loads(sample, SchemaDefinition)
39+
assert len(rslt.classes["AClass"].slots) == 1
40+
assert rslt.classes["AClass"].slots[0] == "a_slot"
41+
assert len(rslt.classes["BClass"].mixins) == 1
42+
assert rslt.classes["BClass"].mixins[0] == "AClass"

0 commit comments

Comments
 (0)