Skip to content

Commit

Permalink
Refactor failing section of test_multi_file_support to reduce number …
Browse files Browse the repository at this point in the history
…of files. Added support for sending in multiple schemas as a list into schema_data variable and merge them into one. Simulates merging multiple data sources into one schema.
  • Loading branch information
Grokzen committed Oct 9, 2019
1 parent 333d445 commit 6505209
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pykwalify/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,17 @@ def __init__(self, source_file=None, schema_files=None, source_data=None, schema
if self.source is None:
log.debug(u"No source file loaded, trying source data variable")
self.source = source_data

if self.schema is None:
log.debug(u"No schema file loaded, trying schema data variable")
self.schema = schema_data

if isinstance(schema_data, list):
merged_schema = {}
for schema in schema_data:
merged_schema.update(schema)
self.schema = merged_schema
else:
self.schema = schema_data

# Test if anything was loaded
if self.source is None:
Expand Down
24 changes: 24 additions & 0 deletions tests/files/partial_schemas/10f.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
data:
point:
x: 3

schema:
type: map
mapping:
point:
mapping:
x:
required: true
include: coordinate_value
y:
required: true
include: coordinate_value

partial-files:
- schema;coordinate_value:
type: number

fail-exception-class: "SchemaError"
fail-validation-errors:
- "Cannot find required key 'y'. Path: '/point'.: Path: '/'"
33 changes: 33 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,39 @@ def test_multi_file_support(self):
),
)

fail_tests = [
self.f('partial_schemas', '10f.yaml')
]

for fail_test in fail_tests:
with open(fail_test, 'r') as stream:
fail_test_raw_data = yaml.safe_load_all(stream)

for document_index, document in enumerate(fail_test_raw_data):
fail_test_data = document

data = fail_test_data['data']
schema = fail_test_data['schema']
partial_files = fail_test_data['partial-files']
fail_exception_class = fail_test_data['fail-exception-class']
fail_validation_errors = fail_test_data['fail-validation-errors']
fail_except_class_instance = None

if fail_exception_class == "SchemaError":
fail_except_class_instance = SchemaError
elif fail_exception_class == "RuleError":
fail_except_class_instance = RuleError

msg = "FAILED TEST FILE: {0}".format(fail_test)

with pytest.raises(fail_except_class_instance, message=msg):
c = Core(
source_data=data,
schema_data=[schema] + partial_files,
)
c.validate()


def test_core_files(self):
# These tests should pass with no exception raised
pass_tests = [
Expand Down

0 comments on commit 6505209

Please sign in to comment.