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.

88 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(
  28. {"foo": "bar"},
  29. {"type": "object", "properties": {"foo": {"type": "string"}}},
  30. )
  31. def test_it_checks_dict_type(self):
  32. with self.assertRaises(ValidationError):
  33. validate("not-object", {"type": "object"})
  34. def test_it_validates_objects_properties(self):
  35. with self.assertRaises(ValidationError):
  36. validate(
  37. {"foo": "bar"},
  38. {"type": "object", "properties": {"foo": {"type": "number"}}},
  39. )
  40. def test_it_handles_required_properties(self):
  41. with self.assertRaises(ValidationError):
  42. validate({"foo": "bar"}, {"type": "object", "required": ["baz"]})
  43. def test_it_validates_arrays(self):
  44. validate(["foo", "bar"], {"type": "array", "items": {"type": "string"}})
  45. def test_it_validates_array_type(self):
  46. with self.assertRaises(ValidationError):
  47. validate("not-an-array", {"type": "array", "items": {"type": "string"}})
  48. def test_it_validates_array_elements(self):
  49. with self.assertRaises(ValidationError):
  50. validate(["foo", "bar"], {"type": "array", "items": {"type": "number"}})
  51. def test_it_validates_enum(self):
  52. validate("foo", {"enum": ["foo", "bar"]})
  53. def test_it_rejects_a_value_not_in_enum(self):
  54. with self.assertRaises(ValidationError):
  55. validate("baz", {"enum": ["foo", "bar"]})
  56. def test_it_checks_cron_format(self):
  57. samples = ["x * * * *", "0 0 31 2 *"]
  58. for sample in samples:
  59. with self.assertRaises(ValidationError):
  60. validate(sample, {"type": "string", "format": "cron"})
  61. def test_it_checks_timezone_format(self):
  62. with self.assertRaises(ValidationError):
  63. validate("X/Y", {"type": "string", "format": "timezone"})
  64. def test_it_checks_boolean_type(self):
  65. with self.assertRaises(ValidationError):
  66. validate(123, {"type": "boolean"})