U
    ,ݛUD>                     @   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   )selfZ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    S r   r   Zstatusesbranchesr   r   <lambda>/       z,FakeResourceTest.test_repr.<locals>.<lambda>getBranchesz0<FakeResource branch-page-resource object at %s>N)dictr   mer    hexidassertEqualrepr)r   Z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   Zpersonr   r   r   test_repr_with_name6   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_id@   s    z"FakeResourceTest.test_repr_with_idN)r   r   r   r   	resourcesr'   r,   r7   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	   Z_applicationr   r;   r   r   r   r   'test_wb_instantiate_without_applicationO   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)Zservice_rootcacheZtimeoutZ
proxy_infoapplicationr:   r   r	   r%   r;   r<   r   r   r    test_instantiate_with_everythingY   s     z2FakeLaunchpadTest.test_instantiate_with_everythingc                 C   s&   t  }t|t d}| ||j dS )z8A L{FakeLaunchpad} can be instantiated with credentials.)r?   Nr@   r<   r   r   r   !test_instantiate_with_credentialse   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)ZassertIsNoner   r;   r   r   r   r   $test_instantiate_without_credentialsk   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   rC   r   r   r   test_set_undefined_propertyr   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"   rF   AttributeErrorgetattrrC   r   r   r   test_get_undefined_resourcey   s    z-FakeLaunchpadTest.test_get_undefined_resourcec                 C   s$   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*   rC   r   r   r   test_string_property   s    z&FakeLaunchpadTest.test_string_propertyc                 C   s$   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{unicode} strings.
        r(   r)   NrN   rC   r   r   r   test_unicode_property   s    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   Zutcnowr!   r   r"   r%   rR   )r   nowr   r   r   test_datetime_property   s    z(FakeLaunchpadTest.test_datetime_propertyc              	   C   s   |  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"   rS   rQ   NrF   r   rG   r   r!   rC   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(   rI   )r*   rJ   N)r!   r   r"   r%   r*   rJ   rC   r   r   r   test_multiple_string_properties   s    z1FakeLaunchpadTest.test_multiple_string_propertiesc              	   C   s   |  tt| jdtdd dS )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"   rE   r(   NrU   rC   r   r   r   test_invalid_property_name   s    z,FakeLaunchpadTest.test_invalid_property_namec              	   C   s   |  tt| jdtdd dS )z
        The types of sample data values set on L{FakeLaunchpad} instances are
        validated against types defined in the WADL definition.
        r"   f   r)   NrU   rC   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                    s    S r   r   r   r   r   r   r      r   z1FakeLaunchpadTest.test_callable.<locals>.<lambda>r   N)r!   r   r"   ZassertNotEqualr    rC   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>rX   NrU   rC   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                    s    S 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   rC   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                    s    S r   r   r   r   r   r   r      r   zLFakeLaunchpadTest.test_invalid_callable_object_return_type.<locals>.<lambda>r   N)r!   r   r"   rF   r   r    rC   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                    s    S r   r   r)   branchr   r   r      r   z<FakeLaunchpadTest.test_collection_property.<locals>.<lambda>getByUniqueNameN)r!   r   r   re   r%   r*   rC   r   rb   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.
        r-   r.   r/   r1      r   N)r!   r   r3   r4   r%   lenr$   r0   )r   r6   r3   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.
        rE   rX   r3   r1   N)r!   rF   r   rG   r   r5   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.
        r-   r.   r/   2Bug #23zBug #3r1   rg         r   N)r!   r   r3   r%   rh   r$   )r   bug1bug2Zbug3r3   r   r   r   test_slice_collection  s    z'FakeLaunchpadTest.test_slice_collectionc                    sV   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.
        r-   r.   r/   rk   rl   r1   c                      s    j jdd  S Nr   r3   r   rC   r   r   r     r   zMFakeLaunchpadTest.test_slice_collection_with_negative_start.<locals>.<lambda>c                      s    j jdd S )Nrt   ro   ru   r   rC   r   r   r     r   Nr!   r   r3   rF   
ValueErrorr   rp   rq   r   rC   r   )test_slice_collection_with_negative_start  s
    z;FakeLaunchpadTest.test_slice_collection_with_negative_startc                    sV   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 stop value is used when
        slicing a sample collection set on a L{FakeLaunchpad} instance.
        r-   r.   r/   rk   rl   r1   c                      s    j jd d S rs   ru   r   rC   r   r   r   "  r   zLFakeLaunchpadTest.test_slice_collection_with_negative_stop.<locals>.<lambda>c                      s    j jdd S )Nr   rt   ru   r   rC   r   r   r   #  r   Nrv   rx   r   rC   r   (test_slice_collection_with_negative_stop  s
    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.
        r-   r.   r/   r1   c                      s    j jd S )Nro   ru   r   rC   r   r   r   ,  r   zHFakeLaunchpadTest.test_subscript_operator_out_of_range.<locals>.<lambda>N)r!   r   r3   rF   
IndexError)r   rp   r   rC   r   $test_subscript_operator_out_of_range%  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)   rE   NrN   r+   r   r   r   test_replace_property.  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                    s    S r   r   r)   )branch1r   r   r   ;  r   z7FakeLaunchpadTest.test_replace_method.<locals>.<lambda>rd   c                    s    S r   r   r)   )branch2r   r   r   <  r   N)r!   r   r   re   r%   r~   )r   rc   r   )r   r   r   test_replace_method7  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*   rg   N)r!   r   r"   rF   r   rG   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)   rE   NrN   rC   r   r   r   test_replace_resourceF  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)   rI   N)r!   r   r"   rJ   r%   r*   r+   r   r   r   test_add_propertyM  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"   rF   rK   rL   r*   r%   rC   r   r   r   !test_add_property_to_empty_objectW  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*   tokenZsecretN)r   Zloginr   r   r   r   r   r   r   
test_login^  s    zFakeLaunchpadTest.test_loginc                 C   s   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   Zget_token_and_loginr   r   r   r   r   r   test_get_token_and_loginf  s    
z*FakeLaunchpadTest.test_get_token_and_loginc                 C   s   t d}| t|t  dS )z
        L{FakeLaunchpad.login_with} ignores all parameters and returns a new
        instance using the builtin WADL definition.
        r*   N)r   Z
login_withr   r   r   r   r   r   test_login_withn  s    
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_saverC   r   r   r   test_lp_savev  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   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   rC   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   s   dS r   r   r   r   r   r   r     r   z;FakeLaunchpadTest.test_set_custom_lp_save.<locals>.<lambda>r   N)r!   r   r"   r   r%   rC   r   r   r   test_set_custom_lp_save  s    z)FakeLaunchpadTest.test_set_custom_lp_saveN))r   r   r   r   r8   r=   rA   rB   rD   rH   rM   rO   rP   rT   rV   rW   rY   r[   r\   r]   r_   ra   rf   ri   rj   rr   ry   rz   r|   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r9   K   sJ   
					
				
r9   N)r   Ztestresourcesr   Zlaunchpadlib.testing.launchpadr   r   r   r   Zlaunchpadlib.testing.resourcesr   r	   r
   r   r9   r   r   r   r   <module>   s   #