
    {gL                         S SK Jr  S SKrS SKJr  S SKJr   " S S\5      r " S S\5      r " S	 S
\5      r	 " S S\5      r
 " S S\5      rg)    )TestCaseN)
exceptions)_LATEST_VERSIONc                   t    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rSrg)TestBestMatch   c                 2   [        [        U5      R                  U5      5      n[        R                  " [        U5      5      n[        R                  " [        U5      5      nU R                  UR                  5       UR                  5       SU SU 35        U$ )NzNo consistent best match!
Got: z

Then: )	listr   iter_errorsr   
best_matchiterreversedassertEqual	_contents)selfinstanceschemaerrorsbestreversed_bests         B/usr/lib/python3/dist-packages/jsonschema/tests/test_exceptions.pybest_match_ofTestBestMatch.best_match_of	   s    of-99(CD$$T&\2"--hv.>?NN##%.tfJ}oN	

     c                 ~    SSSSSS00S.00nU R                  SS/ 00US9nU R                  UR                  S	5        g )
N
propertiesfoo   bartypeobject)minPropertiesr   r   r   r"   r   r   	validatorr   r   r   s      r   (test_shallower_errors_are_better_matches6TestBestMatch.test_shallower_errors_are_better_matches   s]    %&#(68*<"=
 !!EE2;+?!O9r   c                     SSS0SS0/SS0SS0/S.nU R                  0 US9nU R                  UR                  S5        g)	zV
A property you *must* match is probably better than one you have to
match a part of.
r   r    stringnumber)r"   anyOfoneOfr#   r"   Nr$   r&   s      r   %test_oneOf_and_anyOf_are_weak_matches3TestBestMatch.test_oneOf_and_anyOf_are_weak_matches    s]     x(68*<=x(68*<=

 !!2f!=9r   c                     SSSSS0SSSS000/000nU R                  SSS00US	9nU R                  UR                  S5        g
)a  
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.
r   r   r,   r    r*   r   array   r#   Nr   r   validator_valuer&   s      r   8test_if_the_most_relevant_error_is_anyOf_it_is_traversedFTestBestMatch.test_if_the_most_relevant_error_is_anyOf_it_is_traversed.   s     *%/@'AB	
 !!EE2;+?!O--w7r   c                 v    SSS0SS0SS0/0nU R                  / US9nU R                  UR                  S5        g)zz
We don't traverse into an anyOf (as above) if all of its context errors
seem to be equally "wrong" against the instance.
r,   r    r*   integerr!   r#   Nr$   r&   s      r   3test_no_anyOf_traversal_for_equally_relevant_errorsATestBestMatch.test_no_anyOf_traversal_for_equally_relevant_errorsE   V     "#"
 !!2f!=1r   c                 j    SSS0/0nU R                  / US9nU R                  UR                  S5        g)zl
We *do* traverse anyOf with a single nested error, even though it is
vacuously equally relevant to itself.
r,   r    r*   r#   Nr$   r&   s      r   6test_anyOf_traversal_for_single_equally_relevant_errorDTestBestMatch.test_anyOf_traversal_for_single_equally_relevant_errorU   F     "

 !!2f!=0r   c                     SSSSS0SSSS000/000nU R                  SSS00US	9nU R                  UR                  S5        g
)a  
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.
r   r   r-   r    r*   r   r1   r2   r#   Nr3   r&   s      r   8test_if_the_most_relevant_error_is_oneOf_it_is_traversedFTestBestMatch.test_if_the_most_relevant_error_is_oneOf_it_is_traversedc   r7   r   c                 v    SSS0SS0SS0/0nU R                  / US9nU R                  UR                  S5        g)zz
We don't traverse into an oneOf (as above) if all of its context errors
seem to be equally "wrong" against the instance.
r-   r    r*   r9   r!   r#   Nr$   r&   s      r   3test_no_oneOf_traversal_for_equally_relevant_errorsATestBestMatch.test_no_oneOf_traversal_for_equally_relevant_errorsz   r<   r   c                 j    SSS0/0nU R                  / US9nU R                  UR                  S5        g)zl
We *do* traverse oneOf with a single nested error, even though it is
vacuously equally relevant to itself.
r-   r    r*   r#   Nr$   r&   s      r   6test_oneOf_traversal_for_single_equally_relevant_errorDTestBestMatch.test_oneOf_traversal_for_single_equally_relevant_error   r@   r   c                     SSSSS0SSSS000/000nU R                  SSS00US	9nU R                  UR                  S5        g
)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.
r   r   allOfr    r*   r   r1   r2   r#   Nr3   r&   s      r   8test_if_the_most_relevant_error_is_allOf_it_is_traversedFTestBestMatch.test_if_the_most_relevant_error_is_allOf_it_is_traversed   ss     *%/@'AB	
 !!EE2;+?!O--x8r   c           
          SSSSS0SSS0SSSS000/0/000nU R                  SSS00US	9nU R                  UR                  S5        g
)zX
We traverse into nested contexts (a oneOf containing an error in a
nested oneOf here).
r   r   r-   r    r*   r   r1   r2   r#   Nr3   r&   s      r   test_nested_context_for_oneOf+TestBestMatch.test_nested_context_for_oneOf   s     *#!' 2$0(-/@3&!"&	
& !!EE2;+?!O--w7r   c                     SSSSSS.SSS	./000nU R                  SS
0US9nU R                  UR                  S5        SSSSSS	.SSS./000nU R                  SS
0US9nU R                  UR                  S5        g )Nr   r   r,   r1   r   r    minItemsr*   
   r    	minLengthr   r#   rV   r$   r   r   r   	reordereds       r   "test_it_prioritizes_matching_types0TestBestMatch.test_it_prioritizes_matching_types   s    !(a8!);	
 !!E5>&!I5 !);!(a8	
	 !!E5>)!L5r   c                    SSSSS/SS.SS	/S
S./000nU R                  SS0US9nU R                  UR                  S5        SSSS	S
S.SSS./000nU R                  SS0US9nU R                  UR                  S5        g )Nr   r   r,   r1   r!   r   rR   r9   r*   rT   rU   r   r#   rV   r$   rW   s       r   (test_it_prioritizes_matching_union_types6TestBestMatch.test_it_prioritizes_matching_union_types   s    ")8!4!D"+X!6RH	
 !!E5>&!I5 !);!(a8	
	 !!E5>)!L5r   c                 j    SSS00nU R                  SS0US9nU R                  UR                  5        g )Nr   r   Fr   r#   )r   assertIsNoner%   r&   s      r   test_boolean_schemas"TestBestMatch.test_boolean_schemas   s;    /!!E5>&!I$..)r   c                     [        SS05      nUR                  0 5      u  nU R                  [        R                  " UR                  0 5      5      R
                  S5        g )Nr"   r   )r   r   r   r   r   r%   )r   r%   errors      r   test_one_errorTestBestMatch.test_one_error  sS    #_a$89	&&r*!!)"7"7";<FF	
r   c                     [        0 5      nU R                  [        R                  " UR	                  0 5      5      5        g )N)r   r_   r   r   r   )r   r%   s     r   test_no_errorsTestBestMatch.test_no_errors
  s0    #B'	*//	0E0Eb0IJKr    N)__name__
__module____qualname____firstlineno__r   r'   r.   r5   r:   r>   rB   rE   rH   rL   rO   rY   r\   r`   rd   rg   __static_attributes__ri   r   r   r   r      sS    	
::8.2 18.2 19&886666*

Lr   r   c                   ,    \ rS rSrS rS rS rS rSrg)TestByRelevancei  c                    [         R                  " SS/S9n[         R                  " SSS/S9n[        X/[         R                  S9nU R	                  X15        [        X!/[         R                  S9nU R	                  X15        g )NOh no!bazpathOh yes!r   r   key)r   ValidationErrormax	relevanceassertIs)r   shallowdeepmatchs       r   #test_short_paths_are_better_matches3TestByRelevance.test_short_paths_are_better_matches  sl    ,,XUGD)))5%.IWO)=)=>e%TO)=)=>e%r   c                    [         R                  " S/ S9n[         R                  " SS/S9n[        X/[         R                  S9nU R	                  U Vs/ s H  n[        UR                  5      PM     snS// /5        [        X!/[         R                  S9nU R	                  U Vs/ s H  n[        UR                  5      PM     snS// /5        g s  snf s  snf )Nrr   rt   rv   r   rw   )r   ry   sortedr{   r   r
   ru   )r   r}   r~   r   rc   s        r   *test_global_errors_are_even_better_matches:TestByRelevance.test_global_errors_are_even_better_matches  s    ,,XB?)))5'BZ-A-AB+126%T%**62WbM	

 Z-A-AB+126%T%**62WbM	
 3 3s   C&Cc                     [         R                  " S/ SS9n[         R                  " S/ SS9n[         R                  " SS9n[        X/US9nU R	                  XB5        [        X!/US9nU R	                  XB5        g )Nrr   aru   r%   rv   b)weakrw   r   ry   by_relevancerz   r|   )r   r   normalr   r   s        r   %test_weak_keywords_are_lower_priority5TestByRelevance.test_weak_keywords_are_lower_priority)  sn    ))(sK++IB#N,,#6
TN
3e$VN
3e$r   c                 &   [         R                  " S/ SS9n[         R                  " S/ SS9n[         R                  " S/ SS9n[         R                  " SSS9n[        XU/US	9nU R	                  XS5        [        X2U/US	9nU R	                  XS5        g )
Nrr   r   r   rv   r   zOh fine!c)r   strongrw   r   )r   r   r   r   r   r   s         r   (test_strong_keywords_are_higher_priority8TestByRelevance.test_strong_keywords_are_higher_priority5  s    ))(sK++IB#N++JR3O,,#cB
T6*
;e$VT*
;e$r   ri   N)	rj   rk   rl   rm   r   r   r   r   rn   ri   r   r   rp   rp     s    &
 
%%r   rp   c                   \    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rS rS rSrg)TestErrorTreeiC  c                     [        S5       Vs/ s H  n[        R                  " SUS9PM     nn[        R                  " U5      nU R	                  UR
                  S5        g s  snf )Nr   	Somethingr%   )ranger   ry   	ErrorTreer   total_errors)r   ir   trees       r   /test_it_knows_how_many_total_errors_it_contains=TestErrorTree.test_it_knows_how_many_total_errors_it_containsD  sb     1X
 &&{a@ 	 
 ##F+**A.
s   A"c                     [         R                  " SS/S9/n[         R                  " U5      nU R                  SU5        g )N	a messager   rt   )r   ry   r   assertInr   r   r   s      r   1test_it_contains_an_item_if_the_item_had_an_error?TestErrorTree.test_it_contains_an_item_if_the_item_had_an_errorM  s7    ,,[wGH##F+eT"r   c                     [         R                  " SS/S9/n[         R                  " U5      nU R                  SU5        g )Nr   r   rt   r   )r   ry   r   assertNotInr   s      r   9test_it_does_not_contain_an_item_if_the_item_had_no_errorGTestErrorTree.test_it_does_not_contain_an_item_if_the_item_had_no_errorR  s9    ,,[wGH##F+%r   c                     [         R                  " SSS9n[         R                  " U/5      nU R                  UR                  SU05        g )Nr   r   r   r   ry   r   r   r   r   rc   r   s      r   /test_keywords_that_failed_appear_in_errors_dict=TestErrorTree.test_keywords_that_failed_appear_in_errors_dictW  s>    **;%H##UG,uen5r   c                     [         R                  " SS/S9[         R                  " SSS/S9/n[         R                  " U5      nU R                  SUS   5        U R	                  SUS   5        g )Nza bar messager   rt   za bar -> 0 messager      )r   ry   r   r   r   r   s      r   1test_it_creates_a_child_tree_for_each_nested_path?TestErrorTree.test_it_creates_a_child_tree_for_each_nested_path\  sh    &&eWE&&';5!*M
 ##F+ae%DK(r   c                     [         R                  " SSSS/S9[         R                  " SSSS/S9p![         R                  " X/5      nU R                  US   S   R                  XS.5        g )	N1r   r   r   )r%   ru   2quux)r   r   r   r   e1e2r   s       r   +test_children_have_their_errors_dicts_built9TestErrorTree.test_children_have_their_errors_dicts_builte  sh    &&se5!*M&&sfE1:N  ##RH-eQ..0GHr   c                     [         R                  " SSSS/SS9[         R                  " SSS	S
/SS9p![         R                  " X/5        g )Nr   r   r   bar2i1r%   ru   r   r   r   foobarr   i2)r   ry   r   )r   r   r   s      r   "test_multiple_errors_with_instance0TestErrorTree.test_multiple_errors_with_instancem  sW    &&V_	
 && ]	  	bX&r   c                     [         R                  " SS/ S9n[         R                  " U/5      nU R                  [        5         US     S S S 5        g ! , (       d  f       g = f)N123r   )r%   r   r   )r   ry   r   assertRaises
IndexErrorr   s      r   >test_it_does_not_contain_subtrees_that_are_not_in_the_instanceLTestErrorTree.test_it_does_not_contain_subtrees_that_are_not_in_the_instance|  sI    **5EBO##UG,z*G +**s   A
A c                     [         R                  " SS0 S/S9n[         R                  " U/5      nU R                  US   [         R                  5        g)zt
If a keyword refers to a path that isn't in the instance, the
tree still properly returns a subtree for that path.
r   r   )r%   r   ru   N)r   ry   r   assertIsInstancer   s      r   9test_if_its_in_the_tree_anyhow_it_does_not_raise_an_errorGTestErrorTree.test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error  sM     **52UG
 ##UG,d5k:+?+?@r   c                     [         R                  " SSSS/SS9n[         R                  " U/5      nU R                  [	        U5      S5        g )Nr   r   r   r   r   r   z<ErrorTree (1 total error)>r   ry   r   r   reprr   s      r   test_repr_singleTestErrorTree.test_repr_single  sK    **	
 ##UG,d%BCr   c                     [         R                  " SSSS/SS9[         R                  " SSS	S
/SS9p![         R                  " X/5      nU R                  [	        U5      S5        g )Nr   r   r   r   r   r   r   r   r   r   r   z<ErrorTree (2 total errors)>r   r   s       r   test_repr_multiple TestErrorTree.test_repr_multiple  sn    &&V_	
 && ]	  ##RH-d%CDr   c                 f    [         R                  " / 5      nU R                  [        U5      S5        g )Nz<ErrorTree (0 total errors)>)r   r   r   r   )r   r   s     r   test_repr_emptyTestErrorTree.test_repr_empty  s'    ##B'd%CDr   ri   N)rj   rk   rl   rm   r   r   r   r   r   r   r   r   r   r   r   r   rn   ri   r   r   r   r   C  sC    /#
&
6
)I'
ADE Er   r   c                   P    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rS rSrg)TestErrorInitReprStri  c                 n    [        SSSSSS0S9nUR                  U5        [        R                  " S0 UD6$ )Nhellor    r*      )messager%   r4   r   r   ri   )dictupdater   ry   )r   kwargsdefaultss      r   
make_errorTestErrorInitReprStr.make_error  sB    $H%
 	))5H55r   c                    [         R                  " U5      R                  S5      nU R                  " S0 UD6n[	        U5      R                  S5      u  pEnU R                  XCR                  5        U R                  Xa5        g )N
ri   )textwrapdedentrstripr   str	partitionr   r   )r   expectedr   rc   message_line_rests          r   assertShows TestErrorInitReprStr.assertShows  se    ??8,33D9)&) #E
 4 4T :}}5(r   c                 n    U R                  5       nU R                  [        UR                  5      S5        g )Nr   )r   assertGreaterlenargs)r   rc   s     r   !test_it_calls_super_and_sets_args6TestErrorInitReprStr.test_it_calls_super_and_sets_args  s&    !3uzz?A.r   c                 ^    U R                  [        [        R                  " SS95      S5        g )NzHello!r   z<ValidationError: 'Hello!'>)r   r   r   ry   r   s    r   	test_reprTestErrorInitReprStr.test_repr  s&    ++H=>)	
r   c                    [         R                  " S5      nU R                  [        U5      S5        SSSSS0S.nU HA  n[	        U5      nXC	 [         R                  " S0 UD6nU R                  [        U5      S5        MC     g )Nr   r    r*   r   )r%   r4   r   r   r   )r   ry   r   r   r   )r   rc   r   attrks        r   test_unset_error%TestErrorInitReprStr.test_unset_error  s    **95UY/  'x(	
 DVA..>A>ESZ3	 r   c                 &    U R                  S/ / S9  g )Nz
            Failed validating 'type' in schema:
                {'type': 'string'}

            On instance:
                5
            ru   schema_pathr   r   s    r   test_empty_paths%TestErrorInitReprStr.test_empty_paths  s"      	 
	
r   c                 *    U R                  SS/S/S9  g )Nz
            Failed validating 'type' in schema:
                {'type': 'string'}

            On instance[0]:
                5
            r   itemsr  r  r   s    r   test_one_item_paths(TestErrorInitReprStr.test_one_item_paths  s&      	 	 
	
r   c                 .    U R                  SSS// SQS9  g )Nz
            Failed validating 'type' in schema['items'][0]:
                {'type': 'string'}

            On instance[0]['a']:
                5
            r   r   )r
  r   r   r  r  r   s    r   test_multiple_item_paths-TestErrorInitReprStr.test_multiple_item_paths  s&     S' 	 
	
r   c                     U R                  S[        [        S5      5      [        [	        [        S5      [        S5      5      5      SS9  g )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)r   r   r%   )r   r
   r   r   zipr   s    r   test_uses_pprint%TestErrorInitReprStr.test_uses_pprint  sC    1d %)_E"IuRy12!k 	 6	
r   c                    ^   " U 4S jS5      nU" 5       n[         R                  " SSUSSS9nT R                  [        U5      [	        U5      5        g)	z
Check for #164 which rendered exceptions unusable when a
`ValidationError` involved instances with an `__eq__` method
that returned truthy values.
c                   .   > \ rS rSrU 4S jrU 4S jrSrg)dTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBroiD  c                 (   > TR                  S5        g NzDon't!failthisotherr   s     r   __eq__kTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__eq__E      		(#r   c                 (   > TR                  S5        g r  r  r  s     r   __ne__kTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__ne__H  r#  r   ri   N)rj   rk   rl   rm   r!  r%  rn   r   s   r   DontEQMeBror  D  s    $$ $r   r'  r   r   somer   )r%   r   r4   r   N)r   ry   r   r   r   )r   r'  r   rc   s   `   r   :test_str_works_with_instances_having_overriden_eq_operatorOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator=  sM    	$ 	$ =**"
 	d8nc%j1r   ri   N)rj   rk   rl   rm   r   r   r   r   r  r  r  r  r  r)  rn   ri   r   r   r   r     s5    	6)/
4"


7
r2r   r   c                       \ rS rSrS rSrg)TestHashableiV  c                     [        [        R                  " S5      /5        [        [        R                  " S5      /5        g )N )setr   ry   SchemaErrorr   s    r   test_hashableTestHashable.test_hashableW  s.    Z''+,-Z##B'()r   ri   N)rj   rk   rl   rm   r1  rn   ri   r   r   r,  r,  V  s    *r   r,  )unittestr   r   
jsonschemar   jsonschema.validatorsr   r   rp   r   r   r,  ri   r   r   <module>r6     s[      ! 1DLH DLN1%h 1%hhEH hEVe28 e2P*8 *r   