Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/launchpadlib/testing/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyc


Nc@sdZddlmZdZdefdYZdefdYZdZd	Zd
efdYZ	de	fd
YZ
de	fdYZde	fdYZdS(sTesting 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.
i(tdatetimesapplication/jsontIntegrityErrorcBseZdZRS(sERaised when bad sample data is used with a L{FakeLaunchpad} instance.(t__name__t
__module__t__doc__(((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRJst
FakeLaunchpadcBseZdZdddddddZdZdZedddddZedddddZ	edddddZ
RS(sA 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.
    c	Cs]|dkr(ddlm}|}nt|}|jji|d6|d6|d6dS(Ni(tget_applicationtcredentialst_applicationt
_service_root(tNonetlaunchpadlib.testing.resourcesRtFakeRoott__dict__tupdate(	tselfRtservice_roottcachettimeoutt
proxy_infotapplicationRt
root_resource((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt__init__UscCs|j}t|||dS(sSet 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	tsetattr(RtnametvaluesR((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt__setattr___s	cCst|j|S(sJGet sample data.

        @param name: The name of the attribute.
        (tgetattrR	(RR((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt__getattr__isc	Cs&ddlm}|td|S(s.Convenience for setting up access credentials.i(RR(RRtobject(	tclst
consumer_namettoken_stringt
access_secretRRRRR((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pytloginpscCs&ddlm}|td|S(s=Get credentials from Launchpad and log into the service root.i(RR(RRR(RRRRRRR((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pytget_token_and_loginwscCs&ddlm}|td|S(s5Log in to Launchpad with possibly cached credentials.i(RR(RRR(RRRtlaunchpadlib_dirRRR((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt
login_with~sN(RRRR
RRRtclassmethodR"R#R%(((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRNs			
	cCs,g|D]!}|j||kr|^qS(sKFind children of 'element' where attribute 'name' is equal to 'value'.
    (tget(telementRtvaluetchild((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pytfind_by_attributescCs"|j|r|t| S|S(N(tendswithtlen(tstringtsuffix((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pytstrip_suffixstFakeResourcecBseZdZdgZddZdZedZdZ	dZ
dZdZd	Z
d
ZdZdZd
ZdZdZdZdZdZRS(s
    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.
    tlp_savecCsE|dkri}n|jji|d6|d6id6|d6dS(sJConstruct 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.
        Rt_resource_typet	_childrent_valuesN(R
R
R(RRt
resource_typeR((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRs		cCsot|tr+|j|||j|<n@i}|j|j|||<|j|j|||jd<dS(sSet 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.
        R5N(	t
isinstancetdictt_create_child_resourceR4RR5t_check_resource_typeR3R
(RRR)R((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRs
cCs|jj||}||krU|jj||}t|rU|j||Sn||jkrkdS||krtd||fn|S(sJGet sample data.

        @param name: The name of the attribute.
        cSstS(N(tTrue(((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt<lambda>ss%r has no attribute '%s'(R4R'R5tcallablet_wrap_methodtspecial_methodstAttributeError(RRt_markertresult((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRscsfd}|S(sWrapper 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.
        csj||S(N(t_run_method(targstkwargs(tmethodRR(sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pytwrappers((RRRFRG((RRRFsB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyR>scCs|jjd}t}|j|dt}|dkrYt}|j|dt}n|dkr{td|fn|j|}|r|j	||t
|j||S|j||\}}t|j||||SdS(s
        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.
        tt_collection_linkt_links%s isn't a valid property.N(
Rtget_resource_by_pathtFalset
get_parametertJSON_MEDIA_TYPER
R;Rt_get_resource_typeR:t	FakeEntryt_check_collection_typetFakeCollection(RRRRtis_linktparamR6tchild_resource_type((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyR9s 
	cCs1t|j\}|jd}|jj|S(sGet 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.
        R6(tlistttagR'Rtget_resource_type(RRTtlinkR((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyROscCsSxL|jD]>\}}t|r8|j||q
|j|||q
WdS(sk
        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(t	iteritemsR=t_get_methodt_check_attribute(RR6tpartial_objectRR)((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyR:
s
cCsd}d}xs|jD]e\}}|dkrL|j||\}}qt|rk|j||q|j|||qW||fS(sJ
        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.
        tentriesN(R
RZt_check_entriesR=R[R\(RR6R]RRUR)((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRQscCsc|j||}xJ|D]B}x9|D]1}|jd}|dk	r&|jj|Sq&WqWdS(sFind 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).
        threfN(R[R'R
Rt
lookup_xml_id(RR6Rt
get_methodtresponsetrepresentationtrepresentation_url((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt_find_representation_id9s

cCs)|j|d}|j|||dS(s
        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.
        R'N(Rft_check_attribute_representation(RR6RR)txml_id((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyR\HscCs|jj|}td|jD}||krHtd|n||}|jd}|dkrt|tstd||fqn7|dkrt|t	std||fqndS(s
        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.
        css$|]}|jd|fVqdS(RN(R'(t.0R*((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pys	<genexpr>dss%s not foundttypes!%s is not a str or unicode for %ssxsd:dateTimes%s is not a datetime for %sN(
Rtrepresentation_definitionsR8RWRR'R
R7t
basestringR(RRhRR)Rdt
parameterst	parametert	data_type((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRgWs	
cCs||jkrdS|jjd}d||f}yt|jd|\}Wn'tk
rztd||fnX|S(sOGet 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.
        Ntids%s-%ss%s is not a method of %s(R?RWR'R+t
ValueErrorR(RR6Rt
resource_nameRhRb((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyR[ss	
cOs<|||}||jkr"|S|j|j||SdS(s8Run 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.
        N(R?t_create_resourceR3(RRRFRDRERB((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRCscCs|jjd}||kr'd}n|j||}t|d}||jjkrg|d7}n|jj|}|j||t|j||S(sCreate a 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}.
        RpR's-fulls	-resource(RWR'RfR0Rtresource_typesR:R1(RR6RRBRrRhtresult_resource_type((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRss
	
cCs|j|d}|jj|}t|jdd\}t|\}|jd}|jdd}||jj|fS(s7Get 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.
        R'Rtentry_linksR6t#i(	RfRRkR+RWRVR'tsplitRX(RR6Rhtrepresentation_definitionRvtresource_type_urltresource_type_name((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt_get_child_resource_typescCs@|j|\}}x|D]}|j||qW||fS(sEnsure 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|R:(RR6R^RRUtentry((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyR_s
cCsk|jjjd}d}|jjd|}|jjd|}d|jj||tt|fS(s
        The resource type, identifier if available, and memory address are
        used to generate a representation of this fake resource.
        RpRRs<%s %s %s at %s>(R3RWR'R5t	__class__RthexRp(RRtkey((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt__repr__sN(RRRR?R
RRRRR>R9ROR:RQRfR\RgR[RCRsR|R_R(((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyR1s&			
	"											RcBseZdZdZRS(s$Fake root object for an application.cCs3|j|jd}tt|j||dS(sCreate a L{FakeResource} for the service root of C{application}.

        @param application: A C{wadllib.application.Application} instance.
        s
#service-rootN(RXt
markup_urltsuperRR(RRR6((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRs(RRRR(((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRsRPcBseZdZRS(sA fake resource for an entry.(RRR(((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRPsRRcBs2eZdZddddZdZdZRS(s!A fake resource for a collection.cCs>tt|j||||jji|d6|d6dS(Nt_namet_child_resource_type(RRRRR
R(RRR6RRRU((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRsccsD|jjdd}x(|D] }|j|j|j|VqWdS(s;Iterate items if this resource has an C{entries} attribute.R^N((R5R'RsRR(RR^R}((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt__iter__s
cCst|}t|trv|jp'd}|j}|dkrNtdn|dkritdn|j|St|tr|j|StddS(sLook 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}.
        is6Collection slices must have a nonnegative start point.s>Collection slices must have a definite, nonnegative end point.s!Do not support index lookups yet.N(	RVR7tslicetstarttstopRqt__getitem__tintt
IndexError(RRR^RR((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRs	

N(RRRR
RRR(((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyRRs
	N(
RRRNt	ExceptionRRRR+R0R1RRPRR(((sB/usr/lib/python2.7/dist-packages/launchpadlib/testing/launchpad.pyt<module>Bs8		R