B
    ÔùÓ`ô;  ã               @   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               @   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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ÚassertEqualZ	_contentsÚformat)ÚselfÚerrorsÚbestZreversed_bestr   © r   úD/tmp/pip-unpacked-wheel-qkyy6ibz/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   N)r   r	   r   r   r   )r   r   r   r   r   r   Ú%test_oneOf_and_anyOf_are_weak_matches"   s    z3TestBestMatch.test_oneOf_and_anyOf_are_weak_matchesc          	   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 )
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_value)r   r   r   r   r   r   Ú8test_if_the_most_relevant_error_is_anyOf_it_is_traversed2   s    
zFTestBestMatch.test_if_the_most_relevant_error_is_anyOf_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 )
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"   N)r   r	   r   r   r#   )r   r   r   r   r   r   Ú8test_if_the_most_relevant_error_is_oneOf_it_is_traversedK   s    
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   ZallOfr   r   r   r!   r"   N)r   r	   r   r   r#   )r   r   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#   )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   Z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&   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 )NzOh no!Zbaz)Úpathz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 )	NzOh no!)r/   zOh yes!r   )r0   c             S   s   g | ]}t |jƒ‘qS r   )r   r/   )Ú.0r(   r   r   r   ú
<listcomp>¯   s    zNTestByRelevance.test_global_errors_are_even_better_matches.<locals>.<listcomp>c             S   s   g | ]}t |jƒ‘qS r   )r   r/   )r9   r(   r   r   r   r:   µ   s    )r   r1   Úsortedr3   r   )r   r5   r6   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 )NzOh no!Úa)r/   r   zOh yes!Úb)Úweak)r0   )r   r1   Úby_relevancer2   r4   )r   r?   Únormalr	   r7   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 )
NzOh no!r=   )r/   r   zOh yes!r>   zOh fine!Úc)r?   Ústrong)r0   )r   r1   r@   r2   r4   )r   r?   rA   rD   r	   r7   r   r   r   Ú*test_strong_validators_are_higher_priorityÅ   s    z:TestByRelevance.test_strong_validators_are_higher_priorityN)r+   r,   r-   r8   r<   rB   rE   r   r   r   r   r.   Ÿ   s   	r.   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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   r1   )r9   Úir   r   r   r:   ×   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 )Nz	a messager   )r/   )r   r1   rJ   ÚassertIn)r   r   rK   r   r   r   Ú1test_it_contains_an_item_if_the_item_had_an_errorÝ   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 )Nz	a messager   )r/   r   )r   r1   rJ   ÚassertNotIn)r   r   rK   r   r   r   Ú9test_it_does_not_contain_an_item_if_the_item_had_no_errorâ   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 )Nz	a messager   )r   )r   r1   rJ   r   r   )r   r(   rK   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   r1   rJ   rM   rO   )r   r   rK   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   rV   )r   r1   rJ   r   r   )r   Úe1Úe2rK   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 )NrT   r   r   Zbar2Úi1)r   r/   ÚinstancerU   rV   Zfoobarr   Úi2)r   r1   rJ   )r   rW   rX   r   r   r   Ú"test_multiple_errors_with_instanceý   s    z0TestErrorTree.test_multiple_errors_with_instancec          	   C   s>   t jddg d�}t  |g¡}|  t¡� |d  W d Q R X d S )NZ123r   )r   r[   r   )r   r1   rJ   ÚassertRaisesÚ
IndexError)r   r(   rK   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.
        z	a messager   )r   r[   r/   N)r   r1   rJ   ZassertIsInstance)r   r(   rK   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)r+   r,   r-   rL   rN   rP   rQ   rS   rY   r]   r`   ra   r   r   r   r   rF   Ó   s   		rF   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   s,   t ddddddid�}| |¡ tjf |ŽS )NZhellor   r   é   )Úmessager   r#   r[   Úschema)ÚdictÚupdater   r1   )r   ÚkwargsÚdefaultsr   r   r   Ú
make_error"  s    
zTestErrorInitReprStr.make_errorc             K   s^   t r| dd¡}t |¡ d¡}| jf |Ž}t|ƒ d¡\}}}|  ||j	¡ |  ||¡ d S )Nzu'ú'Ú
)
r   ÚreplaceÚtextwrapÚdedentÚrstriprj   ÚstrÚ	partitionr   rd   )r   Úexpectedrh   r(   Zmessage_lineÚ_Úrestr   r   r   ÚassertShows-  s    z TestErrorInitReprStr.assertShowsc             C   s   |   ¡ }|  t|jƒd¡ d S )NrR   )rj   ZassertGreaterÚlenÚargs)r   r(   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!)rd   z<ValidationError: %r>)r   Úreprr   r1   )r   r   r   r   Ú	test_repr;  s    zTestErrorInitReprStr.test_reprc             C   sh   t  d¡}|  t|ƒd¡ dddddidœ}x6|D ].}t|ƒ}||= t jd|Ž}|  t|ƒd¡ q2W d S )Nrd   r   r   rc   )r   r#   r[   re   )rd   )r   r1   r   rq   rf   )r   r(   rh   Ú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)rv   )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   Úitems)r/   r   )rv   )r   r   r   r   Útest_one_item_paths_  s    z(TestErrorInitReprStr.test_one_item_pathsc             C   s   | j dddgdddgd� d S )Nz¥
            Failed validating u'type' in schema[u'items'][0]:
                {u'type': u'string'}

            On instance[0][u'a']:
                5
            r   r=   r�   rR   )r/   r   )rv   )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]
            é   é   Z	maxLength)r[   re   r   )rv   r   rI   rf   Úzip)r   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                s   ˆ   d¡ d S )NzDon't!)Úfail)ÚthisÚother)r   r   r   Ú__eq__º  s    zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__eq__c                s   ˆ   d¡ d S )NzDon't!)rˆ   )r‰   rŠ   )r   r   r   Ú__ne__½  s    zkTestErrorInitReprStr.test_str_works_with_instances_having_overriden_eq_operator.<locals>.DontEQMeBro.__ne__N)r+   r,   r-   r‹   rŒ   r   )r   r   r   ÚDontEQMeBro¹  s   r�   z	a messager   Zsomere   )r   r[   r#   re   N)r   r   r1   rM   rz   rq   )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-   rj   rv   ry   r{   r~   r€   r‚   rƒ   r‡   rŽ   r   r   r   r   rb   !  s   
9rb   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   r1   ZSchemaError)r   r   r   r   Útest_hashableÌ  s    zTestHashable.test_hashableN)r+   r,   r-   r’   r   r   r   r   r�   Ë  s   r�   )Zunittestr   rn   Z
jsonschemar   r   Zjsonschema.compatr   r   r.   rF   rb   r�   r   r   r   r   Ú<module>   s    4N +