1
1
import os
2
- import unittest
3
2
from pathlib import Path
4
3
from typing import Optional , TextIO , Union
5
4
5
+ import pytest
6
6
from hbreader import FileInfo
7
7
8
8
from linkml_runtime .loaders import RDFLoader , json_loader , rdf_loader , yaml_loader
9
9
from linkml_runtime .utils .yamlutils import YAMLRoot
10
10
from tests .test_loaders_dumpers import LD_11_DIR , LD_11_SSL_SVR , LD_11_SVR
11
11
from tests .test_loaders_dumpers .environment import env
12
- from tests .test_loaders_dumpers .loaderdumpertestcase import LoaderDumperTestCase
13
12
from tests .test_loaders_dumpers .models .termci_schema import Package
14
-
15
-
16
- class LoadersUnitTest (LoaderDumperTestCase ):
17
- env = env
18
-
19
- @classmethod
20
- def setUpClass (cls ) -> None :
21
- cls .context_server = cls .check_context_servers ([LD_11_SVR , LD_11_SSL_SVR ])
22
- if not cls .context_server :
23
- cls .context_server = LD_11_DIR
24
-
25
- def test_yaml_loader (self ):
26
- """Load obo_sample.yaml, emit obo_sample_yaml.yaml and compare to obo_sample_output.yaml"""
27
- self .loader_test ("obo_sample.yaml" , Package , yaml_loader )
28
-
29
- def test_json_loader_path (self ):
30
- """Load obo_sample.json, emit obo_sample_json.yaml and check the results"""
31
- REPO_ROOT = Path (__file__ ).parent .parent .parent
32
- path = REPO_ROOT / "tests" / "test_loaders_dumpers" / "input" / "obo_sample.json"
33
- data = json_loader .load (Path (path ), Package , base_dir = self .env .indir )
34
- assert isinstance (data , Package )
35
- assert "system" in data
36
-
37
- def test_json_loader (self ):
38
- """Load obo_sample.json, emit obo_sample_json.yaml and check the results"""
39
- self .loader_test ("obo_sample.json" , Package , json_loader )
40
-
41
- def test_json_load_to_dict (self ):
42
- data = json_loader .load_as_dict ("obo_sample.json" , base_dir = self .env .indir )
43
- assert isinstance (data , dict )
44
- assert "system" in data
45
-
46
- def test_yaml_load_to_dict (self ):
47
- data = yaml_loader .load_as_dict ("obo_sample.yaml" , base_dir = self .env .indir )
48
- assert isinstance (data , dict )
49
- assert "system" in data
50
-
51
- @unittest .skipIf (True , "This test will not work until https://github.com/digitalbazaar/pyld/issues/149 is fixed" )
52
- def test_rdf_loader (self ):
53
- """Load obo_sample.ttl, emit obo_sample_ttl.yaml and check the results
54
- Load obo_sample.jsonld, emit obo_sample_jsonld.yaml and check the results
55
- """
56
- if self .context_server == LD_11_DIR :
57
- raise unittest .SkipTest ("*****> Loading skipped until JSON-LD processor can handle non-http files" )
58
-
59
- contexts = os .path .join (self .context_server , "termci_schema_inlined.context.jsonld" )
60
- fmt = "turtle"
61
-
62
- class RDFLoaderWrapper (RDFLoader ):
63
- def load (
64
- self ,
65
- source : Union [str , dict , TextIO ],
66
- target_class : type [YAMLRoot ],
67
- * ,
68
- base_dir : Optional [str ] = None ,
69
- metadata : Optional [FileInfo ] = None ,
70
- ** _ ,
71
- ) -> YAMLRoot :
72
- return rdf_loader .load (
73
- source ,
74
- target_class ,
75
- base_dir = LoadersUnitTest .env .indir ,
76
- fmt = fmt ,
77
- metadata = metadata ,
78
- contexts = contexts ,
79
- )
80
-
81
- def loads (
82
- self , source : str , target_class : type [YAMLRoot ], * , metadata : Optional [FileInfo ] = None , ** _
83
- ) -> YAMLRoot :
84
- return rdf_loader .loads (source , target_class , contexts = contexts , fmt = fmt , metadata = metadata )
85
-
86
- self .loader_test ("obo_sample.ttl" , Package , RDFLoaderWrapper ())
87
- fmt = "json-ld"
88
- self .loader_test ("obo_sample.jsonld" , Package , RDFLoaderWrapper ())
89
-
90
-
91
- if __name__ == "__main__" :
92
- unittest .main ()
13
+ from tests .test_loaders_dumpers .test_loaders_pydantic import loader_test
14
+
15
+
16
+ @pytest .fixture (scope = "module" )
17
+ def context_server ():
18
+ """Set up context server for testing."""
19
+ # Check context servers - this mimics the original setUpClass logic
20
+ from tests .test_loaders_dumpers .loaderdumpertestcase import LoaderDumperTestCase
21
+ context_server = LoaderDumperTestCase .check_context_servers ([LD_11_SVR , LD_11_SSL_SVR ])
22
+ if not context_server :
23
+ context_server = LD_11_DIR
24
+ return context_server
25
+
26
+
27
+ def test_yaml_loader ():
28
+ """Load obo_sample.yaml, emit obo_sample_yaml.yaml and compare to obo_sample_output.yaml"""
29
+ loader_test ("obo_sample.yaml" , Package , yaml_loader )
30
+
31
+
32
+ def test_json_loader_path ():
33
+ """Load obo_sample.json using Path object and check the results"""
34
+ REPO_ROOT = Path (__file__ ).parent .parent .parent
35
+ path = REPO_ROOT / "tests" / "test_loaders_dumpers" / "input" / "obo_sample.json"
36
+ data = json_loader .load (Path (path ), Package , base_dir = env .indir )
37
+ assert isinstance (data , Package )
38
+ assert "system" in data
39
+
40
+
41
+ def test_json_loader ():
42
+ """Load obo_sample.json, emit obo_sample_json.yaml and check the results"""
43
+ loader_test ("obo_sample.json" , Package , json_loader )
44
+
45
+
46
+ def test_json_load_to_dict ():
47
+ """Test loading JSON file as dictionary"""
48
+ data = json_loader .load_as_dict ("obo_sample.json" , base_dir = env .indir )
49
+ assert isinstance (data , dict )
50
+ assert "system" in data
51
+
52
+
53
+ def test_yaml_load_to_dict ():
54
+ """Test loading YAML file as dictionary"""
55
+ data = yaml_loader .load_as_dict ("obo_sample.yaml" , base_dir = env .indir )
56
+ assert isinstance (data , dict )
57
+ assert "system" in data
58
+
59
+
60
+ @pytest .mark .skip (reason = "This test will not work until https://github.com/digitalbazaar/pyld/issues/149 is fixed" )
61
+ def test_rdf_loader (context_server ):
62
+ """Load obo_sample.ttl and obo_sample.jsonld, emit yaml and check the results"""
63
+ if context_server == LD_11_DIR :
64
+ pytest .skip ("*****> Loading skipped until JSON-LD processor can handle non-http files" )
65
+
66
+ contexts = os .path .join (context_server , "termci_schema_inlined.context.jsonld" )
67
+ fmt = "turtle"
68
+
69
+ class RDFLoaderWrapper (RDFLoader ):
70
+ def load (
71
+ self ,
72
+ source : Union [str , dict , TextIO ],
73
+ target_class : type [YAMLRoot ],
74
+ * ,
75
+ base_dir : Optional [str ] = None ,
76
+ metadata : Optional [FileInfo ] = None ,
77
+ ** _ ,
78
+ ) -> YAMLRoot :
79
+ return rdf_loader .load (
80
+ source ,
81
+ target_class ,
82
+ base_dir = env .indir ,
83
+ fmt = fmt ,
84
+ metadata = metadata ,
85
+ contexts = contexts ,
86
+ )
87
+
88
+ def loads (
89
+ self , source : str , target_class : type [YAMLRoot ], * , metadata : Optional [FileInfo ] = None , ** _
90
+ ) -> YAMLRoot :
91
+ return rdf_loader .loads (source , target_class , contexts = contexts , fmt = fmt , metadata = metadata )
92
+
93
+ loader_test ("obo_sample.ttl" , Package , RDFLoaderWrapper ())
94
+ fmt = "json-ld"
95
+ loader_test ("obo_sample.jsonld" , Package , RDFLoaderWrapper ())
0 commit comments