`dJddlmZddlZddlmZddlmZGddeZGddeZGd d eZ Gd d eZ Gd deZ y))TestCaseN) exceptions)_LATEST_VERSIONcleZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZy) TestBestMatchc.tt|j|}tjt |}tjt |}|j|j|jd|d||S)NzNo consistent best match! Got: z Then: ) listr iter_errorsr best_matchiterreversed assertEqual _contents)selfinstanceschemaerrorsbest reversed_bests B/usr/lib/python3/dist-packages/jsonschema/tests/test_exceptions.py best_match_ofzTestBestMatch.best_match_of sof-99(CD$$T&\2"--hv.>?   NN   # # %.tfJ}o N  cddddddiidii}|jddgii|}|j|jd y) N propertiesfoobartypeobject) minPropertiesrrrr rr validatorrrrs r(test_shallower_errors_are_better_matchesz6TestBestMatch.test_shallower_errors_are_better_matchess] %&#(68*<"= !!EE2;+?!O 9rcdddiddigddiddigd}|ji|}|j|jdy) zn A property you *must* match is probably better than one you have to match a part of. rrstringnumber)r anyOfoneOfr!r Nr"r$s r%test_oneOf_and_anyOf_are_weak_matchesz3TestBestMatch.test_oneOf_and_anyOf_are_weak_matches s]x(68*<=x(68*<=  !!2f!= 9rcdddddiddddiiigiii}|jdddii| }|j|jdy ) aL If the most relevant error is an anyOf, then we traverse its context and select the otherwise *least* relevant error, since in this case that means the most specific, deep, error inside the instance. I.e. since only one of the schemas must match, we look for the most relevant one. rrr)rr'rarray r!Nrrvalidator_valuer$s r8test_if_the_most_relevant_error_is_anyOf_it_is_traversedzFTestBestMatch.test_if_the_most_relevant_error_is_anyOf_it_is_traversed.s *%/@'AB  !!EE2;+?!O --w7rc|dddiddiddigi}|jg|}|j|jdy)z We don't traverse into an anyOf (as above) if all of its context errors seem to be equally "wrong" against the instance. r)rr'integerrr!Nr"r$s r3test_no_anyOf_traversal_for_equally_relevant_errorszATestBestMatch.test_no_anyOf_traversal_for_equally_relevant_errorsEV "#" !!2f!= 1rcpdddigi}|jg|}|j|jdy)z We *do* traverse anyOf with a single nested error, even though it is vacuously equally relevant to itself. r)rr'r!Nr"r$s r6test_anyOf_traversal_for_single_equally_relevant_errorzDTestBestMatch.test_anyOf_traversal_for_single_equally_relevant_errorUF "  !!2f!= 0rcdddddiddddiiigiii}|jdddii| }|j|jdy ) aL If the most relevant error is an oneOf, then we traverse its context and select the otherwise *least* relevant error, since in this case that means the most specific, deep, error inside the instance. I.e. since only one of the schemas must match, we look for the most relevant one. rrr*rr'rr-r.r!Nr/r$s r8test_if_the_most_relevant_error_is_oneOf_it_is_traversedzFTestBestMatch.test_if_the_most_relevant_error_is_oneOf_it_is_traversedcr2rc|dddiddiddigi}|jg|}|j|jdy)z We don't traverse into an oneOf (as above) if all of its context errors seem to be equally "wrong" against the instance. r*rr'r4rr!Nr"r$s r3test_no_oneOf_traversal_for_equally_relevant_errorszATestBestMatch.test_no_oneOf_traversal_for_equally_relevant_errorszr6rcpdddigi}|jg|}|j|jdy)z We *do* traverse oneOf with a single nested error, even though it is vacuously equally relevant to itself. r*rr'r!Nr"r$s r6test_oneOf_traversal_for_single_equally_relevant_errorzDTestBestMatch.test_oneOf_traversal_for_single_equally_relevant_errorr9rcdddddiddddiiigiii}|jdddii| }|j|jdy ) z Now, if the error is allOf, we traverse but select the *most* relevant error from the context, because all schemas here must match anyways. rrallOfrr'rr-r.r!Nr/r$s r8test_if_the_most_relevant_error_is_allOf_it_is_traversedzFTestBestMatch.test_if_the_most_relevant_error_is_allOf_it_is_traversedss *%/@'AB  !!EE2;+?!O --x8rc dddddidddiddddiiigigiii}|jdddii| }|j|jdy ) zp We traverse into nested contexts (a oneOf containing an error in a nested oneOf here). rrr*rr'rr-r.r!Nr/r$s rtest_nested_context_for_oneOfz+TestBestMatch.test_nested_context_for_oneOfs *#!' 2$0(-/@3&!"&   &!!EE2;+?!O --w7rcddddddddd giii}|jdd i| }|j|jd dddddd dddgiii}|jdd i| }|j|jd y) Nrrr)r-rrminItemsr' r minLengthrr!rJr"rrr reordereds r"test_it_prioritizes_matching_typesz0TestBestMatch.test_it_prioritizes_matching_typess !(a8!);  !!E5>&!I 5 !);!(a8  !!E5>)!L 5rcdddddgdddd gd d giii}|jdd i| }|j|jddddd d d dddgiii}|jdd i| }|j|jdy)Nrrr)r-rrrFr4r'rHrIrr!rJr"rKs r(test_it_prioritizes_matching_union_typesz6TestBestMatch.test_it_prioritizes_matching_union_typess ")8!4!D"+X!6RH  !!E5>&!I 5 !);!(a8  !!E5>)!L 5rcpdddii}|jddi|}|j|jy)NrrFrr!)r assertIsNoner#r$s rtest_boolean_schemasz"TestBestMatch.test_boolean_schemass;/!!E5>&!I $..)rctddi}|ji\}|jtj|jij dy)Nr r)rr rrr r#)rr#errors rtest_one_errorzTestBestMatch.test_one_errorsS#_a$89 &&r*   ! !)"7"7"; < F F  rcti}|jtj|j iy)N)rrQrr r )rr#s rtest_no_errorszTestBestMatch.test_no_errors s0#B'  *// 0E0Eb0IJKrN)__name__ __module__ __qualname__rr%r+r1r5r8r;r=r?rBrDrMrOrRrUrWrrrrsS  : :8.2 18.2 19&886666*  Lrrc$eZdZdZdZdZdZy)TestByRelevancec"tjddg}tjdddg}t||gtj}|j ||t||gtj}|j ||y)NOh no!bazpathOh yes!rrkey)rValidationErrormax relevanceassertIs)rshallowdeepmatchs r#test_short_paths_are_better_matchesz3TestByRelevance.test_short_paths_are_better_matchesst,,XUGD)))5%.IWdO)=)=> eW%T7O)=)=> eW%rctjdg}tjddg}t||gtj}|j |Dcgc]}t |j c}dgggt||gtj}|j |Dcgc]}t |j c}dgggycc}wcc}w)Nr_rarcrrd)rrfsortedrhrr rb)rrjrkrrTs r*test_global_errors_are_even_better_matchesz:TestByRelevance.test_global_errors_are_even_better_matchess,,XB?)))5'B$Z-A-AB +1 2%T%**  2WbM wZ-A-AB +1 2%T%**  2WbM 3 3s C/Cctjdgd}tjdgd}tjd}t||g|}|j ||t||g|}|j ||y)Nr_arbr#rcb)weakrdrrf by_relevancergri)rrunormalr rls r%test_weak_keywords_are_lower_priorityz5TestByRelevance.test_weak_keywords_are_lower_priority)sv))(sK++IB#N,,#6 T6N 3 eV$VTN 3 eV$rcJtjdgd}tjdgd}tjdgd}tjdd}t|||g| }|j ||t|||g| }|j ||y) Nr_rrrsrcrtzOh fine!c)rustrongrdrv)rrurxr|r rls r(test_strong_keywords_are_higher_priorityz8TestByRelevance.test_strong_keywords_are_higher_priority5s))(sK++IB#N++JR3O,,#cB T66* ; eV$VVT* ; eV$rN)rXrYrZrmrpryr}r[rrr]r]s& % %rr]cHeZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) TestErrorTreectdDcgc]}tjd|}}tj|}|j |j dycc}w)N Somethingr#)rangerrf ErrorTreer total_errors)rirtrees r/test_it_knows_how_many_total_errors_it_containsz=TestErrorTree.test_it_knows_how_many_total_errors_it_containsDs_1X   & &{a @  ##F+ **A.  sA ctjddgg}tj|}|jd|y)N a messagerra)rrfrassertInrrrs r1test_it_contains_an_item_if_the_item_had_an_errorz?TestErrorTree.test_it_contains_an_item_if_the_item_had_an_errorMs7,,[wGH##F+ eT"rctjddgg}tj|}|jd|y)Nrrrar)rrfr assertNotInrs r9test_it_does_not_contain_an_item_if_the_item_had_no_errorzGTestErrorTree.test_it_does_not_contain_an_item_if_the_item_had_no_errorRs9,,[wGH##F+ %rctjdd}tj|g}|j|jd|iy)NrrrrrfrrrrrTrs r/test_keywords_that_failed_appear_in_errors_dictz=TestErrorTree.test_keywords_that_failed_appear_in_errors_dictWs>**;%H##UG, uen5rctjddgtjdddgg}tj|}|jd|d|j d|dy)Nz a bar messagerraza bar -> 0 messager)rrfrrrrs r1test_it_creates_a_child_tree_for_each_nested_pathz?TestErrorTree.test_it_creates_a_child_tree_for_each_nested_path\sh  & &eW E  & &';5!* M ##F+ ae% DK(rctjddddgtjddddg}}tj||g}|j|ddj||dy) N1rrr)r#rb2quux)rrrre1e2rs r+test_children_have_their_errors_dicts_builtz9TestErrorTree.test_children_have_their_errors_dicts_builtesn  & &se5!* M  & &sfE1: N ##RH- eQ..B0GHrctjddddgdtjddd d gd }}tj||gy) Nrrrbar2i1r#rbrrrfoobarri2)rrfr)rrrs r"test_multiple_errors_with_instancez0TestErrorTree.test_multiple_errors_with_instancems[  & &V_    & & ]    b"X&rctjddg}tj|g}|jt5|ddddy#1swYyxYw)N123r)r#rr)rrfr assertRaises IndexErrorrs r>test_it_does_not_contain_subtrees_that_are_not_in_the_instancezLTestErrorTree.test_it_does_not_contain_subtrees_that_are_not_in_the_instance|sR**5EBO##UG,   z *  G   s AActjddidg}tj|g}|j|dtjy)z If a keyword refers to a path that isn't in the instance, the tree still properly returns a subtree for that path. rr)r#rrbN)rrfrassertIsInstancers r9test_if_its_in_the_tree_anyhow_it_does_not_raise_an_errorzGTestErrorTree.test_if_its_in_the_tree_anyhow_it_does_not_raise_an_errorsM ** 52UG ##UG, d5k:+?+?@rctjddddgdtjddd d gd }}tj||g}|jt |d y) Nrrrrrrrrrrrz)rrfrrreprrs r test_reprzTestErrorTree.test_reprsr  & &V_    & & ]   ##RH- d%CDrN) rXrYrZrrrrrrrrrrr[rrrrCs7/# & 6 )I ' AErrcHeZdZdZdZdZdZdZdZdZ dZ d Z d Z y ) TestErrorInitReprStrc rtddddddi}|j|tjdi|S)Nhellorr')messager#r0rrr[)dictupdaterrf)rkwargsdefaultss r make_errorzTestErrorInitReprStr.make_errorsB$H%   ))5H55rc tj|jd}|jdi|}t |j d\}}}|j ||j|j ||y)N r[)textwrapdedentrstriprstr partitionrr)rexpectedrrT message_line_rests r assertShowsz TestErrorInitReprStr.assertShowssm??8,33D9)&) #E 4 4T : a u}}5 x(rcn|j}|jt|jdy)Nr)r assertGreaterlenargs)rrTs r!test_it_calls_super_and_sets_argsz6TestErrorInitReprStr.test_it_calls_super_and_sets_argss&! 3uzz?A.rcb|jttjddy)NzHello!rz)rrrrfrs rrzTestErrorInitReprStr.test_reprs&  ++H= > ) rctjd}|jt|ddddddid}|D]@}t |}||=tjdi|}|jt|dBy)Nrrr'r)r#r0rrr)rrfrrr)rrTrattrks rtest_unset_errorz%TestErrorInitReprStr.test_unset_errors**95 UY/ 'x(   4DV A$..>A>E   SZ 3  4rc,|jdggy)Nz Failed validating 'type' in schema: {'type': 'string'} On instance: 5 rb schema_pathrrs rtest_empty_pathsz%TestErrorInitReprStr.test_empty_pathss"    rc0|jddgdgy)Nz Failed validating 'type' in schema: {'type': 'string'} On instance[0]: 5 ritemsrrrs rtest_one_item_pathsz(TestErrorInitReprStr.test_one_item_pathss&     rc4|jdddggdy)Nz Failed validating 'type' in schema['items'][0]: {'type': 'string'} On instance[0]['a']: 5 rrr)rrrrrrs rtest_multiple_item_pathsz-TestErrorInitReprStr.test_multiple_item_pathss&  S'  rc |jdttdtt tdtddy)Na? Failed validating 'maxLength' in schema: {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19} On instance: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]  maxLength)rrr#)rr rrziprs rtest_uses_pprintz%TestErrorInitReprStr.test_uses_pprintsC 1 d%)_E"IuRy12!k 6 rcGfddt}|}tjdd|dd}jt |t |y) z Check for #164 which rendered exceptions unusable when a `ValidationError` involved instances with an `__eq__` method that returned truthy values. c"eZdZfdZfdZy)dTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBroc(jdyNzDon't!failthisotherrs r__eq__zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBro.__eq__7 (#rc(jdyrrrs r__ne__zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBro.__ne__:rrN)rXrYrZrrrsr DontEQMeBror6s  $ $rrrrsomer)r#rr0rN)rrrfrrr)rrrrTs` r:test_str_works_with_instances_having_overriden_eq_operatorzOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator/sO $& $=** "   d8nc%j1rN) rXrYrZrrrrrrrrrrr[rrrrs5 6)/ 4"      7 r2rrceZdZdZy) TestHashablecttjdgttjdgy)N)setrrf SchemaErrorrs r test_hashablezTestHashable.test_hashableIs. Z ' ' + ,- Z # #B ' ()rN)rXrYrZrr[rrrrHs*rr) unittestrr jsonschemarjsonschema.validatorsrrr]rrrr[rrrs[!1DLHDLN1%h1%hZEHZEze28e2P*8*r