
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
+;X	6                 @   s   d dl Z d dlmZmZ d dlmZ d dlmZmZ G dd dej	Z
G dd dej	ZG d	d
 d
ej	ZG dd dej	ZdS )    N)Draft4Validator
exceptions)PY3)mockunittestc               @   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   s@   t |}tj|}tjt|}d}| j|||j||d |S )Nz:Didn't return a consistent best match!
Got: {0}

Then: {1})msg)listr   
best_matchreversedassertEqualformat)selferrorsbestZreversed_bestr    r   %/usr/lib/python3.6/test_exceptions.pyr
   	   s    
zTestBestMatch.best_matchc             C   sH   t ddddddiidii}| j|jddg ii}| j|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}| j|ji }| j|jd dS )zo
        A property you *must* match is probably better than one you have to
        match a part of.

        r   r   stringZ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}| j|jdddii}| j|jd d	S )
aM  
        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}| j|jdddii}| j|jd d	S )
aM  
        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_traversedL   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}| j|jdddii}| j|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_traversedf   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}| j|jdddii}| j|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_oneOf|   s    
z+TestBestMatch.test_nested_context_for_oneOfc             C   s6   t ddi}|ji \}| jtj|j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 }| jtj|j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}| j|| t||gt jd}| j|| 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}| jdd |D dgg g t||gt jd}| j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}| j|| t||g|d}| j|| 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	}| j|| t|||g|d	}| j|| 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|}| j|jd d S )Nc             S   s   g | ]}t j qS r   )r   	MagicMock)r9   _r   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 j|}| jd| d S )Nz	a messager   )r/   )r   r1   rK   assertIn)r   r   rL   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 j|}| jd| d S )Nz	a messager   )r/   r   )r   r1   rK   assertNotIn)r   r   rL   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 j|g}| j|jd|i d S )Nz	a messager   )r   )r   r1   rK   r   r   )r   r(   rL   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 j|}| jd|d  | jd|d  d S )Nza bar messager   )r/   za bar -> 0 messager      )r   r1   rK   rN   rP   )r   r   rL   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 j||g}| j|d d j||d d S )	N1r   r   r   )r   r/   2quux)r   rW   )r   r1   rK   r   r   )r   e1e2rL   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 j||g d S )NrU   r   r   Zbar2Zi1)r   r/   instancerV   rW   Zfoobarr   Zi2)r   r1   rK   )r   rX   rY   r   r   r   -test_regression_multiple_errors_with_instance   s    z;TestErrorTree.test_regression_multiple_errors_with_instancec          	   C   s>   t jddg d}t j|g}| jt |d  W d Q R X d S )NZ123r   )r   r[   r   )r   r1   rK   ZassertRaises
IndexError)r   r(   rL   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 j|g}| j|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   rK   ZassertIsInstance)r   r(   rL   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-   rM   rO   rQ   rR   rT   rZ   r\   r^   r_   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}|j| tjf |S )NZhellor   r      )messager   r#   r[   schema)dictupdater   r1   )r   kwargsZdefaultsr   r   r   
make_error#  s    
zTestErrorInitReprStr.make_errorc             K   s^   t r|jdd}tj|jd}| jf |}t|jd\}}}| j||j	 | j|| d S )Nzu''
)
r   replacetextwrapdedentrstriprg   str	partitionr   rb   )r   Zexpectedrf   r(   Zmessage_linerH   restr   r   r   assertShows.  s    z TestErrorInitReprStr.assertShowsc             C   s   | j  }| jt|jd d S )NrS   )rg   ZassertGreaterlenargs)r   r(   r   r   r   !test_it_calls_super_and_sets_args8  s    z6TestErrorInitReprStr.test_it_calls_super_and_sets_argsc             C   s    | j ttjdddd  d S )NzHello!)rb   z<ValidationError: %r>)r   reprr   r1   )r   r   r   r   	test_repr<  s    zTestErrorInitReprStr.test_reprc             C   sh   t jd}| jt|d dddddid}x6|D ].}t|}||= t jd|}| jt|d q2W d S )Nrb   r   r   ra   )r   r#   r[   rc   )rb   )r   r1   r   rn   rd   )r   r(   rf   attrkr   r   r   test_unset_errorB  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)rq   )r   r   r   r   test_empty_pathsS  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/   rz   )rq   )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|   rS   )r/   rz   )rq   )r   r   r   r   test_multiple_item_pathsm  s    z-TestErrorInitReprStr.test_multiple_item_pathsc          
   C   s4   t jd }t| j  | j|jd W d Q R X d S )Nzpprint.pformatr   )r   Zpatchrn   rg   r   Z
call_count)r   Zpformatr   r   r   test_uses_pprintz  s    z%TestErrorInitReprStr.test_uses_pprintc             C   s6   t j }tjdd|ddd}t| | j|jj 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.

        z	a messager   Zsomerc   )r   r[   r#   rc   N)r   rG   r   r1   rn   ZassertFalse__eq__Zcalled)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-   rg   rq   rt   rv   ry   r{   r}   r~   r   r   r   r   r   r   r`   "  s   
r`   )rk   Z
jsonschemar   r   Zjsonschema.compatr   Zjsonschema.tests.compatr   r   ZTestCaser   r.   rF   r`   r   r   r   r   <module>   s    4L