Skip to content

Commit 70b1c1c

Browse files
committed
Fix linter errors
1 parent 66dbb48 commit 70b1c1c

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

gdcdictionary/schema_test.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
def load_yaml_schema(path):
2727
"""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)
3030

3131

3232
CUR_DIR = os.path.dirname(os.path.realpath(__file__))
3333
DATA_DIR = os.path.join(CUR_DIR, "examples")
3434
project1 = load_yaml_schema(os.path.join(CUR_DIR, "schemas/projects/project1.yaml"))
35-
DEFAULT_PROJECTS = {"project1": project1}
35+
TEST_PROJECTS = {"project1": project1}
3636

3737

3838
def merge_schemas(schema_a, schema_b, path=None):
@@ -62,19 +62,22 @@ def get_project_specific_schema(projects, project, schema, entity_type):
6262
6363
"""
6464
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])
7071
return root
7172

7273

73-
def validate_entity(entity, schemata, project=None, projects=DEFAULT_PROJECTS):
74+
def validate_entity(entity, schemata, project=None, projects=None):
7475
"""Validate an entity by looking up the core schema for its type and
7576
overriding it with any project level overrides
7677
7778
"""
79+
if not projects:
80+
projects = TEST_PROJECTS
7881
local_schema = get_project_specific_schema(
7982
projects, project, schemata[entity["type"]], entity["type"]
8083
)
@@ -88,33 +91,41 @@ def validate_schemata(schemata, metaschema):
8891
for schema_value in schemata.values():
8992
validate(schema_value, metaschema)
9093
s_id = schema_value["id"]
94+
schema_properties = schema_value["properties"]
9195

92-
def assert_link_is_also_prop(link, s_id):
96+
def assert_link_is_also_prop(link, properties, s_id):
9397
assert (
94-
link in schema_value["properties"]
98+
link in properties
9599
), f"Entity '{s_id}' has '{link}' as a link but not property"
96100

97101
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
99105
]:
100-
assert_link_is_also_prop(link, s_id)
106+
assert_link_is_also_prop(link, schema_properties, s_id)
101107
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
103111
]:
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)
106118

107119

108120
class SchemaTest(unittest.TestCase):
109121
def setUp(self):
110122
self.dictionary = gdcdictionary
111-
self.definitions = yaml.safe_load(
112-
open(
123+
with open(
113124
os.path.join(CUR_DIR, "schemas", "_definitions.yaml"),
114125
"r",
115126
encoding="utf8",
116-
)
117-
)
127+
) as def_file:
128+
self.definitions = yaml.safe_load(def_file)
118129

119130
def test_schemas(self):
120131
"""Validate schema against metaschema"""

0 commit comments

Comments
 (0)