25
25
26
26
def load_yaml_schema (path ):
27
27
"""Load yaml schema"""
28
- with open (path , "r" , encoding = "utf8" ) as f :
29
- return yaml .safe_load (f )
28
+ with open (path , "r" , encoding = "utf8" ) as schema_file :
29
+ return yaml .safe_load (schema_file )
30
30
31
31
32
32
CUR_DIR = os .path .dirname (os .path .realpath (__file__ ))
33
33
DATA_DIR = os .path .join (CUR_DIR , "examples" )
34
34
project1 = load_yaml_schema (os .path .join (CUR_DIR , "schemas/projects/project1.yaml" ))
35
- DEFAULT_PROJECTS = {"project1" : project1 }
35
+ TEST_PROJECTS = {"project1" : project1 }
36
36
37
37
38
38
def merge_schemas (schema_a , schema_b , path = None ):
@@ -62,19 +62,22 @@ def get_project_specific_schema(projects, project, schema, entity_type):
62
62
63
63
"""
64
64
root = copy .deepcopy (schema )
65
- project_overrides = projects .get (project )
66
- if project_overrides :
67
- overrides = project_overrides .get (entity_type )
68
- if overrides :
69
- merge_schemas (root , overrides , [entity_type ])
65
+ if projects is not None :
66
+ project_overrides = projects .get (project )
67
+ if project_overrides :
68
+ overrides = project_overrides .get (entity_type )
69
+ if overrides :
70
+ merge_schemas (root , overrides , [entity_type ])
70
71
return root
71
72
72
73
73
- def validate_entity (entity , schemata , project = None , projects = DEFAULT_PROJECTS ):
74
+ def validate_entity (entity , schemata , project = None , projects = None ):
74
75
"""Validate an entity by looking up the core schema for its type and
75
76
overriding it with any project level overrides
76
77
77
78
"""
79
+ if not projects :
80
+ projects = TEST_PROJECTS
78
81
local_schema = get_project_specific_schema (
79
82
projects , project , schemata [entity ["type" ]], entity ["type" ]
80
83
)
@@ -88,33 +91,41 @@ def validate_schemata(schemata, metaschema):
88
91
for schema_value in schemata .values ():
89
92
validate (schema_value , metaschema )
90
93
s_id = schema_value ["id" ]
94
+ schema_properties = schema_value ["properties" ]
91
95
92
- def assert_link_is_also_prop (link , s_id ):
96
+ def assert_link_is_also_prop (link , properties , s_id ):
93
97
assert (
94
- link in schema_value [ " properties" ]
98
+ link in properties
95
99
), f"Entity '{ s_id } ' has '{ link } ' as a link but not property"
96
100
97
101
for link in [
98
- schema_link ["name" ] for schema_link in schema_value ["links" ] if "name" in schema_link
102
+ schema_link ["name" ]
103
+ for schema_link in schema_value ["links" ]
104
+ if "name" in schema_link
99
105
]:
100
- assert_link_is_also_prop (link , s_id )
106
+ assert_link_is_also_prop (link , schema_properties , s_id )
101
107
for subgroup in [
102
- schema_link ["subgroup" ] for schema_link in schema_value ["links" ] if "name" not in schema_link
108
+ schema_link ["subgroup" ]
109
+ for schema_link in schema_value ["links" ]
110
+ if "name" not in schema_link
103
111
]:
104
- for link in [link_subgroup ["name" ] for link_subgroup in subgroup if "name" in link_subgroup ]:
105
- assert_link_is_also_prop (link , s_id )
112
+ for link in [
113
+ link_subgroup ["name" ]
114
+ for link_subgroup in subgroup
115
+ if "name" in link_subgroup
116
+ ]:
117
+ assert_link_is_also_prop (link , schema_properties , s_id )
106
118
107
119
108
120
class SchemaTest (unittest .TestCase ):
109
121
def setUp (self ):
110
122
self .dictionary = gdcdictionary
111
- self .definitions = yaml .safe_load (
112
- open (
123
+ with open (
113
124
os .path .join (CUR_DIR , "schemas" , "_definitions.yaml" ),
114
125
"r" ,
115
126
encoding = "utf8" ,
116
- )
117
- )
127
+ ) as def_file :
128
+ self . definitions = yaml . safe_load ( def_file )
118
129
119
130
def test_schemas (self ):
120
131
"""Validate schema against metaschema"""
0 commit comments