o
    >�Ò]ô;  ã                   @   s„   d dl mZ d dlZd dlmZ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)ÚDraft4ValidatorÚ
exceptions)ÚPY3c                   @   óT   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S )ÚTestBestMatchc                 C   sH   t |ƒ}t |¡}t t|ƒ¡}d}| j| ¡ | ¡ | ||¡d� |S )Nz:Didn't return a consistent best match!
Got: {0}

Then: {1})Úmsg)Úlistr   Ú
best_matchÚreversedÚassertEqualÚ	_contentsÚformat)ÚselfÚerrorsÚbestÚreversed_bestr   © r   úB/usr/lib/python3/dist-packages/jsonschema/tests/test_exceptions.pyr
   	   s   

þzTestBestMatch.best_matchc                 C   sH   t ddddddiidœiiƒ}|  | ddg ii¡¡}|  |jd¡ d S )	NÚ
propertiesÚfooé   ÚbarÚtypeÚobject)ÚminPropertiesr   r   ©r   r
   Úiter_errorsr   Ú	validator©r   r   r   r   r   r   Ú(test_shallower_errors_are_better_matches   s   
þÿÿÿ
z6TestBestMatch.test_shallower_errors_are_better_matchesc                 C   sJ   t dddiddigddiddigdœƒ}|  | i ¡¡}|  |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   Nr   r   r   r   r   Ú%test_oneOf_and_anyOf_are_weak_matches"   s   ýÿz3TestBestMatch.test_oneOf_and_anyOf_are_weak_matchesc              	   C   óR   t dddddiddddiiigiiiƒ}|  | dddii¡¡}|  |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é   N©r   r
   r   r   Úvalidator_valuer   r   r   r   Ú8test_if_the_most_relevant_error_is_anyOf_it_is_traversed2   ó   
þÿÿÿÿzFTestBestMatch.test_if_the_most_relevant_error_is_anyOf_it_is_traversedc              	   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(   Nr)   r   r   r   r   Ú8test_if_the_most_relevant_error_is_oneOf_it_is_traversedK   r,   zFTestBestMatch.test_if_the_most_relevant_error_is_oneOf_it_is_traversedc              	   C   sR   t dddddiddddiiigiiiƒ}|  | dddii¡¡}|  |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   ÚallOfr   r!   r   r'   r(   Nr)   r   r   r   r   Ú8test_if_the_most_relevant_error_is_allOf_it_is_traversedd   s   þÿÿÿÿzFTestBestMatch.test_if_the_most_relevant_error_is_allOf_it_is_traversedc                 C   s^   t dddddidddiddddiiigigiiiƒ}|  | dddii¡¡}|  |jd¡ d S )	Nr   r   r$   r   r!   r   r'   r(   r)   r   r   r   r   Útest_nested_context_for_oneOfy   s(   ÿÿþÿþÿÿÿÿz+TestBestMatch.test_nested_context_for_oneOfc                 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   ÚassertIsNoner   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/   r0   r2   r4   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!Ú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 )	Nr9   r;   r=   r   r>   c                 S   ó   g | ]}t |jƒ‘qS r   ©r	   r<   ©Ú.0r1   r   r   r   Ú
<listcomp>¯   ó    zNTestByRelevance.test_global_errors_are_even_better_matches.<locals>.<listcomp>c                 S   rH   r   rI   rJ   r   r   r   rL   µ   rM   )r   r@   ÚsortedrB   r   )r   rD   rE   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 )Nr9   Úa©r<   r   r=   Úb)Úweakr>   ©r   r@   Úby_relevancerA   rC   )r   rS   Únormalr
   rF   r   r   r   Ú'test_weak_validators_are_lower_priority¹   s   z7TestByRelevance.test_weak_validators_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 )
Nr9   rP   rQ   r=   rR   zOh fine!Úc)rS   Ústrongr>   rT   )r   rS   rV   rY   r
   rF   r   r   r   Ú*test_strong_validators_are_higher_priorityÅ   s   z:TestByRelevance.test_strong_validators_are_higher_priorityN)r5   r6   r7   rG   rO   rW   rZ   r   r   r   r   r8   Ÿ   s
    	r8   c                   @   r   )ÚTestErrorTreec                 C   s.   dd„ t dƒD ƒ}t |¡}|  |jd¡ d S )Nc                 S   s   g | ]	}t jd |d�‘qS )Ú	Something©r   )r   r@   )rK   Úir   r   r   rL   Ö   s    ÿÿzQTestErrorTree.test_it_knows_how_many_total_errors_it_contains.<locals>.<listcomp>é   )Úranger   Ú	ErrorTreer   Ú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   r;   )r   r@   ra   ÚassertInrc   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 )Nrf   r   r;   r   )r   r@   ra   ÚassertNotInrc   r   r   r   Ú9test_it_does_not_contain_an_item_if_the_item_had_no_errorâ   ri   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 )Nrf   r   r]   ©r   r@   ra   r   r   ©r   r1   rd   r   r   r   Ú1test_validators_that_failed_appear_in_errors_dictç   s   z?TestErrorTree.test_validators_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   r;   za bar -> 0 messager   é   )r   r@   ra   rg   rj   rc   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   r<   Ú2Úquux)r   rs   rl   )r   Úe1Úe2rd   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 )Nrq   r   r   Úbar2Úi1)r   r<   Úinstancerr   rs   Úfoobarr   Úi2)r   r@   ra   )r   rt   ru   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 )NÚ123r   )r   ry   r   )r   r@   ra   ÚassertRaisesÚ
IndexErrorrm   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 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.
        rf   r   )r   ry   r<   N)r   r@   ra   ÚassertIsInstancerm   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_errorN)r5   r6   r7   re   rh   rk   rn   rp   rv   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	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 )NÚhellor   r!   é   )Úmessager   r*   ry   Úschemar   )ÚdictÚupdater   r@   )r   ÚkwargsÚdefaultsr   r   r   Ú
make_error"  s   û
zTestErrorInitReprStr.make_errorc                 K   sb   t r| dd¡}t |¡ d¡}| jdi |¤Ž}t|ƒ d¡\}}}|  ||j	¡ |  ||¡ d S )Nzu'ú'Ú
r   )
r   ÚreplaceÚtextwrapÚdedentÚrstriprŒ   ÚstrÚ	partitionr   r†   )r   ÚexpectedrŠ   r1   Úmessage_lineÚ_Úrestr   r   r   ÚassertShows-  s   z TestErrorInitReprStr.assertShowsc                 C   s   |   ¡ }|  t|jƒd¡ d S )Nro   )rŒ   ÚassertGreaterÚlenÚargs)r   r1   r   r   r   Ú!test_it_calls_super_and_sets_args7  s   z6TestErrorInitReprStr.test_it_calls_super_and_sets_argsc                 C   s    |   ttjdd�ƒdd ¡ d S )NzHello!©r†   z<ValidationError: %r>)r   Úreprr   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*   ry   r‡   rž   )r   r@   r   r“   rˆ   )r   r1   rŠ   ÚattrÚkr   r   r   Útest_unset_errorA  s   
üüz%TestErrorInitReprStr.test_unset_errorc                 C   s   | j dg g d� d S )Nz�
            Failed validating u'type' in schema:
                {u'type': u'string'}

            On instance:
                5
            ©r<   Úschema_path©r™   r    r   r   r   Útest_empty_pathsR  s
   
÷z%TestErrorInitReprStr.test_empty_pathsc                 C   s   | j ddgdgd� d S )Nz’
            Failed validating u'type' in schema:
                {u'type': u'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 u'type' in schema[u'items'][0]:
                {u'type': u'string'}

            On instance[0][u'a']:
                5
            r   rP   )r©   r   ro   r¥   r§   r    r   r   r   Útest_multiple_item_pathsl  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 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]
            é   é   Ú	maxLength)ry   r‡   r   )r™   r	   r`   rˆ   Úzipr    r   r   r   Útest_uses_pprinty  s   
2
Ëz%TestErrorInitReprStr.test_uses_pprintc                    sF   G ‡ fdd„dt ƒ}|ƒ }tjdd|ddd�}ˆ  t|ƒt|ƒ¡ dS )	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.
        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!©Ú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)r5   r6   r7   r¸   rº   r   r    r   r   ÚDontEQMeBro¹  s    r»   rf   r   Úsomer‡   )r   ry   r*   r‡   N)r   r   r@   rg   rŸ   r“   )r   r»   ry   r1   r   r    r   Ú:test_str_works_with_instances_having_overriden_eq_operator²  s   ûzOTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operatorN)r5   r6   r7   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   r@   ÚSchemaErrorr    r   r   r   Útest_hashableÌ  s   zTestHashable.test_hashableN)r5   r6   r7   rÂ   r   r   r   r   r¾   Ë  s    r¾   )Úunittestr   r�   Ú
jsonschemar   r   Újsonschema.compatr   r   r8   r[   rƒ   r¾   r   r   r   r   Ú<module>   s     4N +