o
    Æ²…a?  ã                   @   st   d dl m Z  d dlmZ d dlmZmZmZmZ d dlm	Z	m
Z
 G dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZdS )é    )Údatetime)ÚResourcedTestCase)ÚFakeLaunchpadÚFakeResourceÚFakeRootÚIntegrityError)ÚFakeLaunchpadResourceÚget_applicationc                   @   s   e Zd Zdd„ ZdS )ÚFakeRootTestc                 C   s   t tƒ ƒ}|  t|tƒ¡ d S ©N)r   r	   Ú
assertTrueÚ
isinstancer   )ÚselfÚroot_resource© r   úK/usr/lib/python3/dist-packages/launchpadlib/testing/tests/test_launchpad.pyÚtest_create_root_resource$   s   
z&FakeRootTest.test_create_root_resourceN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r
   #   s    r
   c                   @   s0   e Zd Zdeƒ fgZdd„ Zdd„ Zdd„ ZdS )	ÚFakeResourceTestÚ	launchpadc                    sR   t dd�‰ t ‡ fdd„d�| j_| jj g ¡‰ ttˆ ƒƒ}|  d| tˆ ƒ¡ dS )z6A custom C{__repr__} is provided for L{FakeResource}s.útest-branch©Ú
total_sizec                    ó   ˆ S r   r   ©Ústatuses©Úbranchesr   r   Ú<lambda>0   ó    z,FakeResourceTest.test_repr.<locals>.<lambda>©ÚgetBranchesz0<FakeResource branch-page-resource object at %s>N)Údictr   Úmer#   ÚhexÚidÚassertEqualÚrepr)r   Úobj_idr   r   r   Ú	test_repr-   s   
þzFakeResourceTest.test_reprc                 C   s6   t dd�| j_| jj}|  dtt|ƒƒ t|ƒ¡ dS )z‰
        If the fake has a C{name} property it's included in the repr string to
        make it easier to figure out what it is.
        Úfoo©Únamez<FakeEntry person foo at %s>N)r$   r   r%   r(   r&   r'   r)   ©r   Úpersonr   r   r   Útest_repr_with_name8   s
   ÿz$FakeResourceTest.test_repr_with_namec                 C   sJ   t ddd�}t |gd�| j_t| jjƒ\}|  dtt|ƒƒ t|ƒ¡ dS )zˆ
        If the fake has an C{id} property it's included in the repr string to
        make it easier to figure out what it is.
        Ú1úBug #1©r'   Útitle©Úentriesz<FakeResource bug 1 at %s>N)r$   r   ÚbugsÚlistr(   r&   r'   r)   ©r   Úbugr   r   r   Útest_repr_with_idC   s   ÿz"FakeResourceTest.test_repr_with_idN)r   r   r   r   Ú	resourcesr+   r1   r<   r   r   r   r   r   )   s
    r   c                   @   s8  e Zd Zdeƒ fg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*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Z d:d;„ Z!d<d=„ Z"d>d?„ Z#d@dA„ Z$dBdC„ Z%dDdE„ Z&dFdG„ Z'dHdI„ Z(dJS )KÚFakeLaunchpadTestr   c                 C   s0   t ƒ }t|ƒ}|  ||j¡ |  tƒ |j¡ dS )zy
        The builtin WADL definition is used if the C{application} is not
        provided during instantiation.
        N)Úobjectr   r(   Úcredentialsr	   Ú_application©r   r@   r   r   r   r   Ú'test_wb_instantiate_without_applicationT   s   z9FakeLaunchpadTest.test_wb_instantiate_without_applicationc                 C   s.   t ƒ }t|ddddtƒ d�}|  ||j¡ dS )zì
        L{FakeLaunchpad} takes the same parameters as L{Launchpad} during
        instantiation, with the addition of an C{application} parameter.  The
        optional parameters are discarded when the object is instantiated.
        N)Úservice_rootÚcacheÚtimeoutÚ
proxy_infoÚapplication©r?   r   r	   r(   r@   rB   r   r   r   Ú test_instantiate_with_everything^   s   úz2FakeLaunchpadTest.test_instantiate_with_everythingc                 C   s&   t ƒ }t|tƒ d�}|  ||j¡ dS )z8A L{FakeLaunchpad} can be instantiated with credentials.)rH   NrI   rB   r   r   r   Ú!test_instantiate_with_credentialso   s   z3FakeLaunchpadTest.test_instantiate_with_credentialsc                 C   s   |   | jj¡ dS )z~
        A L{FakeLaunchpad} instantiated without credentials has its
        C{credentials} attribute set to C{None}.
        N)ÚassertIsNoner   r@   ©r   r   r   r   Ú$test_instantiate_without_credentialsu   s   z6FakeLaunchpadTest.test_instantiate_without_credentialsc                 C   s   |   tt| jdd¡ dS )z™
        An L{IntegrityError} is raised if an attribute is set on a
        L{FakeLaunchpad} instance that isn't present in the WADL definition.
        r,   ÚbarN)ÚassertRaisesr   Úsetattrr   rM   r   r   r   Útest_set_undefined_property|   s   ÿz-FakeLaunchpadTest.test_set_undefined_propertyc                 C   s&   t dd�| j_|  tt| jjd¡ dS )z‡
        An L{AttributeError} is raised if an attribute is accessed on a
        L{FakeLaunchpad} instance that doesn't exist.
        ÚFoo)Údisplay_namer.   N)r$   r   r%   rP   ÚAttributeErrorÚgetattrrM   r   r   r   Útest_get_undefined_resource…   s   z-FakeLaunchpadTest.test_get_undefined_resourcec                 C   ó$   t dd�| j_|  d| jjj¡ dS )zÀ
        Sample data can be created by setting L{FakeLaunchpad} attributes with
        dicts that represent objects.  Plain string values can be represented
        as C{str} values.
        r,   r-   N©r$   r   r%   r(   r.   rM   r   r   r   Útest_string_property�   ó   z&FakeLaunchpadTest.test_string_propertyc                 C   rX   )zÅ
        Sample data can be created by setting L{FakeLaunchpad} attributes with
        dicts that represent objects.  Plain string values can be represented
        as C{unicode} strings.
        r,   r-   NrY   rM   r   r   r   Útest_unicode_property–   r[   z'FakeLaunchpadTest.test_unicode_propertyc                 C   s,   t  ¡ }t|d�| j_|  || jjj¡ dS )zU
        Attributes that represent dates are set with C{datetime} instances.
        ©Údate_createdN)r   Úutcnowr$   r   r%   r(   r^   )r   Únowr   r   r   Útest_datetime_propertyŸ   s   z(FakeLaunchpadTest.test_datetime_propertyc              	   C   ó   |   tt| jdtdd�¡ dS )z
        Only C{datetime} values can be set on L{FakeLaunchpad} instances for
        attributes that represent dates.
        r%   r`   r]   N©rP   r   rQ   r   r$   rM   r   r   r   Útest_invalid_datetime_property§   s   ûz0FakeLaunchpadTest.test_invalid_datetime_propertyc                 C   s8   t ddd�| j_|  d| jjj¡ |  d| jjj¡ dS )z~
        Sample data can be created by setting L{FakeLaunchpad} attributes with
        dicts that represent objects.
        r,   rS   )r.   rT   N)r$   r   r%   r(   r.   rT   rM   r   r   r   Útest_multiple_string_properties´   s   z1FakeLaunchpadTest.test_multiple_string_propertiesc              	   C   rb   )zå
        Sample data set on a L{FakeLaunchpad} instance is validated against
        the WADL definition.  If a key is defined on a resource that doesn't
        match a related parameter, an L{IntegrityError} is raised.
        r%   rO   ©r,   Nrc   rM   r   r   r   Útest_invalid_property_name½   s   ÿz,FakeLaunchpadTest.test_invalid_property_namec              	   C   rb   )z—
        The types of sample data values set on L{FakeLaunchpad} instances are
        validated against types defined in the WADL definition.
        r%   éf   r-   Nrc   rM   r   r   r   Útest_invalid_property_valueÇ   s   ÿz-FakeLaunchpadTest.test_invalid_property_valuec                    s:   t dd�‰ t ‡ fdd„d�| j_|  d| jj g ¡¡ dS )z˜
        A callable set on a L{FakeLaunchpad} instance is validated against the
        WADL definition, to make sure a matching method exists.
        r   r   c                    r   r   r   r   r   r   r   r    Ö   r!   z1FakeLaunchpadTest.test_callable.<locals>.<lambda>r"   N)r$   r   r%   ÚassertNotEqualr#   rM   r   r   r   Útest_callableÐ   s   
zFakeLaunchpadTest.test_callablec              	   C   s"   |   tt| jdtdd„ d�¡ dS )z™
        An L{IntegrityError} is raised if a method is defined on a resource
        that doesn't match a method defined in the WADL definition.
        r%   c                   S   s   d S r   r   r   r   r   r   r    ã   r!   z>FakeLaunchpadTest.test_invalid_callable_name.<locals>.<lambda>rf   Nrc   rM   r   r   r   Útest_invalid_callable_nameÙ   s   ûz,FakeLaunchpadTest.test_invalid_callable_namec                    sP   t dd�‰ t ‡ fdd„d�| j_| jj g ¡‰ |  tˆ tƒ¡ |  dˆ j¡ dS )z“
        The result of a fake method is a L{FakeResource}, automatically
        created from the object used to define the return object.
        Ú8r   c                    r   r   r   r   r   r   r   r    ì   r!   zDFakeLaunchpadTest.test_callable_object_return_type.<locals>.<lambda>r"   N)	r$   r   r%   r#   r   r   r   r(   r   rM   r   r   r   Ú test_callable_object_return_typeæ   s
   
z2FakeLaunchpadTest.test_callable_object_return_typec                    s8   t dd�‰ t ‡ fdd„d�| j_|  t| jjjg ¡ dS )zW
        An L{IntegrityError} is raised if a method returns an invalid result.
        é   r   c                    r   r   r   r   r   r   r   r    ö   r!   zLFakeLaunchpadTest.test_invalid_callable_object_return_type.<locals>.<lambda>r"   N)r$   r   r%   rP   r   r#   rM   r   r   r   Ú(test_invalid_callable_object_return_typeñ   s   
z:FakeLaunchpadTest.test_invalid_callable_object_return_typec                    s@   t dd�‰ t ‡ fdd„d�| j_| jj d¡‰ |  dˆ j¡ dS )z—
        Sample collections can be set on L{FakeLaunchpad} instances.  They are
        validated the same way other sample data is validated.
        r,   r-   c                    r   r   r   r-   ©Úbranchr   r   r    ÿ   r!   z<FakeLaunchpadTest.test_collection_property.<locals>.<lambda>©ÚgetByUniqueNameN)r$   r   r   rt   r(   r.   rM   r   rq   r   Útest_collection_propertyù   s   
z*FakeLaunchpadTest.test_collection_propertyc                 C   s`   t ddd�}t |gd�| j_t| jjƒ}|  dt|ƒ¡ |d }|  d|j¡ |  d|j¡ dS )z�
        Data for a sample collection set on a L{FakeLaunchpad} instance can be
        iterated over if an C{entries} key is defined.
        r2   r3   r4   r6   é   r   N)r$   r   r8   r9   r(   Úlenr'   r5   )r   r;   r8   r   r   r   Útest_iterate_collection  s   z)FakeLaunchpadTest.test_iterate_collectionc              	   C   s*   t dd�}|  tt| jdt |gd�¡ dS )z
        Sample data for each entry in a collection is validated when it's set
        on a L{FakeLaunchpad} instance.
        rO   rf   r8   r6   N)r$   rP   r   rQ   r   r:   r   r   r   Ú$test_collection_with_invalid_entries  s   

ûz6FakeLaunchpadTest.test_collection_with_invalid_entriesc                 C   s€   t ddd�}t ddd�}t ddd�}t |||gd�| j_| jjd	d
… }|  dt|ƒ¡ |  d|d j¡ |  d|d	 j¡ dS )zˆ
        Data for a sample collection set on a L{FakeLaunchpad} instance can be
        sliced if an C{entries} key is defined.
        r2   r3   r4   Ú2úBug #2Ú3zBug #3r6   rv   é   é   r   N)r$   r   r8   r(   rw   r'   )r   Úbug1Úbug2Úbug3r8   r   r   r   Útest_slice_collection  s   z'FakeLaunchpadTest.test_slice_collectionc                    óV   t ddd�}t ddd�}t ||gd�ˆ j_ˆ  t‡ fdd„¡ ˆ  t‡ fd	d„¡ d
S )zš
        A C{ValueError} is raised if a negative start value is used when
        slicing a sample collection set on a L{FakeLaunchpad} instance.
        r2   r3   r4   rz   r{   r6   c                      s   ˆ j jdd … S ©Néÿÿÿÿ©r   r8   r   rM   r   r   r    4  ó    zMFakeLaunchpadTest.test_slice_collection_with_negative_start.<locals>.<lambda>c                      ó   ˆ j jdd… S )Nr…   r~   r†   r   rM   r   r   r    5  r‡   N©r$   r   r8   rP   Ú
ValueError©r   r   r€   r   rM   r   Ú)test_slice_collection_with_negative_start,  ó
   z;FakeLaunchpadTest.test_slice_collection_with_negative_startc                    rƒ   )z™
        A C{ValueError} is raised if a negative stop value is used when
        slicing a sample collection set on a L{FakeLaunchpad} instance.
        r2   r3   r4   rz   r{   r6   c                      s   ˆ j jd d… S r„   r†   r   rM   r   r   r    ?  r‡   zLFakeLaunchpadTest.test_slice_collection_with_negative_stop.<locals>.<lambda>c                      rˆ   )Nr   r…   r†   r   rM   r   r   r    @  r‡   Nr‰   r‹   r   rM   r   Ú(test_slice_collection_with_negative_stop7  r�   z:FakeLaunchpadTest.test_slice_collection_with_negative_stopc                    s4   t ddd�}t |gd�ˆ j_ˆ  t‡ fdd„¡ dS )z
        An C{IndexError} is raised if an invalid index is used when retrieving
        data from a sample collection.
        r2   r3   r4   r6   c                      s   ˆ j jd S )Nr~   r†   r   rM   r   r   r    I  s    zHFakeLaunchpadTest.test_subscript_operator_out_of_range.<locals>.<lambda>N)r$   r   r8   rP   Ú
IndexError)r   r   r   rM   r   Ú$test_subscript_operator_out_of_rangeB  s   z6FakeLaunchpadTest.test_subscript_operator_out_of_rangec                 C   sN   t dd�| j_| jj}|  d|j¡ d|_|  d|j¡ |  d| jjj¡ dS )z<Values already set on fake resource objects can be replaced.r,   r-   rO   NrY   r/   r   r   r   Útest_replace_propertyK  s   z'FakeLaunchpadTest.test_replace_propertyc                    s`   t ddd�‰ t ddd�‰t ‡ fdd„d�| j_‡fdd„| jj_| jj d¡}|  d|j¡ d	S )
z=Methods already set on fake resource objects can be replaced.r,   zlp:~user/project/branch1)r.   Úbzr_identityzlp:~user/project/branch2c                    r   r   r   r-   )Úbranch1r   r   r    X  r!   z7FakeLaunchpadTest.test_replace_method.<locals>.<lambda>rs   c                    r   r   r   r-   )Úbranch2r   r   r    Y  r!   N)r$   r   r   rt   r(   r’   )r   rr   r   )r“   r”   r   Útest_replace_methodT  s   z%FakeLaunchpadTest.test_replace_methodc                 C   s,   t dd�| j_| jj}|  tt|dd¡ dS )z2Values set on fake resource objects are validated.r,   r-   r.   rv   N)r$   r   r%   rP   r   rQ   r/   r   r   r   Ú(test_replace_property_with_invalid_value]  s   z:FakeLaunchpadTest.test_replace_property_with_invalid_valuec                 C   sD   t dd�| j_|  d| jjj¡ t dd�| j_|  d| jjj¡ dS )z:Resources already set on L{FakeLaunchpad} can be replaced.r,   r-   rO   NrY   rM   r   r   r   Útest_replace_resourcec  s   z'FakeLaunchpadTest.test_replace_resourcec                 C   s`   t dd�| j_| jj}d|_|  d|j¡ |  d|j¡ |  d| jjj¡ |  d| jjj¡ dS )z?Sample data set on a L{FakeLaunchpad} instance can be added to.r,   r-   rS   N)r$   r   r%   rT   r(   r.   r/   r   r   r   Útest_add_propertyj  s   z#FakeLaunchpadTest.test_add_propertyc                 C   s>   t ƒ | j_|  tt| jjd¡ d| jj_|  d| jjj¡ dS )z6An empty object can be used when creating sample data.r.   r,   N)r$   r   r%   rP   rU   rV   r.   r(   rM   r   r   r   Ú!test_add_property_to_empty_objectt  s   

z3FakeLaunchpadTest.test_add_property_to_empty_objectc                 C   s"   t  ddd¡}|  t|t ƒ¡ dS )z…
        L{FakeLaunchpad.login} ignores all parameters and returns a new
        instance using the builtin WADL definition.
        r.   ÚtokenÚsecretN)r   Úloginr   r   ©r   r   r   r   r   Ú
test_login{  s   zFakeLaunchpadTest.test_loginc                 C   ó   t  d¡}|  t|t ƒ¡ dS )z“
        L{FakeLaunchpad.get_token_and_login} ignores all parameters and
        returns a new instance using the builtin WADL definition.
        r.   N)r   Úget_token_and_loginr   r   r�   r   r   r   Útest_get_token_and_loginƒ  ó   
z*FakeLaunchpadTest.test_get_token_and_loginc                 C   rŸ   )zŠ
        L{FakeLaunchpad.login_with} ignores all parameters and returns a new
        instance using the builtin WADL definition.
        r.   N)r   Ú
login_withr   r   r�   r   r   r   Útest_login_with‹  r¢   z!FakeLaunchpadTest.test_login_withc                 C   s$   t dd�| j_|  | jj ¡ ¡ dS )zU
        Sample object have an C{lp_save} method that is a no-op by default.
        r,   r-   N)r$   r   r%   r   Úlp_saverM   r   r   r   Útest_lp_save“  s   zFakeLaunchpadTest.test_lp_savec                 C   s,   t ddd„ d�| j_|  d| jj ¡ ¡ dS )z;A custom C{lp_save} method can be set on a L{FakeResource}.r,   c                   S   ó   dS ©NÚcustomr   r   r   r   r   r    œ  r!   z7FakeLaunchpadTest.test_custom_lp_save.<locals>.<lambda>)r.   r¥   r©   N)r$   r   r%   r(   r¥   rM   r   r   r   Útest_custom_lp_saveš  s   z%FakeLaunchpadTest.test_custom_lp_savec                 C   s4   t dd�| j_dd„ | jj_|  d| jj ¡ ¡ dS )zl
        A custom C{lp_save} method can be set on a L{FakeResource} after its
        been created.
        r,   r-   c                   S   r§   r¨   r   r   r   r   r   r    ¥  r!   z;FakeLaunchpadTest.test_set_custom_lp_save.<locals>.<lambda>r©   N)r$   r   r%   r¥   r(   rM   r   r   r   Útest_set_custom_lp_saveŸ  s   z)FakeLaunchpadTest.test_set_custom_lp_saveN))r   r   r   r   r=   rC   rJ   rK   rN   rR   rW   rZ   r\   ra   rd   re   rg   ri   rk   rl   rn   rp   ru   rx   ry   r‚   rŒ   rŽ   r�   r‘   r•   r–   r—   r˜   r™   rž   r¡   r¤   r¦   rª   r«   r   r   r   r   r>   P   sL    
				
		
			
r>   N)r   Útestresourcesr   Úlaunchpadlib.testing.launchpadr   r   r   r   Úlaunchpadlib.testing.resourcesr   r	   r
   r   r>   r   r   r   r   Ú<module>   s   '