+
    Z4i\                         R t ^ RIHt ^ RIHt  ! R R]4      t ! R R4      tR tR tR	 t	 ! R
 R4      t
 ! R R]
4      t ! R R]
4      t ! R R]
4      tR# )a  Testing API allows fake data to be used in unit tests.

Testing launchpadlib code is tricky, because it depends so heavily on a
remote, unique webservice: Launchpad.  This module helps you write tests for
your launchpadlib application that can be run locally and quickly.

Say you were writing some code that needed to call out to Launchpad and get
the branches owned by the logged-in person, and then do something to them. For
example, something like this::

  def collect_unique_names(lp):
      names = []
      for branch in lp.me.getBranches():
          names.append(branch.unique_name)
      return names

To test it, you would first prepare a L{FakeLaunchpad} object, and give it
some sample data of your own devising::

  lp = FakeLaunchpad()
  my_branches = [dict(unique_name='~foo/bar/baz')]
  lp.me = dict(getBranches: lambda status: my_branches)

Then, in the test, call your own code and assert that it behaves correctly
given the data.

  names = collect_unique_names(lp)
  self.assertEqual(['~foo/bar/baz'], names)

And that's it.

The L{FakeLaunchpad} code uses a WADL file to type-check any objects created
or returned.  This means you can be sure that you won't accidentally store
sample data with misspelled attribute names.

The WADL file that we use by default is for version 1.0 of the Launchpad API.
If you want to work against a more recent version of the API, download the
WADL yourself (see <https://help.launchpad.net/API/Hacking>) and construct
your C{FakeLaunchpad} like this::

  from wadllib.application import Application
  lp = FakeLaunchpad(
      Application('https://api.launchpad.net/devel/',
                  '/path/to/wadl.xml'))

Where 'https://api.launchpad.net/devel/' is the URL for the WADL file, found
also in the WADL file itelf.
)Callable)datetimec                       ] tR t^HtRtRtR# )IntegrityErrorzERaised when bad sample data is used with a L{FakeLaunchpad} instance. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       @/usr/lib/python3/dist-packages/launchpadlib/testing/launchpad.pyr   r   H   s    Or   r   c                   v   a  ] tR t^Lt o RtR
R ltR tR t]RR l4       t	]RR l4       t
]RR l4       tR	tV tR# )FakeLaunchpadzA fake Launchpad API class for unit tests that depend on L{Launchpad}.

@param application: A C{wadllib.application.Application} instance for a
    Launchpad WADL definition file.
Nc                    Vf   ^ RI Hp V! 4       p\        V4      pV P                  P	                  RVRVRV/4       R # )Nget_applicationcredentials_application_service_root)launchpadlib.testing.resourcesr   FakeRoot__dict__update)	selfr   service_rootcachetimeout
proxy_infoapplicationr   root_resources	   &&&&&&&  r   __init__FakeLaunchpad.__init__S   sE     F)+K -{	
r   c                6    V P                   p\        W1V4       R# )zSet sample data.

@param name: The name of the attribute.
@param values: A dict representing an object matching a resource
    defined in Launchpad's WADL definition.
N)r   setattr)r   namevaluesr   s   &&& r   __setattr__FakeLaunchpad.__setattr__i   s     ))F+r   c                .    \        V P                  V4      # ):Get sample data.

@param name: The name of the attribute.
)getattrr   )r   r'   s   &&r   __getattr__FakeLaunchpad.__getattr__s   s    
 t))400r   c                <    ^ RI Hp V ! \        4       V! 4       R7      # )z.Convenience for setting up access credentials.r   r!   r   r   object)	clsconsumer_nametoken_stringaccess_secretr   r   r   r    r   s	   &&&&&&&& r   loginFakeLaunchpad.loginz   s     	C68):;;r   c                <    ^ RI Hp V ! \        4       V! 4       R7      # )z=Get credentials from Launchpad and log into the service root.r   r1   r2   )r4   r5   r   r   r   r    r   s   &&&&&& r   get_token_and_login!FakeLaunchpad.get_token_and_login        	C68):;;r   c                <    ^ RI Hp V ! \        4       V! 4       R7      # )z5Log in to Launchpad with possibly cached credentials.r   r1   r2   )r4   r5   r   launchpadlib_dirr   r    r   s   &&&&&& r   
login_withFakeLaunchpad.login_with   r=   r   r   )NNNNNN)NNNN)r   r	   r
   r   r   r#   r)   r.   classmethodr8   r;   r@   r   __classdictcell____classdict__s   @r   r   r   L   sU     
,,1 < < < < < <r   r   c                ^    V  Uu. uF  q3P                  V4      V8X  g   K  VNK  	  up# u upi )z@Find children of element where attribute name is equal to value.)get)elementr'   valuechilds   &&& r   find_by_attributerK      s)    &Cwe))D/U*BEEwCCCs   **c                 R    V P                  V4      '       d   V R \        V4      )  # V # N)endswithlen)stringsuffixs   &&r   strip_suffixrR      s)    vnV%%Mr   c                    RV ,           # )z)Scope a tag name with the WADL namespace.z&{http://research.sun.com/wadl/2006/10}r   )tag_names   &r   wadl_tagrU      s    3h>>r   c                      a  ] tR t^t o RtR.tRR ltR t]! 4       3R lt	R t
R tR	 tR
 tR tR tR tR tR tR tR tR tR tR tRtV tR# )FakeResourcea  
Represents valid sample data on L{FakeLaunchpad} instances.

@ivar _children: A dictionary of child resources, each of type
    C{FakeResource}.
@ivar _values: A dictionary of values associated with this resource. e.g.
    "display_name" or "date_created".  The values of this dictionary will
    never be C{FakeResource}s.

Note that if C{_children} has a key, then C{_values} will not, and vice
versa. That is, they are distinct dicts.
lp_saveNc           
     X    Vf   / pV P                   P                  RVRVR/ RV/4       R# )a  Construct a FakeResource.

@param application: A C{waddlib.application.Application} instance.
@param resource_type: A C{wadllib.application.ResourceType} instance
    for this resource.
@param values: Optionally, a dict representing attribute key/value
    pairs for this resource.
Nr   _resource_type	_children_values)r   r   )r   r!   resource_typer(   s   &&&&r   r#   FakeResource.__init__   s9     >F -R6		
r   c                   \        V\        4      '       d!   V P                  W4      V P                  V&   R# / pVP	                  V P
                  4       W#V&   V P                  V P                  V4       W0P                  R&   R# )zSet sample data.

C{value} can be a dict representing an object matching a resource
defined in the WADL definition.  Alternatively, C{value} could be a
resource itself.  Either way, it is checked for type correctness
against the WADL definition.
r\   N)	
isinstancedict_create_child_resourcer[   r   r\   _check_resource_typerZ   r   )r   r'   rI   r(   s   &&& r   r)   FakeResource.__setattr__   sj     eT""#'#>#>t#KDNN4 FMM$,,' 4L %%d&9&96B'-MM)$r   c                    V P                   P                  W4      pW2J dC   V P                  P                  W4      p\        V\        4      '       d   V P                  W4      # WP                  9   d   R # W2J d   \        V : RV: R24      hV# )r,   c                      R # )Tr   r   r   r   <lambda>*FakeResource.__getattr__.<locals>.<lambda>   s    4r   z has no attribute '')r[   rG   r\   r`   r   _wrap_methodspecial_methodsAttributeError)r   r'   _markerresults   &&& r   r.   FakeResource.__getattr__   s}    
 ##D2\\%%d4F&(++((66''' tT!JKKr   c                   a aa VVV 3R lpV# )zWrapper around methods validates results when it's run.

@param name: The name of the method.
@param method: The callable to run when the method is called.
c                  2   < SP                   ! SS.V O5/ VB # rM   )_run_method)argskwargsmethodr'   r   s   *,r   wrapper*FakeResource._wrap_method.<locals>.wrapper  s     ##D&B4B6BBr   r   )r   r'   ru   rv   s   fff r   rj   FakeResource._wrap_method   s    	C r   c                B   V P                  V P                  R4      pV P                  P                  V,          pVP	                  V4       Uu/ uF  pVP
                  VbK  	  ppRpVP                  VR,           4      pVf   RpVP                  VR,           4      pVf   \        V: R24      hV P                  V4      p	V'       d(   V P                  W4       \        V P                  W4      # V P                  W4      w  r\        V P                  V	VVV
4      # u upi )aq  
Ensure that C{values} is a valid object for the C{name} attribute and
return a resource object to represent it as API data.

@param name: The name of the attribute to check the C{values} object
    against.
@param values: A dict with key/value pairs representing attributes and
    methods of an object matching the C{name} resource's definition.
@return: A L{FakeEntry} for an ordinary resource or a
    L{FakeCollection} for a resource that represents a collection.
@raises IntegrityError: Raised if C{name} isn't a valid attribute for
    this resource or if C{values} isn't a valid object for the C{name}
    attribute.
rG   F_collection_linkT_linkz isn't a valid property.)_find_representation_idrZ   r   representation_definitionsparamsr'   rG   r   _get_resource_typerc   	FakeEntry_check_collection_typeFakeCollection)r   r'   r(   xml_idrepresentationrJ   r~   is_linkparamr]   child_resource_types   &&&        r   rb   #FakeResource._create_child_resource
  s%    --d.A.A5I**EEfM (..~>
> JJ> 	 
 

4"445=GJJtg~.E= !GHH//6%%m<T..FF(,(C(C)%D "!!# '
s   Dc                    VP                   P                  \        R4      4      pVP                  R4      pV P                  P                  V4      # )zGet the resource type for C{param}.

@param param: An object representing a C{_link} or C{_collection_link}
    parameter.
@return: The resource type for the parameter, or None if one isn't
    available.
linkr]   )tagfindrU   rG   r   get_resource_type)r   r   r   r'   s   &&  r   r   FakeResource._get_resource_type6  s@     yy~~hv./xx(  22488r   c                    VP                  4        F@  w  r4\        V\        4      '       d   V P                  W4       K.  V P	                  WV4       KB  	  R# )a3  
Ensure that attributes and methods defined for C{partial_object} match
attributes and methods defined for C{resource_type}.

@param resource_type: The resource type to check the attributes and
    methods against.
@param partial_object: A dict with key/value pairs representing
    attributes and methods.
N)itemsr`   r   _get_method_check_attribute)r   r]   partial_objectr'   rI   s   &&&  r   rc   !FakeResource._check_resource_typeB  sD     *//1KD%**  5%%m5A 2r   c                    RpRpVP                  4        F\  w  r5VR8X  d   V P                  W4      w  r4K!  \        V\        4      '       d   V P	                  W4       KJ  V P                  WV4       K^  	  W43# )a  
Ensure that attributes and methods defined for C{partial_object} match
attributes and methods defined for C{resource_type}.  Collection
entries are treated specially.

@param resource_type: The resource type to check the attributes and
    methods against.
@param partial_object: A dict with key/value pairs representing
    attributes and methods.
@return: (name, resource_type), where 'name' is the name of the child
    resource type and 'resource_type' is the corresponding resource
    type.
Nentries)r   _check_entriesr`   r   r   r   )r   r]   r   r'   r   rI   s   &&&   r   r   #FakeResource._check_collection_typeS  s}     ")//1KDy ,0,?,?!-)) E8,,  5%%m5A 2 ((r   c                    V P                  W4      pV F@  pV F7  pVP                  R4      pVf   K  V P                  P                  V4      u u # 	  KB  	  R# )zFind the WADL XML id for the representation of C{resource_type}.

Looks in the WADL for the first representiation associated with the
method for a resource type.

:return: An XML id (a string).
hrefN)r   rG   r   lookup_xml_id)r   r]   r'   
get_methodresponser   representation_urls   &&&    r   r|   $FakeResource._find_representation_ido  sW     %%m:
"H"*%3%7%7%?"%1,,::;MNN #+ #r   c                N    V P                  VR4      pV P                  WBV4       R# )ag  
Ensure that C{value} is a valid C{name} attribute on C{resource_type}.

Does this by finding the representation for the default, canonical GET
method (as opposed to the many "named" GET methods that exist.)

@param resource_type: The resource type to check the attribute
    against.
@param name: The name of the attribute.
@param value: The value to check.
rG   N)r|   _check_attribute_representation)r   r]   r'   rI   r   s   &&&& r   r   FakeResource._check_attribute~  s&     --mUC,,V5Ar   c                   V P                   P                  V,          pVP                  V4       Uu/ uF  pVP                  VbK  	  ppVR,           V9   d4   V P	                  WbR,           ,          4      pV P                  Ws4      w  rR# VR,           V9   d2   V P	                  WbR,           ,          4      pV P                  Ws4       R# VP                  V4      p
V
f   \        RV,          4      hV
P                  f+   \        V\        4      '       g   \        V: RV: 24      hR# V
P                  R8X  d+   \        V\        4      '       g   \        V: RV: 24      hR# R# u upi )a  
Ensure that C{value} is a valid value for C{name} with the
representation definition matching C{xml_id}.

@param xml_id: The XML ID for the representation to check the
    attribute against.
@param name: The name of the attribute.
@param value: The value to check.
@raises IntegrityError: Raised if C{name} is not a valid attribute
    name or if C{value}'s type is not valid for the attribute.
rz   r{   Nz%s not foundz is not a str or unicode for zxsd:dateTimez is not a datetime for )r   r}   r~   r'   r   r   rc   rG   r   typer`   strr   )r   r   r'   rI   r   rJ   r~   r]   
child_namer   r   s   &&&&       r   r   ,FakeResource._check_attribute_representation  s]    **EEfM (..~>
> JJ> 	 
 $$. 33001M /3.I.I/+J+ G^v% 33F'>4JKM%%m;JJt$E}$^d%:;;zz!!%--(?DdK  . ~-!%22(9>E  3 ./
s   E%c                    W P                   9   d   R# VP                  P                  R4      pV: RV: 2p \        VP                  RV4      w  pV#   \         d    \        T: RT: 24      hi ; i)a  Get the C{name} method on C{resource_type}.

@param resource_type: The method's resource type.
@param name: The name of the method.
@raises IntegrityError: Raised if a method called C{name} is not
    available on C{resource_type}.
@return: The XML element for the method from the WADL.
Nid-z is not a method of )rk   r   rG   rK   
ValueErrorr   )r   r]   r'   resource_namer   r   s   &&&   r   r   FakeResource._get_method  s|     '''%))--d3)40	,]->->fMLZ
 	  	 .2MB 	s   A A1c                v    V! V/ VB pWP                   9   g   Vf   V# V P                  V P                  W4      # )a  Run a method and convert its result into a L{FakeResource}.

If the result represents an object it is validated against the WADL
definition before being returned.

@param name: The name of the method.
@param method: A callable.
@param args: Arguments to pass to the callable.
@param kwargs: Keyword arguments to pass to the callable.
@return: A L{FakeResource} representing the result if it's an object.
@raises IntegrityError: Raised if the return value from the method
    isn't valid.
)rk   _create_resourcerZ   )r   r'   ru   rs   rt   rn   s   &&&*, r   rr   FakeResource._run_method  sA     (('''6>M(()<)<dKKr   c                0   VP                   P                  R4      pWB8X  d   RpV P                  W4      pVf   V# \        VR4      pWPP                  P
                  9  d
   VR,          pV P                  P
                  V,          pVP                  R4      '       d-   V P                  Wc4      w  r'\        V P                  VVVV4      # V P                  Wc4       \        V P                  V4      pVP                  4        F  w  r\        WV
4       K  	  V# )a  Create new L{FakeResource} for C{resource_type} method call result.

@param resource_type: The resource type of the method.
@param name: The name of the method on C{resource_type}.
@param result: The result of calling the method.
@raises IntegrityError: Raised if C{result} is an invalid return value
    for the method.
@return: A L{FakeResource} for C{result}, or just C{result} if no
    response representation is defined for the method.
r   rG   z-fullz	-resourcez-page-resource)r   rG   r|   rR   r   resource_typesrN   r   r   rc   r   r   r&   )r   r]   r'   rn   r   r   result_resource_typer   resourcer   child_values   &&&&       r   r   FakeResource._create_resource  s    &))--d3 D--mB>Mfg.**999k!F#00??G??+,,(,(C(C$)%D "!!$#  %%&:C !2!24HIH+1<<>'
k: ,:Or   c                8   V P                  VR4      pV P                  P                  V,          p\        VP                  RR4      w  p\        V4      w  pVP                  R4      pVP                  R4      ^,          pVV P                  P                  V4      3# )a  Get the name and resource type for the entries in a collection.

@param resource_type: The resource type for a collection.
@return: (name, resource_type), where 'name' is the name of the child
    resource type and 'resource_type' is the corresponding resource
    type.
rG   r'   entry_linksr]   #)	r|   r   r}   rK   r   listrG   splitr   )r   r]   r   representation_definitionr   resource_type_urlresource_type_names   &&     r   _get_child_resource_type%FakeResource._get_child_resource_type  s     --mUC88@ 	" *%))6=
 {+)--o>.44S9!<//0AB
 	
r   c                b    V P                  V4      w  r4V F  pV P                  WE4       K  	  W43# )aw  Ensure that C{entries} are valid for a C{resource_type} collection.

@param resource_type: The resource type of the collection the entries
    are in.
@param entries: A list of dicts representing objects in the
    collection.
@return: (name, resource_type), where 'name' is the name of the child
    resource type and 'resource_type' is the corresponding resource
    type.
)r   rc   )r   r]   r   r'   r   entrys   &&&   r   r   FakeResource._check_entries  s>     %)$A$A%
! E%%&9A ((r   c                2   V P                   P                  P                  R4      pRpV P                  P                  RV4      pV P                  P                  RV4      pRV P                  P
                  : RV: RV: R\        \        V 4      4      : R2	# )z}
The resource type, identifier if available, and memory address are
used to generate a representation of this fake resource.
r   r3   r'   < z at >)rZ   r   rG   r\   	__class__r   hexr   )r   r'   keys   &  r   __repr__FakeResource.__repr__1  sy    
 ""&&**40lltS)llvs+NN##4M	
 	
r   r   rM   )r   r	   r
   r   r   rk   r#   r)   r3   r.   rj   rb   r   rc   r   r|   r   r   r   rr   r   r   r   r   r   rC   rD   s   @r   rW   rW      s{      !kO
(.& )/  
*X
9B")8OB(T*L(%N
0)$
 
r   rW   c                   6   a a ] tR tRt oRtV 3R ltRtVtV ;t# )r   iB  z$Fake root object for an application.c                j   < VP                  VP                  R,           4      p\        SV `  W4       R# )zCreate a L{FakeResource} for the service root of C{application}.

@param application: A C{wadllib.application.Application} instance.
z#service-rootN)r   
markup_urlsuperr#   )r   r!   r]   r   s   && r   r#   FakeRoot.__init__E  s2    
 $55""_4
 	4r   r   )	r   r	   r
   r   r   r#   r   rC   __classcell__r   rE   s   @@r   r   r   B  s     .5 5r   r   c                       ] tR tRtRtRtR# )r   iP  zA fake resource for an entry.r   Nr   r   r   r   r   r   P  s    'r   r   c                   F   a a ] tR tRt oRtRV 3R lltR tR tRtVt	V ;t
# )r   iT  z!A fake resource for a collection.c                f   < \         SV `  WV4       V P                  P                  R VRV/4       R# )_name_child_resource_typeN)r   r#   r   r   )r   r!   r]   r(   r'   r   r   s   &&&&&&r   r#   FakeCollection.__init__W  s4     	V<d24GH	
r   c              #     "   V P                   P                  RR4      pV F,  pV P                  V P                  V P                  V4      x  K.  	  R# 5i)z;Iterate items if this resource has an C{entries} attribute.r   Nr   )r\   rG   r   r   r   )r   r   r   s   &  r   __iter__FakeCollection.__iter__d  sI     ,,""9b1E''))4::u  s   AAc                V   \        V 4      p\        V\        4      '       dX   VP                  ;'       g    ^ pVP                  pV^ 8  d   \        R4      hV^ 8  d   \        R4      hVP                  V4      # \        V\        4      '       d   VP                  V4      # \        R4      h)zLook up a slice, or a subordinate resource by index.

@param key: An individual object key or a C{slice}.
@raises IndexError: Raised if an invalid key is provided.
@return: A L{FakeResource} instance for the entry matching C{key}.
z6Collection slices must have a nonnegative start point.z>Collection slices must have a definite, nonnegative end point.z!Do not support index lookups yet.)	r   r`   slicestartstopr   __getitem__int
IndexError)r   r   r   r   r   s   &&   r   r   FakeCollection.__getitem__l  s     t*c5!!IINNE88Dqy O  ax -  &&s++S!!&&s++@AAr   r   )NNN)r   r	   r
   r   r   r#   r   r   r   rC   r   r   s   @@r   r   r   T  s     +
B Br   r   N)r   collections.abcr   r   	Exceptionr   r   rK   rR   rU   rW   r   r   r   r   r   r   <module>r      sq   &/b % PY PX< X<vD
?
H
 H
V5| 5( (0B\ 0Br   