Skip to content

Commit 80f18a8

Browse files
committed
[qa] Minor style improvements to changes introduced in 325b511
1 parent 6d95dd6 commit 80f18a8

3 files changed

Lines changed: 106 additions & 125 deletions

File tree

netjsonconfig/exceptions.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
from functools import reduce
22

33

4-
def list_error_in_subschema(jsonschema_error):
5-
sub_errors = []
6-
for validator_value, error in zip(jsonschema_error.validator_value, jsonschema_error.context):
7-
sub_errors.append((validator_value, error.message))
4+
def _list_errors(e):
5+
"""
6+
Returns a list of violated schema fragments and related error messages
7+
:param e: ``jsonschema.exceptions.ValidationError`` instance
8+
"""
9+
error_list = []
10+
for value, error in zip(e.validator_value, e.context):
11+
error_list.append((value, error.message))
812
if error.context:
9-
sub_errors += list_error_in_subschema(error)
10-
return sub_errors
13+
error_list += _list_errors(error)
14+
return error_list
1115

1216

1317
class NetJsonConfigException(Exception):
1418
"""
1519
Root netjsonconfig exception
1620
"""
1721
def __str__(self):
18-
suberrors = list_error_in_subschema(self.details)
19-
20-
default_message = "%s %s\n" % (self.__class__.__name__, self.details,)
21-
suberror_fmt = '\nAgainst schema %s\n%s\n'
22-
suberror_message = reduce(lambda x, y: x + suberror_fmt % y, suberrors, '')
23-
24-
return default_message + suberror_message
22+
message = "%s %s\n" % (self.__class__.__name__, self.details,)
23+
errors = _list_errors(self.details)
24+
separator = '\nAgainst schema %s\n%s\n'
25+
details = reduce(lambda x, y: x + separator % y, errors, '')
26+
return message + details
2527

2628

2729
class ValidationError(NetJsonConfigException):

tests/test_exceptions.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import unittest
2+
3+
from jsonschema import ValidationError, validate
4+
5+
from netjsonconfig import OpenWrt
6+
from netjsonconfig.exceptions import _list_errors
7+
8+
schema = {
9+
'$schema': 'http://json-schema.org/draft-04/schema#',
10+
'type': 'object',
11+
'additionalProperties': True,
12+
'definitions': {
13+
'spam_object': {
14+
'additionalProperties': True,
15+
'required': [
16+
'spam',
17+
],
18+
'properties': {
19+
'spam': {
20+
'type': 'string',
21+
},
22+
},
23+
},
24+
'eggs_object': {
25+
'additionalProperties': True,
26+
'required': [
27+
'eggs',
28+
],
29+
'properties': {
30+
'eggs': {
31+
'type': 'boolean',
32+
},
33+
},
34+
},
35+
},
36+
'properties': {
37+
'test_object': {
38+
'type': 'object',
39+
'oneOf': [
40+
{'$ref': '#/definitions/spam_object'},
41+
{'$ref': '#/definitions/eggs_object'},
42+
],
43+
}
44+
}
45+
}
46+
47+
48+
class TestJsonSchema(unittest.TestCase):
49+
"""
50+
tests ValidationError helpers
51+
"""
52+
def test_spam_object(self):
53+
test_i = {'test_object': {'spam': 'lots of'}}
54+
validate(test_i, schema)
55+
56+
def test_eggs_object(self):
57+
test_i = {'test_object': {'eggs': True}}
58+
validate(test_i, schema)
59+
60+
def test_burrito_object(self):
61+
test_i = {'test_object': {'burrito': 'yes'}}
62+
self.assertRaises(ValidationError, validate, test_i, schema)
63+
64+
def test_burrito_error_message(self):
65+
test_i = {'test_object': {'burrito': 'yes'}}
66+
with self.assertRaises(ValidationError) as e:
67+
validate(test_i, schema)
68+
message_list = [
69+
"'spam' is a required property",
70+
"'eggs' is a required property",
71+
]
72+
self.assertEqual([err.message for err in e.exception.context], message_list)
73+
74+
def test_list_errors(self):
75+
test_i = {'test_object': {'burrito': 'yes'}}
76+
with self.assertRaises(ValidationError) as e:
77+
validate(test_i, schema)
78+
suberror_list = [
79+
({'$ref': '#/definitions/spam_object'}, "'spam' is a required property"),
80+
({'$ref': '#/definitions/eggs_object'}, "'eggs' is a required property"),
81+
]
82+
self.assertEqual(_list_errors(e.exception), suberror_list)
83+
84+
def test_error_str(self):
85+
o = OpenWrt({'interfaces': [{'wrong': True}]})
86+
try:
87+
o.validate()
88+
except Exception as e:
89+
self.assertIn('Against schema', str(e))
90+
else:
91+
self.fail('ValidationError not raised')

tests/test_jsonschema_error.py

Lines changed: 0 additions & 112 deletions
This file was deleted.

0 commit comments

Comments
 (0)