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.

76 lines
2.3 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_validates_numbers(self):
  10. validate(123, {"type": "number", "minimum": 0, "maximum": 1000})
  11. def test_it_checks_int_type(self):
  12. with self.assertRaises(ValidationError):
  13. validate("foo", {"type": "number"})
  14. def test_it_checks_min_value(self):
  15. with self.assertRaises(ValidationError):
  16. validate(5, {"type": "number", "minimum": 10})
  17. def test_it_checks_max_value(self):
  18. with self.assertRaises(ValidationError):
  19. validate(5, {"type": "number", "maximum": 0})
  20. def test_it_validates_objects(self):
  21. validate({"foo": "bar"}, {
  22. "type": "object",
  23. "properties": {
  24. "foo": {"type": "string"}
  25. }
  26. })
  27. def test_it_checks_dict_type(self):
  28. with self.assertRaises(ValidationError):
  29. validate("not-object", {"type": "object"})
  30. def test_it_validates_objects_properties(self):
  31. with self.assertRaises(ValidationError):
  32. validate({"foo": "bar"}, {
  33. "type": "object",
  34. "properties": {
  35. "foo": {"type": "number"}
  36. }
  37. })
  38. def test_it_validates_arrays(self):
  39. validate(["foo", "bar"], {
  40. "type": "array",
  41. "items": {"type": "string"}
  42. })
  43. def test_it_validates_array_type(self):
  44. with self.assertRaises(ValidationError):
  45. validate("not-an-array", {
  46. "type": "array",
  47. "items": {"type": "string"}
  48. })
  49. def test_it_validates_array_elements(self):
  50. with self.assertRaises(ValidationError):
  51. validate(["foo", "bar"], {
  52. "type": "array",
  53. "items": {"type": "number"}
  54. })
  55. def test_it_validates_enum(self):
  56. validate("foo", {"enum": ["foo", "bar"]})
  57. def test_it_rejects_a_value_not_in_enum(self):
  58. with self.assertRaises(ValidationError):
  59. validate("baz", {"enum": ["foo", "bar"]})