B pSe;@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_bestrrQ/opt/alt/python37/lib/python3.7/site-packages/jsonschema/tests/test_exceptions.pyr s  zTestBestMatch.best_matchcCsHtddddddiidii}||ddgii}||jddS) N propertiesfoobartypeobject) minPropertiesrr)rr iter_errorsr validator)r rrrrr(test_shallower_errors_are_better_matchessz6TestBestMatch.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. rrstringnumber)ranyOfoneOfrN)rr rr r)r rrrrr%test_oneOf_and_anyOf_are_weak_matches"s z3TestBestMatch.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 N)rr rr validator_value)r rrrrr8test_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. rrrrrrr!r"N)rr rr r#)r rrrrr8test_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"N)rr rr r#)r rrrrr8test_if_the_most_relevant_error_is_allOf_it_is_traverseddszFTestBestMatch.test_if_the_most_relevant_error_is_allOf_it_is_traversedc Cs^tdddddidddiddddiiigigiii}||dddii}||jddS) Nrrrrrrr!r")rr rr r#)r rrrrrtest_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)NzOh no!Zbaz)pathzOh yes!rr)key)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) NzOh no!)r/zOh yes!r)r0cSsg|]}t|jqSr)rr/).0r(rrr szNTestByRelevance.test_global_errors_are_even_better_matches..cSsg|]}t|jqSr)rr/)r9r(rrrr:s)rr1sortedr3r )r r5r6rrrr*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)NzOh no!a)r/rzOh yes!b)weak)r0)rr1 by_relevancer2r4)r r?normalr r7rrr'test_weak_validators_are_lower_prioritys  z7TestByRelevance.test_weak_validators_are_lower_prioritycCs~tjdgdd}tjdgdd}tjdgdd}tjddd}t|||g|d }|||t|||g|d }|||dS) NzOh no!r=)r/rzOh yes!r>zOh fine!c)r?strong)r0)rr1r@r2r4)r r?rArDr r7rrr*test_strong_validators_are_higher_prioritys z:TestByRelevance.test_strong_validators_are_higher_priorityN)r+r,r-r8r<rBrErrrrr.s  r.c@sTeZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ dS) TestErrorTreecCs.ddtdD}t|}||jddS)NcSsg|]}tjd|dqS)Z Something)r)rr1)r9irrrr:szQTestErrorTree.test_it_knows_how_many_total_errors_it_contains..)ranger ErrorTreer Z total_errors)r 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)Nz a messager)r/)rr1rJassertIn)r rrKrrr1test_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)Nz a messager)r/r)rr1rJ assertNotIn)r rrKrrr9test_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)Nz a messager)r)rr1rJr r)r r(rKrrr1test_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 messager)r/za bar -> 0 messager)rr1rJrMrO)r rrKrrr1test_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)rr/2quux)rrV)rr1rJr r)r e1e2rKrrr+test_children_have_their_errors_dicts_builtsz9TestErrorTree.test_children_have_their_errors_dicts_builtcCs@tjddddgddtjddd d gd d}}t||gdS) NrTrrZbar2i1)rr/instancerUrVZfoobarri2)rr1rJ)r rWrXrrr"test_multiple_errors_with_instances z0TestErrorTree.test_multiple_errors_with_instancec Cs>tjddgd}t|g}|t|dWdQRXdS)NZ123r)rr[r)rr1rJZ assertRaises IndexError)r r(rKrrr>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. z a messager)rr[r/N)rr1rJZassertIsInstance)r r(rKrrr9test_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-rLrNrPrQrSrYr]r_r`rrrrrFs  rFc@s\eZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ dS)TestErrorInitReprStrcKs,tddddddid}||tjf|S)NZhellorr)messagerr#r[schema)dictupdaterr1)r kwargsdefaultsrrr make_error"s  zTestErrorInitReprStr.make_errorcKs^tr|dd}t|d}|jf|}t|d\}}}|||j |||dS)Nzu'' ) rreplacetextwrapdedentrstripristr partitionr rc)r Zexpectedrgr(Z message_line_restrrr assertShows-s  z TestErrorInitReprStr.assertShowscCs|}|t|jddS)NrR)riZ assertGreaterlenargs)r r(rrr!test_it_calls_super_and_sets_args7sz6TestErrorInitReprStr.test_it_calls_super_and_sets_argscCs |ttjdddddS)NzHello!)rcz)r reprrr1)r rrr test_repr;szTestErrorInitReprStr.test_reprcCshtd}|t|ddddddid}x6|D].}t|}||=tjd|}|t|dq2WdS)Nrcrrrb)rr#r[rd)rc)rr1r rpre)r r(rgattrkrrrtest_unset_errorAs    z%TestErrorInitReprStr.test_unset_errorcCs|jdggddS)Nz Failed validating u'type' in schema: {u'type': u'string'} On instance: 5 )r/ schema_path)rt)r rrrtest_empty_pathsRsz%TestErrorInitReprStr.test_empty_pathscCs|jddgdgddS)Nz Failed validating u'type' in schema: {u'type': u'string'} On instance[0]: 5 ritems)r/r})rt)r rrrtest_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=rrR)r/r})rt)r rrrtest_multiple_item_pathslsz-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)r[rdr)rtrrIrezip)r rrrtest_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..DontEQMeBrocsddS)NzDon't!)fail)thisother)r rr__eq__szkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBro.__eq__csddS)NzDon't!)r)rr)r rr__ne__szkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator..DontEQMeBro.__ne__N)r+r,r-rrr)r rr DontEQMeBros rz a messagerZsomerd)rr[r#rdN)rrr1rMrxrp)r rr[r(r)r r:test_str_works_with_instances_having_overriden_eq_operatorszOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operatorN) r+r,r-rirtrwryr|r~rrrrrrrrra!s     9rac@seZdZddZdS) TestHashablecCs$ttdgttdgdS)N)setrr1Z SchemaError)r rrr test_hashableszTestHashable.test_hashableN)r+r,r-rrrrrrsr) ZunittestrrmZ jsonschemarrZjsonschema.compatrrr.rFrarrrrrs  4N+