test_registries.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # -*- coding: utf-8 -*-
  2. from cerberus import schema_registry, rules_set_registry, Validator
  3. from cerberus.tests import (assert_fail, assert_normalized,
  4. assert_schema_error, assert_success)
  5. def test_schema_registry_simple():
  6. schema_registry.add('foo', {'bar': {'type': 'string'}})
  7. schema = {'a': {'schema': 'foo'},
  8. 'b': {'schema': 'foo'}}
  9. document = {'a': {'bar': 'a'}, 'b': {'bar': 'b'}}
  10. assert_success(document, schema)
  11. def test_top_level_reference():
  12. schema_registry.add('peng', {'foo': {'type': 'integer'}})
  13. document = {'foo': 42}
  14. assert_success(document, 'peng')
  15. def test_rules_set_simple():
  16. rules_set_registry.add('foo', {'type': 'integer'})
  17. assert_success({'bar': 1}, {'bar': 'foo'})
  18. assert_fail({'bar': 'one'}, {'bar': 'foo'})
  19. def test_allow_unknown_as_reference():
  20. rules_set_registry.add('foo', {'type': 'number'})
  21. v = Validator(allow_unknown='foo')
  22. assert_success({0: 1}, {}, v)
  23. assert_fail({0: 'one'}, {}, v)
  24. def test_recursion():
  25. rules_set_registry.add('self',
  26. {'type': 'dict', 'allow_unknown': 'self'})
  27. v = Validator(allow_unknown='self')
  28. assert_success({0: {1: {2: {}}}}, {}, v)
  29. def test_references_remain_unresolved(validator):
  30. rules_set_registry.extend((('boolean', {'type': 'boolean'}),
  31. ('booleans', {'valueschema': 'boolean'})))
  32. validator.schema = {'foo': 'booleans'}
  33. assert 'booleans' == validator.schema['foo']
  34. assert 'boolean' == rules_set_registry._storage['booleans']['valueschema']
  35. def test_rules_registry_with_anyof_type():
  36. rules_set_registry.add('string_or_integer',
  37. {'anyof_type': ['string', 'integer']})
  38. schema = {'soi': 'string_or_integer'}
  39. assert_success({'soi': 'hello'}, schema)
  40. def test_schema_registry_with_anyof_type():
  41. schema_registry.add('soi_id', {'id': {'anyof_type': ['string', 'integer']}})
  42. schema = {'soi': {'schema': 'soi_id'}}
  43. assert_success({'soi': {'id': 'hello'}}, schema)
  44. def test_normalization_with_rules_set():
  45. # https://github.com/pyeve/cerberus/issues/283
  46. rules_set_registry.add('foo', {'default': 42})
  47. assert_normalized({}, {'bar': 42}, {'bar': 'foo'})
  48. rules_set_registry.add('foo', {'default_setter': lambda _: 42})
  49. assert_normalized({}, {'bar': 42}, {'bar': 'foo'})
  50. rules_set_registry.add('foo', {'type': 'integer', 'nullable': True})
  51. assert_success({'bar': None}, {'bar': 'foo'})
  52. def test_rules_set_with_dict_field():
  53. document = {'a_dict': {'foo': 1}}
  54. schema = {'a_dict': {'type': 'dict', 'schema': {'foo': 'rule'}}}
  55. # the schema's not yet added to the valid ones, so test the faulty first
  56. rules_set_registry.add('rule', {'tüpe': 'integer'})
  57. assert_schema_error(document, schema)
  58. rules_set_registry.add('rule', {'type': 'integer'})
  59. assert_success(document, schema)