a 5q];@sddlmZddlZddlmZmZddlmZGdddeZGdddeZ Gd d d eZ Gd d d eZ Gd ddeZ dS))TestCaseN)Draft4Validator exceptions)PY3c@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS) TestBestMatchcCsHt|}t|}tt|}d}|j|||||d|S)Nz:Didn't return a consistent best match! Got: {0} Then: {1})msg)listr best_matchreversed assertEqualZ _contentsformat)selferrorsbestZ reversed_bestrrD/usr/lib/python3.9/site-packages/jsonschema/tests/test_exceptions.pyr s   zTestBestMatch.best_matchcCsHtddddddiidii}||ddgii}||jddS) N propertiesfoobartypeobject) minPropertiesrrrr iter_errorsr validatorr rrrrr(test_shallower_errors_are_better_matchess  z6TestBestMatch.test_shallower_errors_are_better_matchescCsJtdddiddigddiddigd}||i}||jddS)zn A property you *must* match is probably better than one you have to match a part of. rrstringZnumber)ranyOfoneOfrNrrrrr%test_oneOf_and_anyOf_are_weak_matches"sz3TestBestMatch.test_oneOf_and_anyOf_are_weak_matchesc CsRtdddddiddddiiigiii}||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. rrrrrrarray Nrr rr validator_valuerrrr8test_if_the_most_relevant_error_is_anyOf_it_is_traversed2s  zFTestBestMatch.test_if_the_most_relevant_error_is_anyOf_it_is_traversedc CsRtdddddiddddiiigiii}||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 rrrr"r#Nr$rrrr8test_if_the_most_relevant_error_is_oneOf_it_is_traversedKs  zFTestBestMatch.test_if_the_most_relevant_error_is_oneOf_it_is_traversedc CsRtdddddiddddiiigiii}||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. rrZallOfrrrr"r#Nr$rrrr8test_if_the_most_relevant_error_is_allOf_it_is_traversedds zFTestBestMatch.test_if_the_most_relevant_error_is_allOf_it_is_traversedc Cs^tdddddidddiddddiiigigiii}||dddii}||jddS) Nrrr rrrr"r#r$rrrrtest_nested_context_for_oneOfys(z+TestBestMatch.test_nested_context_for_oneOfcCs6tddi}|i\}|t|ijddS)Nrr)rrr rr r)r rerrorrrrtest_one_errors   zTestBestMatch.test_one_errorcCs"ti}|t|idS)N)rZ assertIsNonerr r)r rrrrtest_no_errorsszTestBestMatch.test_no_errorsN) __name__ __module__ __qualname__r rr!r&r'r(r)r+r,rrrrrs rc@s,eZdZddZddZddZddZd S) TestByRelevancecCsbtjddgd}tjdddgd}t||gtjd}|||t||gtjd}|||dS)NOh no!ZbazpathOh yes!rrkey)rValidationErrormax relevanceassertIs)r shallowdeepmatchrrr#test_short_paths_are_better_matchess  z3TestByRelevance.test_short_paths_are_better_matchescCs~tjdgd}tjddgd}t||gtjd}|dd|Ddgggt||gtjd}|dd|DdgggdS) Nr1r2r4rr5cSsg|]}t|jqSrrr3.0r*rrr zNTestByRelevance.test_global_errors_are_even_better_matches..cSsg|]}t|jqSrr?r@rrrrBrC)rr7sortedr9r )r r;r<rrrr*test_global_errors_are_even_better_matchess  z:TestByRelevance.test_global_errors_are_even_better_matchescCshtjdgdd}tjdgdd}tjdd}t||g|d}|||t||g|d}|||dS)Nr1ar3rr4b)weakr5rr7Z by_relevancer8r:)r rInormalr r=rrr'test_weak_validators_are_lower_prioritys  z7TestByRelevance.test_weak_validators_are_lower_prioritycCs~tjdgdd}tjdgdd}tjdgdd}tjddd}t|||g|d }|||t|||g|d }|||dS) Nr1rFrGr4rHzOh fine!c)rIstrongr5rJ)r rIrKrNr r=rrr*test_strong_validators_are_higher_prioritys z:TestByRelevance.test_strong_validators_are_higher_priorityN)r-r.r/r>rErLrOrrrrr0s  r0c@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS) TestErrorTreecCs.ddtdD}t|}||jddS)NcSsg|]}tjd|dqS)Z Somethingr)rr7)rAirrrrBszQTestErrorTree.test_it_knows_how_many_total_errors_it_contains..)ranger ErrorTreer Z total_errorsr rtreerrr/test_it_knows_how_many_total_errors_it_containss  z=TestErrorTree.test_it_knows_how_many_total_errors_it_containscCs,tjddgdg}t|}|d|dS)N a messagerr2)rr7rUassertInrVrrr1test_it_contains_an_item_if_the_item_had_an_errors z?TestErrorTree.test_it_contains_an_item_if_the_item_had_an_errorcCs,tjddgdg}t|}|d|dS)NrYrr2r)rr7rU assertNotInrVrrr9test_it_does_not_contain_an_item_if_the_item_had_no_errors zGTestErrorTree.test_it_does_not_contain_an_item_if_the_item_had_no_errorcCs0tjddd}t|g}||jd|idS)NrYrrQrr7rUr rr r*rWrrr1test_validators_that_failed_appear_in_errors_dicts z?TestErrorTree.test_validators_that_failed_appear_in_errors_dictcCsPtjddgdtjdddgdg}t|}|d|d|d|ddS)Nz a bar messagerr2za bar -> 0 messager)rr7rUrZr\rVrrr1test_it_creates_a_child_tree_for_each_nested_paths  z?TestErrorTree.test_it_creates_a_child_tree_for_each_nested_pathcCsXtjddddgdtjddddgd}}t||g}||ddj||ddS) N1rrr)rr32quux)rrer^)r e1e2rWrrr+test_children_have_their_errors_dicts_builts z9TestErrorTree.test_children_have_their_errors_dicts_builtcCs@tjddddgddtjddd d gd d}}t||gdS) NrcrrZbar2Zi1)rr3instancerdreZfoobarrZi2)rr7rU)r rfrgrrr"test_multiple_errors_with_instances z0TestErrorTree.test_multiple_errors_with_instancecCsRtjddgd}t|g}|t|dWdn1sD0YdS)NZ123r)rrir)rr7rUZ assertRaises IndexErrorr_rrr>test_it_does_not_contain_subtrees_that_are_not_in_the_instance s  zLTestErrorTree.test_it_does_not_contain_subtrees_that_are_not_in_the_instancecCs6tjddidgd}t|g}||dtjdS)z If a validator is dumb (like :validator:`required` in draft 3) and refers to a path that isn't in the instance, the tree still properly returns a subtree for that path. rYr)rrir3N)rr7rUZassertIsInstancer_rrr9test_if_its_in_the_tree_anyhow_it_does_not_raise_an_errors   zGTestErrorTree.test_if_its_in_the_tree_anyhow_it_does_not_raise_an_errorN) r-r.r/rXr[r]r`rbrhrjrlrmrrrrrPs  rPc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS)TestErrorInitReprStrcKs0tddddddid}||tjfi|S)NZhellorr)messagerr%rischema)dictupdaterr7)r kwargsdefaultsrrr make_error"s zTestErrorInitReprStr.make_errorcKsbtr|dd}t|d}|jfi|}t|d\}}}|||j |||dS)Nzu'' ) rreplacetextwrapdedentrstriprvstr partitionr rp)r Zexpectedrtr*Z message_line_restrrr assertShows-s z TestErrorInitReprStr.assertShowscCs|}|t|jddS)Nra)rvZ assertGreaterlenargs)r r*rrr!test_it_calls_super_and_sets_args7sz6TestErrorInitReprStr.test_it_calls_super_and_sets_argscCs |ttjdddddS)NzHello!)rpz)r reprrr7r rrr test_repr;szTestErrorInitReprStr.test_reprcCshtd}|t|ddddddid}|D]2}t|}||=tjdi|}|t|dq0dS)Nrprrro)rr%rirq)rp)rr7r r}rr)r r*rtattrkrrrtest_unset_errorAs z%TestErrorInitReprStr.test_unset_errorcCs|jdggddS)Nz Failed validating u'type' in schema: {u'type': u'string'} On instance: 5 r3Z schema_pathrrrrrtest_empty_pathsRs z%TestErrorInitReprStr.test_empty_pathscCs|jddgdgddS)Nz Failed validating u'type' in schema: {u'type': u'string'} On instance[0]: 5 ritemsrrrrrrtest_one_item_paths_s z(TestErrorInitReprStr.test_one_item_pathscCs|jdddggdddS)Nz Failed validating u'type' in schema[u'items'][0]: {u'type': u'string'} On instance[0][u'a']: 5 rrF)rrrarrrrrrtest_multiple_item_pathsls z-TestErrorInitReprStr.test_multiple_item_pathscCs0|jdttdtttdtddddS)Na@ Failed validating u'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] Z maxLength)rirqr)rrrTrrziprrrrtest_uses_pprintys 2 z%TestErrorInitReprStr.test_uses_pprintcsFGfdddt}|}tjdd|ddd}t|t|dS) z Check for https://github.com/Julian/jsonschema/issues/164 which rendered exceptions unusable when a `ValidationError` involved instances with an `__eq__` method that returned truthy values. cs$eZdZfddZfddZdS)zdTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBrocsddSNzDon't!Zfailthisotherrrr__eq__szkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBro.__eq__csddSrrrrrr__ne__szkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBro.__ne__N)r-r.r/rrrrrr DontEQMeBros rrYrZsomerq)rrir%rqN)rrr7rZrr})r rrir*rrr:test_str_works_with_instances_having_overriden_eq_operatorszOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operatorN) r-r.r/rvrrrrrrrrrrrrrrn!s     9rnc@seZdZddZdS) TestHashablecCs$ttdgttdgdS)N)setrr7Z SchemaErrorrrrr test_hashableszTestHashable.test_hashableN)r-r.r/rrrrrrsr) ZunittestrrzZ jsonschemarrZjsonschema.compatrrr0rPrnrrrrrs  4N+