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
2.8 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_length(self):
  10. with self.assertRaises(ValidationError):
  11. validate("abcd", {"type": "string", "maxLength": 3})
  12. def test_it_validates_numbers(self):
  13. validate(123, {"type": "number", "minimum": 0, "maximum": 1000})
  14. def test_it_checks_int_type(self):
  15. with self.assertRaises(ValidationError):
  16. validate("foo", {"type": "number"})
  17. def test_it_checks_min_value(self):
  18. with self.assertRaises(ValidationError):
  19. validate(5, {"type": "number", "minimum": 10})
  20. def test_it_checks_max_value(self):
  21. with self.assertRaises(ValidationError):
  22. validate(5, {"type": "number", "maximum": 0})
  23. def test_it_validates_objects(self):
  24. validate({"foo": "bar"}, {
  25. "type": "object",
  26. "properties": {
  27. "foo": {"type": "string"}
  28. }
  29. })
  30. def test_it_checks_dict_type(self):
  31. with self.assertRaises(ValidationError):
  32. validate("not-object", {"type": "object"})
  33. def test_it_validates_objects_properties(self):
  34. with self.assertRaises(ValidationError):
  35. validate({"foo": "bar"}, {
  36. "type": "object",
  37. "properties": {
  38. "foo": {"type": "number"}
  39. }
  40. })
  41. def test_it_validates_arrays(self):
  42. validate(["foo", "bar"], {
  43. "type": "array",
  44. "items": {"type": "string"}
  45. })
  46. def test_it_validates_array_type(self):
  47. with self.assertRaises(ValidationError):
  48. validate("not-an-array", {
  49. "type": "array",
  50. "items": {"type": "string"}
  51. })
  52. def test_it_validates_array_elements(self):
  53. with self.assertRaises(ValidationError):
  54. validate(["foo", "bar"], {
  55. "type": "array",
  56. "items": {"type": "number"}
  57. })
  58. def test_it_validates_enum(self):
  59. validate("foo", {"enum": ["foo", "bar"]})
  60. def test_it_rejects_a_value_not_in_enum(self):
  61. with self.assertRaises(ValidationError):
  62. validate("baz", {"enum": ["foo", "bar"]})
  63. def test_it_checks_cron_format(self):
  64. with self.assertRaises(ValidationError):
  65. validate("x * * * *", {"type": "string", "format": "cron"})
  66. def test_it_checks_timezone_format(self):
  67. with self.assertRaises(ValidationError):
  68. validate("X/Y", {"type": "string", "format": "timezone"})