
    DcTJ                         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e      Z G d de      Z G d	 d
e      Zy)    )datetime)ResourcedTestCase)FakeLaunchpadFakeResourceFakeRootIntegrityError)FakeLaunchpadResourceget_applicationc                       e Zd Zd Zy)FakeRootTestc                 h    t        t                     }| j                  t        |t                     y N)r   r
   
assertTrue
isinstancer   )selfroot_resources     K/usr/lib/python3/dist-packages/launchpadlib/testing/tests/test_launchpad.pytest_create_root_resourcez&FakeRootTest.test_create_root_resource$   s"     !23
=,?@    N)__name__
__module____qualname__r    r   r   r   r   #   s    Ar   r   c                   8    e Zd Zd e       fgZd Zd Zd Zd Zy)FakeResourceTest	launchpadc                     t               }t        |g      | j                  _        t        | j                  j                        \  }| j	                  dt        t        |            z  t        |             y)z3A custom C{__repr__} is provided for L{FakeEntry}s.entriesz<FakeEntry bug object at %s>Ndictr   bugslistassertEqualhexidreprr   bugs     r   test_repr_entryz FakeResourceTest.test_repr_entry-   sV    f"C51T^^(()*SC\949	
r   c                    t        d      t        fd      | j                  _        | j                  j                  j                  g       t	        t                    }| j                  d|z  t                     y)z8A custom C{__repr__} is provided for L{FakeCollection}s.test-branch
total_sizec                     S r   r   statusesbranchess    r   <lambda>z7FakeResourceTest.test_repr_collection.<locals>.<lambda>9       h r   getBranchesz2<FakeCollection branch-page-resource object at %s>N)r!   r   mer6   r%   r&   r$   r'   )r   obj_idr2   s     @r   test_repr_collectionz%FakeResourceTest.test_repr_collection6   sd    =1 -FG>>$$004R\"@6IN	
r   c                     t        d      | j                  _        | j                  j                  }| j                  dt	        t        |            z  t        |             y)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   r7   r$   r%   r&   r'   r   persons     r   test_repr_with_namez$FakeResourceTest.test_repr_with_nameA   sH    
 !e,""*SF_<d6l	
r   c                     t        dd      }t        |g      | j                  _        t        | j                  j                        \  }| j	                  dt        t        |            z  t        |             y)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&   titler   z<FakeEntry bug 1 at %s>Nr    r(   s     r   test_repr_with_idz"FakeResourceTest.test_repr_with_idL   s[    
 c*"C51T^^(()2SC\A49Mr   N)	r   r   r   r	   	resourcesr*   r9   r@   rF   r   r   r   r   r   )   s(    4678I
	
	
Nr   r   c                      e Zd Zd e       fgZd Zd Zd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d  Z#d! Z$d" Z%d# Z&d$ Z'd% Z(d& Z)d' Z*d( Z+d) Z,d* Z-d+ Z.y,)-FakeLaunchpadTestr   c                     t               }t        |      }| j                  ||j                         | j                  t	               |j
                         y)zy
        The builtin WADL definition is used if the C{application} is not
        provided during instantiation.
        N)objectr   r$   credentialsr
   _applicationr   rL   r   s      r   'test_wb_instantiate_without_applicationz9FakeLaunchpadTest.test_wb_instantiate_without_application[   sD    
 h!+.	i&;&;<*I,B,BCr   c           	          t               }t        |ddddt                     }| j                  ||j                         y)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rK   r   r
   r$   rL   rN   s      r    test_instantiate_with_everythingz2FakeLaunchpadTest.test_instantiate_with_everythinge   sB     h!')
	 	i&;&;<r   c                 z    t               }t        |t                     }| j                  ||j                         y)z8A L{FakeLaunchpad} can be instantiated with credentials.)rU   NrV   rN   s      r   !test_instantiate_with_credentialsz3FakeLaunchpadTest.test_instantiate_with_credentialsv   s.    h!+?;LM	i&;&;<r   c                 N    | j                  | j                  j                         y)z~
        A L{FakeLaunchpad} instantiated without credentials has its
        C{credentials} attribute set to C{None}.
        N)assertIsNoner   rL   r   s    r   $test_instantiate_without_credentialsz6FakeLaunchpadTest.test_instantiate_without_credentials|   s    
 	$..445r   c                 R    | j                  t        t        | j                  dd       y)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   r\   s    r   test_set_undefined_propertyz-FakeLaunchpadTest.test_set_undefined_property   s!    
 	GT^^UE	
r   c                     t        d      | j                  _        | j                  t        t
        | j                  j                  d       y)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   r7   r`   AttributeErrorgetattrr\   s    r   test_get_undefined_resourcez-FakeLaunchpadTest.test_get_undefined_resource   s3    
 !e4.'4>>3D3DfMr   c                     t        d      | j                  _        | j                  d| j                  j                  j                         y)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   r7   r$   r=   r\   s    r   test_string_propertyz&FakeLaunchpadTest.test_string_property   s5     !e, 1 1 6 67r   c                     t        d      | j                  _        | j                  d| j                  j                  j                         y)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<   Nrj   r\   s    r   test_unicode_propertyz'FakeLaunchpadTest.test_unicode_property   s5     !f-!2!2!7!78r   c                     t        j                         }t        |      | j                  _        | j                  || j                  j                  j                         y)zU
        Attributes that represent dates are set with C{datetime} instances.
        date_createdN)r   utcnowr!   r   r7   r$   rp   )r   nows     r   test_datetime_propertyz(FakeLaunchpadTest.test_datetime_property   sA     oo c2dnn//<<=r   c           	      f    | j                  t        t        | j                  dt	        d             y)z
        Only C{datetime} values can be set on L{FakeLaunchpad} instances for
        attributes that represent dates.
        r7   rr   ro   Nr`   r   ra   r   r!   r\   s    r   test_invalid_datetime_propertyz0FakeLaunchpadTest.test_invalid_datetime_property   s*    
 	NNe$	
r   c                     t        dd      | j                  _        | j                  d| j                  j                  j                         | j                  d| j                  j                  j
                         y)z~
        Sample data can be created by setting L{FakeLaunchpad} attributes with
        dicts that represent objects.
        r;   rd   )r=   re   N)r!   r   r7   r$   r=   re   r\   s    r   test_multiple_string_propertiesz1FakeLaunchpadTest.test_multiple_string_properties   sW    
 !e%@ 1 1 6 67 1 1 > >?r   c           	      f    | j                  t        t        | j                  dt	        d             y)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.
        r7   r_   r;   Nru   r\   s    r   test_invalid_property_namez,FakeLaunchpadTest.test_invalid_property_name   s%     	GT^^T4E?	
r   c           	      f    | j                  t        t        | j                  dt	        d             y)z
        The types of sample data values set on L{FakeLaunchpad} instances are
        validated against types defined in the WADL definition.
        r7   f   r<   Nru   r\   s    r   test_invalid_property_valuez-FakeLaunchpadTest.test_invalid_property_value   s%    
 	GT^^T4S>	
r   c                     t        d      t        fd      | j                  _        | j                  d| j                  j                  j	                  g              y)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 r   r   r0   s    r   r3   z1FakeLaunchpadTest.test_callable.<locals>.<lambda>   r4   r   r5   N)r!   r   r7   assertNotEqualr6   r   r2   s    @r   test_callablezFakeLaunchpadTest.test_callable   sF    
 =1 -FGD$.."3"3"?"?"CDr   c           	      h    | j                  t        t        | j                  dt	        d              y)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.
        r7   c                       y r   r   r   r   r   r3   z>FakeLaunchpadTest.test_invalid_callable_name.<locals>.<lambda>       r   rz   Nru   r\   s    r   test_invalid_callable_namez,FakeLaunchpadTest.test_invalid_callable_name   s*    
 	NN\"	
r   c                    t        d      t        fd      | j                  _        | j                  j                  j                  g       | j	                  t        t                     | j                  dj                         y)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 r   r   r0   s    r   r3   zDFakeLaunchpadTest.test_callable_object_return_type.<locals>.<lambda>   r4   r   r5   N)	r!   r   r7   r6   r   r   r   r$   r.   r   s    @r    test_callable_object_return_typez2FakeLaunchpadTest.test_callable_object_return_type   sd    
 3' -FG>>$$004
8\:;h112r   c                     t        d      t        fd      | j                  _        | j                  t        | j                  j                  j
                  g        y)zW
        An L{IntegrityError} is raised if a method returns an invalid result.
           r-   c                     S r   r   r0   s    r   r3   zLFakeLaunchpadTest.test_invalid_callable_object_return_type.<locals>.<lambda>   r4   r   r5   N)r!   r   r7   r`   r   r6   r   s    @r   (test_invalid_callable_object_return_typez:FakeLaunchpadTest.test_invalid_callable_object_return_type   sC     1% -FG.$..*;*;*G*GLr   c                     t        d       | j                  _        | j                  | j                  j                  j	                  d             y)z}
        A fake method passes through a return value of None rather than
        trying to create a L{FakeResource}.
        c                      y r   r   r<   s    r   r3   zDFakeLaunchpadTest.test_callable_object_return_None.<locals>.<lambda>  r   r   getByUniqueNamer;   N)r!   r   r2   r[   r   r\   s    r    test_callable_object_return_Nonez2FakeLaunchpadTest.test_callable_object_return_None   s9    
 #'7H"I$..11AA%HIr   c                     t        d       t        fd      | j                  _        | j                  j                  j                  d      | j	                  j                                y)z
        If the WADL definition of a method does not include a response
        representation, then fake versions of that method just pass through
        the return value.
        c                       y)NTr   r   r   r   r3   zSFakeLaunchpadTest.test_callable_object_no_response_representation.<locals>.<lambda>  r   r   )canBeDeletedc                     S r   r   r=   branchs    r   r3   zSFakeLaunchpadTest.test_callable_object_no_response_representation.<locals>.<lambda>      F r   r   r;   N)r!   r   r2   r   r   r   r   r   s    @r   /test_callable_object_no_response_representationzAFakeLaunchpadTest.test_callable_object_no_response_representation  sP     <0"&7J"K((88?++-.r   c                     t        t        d            }t        |g      | j                  _        | j                  j                  d   }| j                  d|j                  j
                         y)zx
        Attributes that represent links to other objects are set using a
        dict representing the object.
        ztest-personr<   ownerr   r   N)r!   r   r"   r$   r   r=   r(   s     r   test_entry_propertyz%FakeLaunchpadTest.test_entry_property  sR    
 =12"C51nn!!!$		7r   c           	          t        t        d            }| j                  t        t        | j                  dt        |g             y)z>
        Sample data for linked entries is validated.
        r_   rz   r   r"   r   Nr!   r`   r   ra   r   r(   s     r   test_invalid_entry_propertyz-FakeLaunchpadTest.test_invalid_entry_property  s:     %)NN#	
r   c                     t        d      t        fd      | j                  _        | j                  j                  j                  d      | j	                  dj
                         y)z
        Sample top-level collections can be set on L{FakeLaunchpad}
        instances.  They are validated the same way other sample data is
        validated.
        r;   r<   c                     S r   r   r   s    r   r3   zFFakeLaunchpadTest.test_top_level_collection_property.<locals>.<lambda>1  r   r   r   N)r!   r   r2   r   r$   r=   r   s    @r   "test_top_level_collection_propertyz4FakeLaunchpadTest.test_top_level_collection_property*  sO     5!"&7J"K((88?,r   c                 8   t        d      }t        t        |g            t        fd      | j                  _        | j                  j                  j                  d      t	        j
                        \  }| j                  d|j                         y)	z
        Attributes that represent links to collections of other objects are
        set using a dict representing the collection.
        rB   )r&   r   linked_bugsc                     S r   r   r   s    r   r3   z<FakeLaunchpadTest.test_collection_property.<locals>.<lambda><  r   r   r   r;   N)r!   r   r2   r   r#   r   r$   r&   r   r)   r   s     @r   test_collection_propertyz*FakeLaunchpadTest.test_collection_property5  sq    
 cl$u"56"&7J"K((88?V''(cff%r   c                 D   t        dd      }t        |g      | j                  _        t        | j                  j                        }| j	                  dt        |             |d   }| j	                  d|j                         | j	                  d|j                         y)z
        Data for a sample collection set on a L{FakeLaunchpad} instance can be
        iterated over if an C{entries} key is defined.
        rB   rC   rD   r      r   N)r!   r   r"   r#   r$   lenr&   rE   )r   r)   r"   s      r   test_iterate_collectionz)FakeLaunchpadTest.test_iterate_collectionA  s|    
 c*"C51DNN''(CI&1gcff%399-r   c           	          t        d      }| j                  t        t        | j                  dt        |g             y)z
        Sample data for each entry in a collection is validated when it's set
        on a L{FakeLaunchpad} instance.
        r_   rz   r"   r   Nr   r(   s     r   .test_top_level_collection_with_invalid_entriesz@FakeLaunchpadTest.test_top_level_collection_with_invalid_entriesN  s5    
 uoNN#	
r   c                     t        d      }t        t        |g            t        fd      | j                  _        | j                  t        | j                  j                  j
                  d       y)	z
        Sample data for each entry in a collection is validated when it's set
        on an attribute representing a link to a collection of objects.
        r_   rz   r   r   c                     S r   r   r   s    r   r3   zHFakeLaunchpadTest.test_collection_with_invalid_entries.<locals>.<lambda>c  r   r   r   r;   N)r!   r   r2   r`   r   r   r   s     @r   $test_collection_with_invalid_entriesz6FakeLaunchpadTest.test_collection_with_invalid_entries\  sX    
 uo$u"56"&7J"KNN##33	
r   c                 r   t        dd      }t        dd      }t        dd      }t        |||g      | j                  _        | j                  j                  d	d
 }| j                  dt	        |             | j                  d|d   j
                         | j                  d|d	   j
                         y)z
        Data for a sample collection set on a L{FakeLaunchpad} instance can be
        sliced if an C{entries} key is defined.
        rB   rC   rD   2Bug #23zBug #3r   r         r   N)r!   r   r"   r$   r   r&   )r   bug1bug2bug3r"   s        r   test_slice_collectionz'FakeLaunchpadTest.test_slice_collectionj  s    
 s(+s(+s(+"D$+=>~~""1Q'CI&d1gjj)d1gjj)r   c                      t        dd      }t        dd      }t        ||g       j                  _         j                  t         fd        j                  t         fd       y	)
z
        A C{ValueError} is raised if a negative start value is used when
        slicing a sample collection set on a L{FakeLaunchpad} instance.
        rB   rC   rD   r   r   r   c                  6     j                   j                  dd  S Nr   r"   r\   s   r   r3   zMFakeLaunchpadTest.test_slice_collection_with_negative_start.<locals>.<lambda>  s    dnn.A.A"#.F r   c                  6     j                   j                  dd S )Nr   r   r   r\   s   r   r3   zMFakeLaunchpadTest.test_slice_collection_with_negative_start.<locals>.<lambda>  s    dnn.A.A"Q.G r   Nr!   r   r"   r`   
ValueErrorr   r   r   s   `  r   )test_slice_collection_with_negative_startz;FakeLaunchpadTest.test_slice_collection_with_negative_startx  T    
 s(+s(+"D$<8*&FG*&GHr   c                      t        dd      }t        dd      }t        ||g       j                  _         j                  t         fd        j                  t         fd       y	)
z
        A C{ValueError} is raised if a negative stop value is used when
        slicing a sample collection set on a L{FakeLaunchpad} instance.
        rB   rC   rD   r   r   r   c                  6     j                   j                  d d S r   r   r\   s   r   r3   zLFakeLaunchpadTest.test_slice_collection_with_negative_stop.<locals>.<lambda>  s    dnn.A.A#2.F r   c                  6     j                   j                  dd S )Nr   r   r   r\   s   r   r3   zLFakeLaunchpadTest.test_slice_collection_with_negative_stop.<locals>.<lambda>  s    dnn.A.A!B.G r   Nr   r   s   `  r   (test_slice_collection_with_negative_stopz:FakeLaunchpadTest.test_slice_collection_with_negative_stop  r   r   c                      t        dd      }t        |g       j                  _         j                  t         fd       y)z
        An C{IndexError} is raised if an invalid index is used when retrieving
        data from a sample collection.
        rB   rC   rD   r   c                  6     j                   j                  d   S )Nr   r   r\   s   r   r3   zHFakeLaunchpadTest.test_subscript_operator_out_of_range.<locals>.<lambda>  s    dnn.A.A!.D r   N)r!   r   r"   r`   
IndexError)r   r   s   ` r   $test_subscript_operator_out_of_rangez6FakeLaunchpadTest.test_subscript_operator_out_of_range  s5    
 s(+"D62*&DEr   c                 D   t        d      | j                  _        | j                  j                  }| j                  d|j                         d|_        | j                  d|j                         | j                  d| j                  j                  j                         y)z<Values already set on fake resource objects can be replaced.r;   r<   r_   Nrj   r>   s     r   test_replace_propertyz'FakeLaunchpadTest.test_replace_property  sq     e,"",, 1 1 6 67r   c                 6   t        dd      t        dd      t        fd      | j                  _        fd| j                  j                  _        | j                  j                  j                  d      }| j	                  d|j
                         y)	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 r   r   )r=   branch1s    r   r3   z7FakeLaunchpadTest.test_replace_method.<locals>.<lambda>  s    G r   r   c                     S r   r   )r=   branch2s    r   r3   z7FakeLaunchpadTest.test_replace_method.<locals>.<lambda>  s    w r   N)r!   r   r2   r   r$   r   )r   r   r   r   s     @@r   test_replace_methodz%FakeLaunchpadTest.test_replace_method  sr    E0JKE0JK"&7K"L2F/((88?3V5H5HIr   c                     t        d      | j                  _        | j                  j                  }| j                  t        t
        |dd       y)z2Values set on fake resource objects are validated.r;   r<   r=   r   N)r!   r   r7   r`   r   ra   r>   s     r   (test_replace_property_with_invalid_valuez:FakeLaunchpadTest.test_replace_property_with_invalid_value  s8     e,"".'661Er   c                 0   t        d      | j                  _        | j                  d| j                  j                  j                         t        d      | j                  _        | j                  d| j                  j                  j                         y)z:Resources already set on L{FakeLaunchpad} can be replaced.r;   r<   r_   Nrj   r\   s    r   test_replace_resourcez'FakeLaunchpadTest.test_replace_resource  sd     e, 1 1 6 67 e, 1 1 6 67r   c                    t        d      | j                  _        | j                  j                  }d|_        | j	                  d|j
                         | j	                  d|j                         | j	                  d| j                  j                  j
                         | j	                  d| j                  j                  j                         y)z?Sample data set on a L{FakeLaunchpad} instance can be added to.r;   r<   rd   N)r!   r   r7   re   r$   r=   r>   s     r   test_add_propertyz#FakeLaunchpadTest.test_add_property  s     e,""#, 3 34 1 1 6 67 1 1 > >?r   c                 ,   t               | j                  _        | j                  t        t
        | j                  j                  d       d| j                  j                  _        | j                  d| j                  j                  j                         y)z6An empty object can be used when creating sample data.r=   r;   N)r!   r   r7   r`   rf   rg   r=   r$   r\   s    r   !test_add_property_to_empty_objectz3FakeLaunchpadTest.test_add_property_to_empty_object  s`     F.'4>>3D3DfM!& 1 1 6 67r   c                 p    t        j                  ddd      }| j                  t        |t                      y)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   s     r   
test_loginzFakeLaunchpadTest.test_login  s+    
 "''B	
9m<=r   c                 l    t        j                  d      }| j                  t        |t                      y)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   s     r   test_get_token_and_loginz*FakeLaunchpadTest.test_get_token_and_login  s'    
 "55f=	
9m<=r   c                 l    t        j                  d      }| j                  t        |t                      y)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   s     r   test_login_withz!FakeLaunchpadTest.test_login_with  s'    
 ",,V4	
9m<=r   c                     t        d      | j                  _        | j                  | j                  j                  j	                                y)zU
        Sample object have an C{lp_save} method that is a no-op by default.
        r;   r<   N)r!   r   r7   r   lp_saver\   s    r   test_lp_savezFakeLaunchpadTest.test_lp_save  s4     !e,))1134r   c                     t        dd       | j                  _        | j                  d| j                  j                  j	                                y)z;A custom C{lp_save} method can be set on a L{FakeResource}.r;   c                       yNcustomr   r   r   r   r3   z7FakeLaunchpadTest.test_custom_lp_save.<locals>.<lambda>  r   r   )r=   r   r   N)r!   r   r7   r$   r   r\   s    r   test_custom_lp_savez%FakeLaunchpadTest.test_custom_lp_save  s9     e5EF4>>#4#4#<#<#>?r   c                     t        d      | j                  _        d | j                  j                  _        | j	                  d| j                  j                  j                                y)zl
        A custom C{lp_save} method can be set on a L{FakeResource} after its
        been created.
        r;   r<   c                       yr   r   r   r   r   r3   z;FakeLaunchpadTest.test_set_custom_lp_save.<locals>.<lambda>  r   r   r   N)r!   r   r7   r   r$   r\   s    r   test_set_custom_lp_savez)FakeLaunchpadTest.test_set_custom_lp_save  sJ    
 !e,$4!4>>#4#4#<#<#>?r   N)/r   r   r   r	   rG   rO   rW   rY   r]   rb   rh   rk   rm   rs   rv   rx   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rI   rI   W   s    4678ID="=6
N89>
@

E
	3MJ	/8
	-
&.

*	I	IF8JF8@8>>>5@
@r   rI   N)r   testresourcesr   launchpadlib.testing.launchpadr   r   r   r   launchpadlib.testing.resourcesr	   r
   r   r   rI   r   r   r   <module>r      sL   &  + A$ A+N( +N\[@) [@r   