3 F\ 6@sddlZddlmZmZddlmZddlmZmZGdddej Z Gdddej Z Gd d d ej Z Gd d d ej Z dS) N)Draft4Validator exceptions)PY3)mockunittestc@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS) TestBestMatchcCs@t|}tj|}tjt|}d}|j|||j||d|S)Nz:Didn't return a consistent best match! Got: {0} Then: {1})msg)listr best_matchreversed assertEqualformat)selferrorsbestZ reversed_bestrr|/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/jsonschema/jsonschema/tests/test_exceptions.pyr s zTestBestMatch.best_matchcCsHtddddddiidii}|j|jddgii}|j|jddS) N propertiesfoobartypeobject) minPropertiesrr)rr iter_errorsr validator)rrrrrr(test_shallower_errors_are_better_matchessz6TestBestMatch.test_shallower_errors_are_better_matchescCsJtdddiddigddiddigd}|j|ji}|j|jddS)zo A property you *must* match is probably better than one you have to match a part of. rrstringnumber)ranyOfoneOfrN)rr rr r)rrrrrr%test_oneOf_and_anyOf_are_weak_matches!s z3TestBestMatch.test_oneOf_and_anyOf_are_weak_matchesc CsRtdddddiddddiiigiii}|j|jdddii}|j|jdd S) aM 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 N)rr rr validator_value)rrrrrr8test_if_the_most_relevant_error_is_anyOf_it_is_traversed2s zFTestBestMatch.test_if_the_most_relevant_error_is_anyOf_it_is_traversedc CsRtdddddiddddiiigiii}|j|jdddii}|j|jdd S) aM 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#N)rr rr r$)rrrrrr8test_if_the_most_relevant_error_is_oneOf_it_is_traversedLs zFTestBestMatch.test_if_the_most_relevant_error_is_oneOf_it_is_traversedc CsRtdddddiddddiiigiii}|j|jdddii}|j|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#N)rr rr r$)rrrrrr8test_if_the_most_relevant_error_is_allOf_it_is_traversedfszFTestBestMatch.test_if_the_most_relevant_error_is_allOf_it_is_traversedc Cs^tdddddidddiddddiiigigiii}|j|jdddii}|j|jddS) Nrrr rrrr"r#)rr rr r$)rrrrrrtest_nested_context_for_oneOf|s z+TestBestMatch.test_nested_context_for_oneOfcCs6tddi}|ji\}|jtj|jijddS)Nrr)rrr rr r)rrerrorrrrtest_one_errors   zTestBestMatch.test_one_errorcCs"ti}|jtj|jidS)N)rZ assertIsNonerr r)rrrrrtest_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}|j||t||gtjd}|j||dS)NzOh no!Zbaz)pathzOh yes!rr)key)rValidationErrormax relevanceassertIs)rshallowdeepmatchrrr#test_short_paths_are_better_matchess  z3TestByRelevance.test_short_paths_are_better_matchescCs~tjdgd}tjddgd}t||gtjd}|jdd|Ddgggt||gtjd}|jdd|DdgggdS) NzOh no!)r0zOh yes!r)r1cSsg|]}t|jqSr)r r0).0r)rrr szNTestByRelevance.test_global_errors_are_even_better_matches..cSsg|]}t|jqSr)r r0)r:r)rrrr;s)rr2sortedr4r )rr6r7rrrr*test_global_errors_are_even_better_matchess   z:TestByRelevance.test_global_errors_are_even_better_matchescCshtjdgdd}tjdgdd}tjdd}t||g|d}|j||t||g|d}|j||dS)NzOh no!a)r0rzOh yes!b)weak)r1)rr2 by_relevancer3r5)rr@normalr r8rrr'test_weak_validators_are_lower_prioritys  z7TestByRelevance.test_weak_validators_are_lower_prioritycCs~tjdgdd}tjdgdd}tjdgdd}tjddd}t|||g|d }|j||t|||g|d }|j||dS) NzOh no!r>)r0rzOh yes!r?zOh fine!c)r@strong)r1)rr2rAr3r5)rr@rBrEr r8rrr*test_strong_validators_are_higher_prioritys z:TestByRelevance.test_strong_validators_are_higher_priorityN)r,r-r.r9r=rCrFrrrrr/s  r/c@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS) TestErrorTreecCs.ddtdD}tj|}|j|jddS)NcSsg|] }tjqSr)r MagicMock)r:_rrrr;szQTestErrorTree.test_it_knows_how_many_total_errors_it_contains..)ranger ErrorTreer Z total_errors)rrtreerrr/test_it_knows_how_many_total_errors_it_containss z=TestErrorTree.test_it_knows_how_many_total_errors_it_containscCs,tjddgdg}tj|}|jd|dS)Nz a messager)r0)rr2rLassertIn)rrrMrrr1test_it_contains_an_item_if_the_item_had_an_errors z?TestErrorTree.test_it_contains_an_item_if_the_item_had_an_errorcCs,tjddgdg}tj|}|jd|dS)Nz a messager)r0r)rr2rL assertNotIn)rrrMrrr9test_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}tj|g}|j|jd|idS)Nz a messager)r)rr2rLr r)rr)rMrrr1test_validators_that_failed_appear_in_errors_dicts z?TestErrorTree.test_validators_that_failed_appear_in_errors_dictcCsPtjddgdtjdddgdg}tj|}|jd|d|jd|ddS)Nz a bar messager)r0za bar -> 0 messager)rr2rLrOrQ)rrrMrrr1test_it_creates_a_child_tree_for_each_nested_paths  z?TestErrorTree.test_it_creates_a_child_tree_for_each_nested_pathcCsXtjddddgdtjddddgd}}tj||g}|j|ddj||ddS) N1rrr)rr02quux)rrX)rr2rLr r)re1e2rMrrr+test_children_have_their_errors_dicts_builtsz9TestErrorTree.test_children_have_their_errors_dicts_builtcCs@tjddddgddtjddd d gd d}}tj||gdS) NrVrrZbar2i1)rr0instancerWrXZfoobarri2)rr2rL)rrYrZrrr-test_regression_multiple_errors_with_instances z;TestErrorTree.test_regression_multiple_errors_with_instancec Cs>tjddgd}tj|g}|jt|dWdQRXdS)NZ123r)rr]r)rr2rLZ assertRaises IndexError)rr)rMrrr>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}tj|g}|j|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. z a messager)rr]r0N)rr2rLZassertIsInstance)rr)rMrrr9test_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.rNrPrRrSrUr[r_rarbrrrrrGs rGc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS)TestErrorInitReprStrcKs,tddddddid}|j|tjf|S)NZhellorr)messagerr$r]schema)dictupdaterr2)rkwargsdefaultsrrr make_error#s  zTestErrorInitReprStr.make_errorcKs^tr|jdd}tj|jd}|jf|}t|jd\}}}|j||j |j||dS)Nzu'' ) rreplacetextwrapdedentrstriprkstr partitionr re)rexpectedrir)Z message_linerIrestrrr assertShows.s  z TestErrorInitReprStr.assertShowscCs|j}|jt|jddS)NrT)rkZ assertGreaterlenargs)rr)rrr!test_it_calls_super_and_sets_args8sz6TestErrorInitReprStr.test_it_calls_super_and_sets_argscCs |jttjdddddS)NzHello!)rez)r reprrr2)rrrr test_repr<szTestErrorInitReprStr.test_reprcCshtjd}|jt|ddddddid}x6|D].}t|}||=tjd|}|jt|dq2WdS)Nrerrrd)rr$r]rf)re)rr2r rrrg)rr)riattrkrrrtest_unset_errorBs    z%TestErrorInitReprStr.test_unset_errorcCs|jdggddS)Nz Failed validating u'type' in schema: {u'type': u'string'} On instance: 5 )r0 schema_path)rv)rrrrtest_empty_pathsSsz%TestErrorInitReprStr.test_empty_pathscCs|jddgdgddS)Nz Failed validating u'type' in schema: {u'type': u'string'} On instance[0]: 5 ritems)r0r)rv)rrrrtest_one_item_paths`sz(TestErrorInitReprStr.test_one_item_pathscCs|jdddgdddgddS)Nz Failed validating u'type' in schema[u'items'][0]: {u'type': u'string'} On instance[0][u'a']: 5 rr>rrT)r0r)rv)rrrrtest_multiple_item_pathsmsz-TestErrorInitReprStr.test_multiple_item_pathsc Cs4tjd }t|j|j|jdWdQRXdS)Nzpprint.pformatr)rpatchrrrkr Z call_count)rpformatrrrtest_uses_pprintzs  z%TestErrorInitReprStr.test_uses_pprintcCs6tj}tjdd|ddd}t||j|jjdS)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. z a messagerZsomerf)rr]r$rfN)rrHrr2rrZ assertFalse__eq__called)rr]r)rrr:test_str_works_with_instances_having_overriden_eq_operatorszOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operatorN) r,r-r.rkrvryr{r~rrrrrrrrrrc"s     rc)roZ jsonschemarrZjsonschema.compatrZjsonschema.tests.compatrrZTestCaserr/rGrcrrrrs 4L