Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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'
def test_assert_value(self):
with pytest.raises(RuleError) as r:
Rule(schema={"type": "seq", "sequence": [{"type": "str", "assert": 1}]})
assert str(r.value) == ""
assert r.value.error_key == 'assert.not_str'
# Test that invalid characters is not present
with pytest.raises(RuleError) as r:
Rule(schema={"type": "seq", "sequence": [{"type": "str", "assert": "__import__"}]})
assert str(r.value) == "" # NOQA: E501
assert r.value.error_key == 'assert.unsupported_content'
def test_mapping(self):
# This tests mapping with a nested type and pattern
r = Rule(schema={"type": "map", "mapping": {"name": {"type": "str", "pattern": ".+@.+"}}})
assert r.type == "map", "rule type is not map"
assert isinstance(r.mapping, dict), "mapping is not dict"
assert r.mapping["name"].type == "str", "nested mapping is not of string type"
assert r.mapping["name"].pattern is not None, "nested mapping has no pattern var set"
assert r.mapping["name"].pattern == ".+@.+", "pattern is not set to correct value"
# when type is specefied, 'mapping' key must be present
with pytest.raises(SchemaConflict) as ex:
Rule(schema={"type": "map"})
assert str(ex.value) == ""
# 'map' and 'enum' can't be used at same time
# TODO: This do not work because it currently raises RuleError:
# with pytest.raises(SchemaConflict):
# r = Rule(schema={"type": "map", "enum": [1, 2, 3]})
def test_desc_value(self):
with pytest.raises(RuleError) as r:
Rule(schema={'type': 'str', 'desc': []})
assert str(r.value) == ""
def test_sequence(self):
# this tests seq type with a internal type of str
r = Rule(schema={"type": "seq", "sequence": [{"type": "str"}]})
assert r.type is not None, "rule not contain type var"
assert r.type == "seq", "type not 'seq'"
assert r.sequence is not None, "rule not contain sequence var"
assert isinstance(r.sequence, list), "rule is not a list"
# Test basic sequence rule
r = Rule(schema={"type": "seq", "sequence": [{"type": "str"}]})
assert r.type == "seq"
assert isinstance(r.sequence, list)
assert isinstance(r.sequence[0], Rule)
assert r.sequence[0].type == "str"
# Test sequence without explicit type
r = Rule(schema={"sequence": [{"type": "str"}]})
assert r.type == "seq"
assert isinstance(r.sequence, list)
assert isinstance(r.sequence[0], Rule)
assert r.sequence[0].type == "str"
# Test short name 'seq'
r = Rule(schema={"seq": [{"type": "str"}]})
assert r.type == "seq"
assert isinstance(r.sequence, list)