<s> from collections import deque <EOL> from contextlib import contextmanager <EOL> import json <EOL> from jsonschema import FormatChecker , ValidationError <EOL> from jsonschema . tests . compat import mock , unittest <EOL> from jsonschema . validators import ( <EOL> RefResolutionError , UnknownType , Draft3Validator , <EOL> Draft4Validator , RefResolver , create , extend , validator_for , validate , <EOL> ) <EOL> class TestCreateAndExtend ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . meta_schema = { u"<STR_LIT>" : { u"<STR_LIT>" : { } } } <EOL> self . smelly = mock . MagicMock ( ) <EOL> self . validators = { u"<STR_LIT>" : self . smelly } <EOL> self . types = { u"<STR_LIT>" : dict } <EOL> self . Validator = create ( <EOL> meta_schema = self . meta_schema , <EOL> validators = self . validators , <EOL> default_types = self . types , <EOL> ) <EOL> self . validator_value = <NUM_LIT:12> <EOL> self . schema = { u"<STR_LIT>" : self . validator_value } <EOL> self . validator = self . Validator ( self . schema ) <EOL> def test_attrs ( self ) : <EOL> self . assertEqual ( self . Validator . VALIDATORS , self . validators ) <EOL> self . assertEqual ( self . Validator . META_SCHEMA , self . meta_schema ) <EOL> self . assertEqual ( self . Validator . DEFAULT_TYPES , self . types ) <EOL> def test_init ( self ) : <EOL> self . assertEqual ( self . validator . schema , self . schema ) <EOL> def test_iter_errors ( self ) : <EOL> instance = "<STR_LIT:hello>" <EOL> self . smelly . return_value = [ ] <EOL> self . assertEqual ( list ( self . validator . iter_errors ( instance ) ) , [ ] ) <EOL> error = mock . Mock ( ) <EOL> self . smelly . return_value = [ error ] <EOL> self . assertEqual ( list ( self . validator . iter_errors ( instance ) ) , [ error ] ) <EOL> self . smelly . assert_called_with ( <EOL> self . validator , self . validator_value , instance , self . schema , <EOL> ) <EOL> def test_if_a_version_is_provided_it_is_registered ( self ) : <EOL> with mock . patch ( "<STR_LIT>" ) as validates : <EOL> validates . side_effect = lambda version : lambda cls : cls <EOL> Validator = create ( meta_schema = { u"<STR_LIT:id>" : "<STR_LIT>" } , version = "<STR_LIT>" ) <EOL> validates . assert_called_once_with ( "<STR_LIT>" ) <EOL> self . assertEqual ( Validator . __name__ , "<STR_LIT>" ) <EOL> def test_if_a_version_is_not_provided_it_is_not_registered ( self ) : <EOL> with mock . patch ( "<STR_LIT>" ) as validates : <EOL> create ( meta_schema = { u"<STR_LIT:id>" : "<STR_LIT:id>" } ) <EOL> self . assertFalse ( validates . called ) <EOL> def test_extend ( self ) : <EOL> validators = dict ( self . Validator . VALIDATORS ) <EOL> new = mock . Mock ( ) <EOL> Extended = extend ( self . Validator , validators = { u"<STR_LIT>" : new } ) <EOL> validators . update ( [ ( u"<STR_LIT>" , new ) ] ) <EOL> self . assertEqual ( Extended . VALIDATORS , validators ) <EOL> self . assertNotIn ( u"<STR_LIT>" , self . Validator . VALIDATORS ) <EOL> self . assertEqual ( Extended . META_SCHEMA , self . Validator . META_SCHEMA ) <EOL> self . assertEqual ( Extended . DEFAULT_TYPES , self . Validator . DEFAULT_TYPES ) <EOL> class TestIterErrors ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . validator = Draft3Validator ( { } ) <EOL> def test_iter_errors ( self ) : <EOL> instance = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> schema = { <EOL> u"<STR_LIT>" : u"<STR_LIT>" , <EOL> u"<STR_LIT>" : [ [ "<STR_LIT:a>" , "<STR_LIT:b>" , "<STR_LIT:c>" ] , [ "<STR_LIT:d>" , "<STR_LIT:e>" , "<STR_LIT:f>" ] ] , <EOL> u"<STR_LIT>" : <NUM_LIT:3> <EOL> } <EOL> got = ( e . message for e in self . validator . iter_errors ( instance , schema ) ) <EOL> expected = [ <EOL> "<STR_LIT>" % ( schema [ "<STR_LIT>" ] , ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" % ( schema [ "<STR_LIT>" ] , ) , <EOL> ] <EOL> self . assertEqual ( sorted ( got ) , sorted ( expected ) ) <EOL> def test_iter_errors_multiple_failures_one_validator ( self ) : <EOL> instance = { "<STR_LIT:foo>" : <NUM_LIT:2> , "<STR_LIT:bar>" : [ <NUM_LIT:1> ] , "<STR_LIT>" : <NUM_LIT:15> , "<STR_LIT>" : "<STR_LIT>" } <EOL> schema = { <EOL> u"<STR_LIT>" : { <EOL> "<STR_LIT:foo>" : { u"<STR_LIT:type>" : "<STR_LIT:string>" } , <EOL> "<STR_LIT:bar>" : { u"<STR_LIT>" : <NUM_LIT:2> } , <EOL> "<STR_LIT>" : { u"<STR_LIT>" : <NUM_LIT:10> , u"<STR_LIT>" : [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> ] } , <EOL> } <EOL> } <EOL> errors = list ( self . validator . iter_errors ( instance , schema ) ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:4> ) <EOL> class TestValidationErrorMessages ( unittest . TestCase ) : <EOL> def message_for ( self , instance , schema , * args , ** kwargs ) : <EOL> kwargs . setdefault ( "<STR_LIT>" , Draft3Validator ) <EOL> with self . assertRaises ( ValidationError ) as e : <EOL> validate ( instance , schema , * args , ** kwargs ) <EOL> return e . exception . message <EOL> def test_single_type_failure ( self ) : <EOL> message = self . message_for ( instance = <NUM_LIT:1> , schema = { u"<STR_LIT:type>" : u"<STR_LIT:string>" } ) <EOL> self . assertEqual ( message , "<STR_LIT>" % u"<STR_LIT:string>" ) <EOL> def test_single_type_list_failure ( self ) : <EOL> message = self . message_for ( instance = <NUM_LIT:1> , schema = { u"<STR_LIT:type>" : [ u"<STR_LIT:string>" ] } ) <EOL> self . assertEqual ( message , "<STR_LIT>" % u"<STR_LIT:string>" ) <EOL> def test_multiple_type_failure ( self ) : <EOL> types = u"<STR_LIT:string>" , u"<STR_LIT:object>" <EOL> message = self . message_for ( instance = <NUM_LIT:1> , schema = { u"<STR_LIT:type>" : list ( types ) } ) <EOL> self . assertEqual ( message , "<STR_LIT>" % types ) <EOL> def test_object_without_title_type_failure ( self ) : <EOL> type = { u"<STR_LIT:type>" : [ { u"<STR_LIT>" : <NUM_LIT:3> } ] } <EOL> message = self . message_for ( instance = <NUM_LIT:1> , schema = { u"<STR_LIT:type>" : [ type ] } ) <EOL> self . assertEqual ( message , "<STR_LIT>" % ( type , ) ) <EOL> def test_object_with_name_type_failure ( self ) : <EOL> name = "<STR_LIT>" <EOL> schema = { u"<STR_LIT:type>" : [ { u"<STR_LIT:name>" : name , u"<STR_LIT>" : <NUM_LIT:3> } ] } <EOL> message = self . message_for ( instance = <NUM_LIT:1> , schema = schema ) <EOL> self . assertEqual ( message , "<STR_LIT>" % ( name , ) ) <EOL> def test_minimum ( self ) : <EOL> message = self . message_for ( instance = <NUM_LIT:1> , schema = { "<STR_LIT>" : <NUM_LIT:2> } ) <EOL> self . assertEqual ( message , "<STR_LIT>" ) <EOL> def test_maximum ( self ) : <EOL> message = self . message_for ( instance = <NUM_LIT:1> , schema = { "<STR_LIT>" : <NUM_LIT:0> } ) <EOL> self . assertEqual ( message , "<STR_LIT>" ) <EOL> def test_dependencies_failure_has_single_element_not_list ( self ) : <EOL> depend , on = "<STR_LIT:bar>" , "<STR_LIT:foo>" <EOL> schema = { u"<STR_LIT>" : { depend : on } } <EOL> message = self . message_for ( { "<STR_LIT:bar>" : <NUM_LIT:2> } , schema ) <EOL> self . assertEqual ( message , "<STR_LIT>" % ( on , depend ) ) <EOL> def test_additionalItems_single_failure ( self ) : <EOL> message = self . message_for ( <EOL> [ <NUM_LIT:2> ] , { u"<STR_LIT>" : [ ] , u"<STR_LIT>" : False } , <EOL> ) <EOL> self . assertIn ( "<STR_LIT>" , message ) <EOL> def test_additionalItems_multiple_failures ( self ) : <EOL> message = self . message_for ( <EOL> [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , { u"<STR_LIT>" : [ ] , u"<STR_LIT>" : False } <EOL> ) <EOL> self . assertIn ( "<STR_LIT>" , message ) <EOL> def test_additionalProperties_single_failure ( self ) : <EOL> additional = "<STR_LIT:foo>" <EOL> schema = { u"<STR_LIT>" : False } <EOL> message = self . message_for ( { additional : <NUM_LIT:2> } , schema ) <EOL> self . assertIn ( "<STR_LIT>" % ( additional , ) , message ) <EOL> def test_additionalProperties_multiple_failures ( self ) : <EOL> schema = { u"<STR_LIT>" : False } <EOL> message = self . message_for ( dict . fromkeys ( [ "<STR_LIT:foo>" , "<STR_LIT:bar>" ] ) , schema ) <EOL> self . assertIn ( repr ( "<STR_LIT:foo>" ) , message ) <EOL> self . assertIn ( repr ( "<STR_LIT:bar>" ) , message ) <EOL> self . assertIn ( "<STR_LIT>" , message ) <EOL> def test_invalid_format_default_message ( self ) : <EOL> checker = FormatChecker ( formats = ( ) ) <EOL> check_fn = mock . Mock ( return_value = False ) <EOL> checker . checks ( u"<STR_LIT>" ) ( check_fn ) <EOL> schema = { u"<STR_LIT>" : u"<STR_LIT>" } <EOL> message = self . message_for ( "<STR_LIT>" , schema , format_checker = checker ) <EOL> self . assertIn ( repr ( "<STR_LIT>" ) , message ) <EOL> self . assertIn ( repr ( "<STR_LIT>" ) , message ) <EOL> self . assertIn ( "<STR_LIT>" , message ) <EOL> class TestValidationErrorDetails ( unittest . TestCase ) : <EOL> def test_anyOf ( self ) : <EOL> instance = <NUM_LIT:5> <EOL> schema = { <EOL> "<STR_LIT>" : [ <EOL> { "<STR_LIT>" : <NUM_LIT:20> } , <EOL> { "<STR_LIT:type>" : "<STR_LIT:string>" } <EOL> ] <EOL> } <EOL> validator = Draft4Validator ( schema ) <EOL> errors = list ( validator . iter_errors ( instance ) ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> e = errors [ <NUM_LIT:0> ] <EOL> self . assertEqual ( e . validator , "<STR_LIT>" ) <EOL> self . assertEqual ( e . validator_value , schema [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( e . instance , instance ) <EOL> self . assertEqual ( e . schema , schema ) <EOL> self . assertIsNone ( e . parent ) <EOL> self . assertEqual ( e . path , deque ( [ ] ) ) <EOL> self . assertEqual ( e . relative_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e . absolute_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e . schema_path , deque ( [ "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e . relative_schema_path , deque ( [ "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e . absolute_schema_path , deque ( [ "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( len ( e . context ) , <NUM_LIT:2> ) <EOL> e1 , e2 = sorted_errors ( e . context ) <EOL> self . assertEqual ( e1 . validator , "<STR_LIT>" ) <EOL> self . assertEqual ( e1 . validator_value , schema [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( e1 . instance , instance ) <EOL> self . assertEqual ( e1 . schema , schema [ "<STR_LIT>" ] [ <NUM_LIT:0> ] ) <EOL> self . assertIs ( e1 . parent , e ) <EOL> self . assertEqual ( e1 . path , deque ( [ ] ) ) <EOL> self . assertEqual ( e1 . absolute_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e1 . relative_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e1 . schema_path , deque ( [ <NUM_LIT:0> , "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e1 . relative_schema_path , deque ( [ <NUM_LIT:0> , "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( <EOL> e1 . absolute_schema_path , deque ( [ "<STR_LIT>" , <NUM_LIT:0> , "<STR_LIT>" ] ) , <EOL> ) <EOL> self . assertFalse ( e1 . context ) <EOL> self . assertEqual ( e2 . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e2 . validator_value , schema [ "<STR_LIT>" ] [ <NUM_LIT:1> ] [ "<STR_LIT:type>" ] ) <EOL> self . assertEqual ( e2 . instance , instance ) <EOL> self . assertEqual ( e2 . schema , schema [ "<STR_LIT>" ] [ <NUM_LIT:1> ] ) <EOL> self . assertIs ( e2 . parent , e ) <EOL> self . assertEqual ( e2 . path , deque ( [ ] ) ) <EOL> self . assertEqual ( e2 . relative_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e2 . absolute_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e2 . schema_path , deque ( [ <NUM_LIT:1> , "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( e2 . relative_schema_path , deque ( [ <NUM_LIT:1> , "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( e2 . absolute_schema_path , deque ( [ "<STR_LIT>" , <NUM_LIT:1> , "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( len ( e2 . context ) , <NUM_LIT:0> ) <EOL> def test_type ( self ) : <EOL> instance = { "<STR_LIT:foo>" : <NUM_LIT:1> } <EOL> schema = { <EOL> "<STR_LIT:type>" : [ <EOL> { "<STR_LIT:type>" : "<STR_LIT>" } , <EOL> { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:foo>" : { "<STR_LIT>" : [ <NUM_LIT:2> ] } <EOL> } <EOL> } <EOL> ] <EOL> } <EOL> validator = Draft3Validator ( schema ) <EOL> errors = list ( validator . iter_errors ( instance ) ) <EOL> self . assertEqual ( len ( errors ) , <NUM_LIT:1> ) <EOL> e = errors [ <NUM_LIT:0> ] <EOL> self . assertEqual ( e . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e . validator_value , schema [ "<STR_LIT:type>" ] ) <EOL> self . assertEqual ( e . instance , instance ) <EOL> self . assertEqual ( e . schema , schema ) <EOL> self . assertIsNone ( e . parent ) <EOL> self . assertEqual ( e . path , deque ( [ ] ) ) <EOL> self . assertEqual ( e . relative_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e . absolute_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e . schema_path , deque ( [ "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( e . relative_schema_path , deque ( [ "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( e . absolute_schema_path , deque ( [ "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( len ( e . context ) , <NUM_LIT:2> ) <EOL> e1 , e2 = sorted_errors ( e . context ) <EOL> self . assertEqual ( e1 . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e1 . validator_value , schema [ "<STR_LIT:type>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:type>" ] ) <EOL> self . assertEqual ( e1 . instance , instance ) <EOL> self . assertEqual ( e1 . schema , schema [ "<STR_LIT:type>" ] [ <NUM_LIT:0> ] ) <EOL> self . assertIs ( e1 . parent , e ) <EOL> self . assertEqual ( e1 . path , deque ( [ ] ) ) <EOL> self . assertEqual ( e1 . relative_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e1 . absolute_path , deque ( [ ] ) ) <EOL> self . assertEqual ( e1 . schema_path , deque ( [ <NUM_LIT:0> , "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( e1 . relative_schema_path , deque ( [ <NUM_LIT:0> , "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( e1 . absolute_schema_path , deque ( [ "<STR_LIT:type>" , <NUM_LIT:0> , "<STR_LIT:type>" ] ) ) <EOL> self . assertFalse ( e1 . context ) <EOL> self . assertEqual ( e2 . validator , "<STR_LIT>" ) <EOL> self . assertEqual ( e2 . validator_value , [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( e2 . instance , <NUM_LIT:1> ) <EOL> self . assertEqual ( e2 . schema , { u"<STR_LIT>" : [ <NUM_LIT:2> ] } ) <EOL> self . assertIs ( e2 . parent , e ) <EOL> self . assertEqual ( e2 . path , deque ( [ "<STR_LIT:foo>" ] ) ) <EOL> self . assertEqual ( e2 . relative_path , deque ( [ "<STR_LIT:foo>" ] ) ) <EOL> self . assertEqual ( e2 . absolute_path , deque ( [ "<STR_LIT:foo>" ] ) ) <EOL> self . assertEqual ( <EOL> e2 . schema_path , deque ( [ <NUM_LIT:1> , "<STR_LIT>" , "<STR_LIT:foo>" , "<STR_LIT>" ] ) , <EOL> ) <EOL> self . assertEqual ( <EOL> e2 . relative_schema_path , deque ( [ <NUM_LIT:1> , "<STR_LIT>" , "<STR_LIT:foo>" , "<STR_LIT>" ] ) , <EOL> ) <EOL> self . assertEqual ( <EOL> e2 . absolute_schema_path , <EOL> deque ( [ "<STR_LIT:type>" , <NUM_LIT:1> , "<STR_LIT>" , "<STR_LIT:foo>" , "<STR_LIT>" ] ) , <EOL> ) <EOL> self . assertFalse ( e2 . context ) <EOL> def test_single_nesting ( self ) : <EOL> instance = { "<STR_LIT:foo>" : <NUM_LIT:2> , "<STR_LIT:bar>" : [ <NUM_LIT:1> ] , "<STR_LIT>" : <NUM_LIT:15> , "<STR_LIT>" : "<STR_LIT>" } <EOL> schema = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:foo>" : { "<STR_LIT:type>" : "<STR_LIT:string>" } , <EOL> "<STR_LIT:bar>" : { "<STR_LIT>" : <NUM_LIT:2> } , <EOL> "<STR_LIT>" : { "<STR_LIT>" : <NUM_LIT:10> , "<STR_LIT>" : [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:8> ] } , <EOL> } <EOL> } <EOL> validator = Draft3Validator ( schema ) <EOL> errors = validator . iter_errors ( instance ) <EOL> e1 , e2 , e3 , e4 = sorted_errors ( errors ) <EOL> self . assertEqual ( e1 . path , deque ( [ "<STR_LIT:bar>" ] ) ) <EOL> self . assertEqual ( e2 . path , deque ( [ "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e3 . path , deque ( [ "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e4 . path , deque ( [ "<STR_LIT:foo>" ] ) ) <EOL> self . assertEqual ( e1 . relative_path , deque ( [ "<STR_LIT:bar>" ] ) ) <EOL> self . assertEqual ( e2 . relative_path , deque ( [ "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e3 . relative_path , deque ( [ "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e4 . relative_path , deque ( [ "<STR_LIT:foo>" ] ) ) <EOL> self . assertEqual ( e1 . absolute_path , deque ( [ "<STR_LIT:bar>" ] ) ) <EOL> self . assertEqual ( e2 . absolute_path , deque ( [ "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e3 . absolute_path , deque ( [ "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e4 . absolute_path , deque ( [ "<STR_LIT:foo>" ] ) ) <EOL> self . assertEqual ( e1 . validator , "<STR_LIT>" ) <EOL> self . assertEqual ( e2 . validator , "<STR_LIT>" ) <EOL> self . assertEqual ( e3 . validator , "<STR_LIT>" ) <EOL> self . assertEqual ( e4 . validator , "<STR_LIT:type>" ) <EOL> def test_multiple_nesting ( self ) : <EOL> instance = [ <NUM_LIT:1> , { "<STR_LIT:foo>" : <NUM_LIT:2> , "<STR_LIT:bar>" : { "<STR_LIT>" : [ <NUM_LIT:1> ] } } , "<STR_LIT>" ] <EOL> schema = { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : [ "<STR_LIT:string>" , "<STR_LIT:object>" ] , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:foo>" : { "<STR_LIT>" : [ <NUM_LIT:1> , <NUM_LIT:3> ] } , <EOL> "<STR_LIT:bar>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:bar>" : { "<STR_LIT>" : True } , <EOL> "<STR_LIT>" : { "<STR_LIT>" : <NUM_LIT:2> } , <EOL> } <EOL> } <EOL> } <EOL> } <EOL> } <EOL> validator = Draft3Validator ( schema ) <EOL> errors = validator . iter_errors ( instance ) <EOL> e1 , e2 , e3 , e4 , e5 , e6 = sorted_errors ( errors ) <EOL> self . assertEqual ( e1 . path , deque ( [ ] ) ) <EOL> self . assertEqual ( e2 . path , deque ( [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( e3 . path , deque ( [ <NUM_LIT:1> , "<STR_LIT:bar>" ] ) ) <EOL> self . assertEqual ( e4 . path , deque ( [ <NUM_LIT:1> , "<STR_LIT:bar>" , "<STR_LIT:bar>" ] ) ) <EOL> self . assertEqual ( e5 . path , deque ( [ <NUM_LIT:1> , "<STR_LIT:bar>" , "<STR_LIT>" ] ) ) <EOL> self . assertEqual ( e6 . path , deque ( [ <NUM_LIT:1> , "<STR_LIT:foo>" ] ) ) <EOL> self . assertEqual ( e1 . schema_path , deque ( [ "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( e2 . schema_path , deque ( [ "<STR_LIT>" , "<STR_LIT:type>" ] ) ) <EOL> self . assertEqual ( <EOL> list ( e3 . schema_path ) , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:bar>" , "<STR_LIT:type>" ] , <EOL> ) <EOL> self . assertEqual ( <EOL> list ( e4 . schema_path ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:bar>" , "<STR_LIT>" , "<STR_LIT:bar>" , "<STR_LIT>" ] , <EOL> ) <EOL> self . assertEqual ( <EOL> list ( e5 . schema_path ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:bar>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> ) <EOL> self . assertEqual ( <EOL> list ( e6 . schema_path ) , [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:foo>" , "<STR_LIT>" ] , <EOL> ) <EOL> self . assertEqual ( e1 . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e2 . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e3 . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e4 . validator , "<STR_LIT>" ) <EOL> self . assertEqual ( e5 . validator , "<STR_LIT>" ) <EOL> self . assertEqual ( e6 . validator , "<STR_LIT>" ) <EOL> def test_recursive ( self ) : <EOL> schema = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : [ "<STR_LIT:name>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:string>" , <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> } , <EOL> } , <EOL> } , <EOL> } ] , <EOL> } , <EOL> } , <EOL> "<STR_LIT:type>" : "<STR_LIT:object>" , <EOL> "<STR_LIT>" : [ "<STR_LIT:root>" ] , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:root>" : { "<STR_LIT>" : "<STR_LIT>" } , <EOL> } <EOL> } <EOL> instance = { <EOL> "<STR_LIT:root>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT:root>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:a>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT:a>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> } <EOL> } <EOL> } , <EOL> } , <EOL> } , <EOL> } <EOL> validator = Draft4Validator ( schema ) <EOL> e , = validator . iter_errors ( instance ) <EOL> self . assertEqual ( e . absolute_path , deque ( [ "<STR_LIT:root>" ] ) ) <EOL> self . assertEqual ( <EOL> e . absolute_schema_path , deque ( [ "<STR_LIT>" , "<STR_LIT:root>" , "<STR_LIT>" ] ) , <EOL> ) <EOL> e1 , = e . context <EOL> self . assertEqual ( e1 . absolute_path , deque ( [ "<STR_LIT:root>" , "<STR_LIT>" , "<STR_LIT:a>" ] ) ) <EOL> self . assertEqual ( <EOL> e1 . absolute_schema_path , deque ( <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:root>" , <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:0> , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> ) , <EOL> ) <EOL> e2 , = e1 . context <EOL> self . assertEqual ( <EOL> e2 . absolute_path , deque ( <EOL> [ "<STR_LIT:root>" , "<STR_LIT>" , "<STR_LIT:a>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ) , <EOL> ) <EOL> self . assertEqual ( <EOL> e2 . absolute_schema_path , deque ( <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:root>" , <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:0> , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> <NUM_LIT:0> , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] , <EOL> ) , <EOL> ) <EOL> def test_additionalProperties ( self ) : <EOL> instance = { "<STR_LIT:bar>" : "<STR_LIT:bar>" , "<STR_LIT:foo>" : <NUM_LIT:2> } <EOL> schema = { <EOL> "<STR_LIT>" : { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:5> } <EOL> } <EOL> validator = Draft3Validator ( schema ) <EOL> errors = validator . iter_errors ( instance ) <EOL> e1 , e2 = sorted_errors ( errors ) <EOL> self . assertEqual ( e1 . path , deque ( [ "<STR_LIT:bar>" ] ) ) <EOL> self . assertEqual ( e2 . path , deque ( [ "<STR_LIT:foo>" ] ) ) <EOL> self . assertEqual ( e1 . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e2 . validator , "<STR_LIT>" ) <EOL> def test_patternProperties ( self ) : <EOL> instance = { "<STR_LIT:bar>" : <NUM_LIT:1> , "<STR_LIT:foo>" : <NUM_LIT:2> } <EOL> schema = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:bar>" : { "<STR_LIT:type>" : "<STR_LIT:string>" } , <EOL> "<STR_LIT:foo>" : { "<STR_LIT>" : <NUM_LIT:5> } <EOL> } <EOL> } <EOL> validator = Draft3Validator ( schema ) <EOL> errors = validator . iter_errors ( instance ) <EOL> e1 , e2 = sorted_errors ( errors ) <EOL> self . assertEqual ( e1 . path , deque ( [ "<STR_LIT:bar>" ] ) ) <EOL> self . assertEqual ( e2 . path , deque ( [ "<STR_LIT:foo>" ] ) ) <EOL> self . assertEqual ( e1 . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e2 . validator , "<STR_LIT>" ) <EOL> def test_additionalItems ( self ) : <EOL> instance = [ "<STR_LIT:foo>" , <NUM_LIT:1> ] <EOL> schema = { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:5> } <EOL> } <EOL> validator = Draft3Validator ( schema ) <EOL> errors = validator . iter_errors ( instance ) <EOL> e1 , e2 = sorted_errors ( errors ) <EOL> self . assertEqual ( e1 . path , deque ( [ <NUM_LIT:0> ] ) ) <EOL> self . assertEqual ( e2 . path , deque ( [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( e1 . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e2 . validator , "<STR_LIT>" ) <EOL> def test_additionalItems_with_items ( self ) : <EOL> instance = [ "<STR_LIT:foo>" , "<STR_LIT:bar>" , <NUM_LIT:1> ] <EOL> schema = { <EOL> "<STR_LIT>" : [ { } ] , <EOL> "<STR_LIT>" : { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : <NUM_LIT:5> } <EOL> } <EOL> validator = Draft3Validator ( schema ) <EOL> errors = validator . iter_errors ( instance ) <EOL> e1 , e2 = sorted_errors ( errors ) <EOL> self . assertEqual ( e1 . path , deque ( [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( e2 . path , deque ( [ <NUM_LIT:2> ] ) ) <EOL> self . assertEqual ( e1 . validator , "<STR_LIT:type>" ) <EOL> self . assertEqual ( e2 . validator , "<STR_LIT>" ) <EOL> class ValidatorTestMixin ( object ) : <EOL> def setUp ( self ) : <EOL> self . instance = mock . Mock ( ) <EOL> self . schema = { } <EOL> self . resolver = mock . Mock ( ) <EOL> self . validator = self . validator_class ( self . schema ) <EOL> def test_valid_instances_are_valid ( self ) : <EOL> errors = iter ( [ ] ) <EOL> with mock . patch . object ( <EOL> self . validator , "<STR_LIT>" , return_value = errors , <EOL> ) : <EOL> self . assertTrue ( <EOL> self . validator . is_valid ( self . instance , self . schema ) <EOL> ) <EOL> def test_invalid_instances_are_not_valid ( self ) : <EOL> errors = iter ( [ mock . Mock ( ) ] ) <EOL> with mock . patch . object ( <EOL> self . validator , "<STR_LIT>" , return_value = errors , <EOL> ) : <EOL> self . assertFalse ( <EOL> self . validator . is_valid ( self . instance , self . schema ) <EOL> ) <EOL> def test_non_existent_properties_are_ignored ( self ) : <EOL> instance , my_property , my_value = mock . Mock ( ) , mock . Mock ( ) , mock . Mock ( ) <EOL> validate ( instance = instance , schema = { my_property : my_value } ) <EOL> def test_it_creates_a_ref_resolver_if_not_provided ( self ) : <EOL> self . assertIsInstance ( self . validator . resolver , RefResolver ) <EOL> def test_it_delegates_to_a_ref_resolver ( self ) : <EOL> resolver = RefResolver ( "<STR_LIT>" , { } ) <EOL> schema = { "<STR_LIT>" : mock . Mock ( ) } <EOL> with mock . patch . object ( resolver , "<STR_LIT>" ) as resolve : <EOL> resolve . return_value = "<STR_LIT:url>" , { "<STR_LIT:type>" : "<STR_LIT>" } <EOL> with self . assertRaises ( ValidationError ) : <EOL> self . validator_class ( schema , resolver = resolver ) . validate ( None ) <EOL> resolve . assert_called_once_with ( schema [ "<STR_LIT>" ] ) <EOL> def test_it_delegates_to_a_legacy_ref_resolver ( self ) : <EOL> """<STR_LIT>""" <EOL> class LegacyRefResolver ( object ) : <EOL> @ contextmanager <EOL> def resolving ( this , ref ) : <EOL> self . assertEqual ( ref , "<STR_LIT>" ) <EOL> yield { "<STR_LIT:type>" : "<STR_LIT>" } <EOL> resolver = LegacyRefResolver ( ) <EOL> schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> with self . assertRaises ( ValidationError ) : <EOL> self . validator_class ( schema , resolver = resolver ) . validate ( None ) <EOL> def test_is_type_is_true_for_valid_type ( self ) : <EOL> self . assertTrue ( self . validator . is_type ( "<STR_LIT:foo>" , "<STR_LIT:string>" ) ) <EOL> def test_is_type_is_false_for_invalid_type ( self ) : <EOL> self . assertFalse ( self . validator . is_type ( "<STR_LIT:foo>" , "<STR_LIT>" ) ) <EOL> def test_is_type_evades_bool_inheriting_from_int ( self ) : <EOL> self . assertFalse ( self . validator . is_type ( True , "<STR_LIT>" ) ) <EOL> self . assertFalse ( self . validator . is_type ( True , "<STR_LIT>" ) ) <EOL> def test_is_type_raises_exception_for_unknown_type ( self ) : <EOL> with self . assertRaises ( UnknownType ) : <EOL> self . validator . is_type ( "<STR_LIT:foo>" , object ( ) ) <EOL> class TestDraft3Validator ( ValidatorTestMixin , unittest . TestCase ) : <EOL> validator_class = Draft3Validator <EOL> def test_is_type_is_true_for_any_type ( self ) : <EOL> self . assertTrue ( self . validator . is_valid ( mock . Mock ( ) , { "<STR_LIT:type>" : "<STR_LIT>" } ) ) <EOL> def test_is_type_does_not_evade_bool_if_it_is_being_tested ( self ) : <EOL> self . assertTrue ( self . validator . is_type ( True , "<STR_LIT>" ) ) <EOL> self . assertTrue ( self . validator . is_valid ( True , { "<STR_LIT:type>" : "<STR_LIT>" } ) ) <EOL> def test_non_string_custom_types ( self ) : <EOL> schema = { '<STR_LIT:type>' : [ None ] } <EOL> cls = self . validator_class ( schema , types = { None : type ( None ) } ) <EOL> cls . validate ( None , schema ) <EOL> class TestDraft4Validator ( ValidatorTestMixin , unittest . TestCase ) : <EOL> validator_class = Draft4Validator <EOL> class TestBuiltinFormats ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> for format in FormatChecker . checkers : <EOL> def test ( self , format = format ) : <EOL> v = Draft4Validator ( { "<STR_LIT>" : format } , format_checker = FormatChecker ( ) ) <EOL> v . validate ( <NUM_LIT> ) <EOL> name = "<STR_LIT>" . format ( format ) <EOL> test . __name__ = name <EOL> setattr ( TestBuiltinFormats , name , test ) <EOL> del test <EOL> class TestValidatorFor ( unittest . TestCase ) : <EOL> def test_draft_3 ( self ) : <EOL> schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertIs ( validator_for ( schema ) , Draft3Validator ) <EOL> schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertIs ( validator_for ( schema ) , Draft3Validator ) <EOL> def test_draft_4 ( self ) : <EOL> schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertIs ( validator_for ( schema ) , Draft4Validator ) <EOL> schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertIs ( validator_for ( schema ) , Draft4Validator ) <EOL> def test_custom_validator ( self ) : <EOL> Validator = create ( meta_schema = { "<STR_LIT:id>" : "<STR_LIT>" } , version = "<STR_LIT>" ) <EOL> schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> self . assertIs ( validator_for ( schema ) , Validator ) <EOL> def test_validator_for_jsonschema_default ( self ) : <EOL> self . assertIs ( validator_for ( { } ) , Draft4Validator ) <EOL> def test_validator_for_custom_default ( self ) : <EOL> self . assertIs ( validator_for ( { } , default = None ) , None ) <EOL> class TestValidate ( unittest . TestCase ) : <EOL> def test_draft3_validator_is_chosen ( self ) : <EOL> schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> with mock . patch . object ( Draft3Validator , "<STR_LIT>" ) as chk_schema : <EOL> validate ( { } , schema ) <EOL> chk_schema . assert_called_once_with ( schema ) <EOL> schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> with mock . patch . object ( Draft3Validator , "<STR_LIT>" ) as chk_schema : <EOL> validate ( { } , schema ) <EOL> chk_schema . assert_called_once_with ( schema ) <EOL> def test_draft4_validator_is_chosen ( self ) : <EOL> schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> with mock . patch . object ( Draft4Validator , "<STR_LIT>" ) as chk_schema : <EOL> validate ( { } , schema ) <EOL> chk_schema . assert_called_once_with ( schema ) <EOL> def test_draft4_validator_is_the_default ( self ) : <EOL> with mock . patch . object ( Draft4Validator , "<STR_LIT>" ) as chk_schema : <EOL> validate ( { } , { } ) <EOL> chk_schema . assert_called_once_with ( { } ) <EOL> class TestRefResolver ( unittest . TestCase ) : <EOL> base_uri = "<STR_LIT>" <EOL> stored_uri = "<STR_LIT>" <EOL> stored_schema = { "<STR_LIT>" : "<STR_LIT>" } <EOL> def setUp ( self ) : <EOL> self . referrer = { } <EOL> self . store = { self . stored_uri : self . stored_schema } <EOL> self . resolver = RefResolver ( self . base_uri , self . referrer , self . store ) <EOL> def test_it_does_not_retrieve_schema_urls_from_the_network ( self ) : <EOL> ref = Draft3Validator . META_SCHEMA [ "<STR_LIT:id>" ] <EOL> with mock . patch . object ( self . resolver , "<STR_LIT>" ) as remote : <EOL> with self . resolver . resolving ( ref ) as resolved : <EOL> self . assertEqual ( resolved , Draft3Validator . META_SCHEMA ) <EOL> self . assertFalse ( remote . called ) <EOL> def test_it_resolves_local_refs ( self ) : <EOL> ref = "<STR_LIT>" <EOL> self . referrer [ "<STR_LIT>" ] = { "<STR_LIT:foo>" : object ( ) } <EOL> with self . resolver . resolving ( ref ) as resolved : <EOL> self . assertEqual ( resolved , self . referrer [ "<STR_LIT>" ] [ "<STR_LIT:foo>" ] ) <EOL> def test_it_resolves_local_refs_with_id ( self ) : <EOL> schema = { "<STR_LIT:id>" : "<STR_LIT>" , "<STR_LIT:a>" : { "<STR_LIT:foo>" : "<STR_LIT:bar>" } } <EOL> resolver = RefResolver . from_schema ( schema ) <EOL> with resolver . resolving ( "<STR_LIT>" ) as resolved : <EOL> self . assertEqual ( resolved , schema [ "<STR_LIT:a>" ] ) <EOL> with resolver . resolving ( "<STR_LIT>" ) as resolved : <EOL> self . assertEqual ( resolved , schema [ "<STR_LIT:a>" ] ) <EOL> def test_it_retrieves_stored_refs ( self ) : <EOL> with self . resolver . resolving ( self . stored_uri ) as resolved : <EOL> self . assertIs ( resolved , self . stored_schema ) <EOL> self . resolver . store [ "<STR_LIT>" ] = { "<STR_LIT:foo>" : <NUM_LIT:12> } <EOL> with self . resolver . resolving ( "<STR_LIT>" ) as resolved : <EOL> self . assertEqual ( resolved , <NUM_LIT:12> ) <EOL> def test_it_retrieves_unstored_refs_via_requests ( self ) : <EOL> ref = "<STR_LIT>" <EOL> schema = { "<STR_LIT>" : <NUM_LIT:12> } <EOL> with mock . patch ( "<STR_LIT>" ) as requests : <EOL> requests . get . return_value . json . return_value = schema <EOL> with self . resolver . resolving ( ref ) as resolved : <EOL> self . assertEqual ( resolved , <NUM_LIT:12> ) <EOL> requests . get . assert_called_once_with ( "<STR_LIT>" ) <EOL> def test_it_retrieves_unstored_refs_via_urlopen ( self ) : <EOL> ref = "<STR_LIT>" <EOL> schema = { "<STR_LIT>" : <NUM_LIT:12> } <EOL> with mock . patch ( "<STR_LIT>" , None ) : <EOL> with mock . patch ( "<STR_LIT>" ) as urlopen : <EOL> urlopen . return_value . read . return_value = ( <EOL> json . dumps ( schema ) . encode ( "<STR_LIT:utf8>" ) ) <EOL> with self . resolver . resolving ( ref ) as resolved : <EOL> self . assertEqual ( resolved , <NUM_LIT:12> ) <EOL> urlopen . assert_called_once_with ( "<STR_LIT>" ) <EOL> def test_it_can_construct_a_base_uri_from_a_schema ( self ) : <EOL> schema = { "<STR_LIT:id>" : "<STR_LIT:foo>" } <EOL> resolver = RefResolver . from_schema ( schema ) <EOL> self . assertEqual ( resolver . base_uri , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( resolver . resolution_scope , "<STR_LIT:foo>" ) <EOL> with resolver . resolving ( "<STR_LIT>" ) as resolved : <EOL> self . assertEqual ( resolved , schema ) <EOL> with resolver . resolving ( "<STR_LIT:#>" ) as resolved : <EOL> self . assertEqual ( resolved , schema ) <EOL> with resolver . resolving ( "<STR_LIT:foo>" ) as resolved : <EOL> self . assertEqual ( resolved , schema ) <EOL> with resolver . resolving ( "<STR_LIT>" ) as resolved : <EOL> self . assertEqual ( resolved , schema ) <EOL> def test_it_can_construct_a_base_uri_from_a_schema_without_id ( self ) : <EOL> schema = { } <EOL> resolver = RefResolver . from_schema ( schema ) <EOL> self . assertEqual ( resolver . base_uri , "<STR_LIT>" ) <EOL> self . assertEqual ( resolver . resolution_scope , "<STR_LIT>" ) <EOL> with resolver . resolving ( "<STR_LIT>" ) as resolved : <EOL> self . assertEqual ( resolved , schema ) <EOL> with resolver . resolving ( "<STR_LIT:#>" ) as resolved : <EOL> self . assertEqual ( resolved , schema ) <EOL> def test_custom_uri_scheme_handlers ( self ) : <EOL> schema = { "<STR_LIT:foo>" : "<STR_LIT:bar>" } <EOL> ref = "<STR_LIT>" <EOL> foo_handler = mock . Mock ( return_value = schema ) <EOL> resolver = RefResolver ( "<STR_LIT>" , { } , handlers = { "<STR_LIT:foo>" : foo_handler } ) <EOL> with resolver . resolving ( ref ) as resolved : <EOL> self . assertEqual ( resolved , schema ) <EOL> foo_handler . assert_called_once_with ( ref ) <EOL> def test_cache_remote_on ( self ) : <EOL> ref = "<STR_LIT>" <EOL> foo_handler = mock . Mock ( ) <EOL> resolver = RefResolver ( <EOL> "<STR_LIT>" , { } , cache_remote = True , handlers = { "<STR_LIT:foo>" : foo_handler } , <EOL> ) <EOL> with resolver . resolving ( ref ) : <EOL> pass <EOL> with resolver . resolving ( ref ) : <EOL> pass <EOL> foo_handler . assert_called_once_with ( ref ) <EOL> def test_cache_remote_off ( self ) : <EOL> ref = "<STR_LIT>" <EOL> foo_handler = mock . Mock ( ) <EOL> resolver = RefResolver ( <EOL> "<STR_LIT>" , { } , cache_remote = False , handlers = { "<STR_LIT:foo>" : foo_handler } , <EOL> ) <EOL> with resolver . resolving ( ref ) : <EOL> pass <EOL> self . assertEqual ( foo_handler . call_count , <NUM_LIT:1> ) <EOL> def test_if_you_give_it_junk_you_get_a_resolution_error ( self ) : <EOL> ref = "<STR_LIT>" <EOL> foo_handler = mock . Mock ( side_effect = ValueError ( "<STR_LIT>" ) ) <EOL> resolver = RefResolver ( "<STR_LIT>" , { } , handlers = { "<STR_LIT:foo>" : foo_handler } ) <EOL> with self . assertRaises ( RefResolutionError ) as err : <EOL> with resolver . resolving ( ref ) : <EOL> pass <EOL> self . assertEqual ( str ( err . exception ) , "<STR_LIT>" ) <EOL> def test_helpful_error_message_on_failed_pop_scope ( self ) : <EOL> resolver = RefResolver ( "<STR_LIT>" , { } ) <EOL> resolver . pop_scope ( ) <EOL> with self . assertRaises ( RefResolutionError ) as exc : <EOL> resolver . pop_scope ( ) <EOL> self . assertIn ( "<STR_LIT>" , str ( exc . exception ) ) <EOL> class UniqueTupleItemsMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def test_it_properly_formats_an_error_message ( self ) : <EOL> validator = self . validator_class ( <EOL> schema = { "<STR_LIT>" : True } , <EOL> types = { "<STR_LIT>" : ( tuple , ) } , <EOL> ) <EOL> with self . assertRaises ( ValidationError ) as e : <EOL> validator . validate ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> self . assertIn ( "<STR_LIT>" , str ( e . exception ) ) <EOL> class TestDraft4UniqueTupleItems ( UniqueTupleItemsMixin , unittest . TestCase ) : <EOL> validator_class = Draft4Validator <EOL> class TestDraft3UniqueTupleItems ( UniqueTupleItemsMixin , unittest . TestCase ) : <EOL> validator_class = Draft3Validator <EOL> def sorted_errors ( errors ) : <EOL> def key ( error ) : <EOL> return ( <EOL> [ str ( e ) for e in error . path ] , <EOL> [ str ( e ) for e in error . schema_path ] <EOL> ) <EOL> return sorted ( errors , key = key ) </s>
