Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Test unicode data inside seq but wrong type
(u"3f.yaml", SchemaError),
]
for failing_test, exception_type in _fail_tests:
f = self.f(failing_test)
with open(f, "r") as stream:
yaml_data = yaml.safe_load(stream)
data = yaml_data["data"]
schema = yaml_data["schema"]
errors = yaml_data["errors"]
try:
print(u"Running test files: {0}".format(f))
c = Core(source_data=data, schema_data=schema)
c.validate()
except exception_type:
pass # OK
else:
raise AssertionError(u"Exception {0} not raised as expected... FILES: {1} : {2}".format(exception_type, exception_type))
compare(sorted(c.validation_errors), sorted(errors), prefix=u"Wrong validation errors when parsing files : {0}".format(f))
def test_load_yaml_files(self, tmpdir):
"""
Load source & schema files that has yaml file ending.
"""
source_f = tmpdir.join("foo.yaml")
source_f.write("3.14159")
schema_f = tmpdir.join("bar.yaml")
schema_f.write("type: float")
Core(source_file=str(source_f), schema_files=[str(schema_f)])
for passing_test in pass_tests:
try:
c = Core(source_file=passing_test[1], schema_files=passing_test[0])
c.validate()
compare(c.validation_errors, [], prefix="No validation errors should exist...")
except Exception as e:
print("ERROR RUNNING FILE: {0} : {1}".format(passing_test[0], passing_test[1]))
raise e
# This serve as an extra schema validation that tests more complex structures then testrule.py do
compare(c.root_rule.schema_str, passing_test[2], prefix="Parsed rules is not correct, something have changed...")
for failing_test in failing_tests:
with pytest.raises(failing_test[2], msg="Test files: {0} : {1}".format(", ".join(failing_test[0]), failing_test[1])):
c = Core(schema_files=failing_test[0], source_file=failing_test[1])
c.validate()
if not c.validation_errors:
raise AssertionError("No validation_errors was raised...")
compare(
sorted(c.validation_errors),
sorted(failing_test[3]),
prefix="Wrong validation errors when parsing files : {0} : {1}".format(
failing_test[0],
failing_test[1],
),
def test_create_sub_class_exceptions(self):
u_e = errors.UnknownError()
assert u_e.retcode == 1
s_e = errors.SchemaError()
assert s_e.retcode == 2
c_e = errors.CoreError()
assert c_e.retcode == 3
r_e = errors.RuleError()
assert r_e.retcode == 4
sc_e = errors.SchemaConflict()
assert sc_e.retcode == 5
def test_date_and_format_value(self):
r = Rule(schema={"type": "date", "format": "%y"})
assert r.format is not None, "date var not set proper"
assert isinstance(r.format, list), "date format should be a list"
with pytest.raises(RuleError) as r:
Rule(schema={"type": "date", "format": 1})
assert str(r.value) == ""
with pytest.raises(RuleError) as r:
Rule(schema={"type": "map", "format": "%y"})
assert str(r.value) == ""
r = Rule(schema={'type': 'seq', 'sequence': [{'type': 'str'}, {'type': 'int'}]})
assert r.type == "seq"
assert r.matching == "any"
assert len(r.sequence) == 2
assert isinstance(r.sequence, list)
assert all(isinstance(r.sequence[i], Rule) for i in range(len(r.sequence)))
assert r.sequence[0].type == "str"
assert r.sequence[1].type == "int"
# Test sequence without explicit type
r = Rule(schema={'sequence': [{'type': 'str'}, {'type': 'int'}]})
assert r.type == "seq"
assert r.matching == "any"
assert len(r.sequence) == 2
assert isinstance(r.sequence, list)
assert all(isinstance(r.sequence[i], Rule) for i in range(len(r.sequence)))
assert r.sequence[0].type == "str"
assert r.sequence[1].type == "int"
# assert str(r.value) == ""
# assert r.value.error_key == 'type.missing'
# Test a valid rule with both "str" and "unicode" types work
r = Rule(schema={"type": str("str")})
r = Rule(schema={"type": unicode("str")})
# Test that type key must be string otherwise exception is raised
with pytest.raises(RuleError) as r:
Rule(schema={"type": 1})
assert str(r.value) == ""
assert r.value.error_key == 'type.not_string'
# this tests that the type key must be a string
with pytest.raises(RuleError) as r:
Rule(schema={"type": 1}, parent=None)
assert str(r.value) == ""
assert r.value.error_key == 'type.not_string'
def test_range_value(self):
r = Rule(schema={"type": "int", "range": {"max": 10, "min": 1}})
assert r.range is not None, "range var not set proper"
assert isinstance(r.range, dict), "range var is not of dict type"
# this tests that the range key must be a dict
with pytest.raises(RuleError) as r:
Rule(schema={"type": "int", "range": []})
assert str(r.value) == ""
assert r.value.error_key == 'range.not_map'
with pytest.raises(RuleError) as r:
Rule(schema={"type": "str", "range": {"max": "z"}})
assert str(r.value) == ""
assert r.value.error_key == 'range.max.not_number'
# this tests that min is bigger then max that should not be possible
with pytest.raises(RuleError) as r:
# scalar type and sequence can't be used at same time
with pytest.raises(SchemaConflict) as ex:
Rule(schema={"type": "int", "sequence": [{"type": "str"}]})
assert str(ex.value) == ""
assert ex.value.error_key == 'scalar.conflict.sequence'
# scalar type and mapping can't be used at same time
with pytest.raises(SchemaConflict) as ex:
Rule(schema={"type": "int", "mapping": {"foo": {"type": "str"}}})
assert str(ex.value) == ""
assert ex.value.error_key == 'scalar.conflict.mapping'
# scalar type and enum can't be used at same time
with pytest.raises(SchemaConflict) as ex:
Rule(schema={"type": "int", "enum": [1, 2, 3], "range": {"max": 10, "min": 1}})
assert str(ex.value) == ""
assert ex.value.error_key == 'enum.conflict.range'
def test_matching_rule(self):
# Test that exception is raised when a invalid matching rule is used
with pytest.raises(RuleError) as r:
Rule(schema={"type": "map", "matching-rule": "foobar", "mapping": {"regex;.+": {"type": "seq", "sequence": [{"type": "str"}]}}})
assert str(r.value) == ""
assert r.value.error_key == 'matching_rule.not_allowed'