add more checking to the tests
MODIFY
fastavro/_validation.pyx
fastavro/_validation.pyx
@@ -77,7 +77,7 @@ cdef inline bint validate_enum(datum, dict schema,
cdef inline bint validate_array(datum, dict schema,
- str parent_ns='', bint raise_errors=True):
+ str parent_ns='', bint raise_errors=True) except -1:
if not isinstance(datum, Iterable) or is_str(datum):
return False
@@ -94,7 +94,7 @@ cdef inline bint validate_array(datum, dict schema,
cdef inline bint validate_map(object datum, dict schema, str parent_ns='',
- bint raise_errors=True):
+ bint raise_errors=True) except -1:
# initial checks for map type
if not isinstance(datum, Mapping):
return False
@@ -115,7 +115,7 @@ cdef inline bint validate_map(object datum, dict schema, str parent_ns='',
cdef inline bint validate_record(object datum, dict schema, str parent_ns='',
- bint raise_errors=True):
+ bint raise_errors=True) except -1:
if not isinstance(datum, Mapping):
return False
if raise_errors:
@@ -132,7 +132,7 @@ cdef inline bint validate_record(object datum, dict schema, str parent_ns='',
cdef inline bint validate_union(object datum, list schema, str parent_ns=None,
- bint raise_errors=True):
+ bint raise_errors=True) except -1:
if isinstance(datum, tuple):
(name, datum) = datum
for candidate in schema:

MODIFY
tests/test_validation.py
tests/test_validation.py
@@ -6,6 +6,14 @@ from fastavro.validation import (
)
import pytest
import numpy as np
+import sys
+
+# In PY2 when you do type(int) you get <type 'type'> but in PY3 you get
+# <class 'type'>
+if sys.version_info >= (3, 0):
+ type_type = 'class'
+else:
+ type_type = 'type'
schema = {
"fields": [
@@ -51,9 +59,14 @@ def test_validate_string_in_int_raises():
'integ': 21,
}]
- with pytest.raises((ValidationError,)):
+ with pytest.raises(ValidationError) as exc:
validation_raise(schema, *records)
+ for error in exc.value.errors:
+ expected_type = error.schema
+ assert expected_type in ['null', 'int']
+ assert error.field == 'namespace.missingerror.integ_null'
+
def test_validate_string_in_int_false():
records = [{
@@ -82,9 +95,15 @@ def test_validate_string_in_int_null_raises():
'integ_null': 11,
'integ': 'str',
}]
- with pytest.raises((ValidationError,)):
+
+ with pytest.raises(ValidationError) as exc:
validation_raise(schema, *records)
+ for error in exc.value.errors:
+ expected_type = error.schema
+ assert expected_type == 'int'
+ assert error.field == 'namespace.missingerror.integ'
+
def test_validate_string_in_int_null_false():
records = [{
@@ -104,9 +123,15 @@ def test_validate_int_in_string_null_raises():
'integ_null': 21,
'integ': 21,
}]
- with pytest.raises((ValidationError,)):
+
+ with pytest.raises(ValidationError) as exc:
validation_raise(schema, *records)
+ for error in exc.value.errors:
+ expected_type = error.schema
+ assert expected_type in ['string', 'null']
+ assert error.field == 'namespace.missingerror.str_null'
+
def test_validate_int_in_string_null_false():
records = [{
@@ -126,9 +151,14 @@ def test_validate_int_in_string_raises():
'integ': 21,
}]
- with pytest.raises((ValidationError,)):
+ with pytest.raises(ValidationError) as exc:
validation_raise(schema, *records)
+ for error in exc.value.errors:
+ expected_type = error.schema
+ assert expected_type == 'string'
+ assert error.field == 'namespace.missingerror.str'
+
def test_validate_int_in_string_false():
records = [{
@@ -169,9 +199,8 @@ def test_validate_error_raises():
raise ValidationError()
error = ValidationErrorData(10, "string", "test1")
- # PY2 says <type 'int'> and PY3 says <class 'int'> so just split there
- assert "test1 is <10> of type <" in str(error)
- assert "'int'> expected string" in str(error)
+ msg = "test1 is <10> of type <{} 'int'> expected string".format(type_type)
+ assert msg in str(error)
def test_validator_numeric():

add more checking to the tests