|
| 1 | +import unittest |
| 2 | + |
| 3 | +from jsonschema import ValidationError, validate |
| 4 | + |
| 5 | +from netjsonconfig.exceptions import list_error_in_subschema |
| 6 | + |
| 7 | +schema = { |
| 8 | + '$schema': 'http://json-schema.org/draft-04/schema#', |
| 9 | + 'type': 'object', |
| 10 | + 'additionalProperties': True, |
| 11 | + 'definitions': { |
| 12 | + 'spam_object': { |
| 13 | + 'additionalProperties': True, |
| 14 | + 'required': [ |
| 15 | + 'spam', |
| 16 | + ], |
| 17 | + 'properties': { |
| 18 | + 'spam': { |
| 19 | + 'type': 'string', |
| 20 | + }, |
| 21 | + }, |
| 22 | + }, |
| 23 | + 'eggs_object': { |
| 24 | + 'additionalProperties': True, |
| 25 | + 'required': [ |
| 26 | + 'eggs', |
| 27 | + ], |
| 28 | + 'properties': { |
| 29 | + 'eggs': { |
| 30 | + 'type': 'boolean', |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + 'properties': { |
| 36 | + 'test_object': { |
| 37 | + 'type': 'object', |
| 38 | + 'oneOf': [ |
| 39 | + {'$ref': '#/definitions/spam_object'}, |
| 40 | + {'$ref': '#/definitions/eggs_object'}, |
| 41 | + ], |
| 42 | + } |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +class TestJsonSchema(unittest.TestCase): |
| 48 | + """ |
| 49 | + tests ValidationError helpers |
| 50 | + """ |
| 51 | + |
| 52 | + def test_spam_object(self): |
| 53 | + test_i = { |
| 54 | + 'test_object': { |
| 55 | + 'spam': 'lots of', |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + validate(test_i, schema) |
| 60 | + |
| 61 | + def test_eggs_object(self): |
| 62 | + test_i = { |
| 63 | + 'test_object': { |
| 64 | + 'eggs': True, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + validate(test_i, schema) |
| 69 | + |
| 70 | + def test_burrito_object(self): |
| 71 | + test_i = { |
| 72 | + 'test_object': { |
| 73 | + 'burrito': 'yes', |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + self.assertRaises(ValidationError, validate, test_i, schema) |
| 78 | + |
| 79 | + def test_burrito_error_message(self): |
| 80 | + test_i = { |
| 81 | + 'test_object': { |
| 82 | + 'burrito': 'yes', |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + with self.assertRaises(ValidationError) as error_container: |
| 87 | + validate(test_i, schema) |
| 88 | + |
| 89 | + message_list = [ |
| 90 | + "'spam' is a required property", |
| 91 | + "'eggs' is a required property", |
| 92 | + ] |
| 93 | + |
| 94 | + self.assertEqual([e.message for e in error_container.exception.context], message_list) |
| 95 | + |
| 96 | + def test_list_error_in_subschema(self): |
| 97 | + test_i = { |
| 98 | + 'test_object': { |
| 99 | + 'burrito': 'yes', |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + with self.assertRaises(ValidationError) as error_container: |
| 104 | + validate(test_i, schema) |
| 105 | + |
| 106 | + suberror_list = [ |
| 107 | + ({'$ref': '#/definitions/spam_object'}, "'spam' is a required property"), |
| 108 | + |
| 109 | + ({'$ref': '#/definitions/eggs_object'}, "'eggs' is a required property"), |
| 110 | + ] |
| 111 | + |
| 112 | + self.assertEqual(list_error_in_subschema(error_container.exception), suberror_list) |
0 commit comments