rcJddlmZddlZddlmZddlmZGddeZGddeZGd d eZ Gd d eZ Gd deZ dS))TestCaseN) exceptions)_LATEST_VERSIONcneZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdS) TestBestMatchcjtt||}tjt |}tjt |}|||d|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(CCDD$T&\\22"-hv.>.>??   NN    # # % % Nt N N} N N    cddddddiidii}|ddgii|}||jd dS) N propertiesfoobartypeobject) minPropertiesrrrr rr validatorrrrs r(test_shallower_errors_are_better_matchesz6TestBestMatch.test_shallower_errors_are_better_matchesso %&#(68*<"= !!EE2;+?!OO 99999rcdddiddigddiddigd}|i|}||jddS) 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 sox(68*<=x(68*<=   !!2f!== 99999rcdddddiddddiiigiii}|dddii| }||jdd S) 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. *%/@'AB  !!EE2;+?!OO -w77777rcdddiddiddigi}|g|}||jddS)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_errorsEc "#" !!2f!== 11111rcxdddigi}|g|}||jddS)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_errorUS "  !!2f!== 00000rcdddddiddddiiigiii}|dddii| }||jdd S) 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_traversedcr2rcdddiddiddigi}|g|}||jddS)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_errorszr6rcxdddigi}|g|}||jddS)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}|dddii| }||jdd S) 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_traverseds *%/@'AB  !!EE2;+?!OO -x88888rc dddddidddiddddiiigigiii}|dddii| }||jdd S) 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;+?!OO -w77777rcddddddddd giii}|dd i| }||jd dddddd dddgiii}|dd i| }||jd dS) Nrrr)r-rrminItemsr' r minLengthrr!rJr"rrr reordereds r"test_it_prioritizes_matching_typesz0TestBestMatch.test_it_prioritizes_matching_typess !(a88!);;  !!E5>&!II 555 !);;!(a88  !!E5>)!LL 55555rcdddddgdddd gd d giii}|dd i| }||jddddd d d dddgiii}|dd i| }||jddS)Nrrr)r-rrrFr4r'rHrIrr!rJr"rKs r(test_it_prioritizes_matching_union_typesz6TestBestMatch.test_it_prioritizes_matching_union_typess ")8!4!DD"+X!6RHH  !!E5>&!II 555 !);;!(a88  !!E5>)!LL 55555rcxdddii}|ddi|}||jdS)NrrFrr!)r assertIsNoner#r$s rtest_boolean_schemasz"TestBestMatch.test_boolean_schemassH/!!E5>&!II $.)))))rctddi}|i\}|tj|ijddS)Nr r)rr rrr r#)rr#errors rtest_one_errorzTestBestMatch.test_one_errorsm#_a$899 &&r**   !)"7"7";"; < < F      rcti}|tj|idS)N)rrQrr r )rr#s rtest_no_errorszTestBestMatch.test_no_errors sB#B''  */ 0E0Eb0I0IJJKKKKKrN)__name__ __module__ __qualname__rr%r+r1r5r8r;r=r?rBrDrMrOrRrUrWrrrrs     : : : : : :888.222 1 1 1888.222 1 1 1999&888866666666***    LLLLLrrc&eZdZdZdZdZdZdS)TestByRelevancec0tjddg}tjdddg}t||gtj}|||t||gtj}|||dS)NOh no!bazpathOh yes!rrkey)rValidationErrormax relevanceassertIs)rshallowdeepmatchs r#test_short_paths_are_better_matchesz3TestByRelevance.test_short_paths_are_better_matchess,XUGDDD))5%.IIIWdO)=>>> eW%%%T7O)=>>> eW%%%%%rc`tjdg}tjddg}t||gtj}|d|Ddgggt||gtj}|d|DdgggdS)Nr_rarcrrdc6g|]}t|jSr[r rb.0rTs r zNTestByRelevance.test_global_errors_are_even_better_matches.." 2 2 2%T%*   2 2 2rc6g|]}t|jSr[rprqs rrszNTestByRelevance.test_global_errors_are_even_better_matches..%rtr)rrfsortedrhr)rrjrkrs r*test_global_errors_are_even_better_matchesz:TestByRelevance.test_global_errors_are_even_better_matchess,XB???))5'BBB$Z-ABBB  2 26 2 2 2WbM   wZ-ABBB  2 26 2 2 2WbM     rc0tjdgd}tjdgd}tjd}t||g|}|||t||g|}|||dS)Nr_arbr#rcb)weakrdrrf by_relevancergri)rr|normalr rls r%test_weak_keywords_are_lower_priorityz5TestByRelevance.test_weak_keywords_are_lower_priority)s)(sKKK+IB#NNN,#666 T6N 333 eV$$$VTN 333 eV$$$$$rcdtjdgd}tjdgd}tjdgd}tjdd}t|||g| }|||t|||g| }|||dS) Nr_ryrzrcr{zOh fine!c)r|strongrdr})rr|rrr rls r(test_strong_keywords_are_higher_priorityz8TestByRelevance.test_strong_keywords_are_higher_priority5s)(sKKK+IB#NNN+JR3OOO,#cBBB T66* ;;; eV$$$VVT* ;;; eV$$$$$rN)rXrYrZrmrwrrr[rrr]r]sP&&&   % % % % % % % %rr]cJeZdZdZdZdZdZdZdZdZ dZ d Z d Z d S) TestErrorTreecdtdD}tj|}||jddS)Nc:g|]}tjd|S) Somethingr#)rrf)rris rrszQTestErrorTree.test_it_knows_how_many_total_errors_it_contains..Fs8     &{a @ @ @   r)ranger ErrorTreer total_errorsrrtrees r/test_it_knows_how_many_total_errors_it_containsz=TestErrorTree.test_it_knows_how_many_total_errors_it_containsDsW  1XX   #F++ *A.....rctjddgg}tj|}|d|dS)N a messagerra)rrfrassertInrs r1test_it_contains_an_item_if_the_item_had_an_errorz?TestErrorTree.test_it_contains_an_item_if_the_item_had_an_errorMsF,[wGGGH#F++ eT"""""rctjddgg}tj|}|d|dS)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_errorRsH,[wGGGH#F++ %%%%%rctjdd}tj|g}||jd|idS)NrrrrrfrrrrrTrs r/test_keywords_that_failed_appear_in_errors_dictz=TestErrorTree.test_keywords_that_failed_appear_in_errors_dictWsK*;%HHH#UG,, uen55555rctjddgtjdddgg}tj|}|d|d|d|ddS)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\s  &eW E E E  &';5!* M M M #F++ ae%%% DK(((((rctjddddgtjddddg}}tj||g}||ddj||ddS) N1rrr)r#rb2quux)rrrre1e2rs r+test_children_have_their_errors_dicts_builtz9TestErrorTree.test_children_have_their_errors_dicts_builtes  &se5!* M M M  &sfE1: N N N #RH-- eQ.B0G0GHHHHHrctjddddgdtjddd d gd }}tj||gdS) Nrrrbar2i1r#rbrrrfoobarri2)rrfr)rrrs r"test_multiple_errors_with_instancez0TestErrorTree.test_multiple_errors_with_instancemsr  &V_      & ]      b"X&&&&&rctjddg}tj|g}|t5|dddddS#1swxYwYdS)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|s*5EBOOO#UG,,   z * *   GG                  s AA!$A!ctjddidg}tj|g}||dtjdS)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_errors] * 52UG   #UG,, d5k:+?@@@@@rctjddddgdtjddd d gd }}tj||g}|t |d dS) Nrrrrrrrrrrrz)rrfrrreprrs r test_reprzTestErrorTree.test_reprs  &V_      & ]     #RH-- d%CDDDDDrN) rXrYrZrrrrrrrrrrr[rrrrCs///### &&& 666 )))III ' ' ' A A AEEEEErrcJeZdZdZdZdZdZdZdZdZ dZ d Z d Z d S) TestErrorInitReprStrc ztddddddi}||tjdi|S)Nhellorr')messager#r0rrr[)dictupdaterrf)rkwargsdefaultss r make_errorzTestErrorInitReprStr.make_errorsU$H%     )55H555rc tj|d}|jdi|}t |d\}}}|||j|||dS)N r[)textwrapdedentrstriprstr partitionrr)rexpectedrrT message_line_rests r assertShowsz TestErrorInitReprStr.assertShowss?8,,33D99))&)) #E 4 4T : : a u}555 x(((((rc~|}|t|jddS)Nr)r assertGreaterlenargs)rrTs r!test_it_calls_super_and_sets_argsz6TestErrorInitReprStr.test_it_calls_super_and_sets_argss5!! 3uz??A.....rcr|ttjdddS)NzHello!rz)rrrrfrs rrzTestErrorInitReprStr.test_reprs@  +H=== > > )     rctjd}|t|ddddddid}|D]H}t |}||=tjdi|}|t|dIdS)Nrrr'r)r#r0rrr)rrfrrr)rrTrattrks rtest_unset_errorz%TestErrorInitReprStr.test_unset_errors*955 UY/// 'x(    4 4DV A$.>>A>>E   SZZ 3 3 3 3  4 4rc6|dggdS)Nz Failed validating 'type' in schema: {'type': 'string'} On instance: 5 rb schema_pathrrs rtest_empty_pathsz%TestErrorInitReprStr.test_empty_pathss6    rc:|ddgdgdS)Nz Failed validating 'type' in schema: {'type': 'string'} On instance[0]: 5 ritemsrrrs rtest_one_item_pathsz(TestErrorInitReprStr.test_one_item_pathss:     rc>|dddggddS)Nz Failed validating 'type' in schema['items'][0]: {'type': 'string'} On instance[0]['a']: 5 rry)rrrrrrs rtest_multiple_item_pathsz-TestErrorInitReprStr.test_multiple_item_pathss>  S'  rc |dttdtt tdtdddS)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_pprintse 1 d%))__E"IIuRyy1122!k 6 6 6 6 6 rcGfddt}|}tjdd|dd}t |t |dS) z Check for #164 which rendered exceptions unusable when a `ValidationError` involved instances with an `__eq__` method that returned truthy values. c$eZdZfdZfdZdS)dTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBroc2ddSNzDon't!failthisotherrs r__eq__zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBro.__eq__7 (#####rc2ddSrrrs r__ne__zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBro.__ne__:rrN)rXrYrZrrrsr DontEQMeBror6sG $ $ $ $ $ $ $ $ $ $ $ $rrrrsomer)r#rr0rN)rrrfrrr)rrrrTs` r:test_str_works_with_instances_having_overriden_eq_operatorzOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator/s $ $ $ $ $ $ $& $ $ $;==* "     d8nnc%jj11111rN) rXrYrZrrrrrrrrrrr[rrrrs 6 6 6)))///   444"            7 7 7 r22222rrceZdZdZdS) TestHashablecttjdgttjdgdS)N)setrrf SchemaErrorrs r test_hashablezTestHashable.test_hashableIs@ Z ' + + ,--- Z #B ' ' ()))))rN)rXrYrZrr[rrrrHs#*****rr) unittestrr jsonschemarjsonschema.validatorsrrr]rrrr[rrr sL!!!!!!111111DLDLDLDLDLHDLDLDLN1%1%1%1%1%h1%1%1%hZEZEZEZEZEHZEZEZEze2e2e2e2e28e2e2e2P*****8*****r