a
    �Ï?e·L  ã                   @   s€   d dl m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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!S )"ÚTestBestMatchc                 C   sT   t t|ƒ |¡ƒ}t t|ƒ¡}t t|ƒ¡}|  | ¡ | ¡ d|› d|› �¡ |S )NzNo consistent best match!
Got: z

Then: )	Úlistr   Úiter_errorsr   Ú
best_matchÚiterÚreversedÚassertEqualZ	_contents)ÚselfÚinstanceÚschemaÚerrorsÚbestZreversed_best© r   úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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   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 )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_traversed.   s    þÿÿÿ
zFTestBestMatch.test_if_the_most_relevant_error_is_anyOf_it_is_traversedc                 C   s:   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_errorsE   s    ýÿzATestBestMatch.test_no_anyOf_traversal_for_equally_relevant_errorsc                 C   s.   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_errorU   s    ÿÿzDTestBestMatch.test_anyOf_traversal_for_single_equally_relevant_errorc                 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 )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_traversedc   s    þÿÿÿ
zFTestBestMatch.test_if_the_most_relevant_error_is_oneOf_it_is_traversedc                 C   s:   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 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_errorsz   s    ýÿzATestBestMatch.test_no_oneOf_traversal_for_equally_relevant_errorsc                 C   s.   dddigi}| j g |d�}|  |jd¡ dS )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Š   s    ÿÿzDTestBestMatch.test_oneOf_traversal_for_single_equally_relevant_errorc                 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   r5   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   r2   r*   r    r3   r4   r   r   r5   r   r6   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   s6   t ddiƒ}| i ¡\}|  t | i ¡¡jd¡ d S )Nr   r   )r   r   r   r   r   r   )r   r   Úerrorr   r   r   Útest_one_error  s    þzTestBestMatch.test_one_errorc                 C   s"   t i ƒ}|  t | i ¡¡¡ d S )N)r   r9   r   r   r   )r   r   r   r   r   Útest_no_errors
  s    zTestBestMatch.test_no_errorsN)Ú__name__Ú
__module__Ú__qualname__r   r   r$   r)   r+   r,   r-   r.   r/   r0   r1   r7   r8   r:   r<   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	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_matches  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 )	NrB   rC   rE   r   rF   c                 S   s   g | ]}t |jƒ‘qS r   ©r   rD   ©Ú.0r;   r   r   r   Ú
<listcomp>  ó    zNTestByRelevance.test_global_errors_are_even_better_matches.<locals>.<listcomp>c                 S   s   g | ]}t |jƒ‘qS r   rP   rQ   r   r   r   rS   %  rT   )r   rH   ÚsortedrJ   r   )r   rL   rM   r   r   r   r   Ú*test_global_errors_are_even_better_matches  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 )NrB   Úa©rD   r   rE   Úb)ÚweakrF   ©r   rH   Zby_relevancerI   rK   )r   rZ   Únormalr   rN   r   r   r   Ú%test_weak_keywords_are_lower_priority)  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 )
NrB   rW   rX   rE   rY   zOh fine!Úc)rZ   ÚstrongrF   r[   )r   rZ   r\   r_   r   rN   r   r   r   Ú(test_strong_keywords_are_higher_priority5  s    z8TestByRelevance.test_strong_keywords_are_higher_priorityN)r>   r?   r@   rO   rV   r]   r`   r   r   r   r   rA     s   	rA   c                   @   sl   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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   rH   )rR   Úir   r   r   rS   F  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_containsD  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   rC   )r   rH   rf   ÚassertInrg   r   r   r   Ú1test_it_contains_an_item_if_the_item_had_an_errorM  s    
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 )Nrj   r   rC   r   )r   rH   rf   ÚassertNotInrg   r   r   r   Ú9test_it_does_not_contain_an_item_if_the_item_had_no_errorR  s    
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 )Nrj   r   rb   ©r   rH   rf   r   r   ©r   r;   rh   r   r   r   Ú/test_keywords_that_failed_appear_in_errors_dictW  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   rC   za bar -> 0 messager   é   )r   rH   rf   rk   rm   rg   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   rD   Ú2Úquux)r   rv   ro   ©r   Úe1Úe2rh   r   r   r   Ú+test_children_have_their_errors_dicts_builte  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 )Nrt   r   r   Úbar2Úi1©r   rD   r   ru   rv   Úfoobarr   Úi2)r   rH   rf   )r   rx   ry   r   r   r   Ú"test_multiple_errors_with_instancem  s    üüúz0TestErrorTree.test_multiple_errors_with_instancec                 C   sR   t jddg d�}t  |g¡}|  t¡� |d  W d   ƒ n1 sD0    Y  d S )NZ123r   )r   r   r   )r   rH   rf   ÚassertRaisesÚ
IndexErrorrp   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.
        rj   r   )r   r   rD   N)r   rH   rf   ZassertIsInstancerp   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   s6   t jddddgdd�}t  |g¡}|  t|ƒd¡ d S )Nrt   r   r   r{   r|   r}   z<ErrorTree (1 total error)>©r   rH   rf   r   Úreprrp   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 )Nrt   r   r   r{   r|   r}   ru   rv   r~   r   r   z<ErrorTree (2 total errors)>r…   rw   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   rf   r   r†   )r   rh   r   r   r   Útest_repr_empty©  s    
zTestErrorTree.test_repr_emptyN)r>   r?   r@   ri   rl   rn   rq   rs   rz   r€   rƒ   r„   r‡   rˆ   r‰   r   r   r   r   ra   C  s   		
ra   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S )ÚTestErrorInitReprStrc                 K   s0   t ddddddid�}| |¡ tjf i |¤ŽS )NZhellor   r    é   )Úmessager   r(   r   r   )ÚdictÚupdater   rH   )r   ÚkwargsÚdefaultsr   r   r   Ú
make_error¯  s    û
zTestErrorInitReprStr.make_errorc                 K   sR   t  |¡ d¡}| jf i |¤Ž}t|ƒ d¡\}}}|  ||j¡ |  ||¡ d S )NÚ
)ÚtextwrapÚdedentÚrstripr‘   ÚstrÚ	partitionr   rŒ   )r   Úexpectedr�   r;   Zmessage_lineÚ_Úrestr   r   r   ÚassertShowsº  s
    z TestErrorInitReprStr.assertShowsc                 C   s   |   ¡ }|  t|jƒd¡ d S )Nrr   )r‘   ZassertGreaterÚlenÚargs)r   r;   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   rH   ©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 ]2}t|ƒ}||= t jdi |¤Ž}|  t|ƒd¡ q0d S )NrŒ   r   r    r‹   )r   r(   r   r   )rŒ   )r   rH   r   r–   r�   )r   r;   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
            ©rD   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   Úitemsr¤   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   rW   )r§   r   rr   r¤   r¥   rŸ   r   r   r   Útest_multiple_item_paths÷  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   re   r�   ÚziprŸ   r   r   r   Útest_uses_pprint  s    2
Ëz%TestErrorInitReprStr.test_uses_pprintc                    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                    s   ˆ   d¡ d S ©NzDon't!©Zfail©ÚthisÚotherrŸ   r   r   Ú__eq__E  s    zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__eq__c                    s   ˆ   d¡ d S r®   r¯   r°   rŸ   r   r   Ú__ne__H  s    zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__ne__N)r>   r?   r@   r³   r´   r   rŸ   r   r   ÚDontEQMeBroD  s   rµ   rj   r   Zsomer   )r   r   r(   r   N)r   rH   rk   r†   r–   )r   rµ   r   r;   r   rŸ   r   Ú:test_str_works_with_instances_having_overriden_eq_operator=  s    ûzOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operatorN)r>   r?   r@   r‘   r›   rž   r    r£   r¦   r¨   r©   r­   r¶   r   r   r   r   rŠ   ®  s   9rŠ   c                   @   s   e Zd Zdd„ ZdS )ÚTestHashablec                 C   s$   t t d¡gƒ t t d¡gƒ d S )NÚ )Úsetr   rH   ZSchemaErrorrŸ   r   r   r   Útest_hashableW  s    zTestHashable.test_hashableN)r>   r?   r@   rº   r   r   r   r   r·   V  s   r·   )Zunittestr   r“   Z
jsonschemar   Zjsonschema.validatorsr   r   rA   ra   rŠ   r·   r   r   r   r   Ú<module>   s     	4k )