
    `dJ                         d dl mZ d dlZd dlmZ d dlmZ  G d de      Z G d de      Z G d	 d
e      Z	 G d de      Z
 G d de      Zy)    )TestCaseN)
exceptions)_LATEST_VERSIONc                   l    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)TestBestMatchc                 .   t        t        |      j                  |            }t        j                  t        |            }t        j                  t        |            }| j                  |j                         |j                         d| d|        |S )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_ofzTestBestMatch.best_match_of	   s    of-99(CD$$T&\2"--hv.>?NN##%.tfJ}oN	

     c                     ddddddiidii}| j                  ddg ii|      }| j                  |j                  d	       y )
N
propertiesfoo   bartypeobject)minPropertiesr   r   r   r    r   r   	validatorr   r   r   s      r   (test_shallower_errors_are_better_matchesz6TestBestMatch.test_shallower_errors_are_better_matches   s]    %&#(68*<"=
 !!EE2;+?!O9r   c                     dddiddigddiddigd}| j                  i |      }| j                  |j                  d       y)	zn
        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_matchesz3TestBestMatch.test_oneOf_and_anyOf_are_weak_matches    s]     x(68*<=x(68*<=

 !!2f!=9r   c                     dddddiddddiiigiii}| j                  dddii|	      }| j                  |j                  d       y
)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.
        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_traversedzFTestBestMatch.test_if_the_most_relevant_error_is_anyOf_it_is_traversed.   s     *%/@'AB	
 !!EE2;+?!O--w7r   c                 |    dddiddiddigi}| j                  g |      }| j                  |j                  d       y)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)   r   r'   integerr   r!   Nr"   r$   s      r   3test_no_anyOf_traversal_for_equally_relevant_errorszATestBestMatch.test_no_anyOf_traversal_for_equally_relevant_errorsE   V     "#"
 !!2f!=1r   c                 p    dddigi}| j                  g |      }| j                  |j                  d       y)z
        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_errorzDTestBestMatch.test_anyOf_traversal_for_single_equally_relevant_errorU   F     "

 !!2f!=0r   c                     dddddiddddiiigiii}| j                  dddii|	      }| j                  |j                  d       y
)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.
        r   r   r*   r   r'   r   r-   r.   r!   Nr/   r$   s      r   8test_if_the_most_relevant_error_is_oneOf_it_is_traversedzFTestBestMatch.test_if_the_most_relevant_error_is_oneOf_it_is_traversedc   r2   r   c                 |    dddiddiddigi}| j                  g |      }| j                  |j                  d       y)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*   r   r'   r4   r   r!   Nr"   r$   s      r   3test_no_oneOf_traversal_for_equally_relevant_errorszATestBestMatch.test_no_oneOf_traversal_for_equally_relevant_errorsz   r6   r   c                 p    dddigi}| j                  g |      }| j                  |j                  d       y)z
        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_errorzDTestBestMatch.test_oneOf_traversal_for_single_equally_relevant_error   r9   r   c                     dddddiddddiiigiii}| j                  dddii|	      }| j                  |j                  d       y
)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   r-   r.   r!   Nr/   r$   s      r   8test_if_the_most_relevant_error_is_allOf_it_is_traversedzFTestBestMatch.test_if_the_most_relevant_error_is_allOf_it_is_traversed   ss     *%/@'AB	
 !!EE2;+?!O--x8r   c           
          dddddidddiddddiiigigiii}| j                  dddii|	      }| j                  |j                  d       y
)zp
        We traverse into nested contexts (a oneOf containing an error in a
        nested oneOf here).
        r   r   r*   r   r'   r   r-   r.   r!   Nr/   r$   s      r   test_nested_context_for_oneOfz+TestBestMatch.test_nested_context_for_oneOf   s     *#!' 2$0(-/@3&!"&	
& !!EE2;+?!O--w7r   c                    ddddddddd	giii}| j                  dd
i|      }| j                  |j                  d       dddddd	dddgiii}| j                  dd
i|      }| j                  |j                  d       y )Nr   r   r)   r-   r   r   minItemsr'   
   r   	minLengthr   r!   rJ   r"   r   r   r   	reordereds       r   "test_it_prioritizes_matching_typesz0TestBestMatch.test_it_prioritizes_matching_types   s    !(a8!);	
 !!E5>&!I5 !);!(a8	
	 !!E5>)!L5r   c                    dddddgdddd	gd
dgiii}| j                  ddi|      }| j                  |j                  d       dddd	d
ddddgiii}| j                  ddi|      }| j                  |j                  d       y )Nr   r   r)   r-   r   r   rF   r4   r'   rH   rI   r   r!   rJ   r"   rK   s       r   (test_it_prioritizes_matching_union_typesz6TestBestMatch.test_it_prioritizes_matching_union_types   s    ")8!4!D"+X!6RH	
 !!E5>&!I5 !);!(a8	
	 !!E5>)!L5r   c                 p    dddii}| j                  ddi|      }| j                  |j                         y )Nr   r   Fr   r!   )r   assertIsNoner#   r$   s      r   test_boolean_schemasz"TestBestMatch.test_boolean_schemas   s;    /!!E5>&!I$..)r   c                     t        ddi      }|j                  i       \  }| j                  t        j                  |j                  i             j
                  d       y )Nr    r   )r   r
   r   r   r   r#   )r   r#   errors      r   test_one_errorzTestBestMatch.test_one_error  sS    #_a$89	&&r*!!)"7"7";<FF	
r   c                     t        i       }| j                  t        j                  |j	                  i                    y )N)r   rQ   r   r   r
   )r   r#   s     r   test_no_errorszTestBestMatch.test_no_errors
  s0    #B'	*//	0E0Eb0IJKr   N)__name__
__module____qualname__r   r%   r+   r1   r5   r8   r;   r=   r?   rB   rD   rM   rO   rR   rU   rW    r   r   r   r      sS    	
::8.2 18.2 19&886666*

Lr   r   c                   $    e Zd Zd Zd Zd Zd Zy)TestByRelevancec                 "   t        j                  ddg      }t        j                  dddg      }t        ||gt         j                        }| j	                  ||       t        ||gt         j                        }| j	                  ||       y )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_matchesz3TestByRelevance.test_short_paths_are_better_matches  st    ,,XUGD)))5%.IWdO)=)=>eW%T7O)=)=>eW%r   c                    t        j                  dg       }t        j                  ddg      }t        ||gt         j                        }| j	                  |D cg c]  }t        |j                         c}dgg g       t        ||gt         j                        }| j	                  |D cg c]  }t        |j                         c}dgg g       y c c}w c c}w )Nr_   ra   rc   r   rd   )r   rf   sortedrh   r   r	   rb   )r   rj   rk   r   rT   s        r   *test_global_errors_are_even_better_matchesz:TestByRelevance.test_global_errors_are_even_better_matches  s    ,,XB?)))5'B$Z-A-AB+12%T%**2WbM	

 wZ-A-AB+12%T%**2WbM	
 3 3s   C/Cc                    t        j                  dg d      }t        j                  dg d      }t        j                  d      }t        ||g|      }| j	                  ||       t        ||g|      }| j	                  ||       y )Nr_   arb   r#   rc   b)weakrd   r   rf   by_relevancerg   ri   )r   ru   normalr   rl   s        r   %test_weak_keywords_are_lower_priorityz5TestByRelevance.test_weak_keywords_are_lower_priority)  sv    ))(sK++IB#N,,#6
T6N
3eV$VTN
3eV$r   c                 J   t        j                  dg d      }t        j                  dg d      }t        j                  dg d      }t        j                  dd      }t        |||g|	      }| j	                  ||       t        |||g|	      }| j	                  ||       y )
Nr_   rr   rs   rc   rt   zOh fine!c)ru   strongrd   rv   )r   ru   rx   r|   r   rl   s         r   (test_strong_keywords_are_higher_priorityz8TestByRelevance.test_strong_keywords_are_higher_priority5  s    ))(sK++IB#N++JR3O,,#cB
T66*
;eV$VVT*
;eV$r   N)rX   rY   rZ   rm   rp   ry   r}   r[   r   r   r]   r]     s    &
 
%%r   r]   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)TestErrorTreec                     t        d      D cg c]  }t        j                  d|       }}t        j                  |      }| j	                  |j
                  d       y c c}w )N   	Somethingr#   )ranger   rf   	ErrorTreer   total_errors)r   ir   trees       r   /test_it_knows_how_many_total_errors_it_containsz=TestErrorTree.test_it_knows_how_many_total_errors_it_containsD  s_     1X
 &&{a@
 
 ##F+**A.
s   A c                     t        j                  ddg      g}t        j                  |      }| j                  d|       y )N	a messager   ra   )r   rf   r   assertInr   r   r   s      r   1test_it_contains_an_item_if_the_item_had_an_errorz?TestErrorTree.test_it_contains_an_item_if_the_item_had_an_errorM  s7    ,,[wGH##F+eT"r   c                     t        j                  ddg      g}t        j                  |      }| j                  d|       y )Nr   r   ra   r   )r   rf   r   assertNotInr   s      r   9test_it_does_not_contain_an_item_if_the_item_had_no_errorzGTestErrorTree.test_it_does_not_contain_an_item_if_the_item_had_no_errorR  s9    ,,[wGH##F+%r   c                     t        j                  dd      }t        j                  |g      }| j                  |j                  d|i       y )Nr   r   r   r   rf   r   r   r   r   rT   r   s      r   /test_keywords_that_failed_appear_in_errors_dictz=TestErrorTree.test_keywords_that_failed_appear_in_errors_dictW  s>    **;%H##UG,uen5r   c                     t        j                  ddg      t        j                  dddg      g}t        j                  |      }| j                  d|d          | j	                  d|d          y )Nza bar messager   ra   za bar -> 0 messager      )r   rf   r   r   r   r   s      r   1test_it_creates_a_child_tree_for_each_nested_pathz?TestErrorTree.test_it_creates_a_child_tree_for_each_nested_path\  sh    &&eWE&&';5!*M
 ##F+ae%DK(r   c                     t        j                  ddddg      t        j                  ddddg      }}t        j                  ||g      }| j                  |d   d   j                  ||d       y )	N1r   r   r   )r#   rb   2quux)r   r   r   r   e1e2r   s       r   +test_children_have_their_errors_dicts_builtz9TestErrorTree.test_children_have_their_errors_dicts_builte  sn    &&se5!*M&&sfE1:N  ##RH-eQ..B0GHr   c                     t        j                  ddddgd      t        j                  ddd	d
gd      }}t        j                  ||g       y )Nr   r   r   bar2i1r#   rb   r   r   r   foobarr   i2)r   rf   r   )r   r   r   s      r   "test_multiple_errors_with_instancez0TestErrorTree.test_multiple_errors_with_instancem  s[    &&V_	
 && ]	  	b"X&r   c                     t        j                  ddg       }t        j                  |g      }| j                  t              5  |d    d d d        y # 1 sw Y   y xY w)N123r   )r#   r   r   )r   rf   r   assertRaises
IndexErrorr   s      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|  sR    **5EBO##UG,z* 	G	 	 	s   AAc                     t        j                  ddi dg      }t        j                  |g      }| j                  |d   t         j                         y)z
        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   rb   N)r   rf   r   assertIsInstancer   s      r   9test_if_its_in_the_tree_anyhow_it_does_not_raise_an_errorzGTestErrorTree.test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error  sM     **52UG
 ##UG,d5k:+?+?@r   c                     t        j                  ddddgd      t        j                  ddd	d
gd      }}t        j                  ||g      }| j                  t	        |      d       y )Nr   r   r   r   r   r   r   r   r   r   r   z<ErrorTree (2 total errors)>)r   rf   r   r   reprr   s       r   	test_reprzTestErrorTree.test_repr  sr    &&V_	
 && ]	  ##RH-d%CDr   N)rX   rY   rZ   r   r   r   r   r   r   r   r   r   r   r[   r   r   r   r   C  s7    /#
&
6
)I'
AEr   r   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)TestErrorInitReprStrc                 r    t        ddddddi      }|j                  |       t        j                  di |S )Nhellor   r'      )messager#   r0   r   r   r[   )dictupdater   rf   )r   kwargsdefaultss      r   
make_errorzTestErrorInitReprStr.make_error  sB    $H%
 	))5H55r   c                    t        j                  |      j                  d      } | j                  di |}t	        |      j                  d      \  }}}| j                  ||j                         | j                  ||       y )N
r[   )textwrapdedentrstripr   str	partitionr   r   )r   expectedr   rT   message_line_rests          r   assertShowsz TestErrorInitReprStr.assertShows  sm    ??8,33D9)&) #E
 4 4T :au}}5x(r   c                 n    | j                         }| j                  t        |j                        d       y )Nr   )r   assertGreaterlenargs)r   rT   s     r   !test_it_calls_super_and_sets_argsz6TestErrorInitReprStr.test_it_calls_super_and_sets_args  s&    !3uzz?A.r   c                 b    | j                  t        t        j                  d            d       y )NzHello!r   z<ValidationError: 'Hello!'>)r   r   r   rf   r   s    r   r   zTestErrorInitReprStr.test_repr  s&    ++H=>)	
r   c                     t        j                  d      }| j                  t        |      d       dddddid}|D ]@  }t	        |      }||= t        j                  di |}| j                  t        |      d       B y )Nr   r   r'   r   )r#   r0   r   r   r   )r   rf   r   r   r   )r   rT   r   attrks        r   test_unset_errorz%TestErrorInitReprStr.test_unset_error  s    **95UY/  'x(	
  	4DVA$..>A>ESZ3		4r   c                 ,    | j                  dg g        y )Nz
            Failed validating 'type' in schema:
                {'type': 'string'}

            On instance:
                5
            rb   schema_pathr   r   s    r   test_empty_pathsz%TestErrorInitReprStr.test_empty_paths  s"      	 
	
r   c                 0    | j                  ddgdg       y )Nz
            Failed validating 'type' in schema:
                {'type': 'string'}

            On instance[0]:
                5
            r   itemsr   r   r   s    r   test_one_item_pathsz(TestErrorInitReprStr.test_one_item_paths  s&      	 	 
	
r   c                 4    | j                  dddgg d       y )Nz
            Failed validating 'type' in schema['items'][0]:
                {'type': 'string'}

            On instance[0]['a']:
                5
            r   rr   )r   r   r   r   r   r   s    r   test_multiple_item_pathsz-TestErrorInitReprStr.test_multiple_item_paths  s&     S' 	 
	
r   c                     | j                  dt        t        d            t        t	        t        d      t        d                  d       y )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_pprintz%TestErrorInitReprStr.test_uses_pprint  sC    1d %)_E"IuRy12!k 	 6	
r   c                       G  fddt               } |       }t        j                  dd|dd      } j                  t	        |      t        |             y)	z
        Check for #164 which rendered exceptions unusable when a
        `ValidationError` involved instances with an `__eq__` method
        that returned truthy values.
        c                   "    e Zd Z fdZ fdZy)dTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBroc                 (    j                  d       y NzDon't!failthisotherr   s     r   __eq__zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__eq__7      		(#r   c                 (    j                  d       y r   r   r   s     r   __ne__zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__ne__:  r   r   N)rX   rY   rZ   r   r   r   s   r   DontEQMeBror   6  s    $$r   r   r   r   somer   )r#   r   r0   r   N)r   r   rf   r   r   r   )r   r   r   rT   s   `   r   :test_str_works_with_instances_having_overriden_eq_operatorzOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator/  sO    	$& 	$ =**"
 	d8nc%j1r   N)rX   rY   rZ   r   r   r   r   r   r   r   r   r   r   r[   r   r   r   r     s5    	6)/
4"


7
r2r   r   c                       e Zd Zd Zy)TestHashablec                     t        t        j                  d      g       t        t        j                  d      g       y )N )setr   rf   SchemaErrorr   s    r   test_hashablezTestHashable.test_hashableI  s.    Z''+,-Z##B'()r   N)rX   rY   rZ   r   r[   r   r   r   r   H  s    *r   r   )unittestr   r   
jsonschemar   jsonschema.validatorsr   r   r]   r   r   r   r[   r   r   <module>r     s[      ! 1DLH DLN1%h 1%hZEH ZEze28 e2P*8 *r   