test_schema.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. from cerberus import Validator, errors, SchemaError
  4. from cerberus.schema import UnvalidatedSchema
  5. from cerberus.tests import assert_schema_error
  6. def test_empty_schema():
  7. validator = Validator()
  8. with pytest.raises(SchemaError, message=errors.SCHEMA_ERROR_MISSING):
  9. validator({}, schema=None)
  10. def test_bad_schema_type(validator):
  11. schema = "this string should really be dict"
  12. exp_msg = errors.SCHEMA_ERROR_DEFINITION_TYPE.format(schema)
  13. with pytest.raises(SchemaError, message=exp_msg):
  14. validator.schema = schema
  15. def test_bad_schema_type_field(validator):
  16. field = 'foo'
  17. schema = {field: {'schema': {'bar': {'type': 'strong'}}}}
  18. with pytest.raises(SchemaError):
  19. validator.schema = schema
  20. def test_unknown_rule(validator):
  21. message = "{'foo': [{'unknown': ['unknown rule']}]}"
  22. with pytest.raises(SchemaError, message=message):
  23. validator.schema = {'foo': {'unknown': 'rule'}}
  24. def test_unknown_type(validator):
  25. field = 'name'
  26. value = 'catch_me'
  27. message = str({field: [{'type': ['unallowed value %s' % value]}]})
  28. with pytest.raises(SchemaError, message=message):
  29. validator.schema = {'foo': {'unknown': 'rule'}}
  30. def test_bad_schema_definition(validator):
  31. field = 'name'
  32. message = str({field: ['must be of dict type']})
  33. with pytest.raises(SchemaError, message=message):
  34. validator.schema = {field: 'this should really be a dict'}
  35. def test_bad_of_rules():
  36. schema = {'foo': {'anyof': {'type': 'string'}}}
  37. assert_schema_error({}, schema)
  38. def test_normalization_rules_are_invalid_in_of_rules():
  39. schema = {0: {'anyof': [{'coerce': lambda x: x}]}}
  40. assert_schema_error({}, schema)
  41. def test_anyof_allof_schema_validate():
  42. # make sure schema with 'anyof' and 'allof' constraints are checked
  43. # correctly
  44. schema = {'doc': {'type': 'dict',
  45. 'anyof': [
  46. {'schema': [{'param': {'type': 'number'}}]}]}}
  47. assert_schema_error({'doc': 'this is my document'}, schema)
  48. schema = {'doc': {'type': 'dict',
  49. 'allof': [
  50. {'schema': [{'param': {'type': 'number'}}]}]}}
  51. assert_schema_error({'doc': 'this is my document'}, schema)
  52. def test_repr():
  53. v = Validator({'foo': {'type': 'string'}})
  54. assert repr(v.schema) == "{'foo': {'type': 'string'}}"
  55. def test_validated_schema_cache():
  56. v = Validator({'foozifix': {'coerce': int}})
  57. cache_size = len(v._valid_schemas)
  58. v = Validator({'foozifix': {'type': 'integer'}})
  59. cache_size += 1
  60. assert len(v._valid_schemas) == cache_size
  61. v = Validator({'foozifix': {'coerce': int}})
  62. assert len(v._valid_schemas) == cache_size
  63. max_cache_size = 147
  64. assert cache_size <= max_cache_size, \
  65. "There's an unexpected high amount (%s) of cached valid " \
  66. "definition schemas. Unless you added further tests, " \
  67. "there are good chances that something is wrong. " \
  68. "If you added tests with new schemas, you can try to " \
  69. "adjust the variable `max_cache_size` according to " \
  70. "the added schemas." % cache_size
  71. def test_expansion_in_nested_schema():
  72. schema = {'detroit': {'schema': {'anyof_regex': ['^Aladdin', 'Sane$']}}}
  73. v = Validator(schema)
  74. assert (v.schema['detroit']['schema'] ==
  75. {'anyof': [{'regex': '^Aladdin'}, {'regex': 'Sane$'}]})
  76. def test_unvalidated_schema_can_be_copied():
  77. schema = UnvalidatedSchema()
  78. schema_copy = schema.copy()
  79. assert schema_copy == schema