summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/files/fail/test_enum.yaml6
-rw-r--r--tests/files/fail/test_mapping.yaml2
-rw-r--r--tests/files/fail/test_type_email.yaml10
-rw-r--r--tests/files/fail/test_type_float.yaml2
-rw-r--r--tests/files/fail/test_type_url.yaml11
-rw-r--r--tests/files/success/test_mapping.yaml45
-rw-r--r--tests/files/success/test_type_email.yaml8
-rw-r--r--tests/files/success/test_type_url.yaml9
-rw-r--r--tests/test_core.py40
-rw-r--r--tests/test_rule.py12
-rw-r--r--tests/test_types.py2
11 files changed, 135 insertions, 12 deletions
diff --git a/tests/files/fail/test_enum.yaml b/tests/files/fail/test_enum.yaml
index 9f619dc..ba223e3 100644
--- a/tests/files/fail/test_enum.yaml
+++ b/tests/files/fail/test_enum.yaml
@@ -11,6 +11,6 @@ schema:
- type: str
enum: [E, F, G, H]
errors:
- - "Enum 'A' does not exist. Path: '/0'"
- - "Enum 'B' does not exist. Path: '/1'"
- - "Enum 'O' does not exist. Path: '/2'"
+ - "Enum 'A' does not exist. Path: '/0' Enum: ['E', 'F', 'G', 'H']"
+ - "Enum 'B' does not exist. Path: '/1' Enum: ['E', 'F', 'G', 'H']"
+ - "Enum 'O' does not exist. Path: '/2' Enum: ['E', 'F', 'G', 'H']"
diff --git a/tests/files/fail/test_mapping.yaml b/tests/files/fail/test_mapping.yaml
index 0561ea4..88b390f 100644
--- a/tests/files/fail/test_mapping.yaml
+++ b/tests/files/fail/test_mapping.yaml
@@ -158,7 +158,7 @@ data:
birth: Jul 01, 1985
errors:
- "Cannot find required key 'name'. Path: ''"
- - "Enum 'ab' does not exist. Path: '/blood'"
+ - "Enum 'ab' does not exist. Path: '/blood' Enum: ['A', 'B', 'O', 'AB']"
- "Key 'nam' was not defined. Path: ''"
- "Value 'foo(at)mail.com' does not match pattern '.+@.+'. Path: '/email'"
- "Value 'twenty' is not of type 'int'. Path: '/age'"
diff --git a/tests/files/fail/test_type_email.yaml b/tests/files/fail/test_type_email.yaml
new file mode 100644
index 0000000..9c8c18f
--- /dev/null
+++ b/tests/files/fail/test_type_email.yaml
@@ -0,0 +1,10 @@
+---
+name: email-1
+desc: basic email type validation. Fails if not conforms with RFC 5322 Official Standard
+#
+schema:
+ type: email
+data:
+ "foobar|gmail.com"
+errors:
+ - "Value 'foobar|gmail.com' is not of type 'email'. Path: ''"
diff --git a/tests/files/fail/test_type_float.yaml b/tests/files/fail/test_type_float.yaml
index bef14b8..972167d 100644
--- a/tests/files/fail/test_type_float.yaml
+++ b/tests/files/fail/test_type_float.yaml
@@ -17,8 +17,6 @@ schema:
seq:
- type: float
errors:
- - "Value 'foo' is not of type 'float'. Path: '/0'"
-errors:
- "Value 'True' is not of type 'float'. Path: '/1'"
- "Value 'foo' is not of type 'float'. Path: '/0'"
---
diff --git a/tests/files/fail/test_type_url.yaml b/tests/files/fail/test_type_url.yaml
new file mode 100644
index 0000000..1e79dfc
--- /dev/null
+++ b/tests/files/fail/test_type_url.yaml
@@ -0,0 +1,11 @@
+---
+name: url2
+desc: basic url type validation. Fails as not confirms with RFC 1808
+#
+schema:
+ type: url
+#
+data:
+ "www.google.com"
+errors:
+ - "Value 'www.google.com' is not of type 'url'. Path: ''"
diff --git a/tests/files/success/test_mapping.yaml b/tests/files/success/test_mapping.yaml
index fb82fd6..66a08a7 100644
--- a/tests/files/success/test_mapping.yaml
+++ b/tests/files/success/test_mapping.yaml
@@ -219,7 +219,7 @@ data:
schema:
type: map
mapping:
- regex;/[A-Z]-/:
+ regex;([A-Z]-):
type: map
mapping:
name:
@@ -305,4 +305,45 @@ schema:
type: map
mapping:
regex;(person[1-9]):
- required: True \ No newline at end of file
+ required: True
+---
+name: mapping19
+desc: Test regex mapping with integer keys
+data:
+ test:
+ 1: test
+ 2: test2
+schema:
+ type: map
+ mapping:
+ test:
+ type: map
+ mapping:
+ regex;(.+):
+ type: str
+---
+name: mapping 20
+desc: Test mapping with integer keys
+data:
+ test:
+ 0: test
+schema:
+ type: map
+ mapping:
+ test:
+ type: map
+ mapping:
+ 0:
+ type: str
+---
+name: mapping21
+desc: Test that 'required' integer regex keys are supported.
+data:
+ 1234: Albert
+ 1235: Tom
+schema:
+ type: map
+ mapping:
+ regex;([0-9]+):
+ required: True
+ type: str
diff --git a/tests/files/success/test_type_email.yaml b/tests/files/success/test_type_email.yaml
new file mode 100644
index 0000000..4dd4bee
--- /dev/null
+++ b/tests/files/success/test_type_email.yaml
@@ -0,0 +1,8 @@
+---
+name: email-1
+desc: basic email type validation. Fails if not conforms with RFC 5322 Official Standard
+#
+schema:
+ type: email
+data:
+ "foobar@gmail.com"
diff --git a/tests/files/success/test_type_url.yaml b/tests/files/success/test_type_url.yaml
new file mode 100644
index 0000000..28e369e
--- /dev/null
+++ b/tests/files/success/test_type_url.yaml
@@ -0,0 +1,9 @@
+---
+name: url1
+desc: basic url type validation
+#
+schema:
+ type: url
+#
+data:
+ "http://www.google.com"
diff --git a/tests/test_core.py b/tests/test_core.py
index 6fcd81e..0dbb826 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -336,7 +336,9 @@ class TestCore(object):
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], message="Test files: {0} : {1}".format(", ".join(failing_test[0]), failing_test[1])):
+ print("Test files: {0} : {1}".format(", ".join(failing_test[0]), failing_test[1]))
+
+ with pytest.raises(failing_test[2]):
c = Core(schema_files=failing_test[0], source_file=failing_test[1])
c.validate()
@@ -352,6 +354,34 @@ class TestCore(object):
),
)
+ def test_python_obj_loading(self, tmp_path):
+ # in latest pytest version, tmp_path is a PosixPath
+ tmp_path = str(tmp_path)
+
+ schema = """
+ allowempty: True
+ mapping:
+ intents:
+ type: !!python/str "seq"
+ sequence:
+ - type: !!python/str "str"
+ """
+ data = """
+ intents:
+ - greet
+ - default
+ - goodbye
+ """
+ schema_path = os.path.join(tmp_path, 'schema.yaml')
+ with open(schema_path, 'w') as stream:
+ stream.write(schema)
+ data_path = os.path.join(tmp_path, 'data.yaml')
+ with open(data_path, 'w') as stream:
+ stream.write(data)
+
+ c = Core(source_file=data_path, schema_files=[schema_path])
+ c.validate()
+
def test_core_files(self):
# These tests should pass with no exception raised
pass_tests = [
@@ -435,6 +465,10 @@ class TestCore(object):
"test_type_text.yaml",
# All tests for TYPE: timestamp
"test_type_timestamp.yaml",
+ # All tests for TYPE: email
+ "test_type_email.yaml",
+ # All tests for TYPE: url
+ "test_type_url.yaml",
]
_fail_tests = [
@@ -516,6 +550,10 @@ class TestCore(object):
("test_type_text.yaml", SchemaError),
# All tests for TYPE: timestamp
("test_type_timestamp.yaml", SchemaError),
+ # All tests for TYPE: email
+ ("test_type_email.yaml", SchemaError),
+ # All tests for TYPE: url
+ ("test_type_url.yaml", SchemaError),
]
# Add override magic to make it easier to test a specific file
diff --git a/tests/test_rule.py b/tests/test_rule.py
index 4b2b7c9..4302b05 100644
--- a/tests/test_rule.py
+++ b/tests/test_rule.py
@@ -37,7 +37,7 @@ class TestRule(unittest.TestCase):
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"}]}}})
+ Rule(schema={"type": "map", "matching-rule": "foobar", "mapping": {"regex;(.+)": {"type": "seq", "sequence": [{"type": "str"}]}}})
assert str(r.value) == "<RuleError: error code 4: Specified rule in key: foobar is not part of allowed rule set : ['any', 'all']: Path: '/'>"
assert r.value.error_key == 'matching_rule.not_allowed'
@@ -349,8 +349,8 @@ class TestRule(unittest.TestCase):
# This will test that a invalid regex will throw error when parsing rules
with pytest.raises(RuleError) as r:
- Rule(schema={"type": "map", "matching-rule": "any", "mapping": {"regex;(+": {"type": "seq", "sequence": [{"type": "str"}]}}})
- assert str(r.value) == "<RuleError: error code 4: Unable to compile regex '(+': Path: '/'>"
+ Rule(schema={"type": "map", "matching-rule": "any", "mapping": {"regex;(+)": {"type": "seq", "sequence": [{"type": "str"}]}}})
+ assert str(r.value) == "<RuleError: error code 4: Unable to compile regex '(+)': Path: '/'>"
assert r.value.error_key == 'mapping.regex.compile_error'
# this tests map/dict but with no elements
@@ -359,6 +359,12 @@ class TestRule(unittest.TestCase):
assert str(r.value) == "<RuleError: error code 4: Mapping do not contain any elements: Path: '/'>"
assert r.value.error_key == 'mapping.no_elements'
+ # Test that regex with missing parentheses are correctly detected.
+ with pytest.raises(RuleError) as r:
+ Rule(schema={"type": "map", "matching-rule": "any", "mapping": {"regex;[a-z]": {"type": "seq", "sequence": [{"type": "str"}]}}})
+ assert str(r.value) == "<RuleError: Regex '[a-z]' should start and end with parentheses: Path: '/'>"
+ assert r.value.error_key == 'mapping.regex.missing_parentheses'
+
def test_default_value(self):
pass
diff --git a/tests/test_types.py b/tests/test_types.py
index de7db87..a7ead5f 100644
--- a/tests/test_types.py
+++ b/tests/test_types.py
@@ -73,3 +73,5 @@ class TestTypes(unittest.TestCase):
assert types.is_none(None)
assert not types.is_none("foo")
+
+ assert types.is_url("https://github.com")