You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.2 KiB

  1. from django.test import TestCase
  2. from hc.lib.jsonschema import ValidationError, validate
  3. class JsonSchemaTestCase(TestCase):
  4. def test_it_validates_strings(self):
  5. validate("foo", {"type": "string"})
  6. def test_it_checks_string_type(self):
  7. with self.assertRaises(ValidationError):
  8. validate(123, {"type": "string"})
  9. def test_it_checks_string_min_length(self):
  10. with self.assertRaises(ValidationError):
  11. validate("abcd", {"type": "string", "minLength": 5})
  12. def test_it_checks_string_length(self):
  13. with self.assertRaises(ValidationError):
  14. validate("abcd", {"type": "string", "maxLength": 3})
  15. def test_it_validates_numbers(self):
  16. validate(123, {"type": "number", "minimum": 0, "maximum": 1000})
  17. def test_it_checks_int_type(self):
  18. with self.assertRaises(ValidationError):
  19. validate("foo", {"type": "number"})
  20. def test_it_checks_min_value(self):
  21. with self.assertRaises(ValidationError):
  22. validate(5, {"type": "number", "minimum": 10})
  23. def test_it_checks_max_value(self):
  24. with self.assertRaises(ValidationError):
  25. validate(5, {"type": "number", "maximum": 0})
  26. def test_it_validates_objects(self):
  27. validate({"foo": "bar"}, {
  28. "type": "object",
  29. "properties": {
  30. "foo": {"type": "string"}
  31. }
  32. })
  33. def test_it_checks_dict_type(self):
  34. with self.assertRaises(ValidationError):
  35. validate("not-object", {"type": "object"})
  36. def test_it_validates_objects_properties(self):
  37. with self.assertRaises(ValidationError):
  38. validate({"foo": "bar"}, {
  39. "type": "object",
  40. "properties": {
  41. "foo": {"type": "number"}
  42. }
  43. })
  44. def test_it_handles_required_properties(self):
  45. with self.assertRaises(ValidationError):
  46. validate({"foo": "bar"}, {
  47. "type": "object",
  48. "required": ["baz"]
  49. })
  50. def test_it_validates_arrays(self):
  51. validate(["foo", "bar"], {
  52. "type": "array",
  53. "items": {"type": "string"}
  54. })
  55. def test_it_validates_array_type(self):
  56. with self.assertRaises(ValidationError):
  57. validate("not-an-array", {
  58. "type": "array",
  59. "items": {"type": "string"}
  60. })
  61. def test_it_validates_array_elements(self):
  62. with self.assertRaises(ValidationError):
  63. validate(["foo", "bar"], {
  64. "type": "array",
  65. "items": {"type": "number"}
  66. })
  67. def test_it_validates_enum(self):
  68. validate("foo", {"enum": ["foo", "bar"]})
  69. def test_it_rejects_a_value_not_in_enum(self):
  70. with self.assertRaises(ValidationError):
  71. validate("baz", {"enum": ["foo", "bar"]})
  72. def test_it_checks_cron_format(self):
  73. with self.assertRaises(ValidationError):
  74. validate("x * * * *", {"type": "string", "format": "cron"})
  75. def test_it_checks_timezone_format(self):
  76. with self.assertRaises(ValidationError):
  77. validate("X/Y", {"type": "string", "format": "timezone"})