__init__.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. from cerberus import errors, Validator, SchemaError, DocumentError
  4. from cerberus.tests.conftest import sample_schema
  5. def assert_exception(exception, document={}, schema=None, validator=None,
  6. msg=None):
  7. """ Tests whether a specific exception is raised. Optionally also tests
  8. whether the exception message is as expected. """
  9. if validator is None:
  10. validator = Validator()
  11. if msg is None:
  12. with pytest.raises(exception) as excinfo:
  13. validator(document, schema)
  14. else:
  15. with pytest.raises(exception, message=msg) as excinfo: # noqa: F841
  16. validator(document, schema)
  17. def assert_schema_error(*args):
  18. """ Tests whether a validation raises an exception due to a malformed
  19. schema. """
  20. assert_exception(SchemaError, *args)
  21. def assert_document_error(*args):
  22. """ Tests whether a validation raises an exception due to a malformed
  23. document. """
  24. assert_exception(DocumentError, *args)
  25. def assert_fail(document, schema=None, validator=None, update=False,
  26. error=None, errors=None, child_errors=None):
  27. """ Tests whether a validation fails. """
  28. if validator is None:
  29. validator = Validator(sample_schema)
  30. result = validator(document, schema, update)
  31. assert isinstance(result, bool)
  32. assert not result
  33. actual_errors = validator._errors
  34. assert not (error is not None and errors is not None)
  35. assert not (errors is not None and child_errors is not None), (
  36. 'child_errors can only be tested in '
  37. 'conjunction with the error parameter'
  38. )
  39. assert not (child_errors is not None and error is None)
  40. if error is not None:
  41. assert len(actual_errors) == 1
  42. assert_has_error(actual_errors, *error)
  43. if child_errors is not None:
  44. assert len(actual_errors[0].child_errors) == len(child_errors)
  45. assert_has_errors(actual_errors[0].child_errors, child_errors)
  46. elif errors is not None:
  47. assert len(actual_errors) == len(errors)
  48. assert_has_errors(actual_errors, errors)
  49. return actual_errors
  50. def assert_success(document, schema=None, validator=None, update=False):
  51. """ Tests whether a validation succeeds. """
  52. if validator is None:
  53. validator = Validator(sample_schema)
  54. result = validator(document, schema, update)
  55. assert isinstance(result, bool)
  56. if not result:
  57. raise AssertionError(validator.errors)
  58. def assert_has_error(_errors, d_path, s_path, error_def, constraint, info=()):
  59. if not isinstance(d_path, tuple):
  60. d_path = (d_path,)
  61. if not isinstance(info, tuple):
  62. info = (info,)
  63. assert isinstance(_errors, errors.ErrorList)
  64. for i, error in enumerate(_errors):
  65. assert isinstance(error, errors.ValidationError)
  66. try:
  67. assert error.document_path == d_path
  68. assert error.schema_path == s_path
  69. assert error.code == error_def.code
  70. assert error.rule == error_def.rule
  71. assert error.constraint == constraint
  72. if not error.is_group_error:
  73. assert error.info == info
  74. except AssertionError:
  75. pass
  76. except Exception:
  77. raise
  78. else:
  79. break
  80. else:
  81. raise AssertionError("""
  82. Error with properties:
  83. document_path={doc_path}
  84. schema_path={schema_path}
  85. code={code}
  86. constraint={constraint}
  87. info={info}
  88. not found in errors:
  89. {errors}
  90. """.format(doc_path=d_path, schema_path=s_path,
  91. code=hex(error.code), info=info,
  92. constraint=constraint, errors=_errors))
  93. return i
  94. def assert_has_errors(_errors, _exp_errors):
  95. assert isinstance(_exp_errors, list)
  96. for error in _exp_errors:
  97. assert isinstance(error, tuple)
  98. assert_has_error(_errors, *error)
  99. def assert_not_has_error(_errors, *args, **kwargs):
  100. try:
  101. assert_has_error(_errors, *args, **kwargs)
  102. except AssertionError:
  103. pass
  104. except Exception as e:
  105. raise e
  106. else:
  107. raise AssertionError('An unexpected error occurred.')
  108. def assert_bad_type(field, data_type, value):
  109. assert_fail({field: value},
  110. error=(field, (field, 'type'), errors.BAD_TYPE, data_type))
  111. def assert_normalized(document, expected, schema=None, validator=None):
  112. if validator is None:
  113. validator = Validator(sample_schema)
  114. assert_success(document, schema, validator)
  115. assert validator.document == expected