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   '