<s> import textwrap <EOL> from jsonschema import Draft4Validator , exceptions <EOL> from jsonschema . compat import PY3 <EOL> from jsonschema . tests . compat import mock , unittest <EOL> class TestBestMatch ( unittest . TestCase ) : <EOL> def best_match ( self , errors ) : <EOL> errors = list ( errors ) <EOL> best = exceptions . best_match ( errors ) <EOL> reversed_best = exceptions . best_match ( reversed ( errors ) ) <EOL> self . assertEqual ( <EOL> best , <EOL> reversed_best , <EOL> msg = "<STR_LIT>" <EOL> "<STR_LIT>" . format ( best , reversed_best ) , <EOL> ) <EOL> return best <EOL> def test_shallower_errors_are_better_matches ( self ) : <EOL> validator = Draft4Validator ( <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:foo>" : { <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : { "<STR_LIT:bar>" : { "<STR_LIT:type>" : "<STR_LIT:object>" } } , <EOL> } <EOL> } <EOL> } <EOL> ) <EOL> best = self . best_match ( validator . iter_errors ( { "<STR_LIT:foo>" : { "<STR_LIT:bar>" : [ ] } } ) ) <EOL> self . assertEqual ( best . validator , "<STR_LIT>" ) <EOL> def test_oneOf_and_anyOf_are_weak_matches ( self ) : <EOL> """<STR_LIT>""" <EOL> validator = Draft4Validator ( <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : [ { "<STR_LIT:type>" : "<STR_LIT:string>" } , { "<STR_LIT:type>" : "<STR_LIT>" } ] , <EOL> "<STR_LIT>" : [ { "<STR_LIT:type>" : "<STR_LIT:string>" } , { "<STR_LIT:type>" : "<STR_LIT>" } ] , <EOL> } <EOL> ) <EOL> best = self . best_match ( validator . iter_errors ( { } ) ) <EOL> self . assertEqual ( best . validator , "<STR_LIT>" ) <EOL> def test_if_the_most_relevant_error_is_anyOf_it_is_traversed ( self ) : <EOL> """<STR_LIT>""" <EOL> validator = Draft4Validator ( <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:foo>" : { <EOL> "<STR_LIT>" : [ <EOL> { "<STR_LIT:type>" : "<STR_LIT:string>" } , <EOL> { "<STR_LIT>" : { "<STR_LIT:bar>" : { "<STR_LIT:type>" : "<STR_LIT>" } } } , <EOL> ] , <EOL> } , <EOL> } , <EOL> } , <EOL> ) <EOL> best = self . best_match ( validator . iter_errors ( { "<STR_LIT:foo>" : { "<STR_LIT:bar>" : <NUM_LIT:12> } } ) ) <EOL> self . assertEqual ( best . validator_value , "<STR_LIT>" ) <EOL> def test_if_the_most_relevant_error_is_oneOf_it_is_traversed ( self ) : <EOL> """<STR_LIT>""" <EOL> validator = Draft4Validator ( <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:foo>" : { <EOL> "<STR_LIT>" : [ <EOL> { "<STR_LIT:type>" : "<STR_LIT:string>" } , <EOL> { "<STR_LIT>" : { "<STR_LIT:bar>" : { "<STR_LIT:type>" : "<STR_LIT>" } } } , <EOL> ] , <EOL> } , <EOL> } , <EOL> } , <EOL> ) <EOL> best = self . best_match ( validator . iter_errors ( { "<STR_LIT:foo>" : { "<STR_LIT:bar>" : <NUM_LIT:12> } } ) ) <EOL> self . assertEqual ( best . validator_value , "<STR_LIT>" ) <EOL> def test_if_the_most_relevant_error_is_allOf_it_is_traversed ( self ) : <EOL> """<STR_LIT>""" <EOL> validator = Draft4Validator ( <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:foo>" : { <EOL> "<STR_LIT>" : [ <EOL> { "<STR_LIT:type>" : "<STR_LIT:string>" } , <EOL> { "<STR_LIT>" : { "<STR_LIT:bar>" : { "<STR_LIT:type>" : "<STR_LIT>" } } } , <EOL> ] , <EOL> } , <EOL> } , <EOL> } , <EOL> ) <EOL> best = self . best_match ( validator . iter_errors ( { "<STR_LIT:foo>" : { "<STR_LIT:bar>" : <NUM_LIT:12> } } ) ) <EOL> self . assertEqual ( best . validator_value , "<STR_LIT:string>" ) <EOL> def test_nested_context_for_oneOf ( self ) : <EOL> validator = Draft4Validator ( <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:foo>" : { <EOL> "<STR_LIT>" : [ <EOL> { "<STR_LIT:type>" : "<STR_LIT:string>" } , <EOL> { <EOL> "<STR_LIT>" : [ <EOL> { "<STR_LIT:type>" : "<STR_LIT:string>" } , <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:bar>" : { "<STR_LIT:type>" : "<STR_LIT>" } <EOL> } , <EOL> } , <EOL> ] , <EOL> } , <EOL> ] , <EOL> } , <EOL> } , <EOL> } , <EOL> ) <EOL> best = self . best_match ( validator . iter_errors ( { "<STR_LIT:foo>" : { "<STR_LIT:bar>" : <NUM_LIT:12> } } ) ) <EOL> self . assertEqual ( best . validator_value , "<STR_LIT>" ) <EOL> def test_one_error ( self ) : <EOL> validator = Draft4Validator ( { "<STR_LIT>" : <NUM_LIT:2> } ) <EOL> error , = validator . iter_errors ( { } ) <EOL> self . assertEqual ( <EOL> exceptions . best_match ( validator . iter_errors ( { } ) ) . validator , <EOL> "<STR_LIT>" , <EOL> ) <EOL> def test_no_errors ( self ) : <EOL> validator = Draft4Validator ( { } ) <EOL> self . assertIsNone ( exceptions . best_match ( validator . iter_errors ( { } ) ) ) <EOL> class TestByRelevance ( unittest . TestCase ) : <EOL> def test_short_paths_are_better_matches ( self ) : <EOL> shallow = exceptions . ValidationError ( "<STR_LIT>" , path = [ "<STR_LIT>" ] ) <EOL> deep = exceptions . ValidationError ( "<STR_LIT>" , path = [ "<STR_LIT:foo>" , "<STR_LIT:bar>" ] ) <EOL> match = max ( [ shallow , deep ] , key = exceptions . relevance ) <EOL> self . assertIs ( match , shallow ) <EOL> match = max ( [ deep , shallow ] , key = exceptions . relevance ) <EOL> self . assertIs ( match , shallow ) <EOL> def test_global_errors_are_even_better_matches ( self ) : <EOL> shallow = exceptions . ValidationError ( "<STR_LIT>" , path = [ ] ) <EOL> deep = exceptions . ValidationError ( "<STR_LIT>" , path = [ "<STR_LIT:foo>" ] ) <EOL> errors = sorted ( [ shallow , deep ] , key = exceptions . relevance ) <EOL> self . assertEqual ( <EOL> [ list ( error . path ) for error in errors ] , <EOL> [ [ "<STR_LIT:foo>" ] , [ ] ] , <EOL> ) <EOL> errors = sorted ( [ deep , shallow ] , key = exceptions . relevance ) <EOL> self . assertEqual ( <EOL> [ list ( error . path ) for error in errors ] , <EOL> [ [ "<STR_LIT:foo>" ] , [ ] ] , <EOL> ) <EOL> def test_weak_validators_are_lower_priority ( self ) : <EOL> weak = exceptions . ValidationError ( "<STR_LIT>" , path = [ ] , validator = "<STR_LIT:a>" ) <EOL> normal = exceptions . ValidationError ( "<STR_LIT>" , path = [ ] , validator = "<STR_LIT:b>" ) <EOL> best_match = exceptions . by_relevance ( weak = "<STR_LIT:a>" ) <EOL> match = max ( [ weak , normal ] , key = best_match ) <EOL> self . assertIs ( match , normal ) <EOL> match = max ( [ normal , weak ] , key = best_match ) <EOL> self . assertIs ( match , normal ) <EOL> def test_strong_validators_are_higher_priority ( self ) : <EOL> weak = exceptions . ValidationError ( "<STR_LIT>" , path = [ ] , validator = "<STR_LIT:a>" ) <EOL> normal = exceptions . ValidationError ( "<STR_LIT>" , path = [ ] , validator = "<STR_LIT:b>" ) <EOL> strong = exceptions . ValidationError ( "<STR_LIT>" , path = [ ] , validator = "<STR_LIT:c>" ) <EOL> best_match = exceptions . by_relevance ( weak = "<STR_LIT:a>" , strong = "<STR_LIT:c>" ) <EOL> match = max ( [ weak , normal , strong ] , key = best_match ) <EOL> self . assertIs ( match , strong ) <EOL> match = max ( [ strong , normal , weak ] , key = best_match ) <EOL> self . assertIs ( match , strong ) <EOL> class TestErrorTree ( unittest . TestCase ) : <EOL> def test_it_knows_how_many_total_errors_it_contains ( self ) : <EOL> errors = [ mock . MagicMock ( ) for _ in range ( <NUM_LIT:8> ) ] <EOL> tree = exceptions . ErrorTree ( errors ) <EOL> self . assertEqual ( tree . total_errors , <NUM_LIT:8> ) <EOL> def test_it_contains_an_item_if_the_item_had_an_error ( self ) : <EOL> errors = [ exceptions . ValidationError ( "<STR_LIT>" , path = [ "<STR_LIT:bar>" ] ) ] <EOL> tree = exceptions . ErrorTree ( errors ) <EOL> self . assertIn ( "<STR_LIT:bar>" , tree ) <EOL> def test_it_does_not_contain_an_item_if_the_item_had_no_error ( self ) : <EOL> errors = [ exceptions . ValidationError ( "<STR_LIT>" , path = [ "<STR_LIT:bar>" ] ) ] <EOL> tree = exceptions . ErrorTree ( errors ) <EOL> self . assertNotIn ( "<STR_LIT:foo>" , tree ) <EOL> def test_validators_that_failed_appear_in_errors_dict ( self ) : <EOL> error = exceptions . ValidationError ( "<STR_LIT>" , validator = "<STR_LIT:foo>" ) <EOL> tree = exceptions . ErrorTree ( [ error ] ) <EOL> self . assertEqual ( tree . errors , { "<STR_LIT:foo>" : error } ) <EOL> def test_it_creates_a_child_tree_for_each_nested_path ( self ) : <EOL> errors = [ <EOL> exceptions . ValidationError ( "<STR_LIT>" , path = [ "<STR_LIT:bar>" ] ) , <EOL> exceptions . ValidationError ( "<STR_LIT>" , path = [ "<STR_LIT:bar>" , <NUM_LIT:0> ] ) , <EOL> ] <EOL> tree = exceptions . ErrorTree ( errors ) <EOL> self . assertIn ( <NUM_LIT:0> , tree [ "<STR_LIT:bar>" ] ) <EOL> self . assertNotIn ( <NUM_LIT:1> , tree [ "<STR_LIT:bar>" ] ) <EOL> def test_children_have_their_errors_dicts_built ( self ) : <EOL> e1 , e2 = ( <EOL> exceptions . ValidationError ( "<STR_LIT:1>" , validator = "<STR_LIT:foo>" , path = [ "<STR_LIT:bar>" , <NUM_LIT:0> ] ) , <EOL> exceptions . ValidationError ( "<STR_LIT:2>" , validator = "<STR_LIT>" , path = [ "<STR_LIT:bar>" , <NUM_LIT:0> ] ) , <EOL> ) <EOL> tree = exceptions . ErrorTree ( [ e1 , e2 ] ) <EOL> self . assertEqual ( tree [ "<STR_LIT:bar>" ] [ <NUM_LIT:0> ] . errors , { "<STR_LIT:foo>" : e1 , "<STR_LIT>" : e2 } ) <EOL> def test_it_does_not_contain_subtrees_that_are_not_in_the_instance ( self ) : <EOL> error = exceptions . ValidationError ( "<STR_LIT>" , validator = "<STR_LIT:foo>" , instance = [ ] ) <EOL> tree = exceptions . ErrorTree ( [ error ] ) <EOL> with self . assertRaises ( IndexError ) : <EOL> tree [ <NUM_LIT:0> ] <EOL> def test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error ( self ) : <EOL> """<STR_LIT>""" <EOL> error = exceptions . ValidationError ( <EOL> "<STR_LIT>" , validator = "<STR_LIT:foo>" , instance = { } , path = [ "<STR_LIT:foo>" ] , <EOL> ) <EOL> tree = exceptions . ErrorTree ( [ error ] ) <EOL> self . assertIsInstance ( tree [ "<STR_LIT:foo>" ] , exceptions . ErrorTree ) <EOL> class TestErrorInitReprStr ( unittest . TestCase ) : <EOL> def make_error ( self , ** kwargs ) : <EOL> defaults = dict ( <EOL> message = u"<STR_LIT:hello>" , <EOL> validator = u"<STR_LIT:type>" , <EOL> validator_value = u"<STR_LIT:string>" , <EOL> instance = <NUM_LIT:5> , <EOL> schema = { u"<STR_LIT:type>" : u"<STR_LIT:string>" } , <EOL> ) <EOL> defaults . update ( kwargs ) <EOL> return exceptions . ValidationError ( ** defaults ) <EOL> def assertShows ( self , expected , ** kwargs ) : <EOL> if PY3 : <EOL> expected = expected . replace ( "<STR_LIT>" , "<STR_LIT:'>" ) <EOL> expected = textwrap . dedent ( expected ) . rstrip ( "<STR_LIT:\n>" ) <EOL> error = self . make_error ( ** kwargs ) <EOL> message_line , _ , rest = str ( error ) . partition ( "<STR_LIT:\n>" ) <EOL> self . assertEqual ( message_line , error . message ) <EOL> self . assertEqual ( rest , expected ) <EOL> def test_it_calls_super_and_sets_args ( self ) : <EOL> error = self . make_error ( ) <EOL> self . assertGreater ( len ( error . args ) , <NUM_LIT:1> ) <EOL> def test_repr ( self ) : <EOL> self . assertEqual ( <EOL> repr ( exceptions . ValidationError ( message = "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" % "<STR_LIT>" , <EOL> ) <EOL> def test_unset_error ( self ) : <EOL> error = exceptions . ValidationError ( "<STR_LIT:message>" ) <EOL> self . assertEqual ( str ( error ) , "<STR_LIT:message>" ) <EOL> kwargs = { <EOL> "<STR_LIT>" : "<STR_LIT:type>" , <EOL> "<STR_LIT>" : "<STR_LIT:string>" , <EOL> "<STR_LIT>" : <NUM_LIT:5> , <EOL> "<STR_LIT>" : { "<STR_LIT:type>" : "<STR_LIT:string>" } <EOL> } <EOL> for attr in kwargs : <EOL> k = dict ( kwargs ) <EOL> del k [ attr ] <EOL> error = exceptions . ValidationError ( "<STR_LIT:message>" , ** k ) <EOL> self . assertEqual ( str ( error ) , "<STR_LIT:message>" ) <EOL> def test_empty_paths ( self ) : <EOL> self . assertShows ( <EOL> """<STR_LIT>""" , <EOL> path = [ ] , <EOL> schema_path = [ ] , <EOL> ) <EOL> def test_one_item_paths ( self ) : <EOL> self . assertShows ( <EOL> """<STR_LIT>""" , <EOL> path = [ <NUM_LIT:0> ] , <EOL> schema_path = [ "<STR_LIT>" ] , <EOL> ) <EOL> def test_multiple_item_paths ( self ) : <EOL> self . assertShows ( <EOL> """<STR_LIT>""" , <EOL> path = [ <NUM_LIT:0> , u"<STR_LIT:a>" ] , <EOL> schema_path = [ u"<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> ) <EOL> def test_uses_pprint ( self ) : <EOL> with mock . patch ( "<STR_LIT>" ) as pformat : <EOL> str ( self . make_error ( ) ) <EOL> self . assertEqual ( pformat . call_count , <NUM_LIT:2> ) <EOL> def test_str_works_with_instances_having_overriden_eq_operator ( self ) : <EOL> """<STR_LIT>""" <EOL> instance = mock . MagicMock ( ) <EOL> error = exceptions . ValidationError ( <EOL> "<STR_LIT>" , <EOL> validator = "<STR_LIT:foo>" , <EOL> instance = instance , <EOL> validator_value = "<STR_LIT>" , <EOL> schema = "<STR_LIT>" , <EOL> ) <EOL> str ( error ) <EOL> self . assertFalse ( instance . __eq__ . called ) </s>
