conftest.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding: utf-8 -*-
  2. from copy import deepcopy
  3. import pytest
  4. from cerberus import Validator
  5. @pytest.fixture
  6. def document():
  7. return deepcopy(sample_document)
  8. @pytest.fixture
  9. def schema():
  10. return deepcopy(sample_schema)
  11. @pytest.fixture
  12. def validator():
  13. return Validator(sample_schema)
  14. sample_schema = {
  15. 'a_string': {
  16. 'type': 'string',
  17. 'minlength': 2,
  18. 'maxlength': 10
  19. },
  20. 'a_binary': {
  21. 'type': 'binary',
  22. 'minlength': 2,
  23. 'maxlength': 10
  24. },
  25. 'a_nullable_integer': {
  26. 'type': 'integer',
  27. 'nullable': True
  28. },
  29. 'an_integer': {
  30. 'type': 'integer',
  31. 'min': 1,
  32. 'max': 100,
  33. },
  34. 'a_restricted_integer': {
  35. 'type': 'integer',
  36. 'allowed': [-1, 0, 1],
  37. },
  38. 'a_boolean': {
  39. 'type': 'boolean',
  40. },
  41. 'a_datetime': {
  42. 'type': 'datetime',
  43. },
  44. 'a_float': {
  45. 'type': 'float',
  46. 'min': 1,
  47. 'max': 100,
  48. },
  49. 'a_number': {
  50. 'type': 'number',
  51. 'min': 1,
  52. 'max': 100,
  53. },
  54. 'a_set': {
  55. 'type': 'set',
  56. },
  57. 'one_or_more_strings': {
  58. 'type': ['string', 'list'],
  59. 'schema': {'type': 'string'}
  60. },
  61. 'a_regex_email': {
  62. 'type': 'string',
  63. 'regex': '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
  64. },
  65. 'a_readonly_string': {
  66. 'type': 'string',
  67. 'readonly': True,
  68. },
  69. 'a_restricted_string': {
  70. 'type': 'string',
  71. 'allowed': ["agent", "client", "vendor"],
  72. },
  73. 'an_array': {
  74. 'type': 'list',
  75. 'allowed': ["agent", "client", "vendor"],
  76. },
  77. 'a_list_of_dicts': {
  78. 'type': 'list',
  79. 'schema': {
  80. 'type': 'dict',
  81. 'schema': {
  82. 'sku': {'type': 'string'},
  83. 'price': {'type': 'integer', 'required': True},
  84. },
  85. },
  86. },
  87. 'a_list_of_values': {
  88. 'type': 'list',
  89. 'items': [{'type': 'string'}, {'type': 'integer'}, ]
  90. },
  91. 'a_list_of_integers': {
  92. 'type': 'list',
  93. 'schema': {'type': 'integer'},
  94. },
  95. 'a_dict': {
  96. 'type': 'dict',
  97. 'schema': {
  98. 'address': {'type': 'string'},
  99. 'city': {'type': 'string', 'required': True}
  100. },
  101. },
  102. 'a_dict_with_valueschema': {
  103. 'type': 'dict',
  104. 'valueschema': {'type': 'integer'}
  105. },
  106. 'a_dict_with_keyschema': {
  107. 'type': 'dict',
  108. 'keyschema': {'type': 'string', 'regex': '[a-z]+'}
  109. },
  110. 'a_list_length': {
  111. 'type': 'list',
  112. 'schema': {'type': 'integer'},
  113. 'minlength': 2,
  114. 'maxlength': 5,
  115. },
  116. 'a_nullable_field_without_type': {
  117. 'nullable': True
  118. },
  119. 'a_not_nullable_field_without_type': {
  120. },
  121. }
  122. sample_document = {'name': 'john doe'}