Skip to content

Commit

Permalink
Merge remote-tracking branch 'playpauseandstop/master'
Browse files Browse the repository at this point in the history
* playpauseandstop/master:
  Provide test cases to previous fix to ensure proper error handling items errors.
  Fix formatting error for unique items if instance is tuple.
  • Loading branch information
Julian committed Sep 17, 2015
2 parents 96c5f47 + 7c4eefb commit 94c60a4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion jsonschema/_validators.py
Expand Up @@ -133,7 +133,7 @@ def uniqueItems(validator, uI, instance, schema):
validator.is_type(instance, "array") and
not _utils.uniq(instance)
):
yield ValidationError("%r has non-unique elements" % instance)
yield ValidationError("%r has non-unique elements" % (instance,))


def pattern(validator, patrn, instance, schema):
Expand Down
60 changes: 60 additions & 0 deletions jsonschema/tests/test_validators.py
Expand Up @@ -899,6 +899,66 @@ def test_helpful_error_message_on_failed_pop_scope(self):
self.assertIn("Failed to pop the scope", str(exc.exception))


OVERRIDE_ARRAY_TYPES = Draft4Validator.DEFAULT_TYPES.copy()
OVERRIDE_ARRAY_TYPES['array'] = (list, tuple)


class ItemsErrorsMixin(object):

array_type = list
schema = {
'type': 'array',
'items': {
'type': 'number',
},
'minItems': 2,
'maxItems': 3,
'uniqueItems': True,
}
validator_class = None

def check_error_message(self, instance):
self.assertIsNotNone(self.validator_class)
self.assertRaises(ValidationError,
validate,
instance,
self.schema,
self.validator_class)

def test_min_items(self):
self.check_error_message(self.array_type([1]))

def test_max_items(self):
self.check_error_message(self.array_type([1, 2, 3, 4]))

def test_unique_items(self):
self.check_error_message(self.array_type([1, 1, 2]))

def test_valid(self):
self.assertIsNotNone(self.validator_class)
validate(self.array_type([1, 2]), self.schema, self.validator_class)


class OverrideArrayTypeValidator(Draft4Validator):

DEFAULT_TYPES = OVERRIDE_ARRAY_TYPES


class TestItemsErrorsDraft3(ItemsErrorsMixin, unittest.TestCase):

validator_class = Draft3Validator


class TestItemsErrorsDraft4(ItemsErrorsMixin, unittest.TestCase):

validator_class = Draft4Validator


class TestItemsErrorMessagesCustom(ItemsErrorsMixin, unittest.TestCase):

validator_class = OverrideArrayTypeValidator


def sorted_errors(errors):
def key(error):
return (
Expand Down

0 comments on commit 94c60a4

Please sign in to comment.