o
    mi^                     @   s   d dl mZ d dlZd dlZd dlmZ d dlmZ G dd deZG dd deZ	G d	d
 d
eZ
G dd deZG dd deZG dd deZdS )    )TestCaseN)
exceptions)_LATEST_VERSIONc                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)S )*TestBestMatchc                 C   st   t t||}d| d|d}| j||d tt|}tt|}| |	 |	 d| d|  |S )NzNo errors found for z under !)msgzNo consistent best match!
Got: z

Then: )
listr   iter_errorsZ
assertTruer   
best_matchiterreversedassertEqualZ	_contents)selfinstanceschemaerrorsr   bestZreversed_best r   Z/home/kim/smarthome/.venv/lib/python3.10/site-packages/jsonschema/tests/test_exceptions.pybest_match_of   s   zTestBestMatch.best_match_ofc                 C   sB   ddddddiidii}| j ddg ii|d}| |jd	 d S )
N
propertiesfoo   bartypeobject)minPropertiesr   r   r   r   r   r   	validatorr   r   r   r   r   r   (test_shallower_errors_are_better_matches   s   
z6TestBestMatch.test_shallower_errors_are_better_matchesc                 C   sD   dddiddigddiddigd}| j i |d}| |jd dS )	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    r   r   r   %test_oneOf_and_anyOf_are_weak_matches&   s   z3TestBestMatch.test_oneOf_and_anyOf_are_weak_matchesc                 C   L   dddddiddddiiigiii}| j dddii|d	}| |jd d
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.
        r   r   r$   r   r"   r   array   r   Nr   r   validator_valuer    r   r   r   8test_if_the_most_relevant_error_is_anyOf_it_is_traversed4      
zFTestBestMatch.test_if_the_most_relevant_error_is_anyOf_it_is_traversedc                 C   :   dddiddiddigi}| j g |d}| |jd dS )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    r   r   r   3test_no_anyOf_traversal_for_equally_relevant_errorsK      zATestBestMatch.test_no_anyOf_traversal_for_equally_relevant_errorsc                 C   .   dddigi}| j g |d}| |jd dS )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    r   r   r   6test_anyOf_traversal_for_single_equally_relevant_error[      zDTestBestMatch.test_anyOf_traversal_for_single_equally_relevant_errorc                 C   6   ddddiigi}| j ddg|d}| |jd dS )z|
        We *do* traverse anyOf with a single subschema that fails multiple
        times (e.g. on multiple items).
        r$   itemsconst%   r)   r   Nr   r    r   r   r   .test_anyOf_traversal_for_single_sibling_errorsi      
z<TestBestMatch.test_anyOf_traversal_for_single_sibling_errorsc                 C   <   dddidddiigi}| j ddg|d}| |jd d	S )
zg
        We *do* traverse anyOf with multiple subschemas when one does not type
        match.
        r$   r   r   r6   r7   r8   r)   r   Nr   r    r   r   r   9test_anyOf_traversal_for_non_type_matching_sibling_errorsw      
zGTestBestMatch.test_anyOf_traversal_for_non_type_matching_sibling_errorsc                 C   r'   )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    r   r   r   8test_if_the_most_relevant_error_is_oneOf_it_is_traversed   r-   zFTestBestMatch.test_if_the_most_relevant_error_is_oneOf_it_is_traversedc                 C   r.   )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"   r/   r   r   Nr   r    r   r   r   3test_no_oneOf_traversal_for_equally_relevant_errors   r1   zATestBestMatch.test_no_oneOf_traversal_for_equally_relevant_errorsc                 C   r2   )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    r   r   r   6test_oneOf_traversal_for_single_equally_relevant_error   r4   zDTestBestMatch.test_oneOf_traversal_for_single_equally_relevant_errorc                 C   r5   )z|
        We *do* traverse oneOf with a single subschema that fails multiple
        times (e.g. on multiple items).
        r%   r6   r7   r8   r)   r   Nr   r    r   r   r   .test_oneOf_traversal_for_single_sibling_errors   r:   z<TestBestMatch.test_oneOf_traversal_for_single_sibling_errorsc                 C   r;   )
zg
        We *do* traverse oneOf with multiple subschemas when one does not type
        match.
        r%   r   r   r6   r7   r8   r)   r   Nr   r    r   r   r   9test_oneOf_traversal_for_non_type_matching_sibling_errors   r=   zGTestBestMatch.test_oneOf_traversal_for_non_type_matching_sibling_errorsc                 C   sL   dddddiddddiiigiii}| j dddii|d	}| |jd d
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.
        r   r   ZallOfr   r"   r   r(   r)   r   Nr*   r    r   r   r   8test_if_the_most_relevant_error_is_allOf_it_is_traversed   s   
zFTestBestMatch.test_if_the_most_relevant_error_is_allOf_it_is_traversedc              
   C   sX   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|d	}| |jd d
S )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    r   r   r   test_nested_context_for_oneOf   s$   z+TestBestMatch.test_nested_context_for_oneOfc                 C   s   ddddddddd	giii}| j dd
i|d}| |jd dddddd	dddgiii}| j dd
i|d}| |jd d S )Nr   r   r$   r(   r   r   ZminItemsr"   
   r   	minLengthr   r   rH   r   r   r   r   Z	reorderedr   r   r   "test_it_prioritizes_matching_types  s,   

z0TestBestMatch.test_it_prioritizes_matching_typesc                 C   s   dddddgdddd	gd
dgiii}| j ddi|d}| |jd dddd	d
ddddgiii}| j ddi|d}| |jd d S )Nr   r   r$   r(   r   r   rE   r/   r"   rF   rG   r   r   rH   r   rI   r   r   r   (test_it_prioritizes_matching_union_types"  s,   

z6TestBestMatch.test_it_prioritizes_matching_union_typesc                 C   s.   dddii}| j ddi|d}| |j d S )Nr   r   Fr   r   )r   assertIsNoner   r    r   r   r   test_boolean_schemas=  s   z"TestBestMatch.test_boolean_schemasc                 C   s4   t ddi}|i  | t|i jd d S )Nr   r   )r   r	   r   r   r
   r   r   r   r   r   r   test_one_errorB  s   
zTestBestMatch.test_one_errorc                 C   s"   t i }| t|i  d S )N)r   rL   r   r
   r	   rN   r   r   r   test_no_errorsJ  s   zTestBestMatch.test_no_errorsN)__name__
__module____qualname__r   r!   r&   r,   r0   r3   r9   r<   r>   r?   r@   rA   rB   rC   rD   rJ   rK   rM   rO   rP   r   r   r   r   r   
   s*    r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestByRelevancec                 C   sb   t jddgd}t jdddgd}t||gt jd}| || t||gt jd}| || d S )NOh no!ZbazpathOh yes!r   r   key)r   ValidationErrormax	relevanceassertIs)r   shallowdeepmatchr   r   r   #test_short_paths_are_better_matchesP  s   z3TestByRelevance.test_short_paths_are_better_matchesc                 C   s~   t jdg d}t jddgd}t||gt jd}| dd |D dgg g t||gt jd}| dd |D dgg g d S )	NrU   rV   rX   r   rY   c                 S      g | ]}t |jqS r   r   rW   .0errorr   r   r   
<listcomp>_      zNTestByRelevance.test_global_errors_are_even_better_matches.<locals>.<listcomp>c                 S   rc   r   rd   re   r   r   r   rh   e  ri   )r   r[   sortedr]   r   )r   r_   r`   r   r   r   r   *test_global_errors_are_even_better_matchesY  s   z:TestByRelevance.test_global_errors_are_even_better_matchesc                 C   sh   t jdg dd}t jdg dd}t jdd}t||g|d}| || t||g|d}| || d S )NrU   arW   r   rX   b)weakrY   r   r[   Zby_relevancer\   r^   )r   ro   normalr
   ra   r   r   r   %test_weak_keywords_are_lower_priorityi  s   z5TestByRelevance.test_weak_keywords_are_lower_priorityc                 C   s~   t jdg dd}t jdg dd}t jdg dd}t jddd}t|||g|d	}| || t|||g|d	}| || d S )
NrU   rl   rm   rX   rn   zOh fine!c)ro   strongrY   rp   )r   ro   rq   rt   r
   ra   r   r   r   (test_strong_keywords_are_higher_priorityu  s   z8TestByRelevance.test_strong_keywords_are_higher_priorityN)rQ   rR   rS   rb   rk   rr   ru   r   r   r   r   rT   O  s
    	rT   c                   @   st   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )TestErrorTreec                 C   s.   dd t dD }t|}| |jd d S )Nc                 S   s   g | ]	}t jd |dqS )Z	Somethingr   )r   r[   )rf   ir   r   r   rh     s    zQTestErrorTree.test_it_knows_how_many_total_errors_it_contains.<locals>.<listcomp>   )ranger   	ErrorTreer   Ztotal_errorsr   r   treer   r   r   /test_it_knows_how_many_total_errors_it_contains  s
   
z=TestErrorTree.test_it_knows_how_many_total_errors_it_containsc                 C   s,   t jddgdg}t |}| d| d S )N	a messager   rV   )r   r[   r{   assertInr|   r   r   r   1test_it_contains_an_item_if_the_item_had_an_error     
z?TestErrorTree.test_it_contains_an_item_if_the_item_had_an_errorc                 C   s,   t jddgdg}t |}| d| d S )Nr   r   rV   r   )r   r[   r{   assertNotInr|   r   r   r   9test_it_does_not_contain_an_item_if_the_item_had_no_error  r   zGTestErrorTree.test_it_does_not_contain_an_item_if_the_item_had_no_errorc                 C   s0   t jddd}t |g}| |jd|i d S )Nr   r   rw   r   r[   r{   r   r   r   rg   r}   r   r   r   /test_keywords_that_failed_appear_in_errors_dict  s   z=TestErrorTree.test_keywords_that_failed_appear_in_errors_dictc                 C   sP   t jddgdt jdddgdg}t |}| d|d  | d|d  d S )Nza bar messager   rV   za bar -> 0 messager      )r   r[   r{   r   r   r|   r   r   r   1test_it_creates_a_child_tree_for_each_nested_path  s   
z?TestErrorTree.test_it_creates_a_child_tree_for_each_nested_pathc                 C   sX   t jddddgdt jddddgd}}t ||g}| |d d j||d d S )	N1r   r   r   )r   rW   2quux)r   r   r   r   e1e2r}   r   r   r   +test_children_have_their_errors_dicts_built  s
    z9TestErrorTree.test_children_have_their_errors_dicts_builtc                 C   s@   t jddddgddt jddd	d
gdd}}t ||g d S Nr   r   r   bar2i1r   rW   r   r   r   foobarr   i2)r   r[   r{   )r   r   r   r   r   r   "test_multiple_errors_with_instance  s   z0TestErrorTree.test_multiple_errors_with_instancec                 C   sT   t jddg d}t |g}| t |d  W d    d S 1 s#w   Y  d S )NZ123r   )r   r   r   )r   r[   r{   ZassertRaises
IndexErrorr   r   r   r   >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_instancec                 C   s6   t jddi dgd}t |g}| |d t j dS )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   rW   N)r   r[   r{   ZassertIsInstancer   r   r   r   9test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error  s
   
zGTestErrorTree.test_if_its_in_the_tree_anyhow_it_does_not_raise_an_errorc                 C   sT   t jddddgddt jddd	d
gdd}}t ||g}| t|dd	h d S r   )r   r[   r{   r   setr   r   r   r   	test_iter  s   zTestErrorTree.test_iterc                 C   s6   t jddddgdd}t |g}| t|d d S )Nr   r   r   r   r   r   z<ErrorTree (1 total error)>r   r[   r{   r   reprr   r   r   r   test_repr_single  s   zTestErrorTree.test_repr_singlec                 C   sP   t jddddgddt jddd	d
gdd}}t ||g}| t|d d S )Nr   r   r   r   r   r   r   r   r   r   r   z<ErrorTree (2 total errors)>r   r   r   r   r   test_repr_multiple  s   z TestErrorTree.test_repr_multiplec                 C   s   t g }| t|d d S )Nz<ErrorTree (0 total errors)>)r   r{   r   r   )r   r}   r   r   r   test_repr_empty  s   
zTestErrorTree.test_repr_emptyN)rQ   rR   rS   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rv     s    		
rv   c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )TestErrorInitReprStrc                 K   s0   t ddddddid}|| tjdi |S )NZhellor   r"      )messager   r+   r   r   r   )dictupdater   r[   )r   kwargsdefaultsr   r   r   
make_error  s   
zTestErrorInitReprStr.make_errorc                 K   sR   t |d}| jdi |}t|d\}}}| ||j | || d S )N
r   )textwrapdedentrstripr   str	partitionr   r   )r   expectedr   rg   Zmessage_line_restr   r   r   assertShows
  s
   z TestErrorInitReprStr.assertShowsc                 C   s   |   }| t|jd d S )Nr   )r   ZassertGreaterlenargs)r   rg   r   r   r   !test_it_calls_super_and_sets_args  s   z6TestErrorInitReprStr.test_it_calls_super_and_sets_argsc                 C   s   |  ttjddd d S )NzHello!r   z<ValidationError: 'Hello!'>)r   r   r   r[   r   r   r   r   	test_repr  s   zTestErrorInitReprStr.test_reprc                 C   sh   t d}| t|d dddddid}|D ]}t|}||= t jdi |}| t|d qd S )Nr   r   r"   r   )r   r+   r   r   r   )r   r[   r   r   r   )r   rg   r   attrkr   r   r   test_unset_error  s   
z%TestErrorInitReprStr.test_unset_errorc                 C   s   | j dg g d d S )Nz
            Failed validating 'type' in schema:
                {'type': 'string'}

            On instance:
                5
            rW   Zschema_pathr   r   r   r   r   test_empty_paths-  s
   
z%TestErrorInitReprStr.test_empty_pathsc                 C   s   | j ddgdgd d S )Nz
            Failed validating 'type' in schema:
                {'type': 'string'}

            On instance[0]:
                5
            r   r6   r   r   r   r   r   r   test_one_item_paths:  s
   
z(TestErrorInitReprStr.test_one_item_pathsc                 C   s   | j dddgg dd d S )Nz
            Failed validating 'type' in schema['items'][0]:
                {'type': 'string'}

            On instance[0]['a']:
                5
            r   rl   )r6   r   r   r   r   r   r   r   r   test_multiple_item_pathsG  s
   
z-TestErrorInitReprStr.test_multiple_item_pathsc                 C   s0   | j dttdtttdtddd d S )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]
                  Z	maxLength)r   r   r   )r   r   rz   r   zipr   r   r   r   test_uses_pprintT  s   
2
z%TestErrorInitReprStr.test_uses_pprintc                 C   s(   | j dddddddddddd d S )	Nz
            Failed validating 'type' in schema:
                {'do': 3, 'not': 7, 'sort': 37, 'me': 73}

            On instance:
                {'here': 73, 'too': 37, 'no': 7, 'sorting': 3}
                  r8   I   )donotsortme)hereZtoonoZsorting)r   r   r   r   r   r   r   test_does_not_reorder_dicts  s   
z0TestErrorInitReprStr.test_does_not_reorder_dictsc                    sD   G  fddd}| }t jdd|ddd} t|t| dS )	z
        Check for #164 which rendered exceptions unusable when a
        `ValidationError` involved instances with an `__eq__` method
        that returned truthy values.
        c                       s$   e Zd Z fddZ fddZdS )zdTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBroc                         d d S NzDon't!Zfailthisotherr   r   r   __eq__     zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__eq__c                    r   r   r   r   r   r   r   __ne__  r   zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__ne__N)rQ   rR   rS   r   r   r   r   r   r   DontEQMeBro  s    r   r   r   Zsomer   )r   r   r+   r   N)r   r[   r   r   r   )r   r   r   rg   r   r   r   :test_str_works_with_instances_having_overriden_eq_operator  s   zOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operatorN)rQ   rR   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    9r   c                   @   s   e Zd Zdd ZdS )TestHashablec                 C   s   t dh t dh d S )N )r   r[   ZSchemaErrorr   r   r   r   test_hashable  s   zTestHashable.test_hashableN)rQ   rR   rS   r   r   r   r   r   r     s    r   c                   @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd ZdS )TestJsonPathRenderingc                 C   sH   t j|gdddd}|j}| || t|jjd }| || d S )Nr   r   r   )rW   r   r   r   r   )r   r[   Z	json_pathr   jsonpath_ngparserightfields)r   Zproperty_nameZexpected_pathrg   Zrendered_json_pathZre_parsed_namer   r   r   validate_json_path_rendering  s   z2TestJsonPathRendering.validate_json_path_renderingc                 C      |  dd d S )Nxz$.xr   r   r   r   r   
test_basic     z TestJsonPathRendering.test_basicc                 C   r   )Nr   z$['']r   r   r   r   r   
test_empty  r   z TestJsonPathRendering.test_emptyc                 C   r   )Nr   z$['1']r   r   r   r   r   test_number  r   z!TestJsonPathRendering.test_numberc                 C   r   )N.z$['.']r   r   r   r   r   test_period  r   z!TestJsonPathRendering.test_periodc                 C   r   )N'z$['\'']r   r   r   r   r   test_single_quote  r   z'TestJsonPathRendering.test_single_quotec                 C   r   )N z$[' ']r   r   r   r   r   
test_space  r   z TestJsonPathRendering.test_spacec                 C   r   )N\z$['\\']r   r   r   r   r   test_backslash  r   z$TestJsonPathRendering.test_backslashc                 C   r   )Nz\'z	$['\\\'']r   r   r   r   r   test_backslash_single_quote  r   z1TestJsonPathRendering.test_backslash_single_quotec                 C   r   )Nr   z$['_']r   r   r   r   r   test_underscore  r   z%TestJsonPathRendering.test_underscorec                 C   r   )N"z$['"']r   r   r   r   r   test_double_quote  r   z'TestJsonPathRendering.test_double_quotec                 C   r   )N-z$['-']r   r   r   r   r   test_hyphen  r   z!TestJsonPathRendering.test_hyphenc                 C   r   )Nza[0]z	$['a[0]']r   r   r   r   r   test_json_path_injection  r   z.TestJsonPathRendering.test_json_path_injectionc                 C   r   )N[z$['[']r   r   r   r   r   test_open_bracket  r   z'TestJsonPathRendering.test_open_bracketN)rQ   rR   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s    r   )Zunittestr   r   r   Z
jsonschemar   Zjsonschema.validatorsr   r   rT   rv   r   r   r   r   r   r   r   <module>   s      G4{ @