o x۔aUX@sdZddlmZzddlmZWneyddlmZYnwddlZejddkr-eZ dZ Gddde Z Gd d d e Zd d Zd dZGddde ZGdddeZGdddeZGdddeZdS)aTesting 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 ) 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. )datetime)CallableNzapplication/jsonc@eZdZdZdS)IntegrityErrorzERaised when bad sample data is used with a L{FakeLaunchpad} instance.N__name__ __module__ __qualname____doc__r r @/usr/lib/python3/dist-packages/launchpadlib/testing/launchpad.pyrRrc@sxeZdZdZ      dddZddZddZe    dd d Ze    dd d Z e    dd dZ dS) 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 Cs:|dur ddlm}|}t|}|j|||ddS)Nrget_application) credentials _application _service_root)launchpadlib.testing.resourcesrFakeRoot__dict__update) selfr service_rootcachetimeout proxy_info applicationr root_resourcer r r __init__]s zFakeLaunchpad.__init__cCs|j}t|||dS)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)rsetattr)rnamevaluesrr r r __setattr__sszFakeLaunchpad.__setattr__cCs t|j|S)JGet sample data. @param name: The name of the attribute. )getattrr)rr"r r r __getattr__}s zFakeLaunchpad.__getattr__c Csddlm}|t|dS)z.Convenience for setting up access credentials.rrrrrobject) cls consumer_name token_string access_secretrrrrrr r r logins zFakeLaunchpad.logincCddlm}|t|dS)z=Get credentials from Launchpad and log into the service root.rrr(r))r+r,rrrrrr r r get_token_and_login z!FakeLaunchpad.get_token_and_logincCr0)z5Log in to Launchpad with possibly cached credentials.rrr(r))r+r,rlaunchpadlib_dirrrrr r r login_withr2zFakeLaunchpad.login_with)NNNNNN)NNNN) rr r r r r$r' classmethodr/r1r4r r r r rVs:     rcsfdd|DS)z@Find children of element where attribute name is equal to value.csg|] }|kr|qSr get.0childr"valuer r sz%find_by_attribute..r )elementr"r<r r;r find_by_attributesr?cCs ||r|dt| S|SN)endswithlen)stringsuffixr r r strip_suffixs rEc@seZdZdZdgZd&ddZddZefdd Zd d Z d d Z ddZ ddZ ddZ ddZddZddZddZddZddZd d!Zd"d#Zd$d%ZdS)' 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_saveNcCs&|duri}|j||i|ddS)aJConstruct 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. N)r_resource_type _children_values)rr)rr resource_typer#r r r r s zFakeResource.__init__cCsTt|tr||||j|<dSi}||j|||<||j|||jd<dS)aSet 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. rJN) isinstancedict_create_child_resourcerIrrJ_check_resource_typerHr)rr"r<r#r r r r$s  zFakeResource.__setattr__cCsh|j||}||ur|j||}t|tr|||S||jvr&ddS||ur2td||f|S)r%cSsdS)NTr r r r r sz*FakeResource.__getattr__..z%r has no attribute '%s')rIr7rJrLr _wrap_methodspecial_methodsAttributeError)rr"_markerresultr r r r's   zFakeResource.__getattr__csfdd}|S)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. csjg|Ri|Sr@) _run_method)argskwargsmethodr"rr r wrapper sz*FakeResource._wrap_method..wrapperr )rr"rZr[r rYr rQszFakeResource._wrap_methodcCs|jd}d}||dt}|durd}||dt}|dur)td|f||}|r=|||t|j||S|||\}}t |j||||S)a 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. F_collection_linkNT_linkz%s isn't a valid property.) rget_resource_by_path get_parameterJSON_MEDIA_TYPEr_get_resource_typerO FakeEntry_check_collection_typeFakeCollection)rr"r#ris_linkparamrKchild_resource_typer r r rNs4   z#FakeResource._create_child_resourcecCs"t|j\}|d}|j|S)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. rK)listtagr7rget_resource_type)rrglinkr"r r r rb:s   zFakeResource._get_resource_typecCs<|D]\}}t|tr|||q||||qdS)ak 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)itemsrLr _get_method_check_attribute)rrKpartial_objectr"r<r r r rOFs  z!FakeResource._check_resource_typecCsbd}d}|D]$\}}|dkr|||\}}qt|tr%|||q||||q||fS)aJ 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. Nentries)rm_check_entriesrLrrnro)rrKrpr"rhr<r r r rdWs z#FakeResource._check_collection_typecCsJ|||}|D]}|D]}|d}|dur!|j|Sq qdS)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)rnr7r lookup_xml_id)rrKr" get_methodresponserepresentationrepresentation_urlr r r _find_representation_idss  z$FakeResource._find_representation_idcCs||d}||||dS)a 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. r7N)ry_check_attribute_representation)rrKr"r<xml_idr r r ros zFakeResource._check_attributecCs|jj|}dd|jD}||vrtd|||}|d}|dur4t|ts2td||fdS|dkrEt|tsGtd||fdSdS) 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. cSsi|]}|d|qS)r"r6r8r r r sz@FakeResource._check_attribute_representation..z %s not foundtypeNz!%s is not a str or unicode for %sz xsd:dateTimez%s is not a datetime for %s)rrepresentation_definitionsrjrr7rL basestringr)rr{r"r<rw parameters parameter data_typer r r rzs&       z,FakeResource._check_attribute_representationcCs\||jvrdS|jd}d||f}z t|jd|\}W|Sty-td||fw)aOGet 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. Nidz%s-%sz%s is not a method of %s)rRrjr7r? ValueErrorr)rrKr" resource_namer{rur r r rns     zFakeResource._get_methodcOs,||i|}||jvr|S||j||S)a8Run 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. )rR_create_resourcerH)rr"rZrWrXrUr r r rVs zFakeResource._run_methodcCsh|jd}||kr d}|||}t|d}||jjvr!|d7}|jj|}|||t|j||S)aCreate 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}. rr7z-fullz -resource)rjr7ryrErresource_typesrOrF)rrKr"rUrr{result_resource_typer r r rs      zFakeResource._create_resourcecCsZ||d}|jj|}t|jdd\}t|\}|d}|dd}||j|fS)a7Get 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. r7r" entry_linksrK#) ryrr~r?rjrir7splitrk)rrKr{representation_definitionrresource_type_urlresource_type_namer r r _get_child_resource_types     z%FakeResource._get_child_resource_typecCs,||\}}|D]}|||q ||fS)aEnsure 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. )rrO)rrKrqr"rhentryr r r rrs  zFakeResource._check_entriescCsJ|jjd}d}|jd|}|jd|}d|jj||tt|fS)z The resource type, identifier if available, and memory address are used to generate a representation of this fake resource. rr*r"z<%s %s %s at %s>)rHrjr7rJ __class__rhexr)rr"keyr r r __repr__s zFakeResource.__repr__r@)rr r r rRr r$r*r'rQrNrbrOrdryrorzrnrVrrrrrr r r r rFs(  +  rFcs eZdZdZfddZZS)rz$Fake root object for an application.cs&||jd}tt|||dS)zCreate a L{FakeResource} for the service root of C{application}. @param application: A C{wadllib.application.Application} instance. z #service-rootN)rk markup_urlsuperrr )rrrKrr r r +szFakeRoot.__init__)rr r r r __classcell__r r rr r(src@r)rczA fake resource for an entry.Nrr r r r rc6rrccs8eZdZdZ   d fdd ZddZddZZS) rez!A fake resource for a collection.Ncs*tt|||||j||ddS)N)_name_child_resource_type)rrer rr)rrrKr#r"rhrr r r =s zFakeCollection.__init__ccs2|jdd}|D] }||j|j|Vq dS)z;Iterate items if this resource has an C{entries} attribute.rqr N)rJr7rrr)rrqrr r r __iter__Ls zFakeCollection.__iter__cCsht|}t|tr&|jp d}|j}|dkrtd|dkr!td||St|tr0||Std)aLook 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}. rz6Collection slices must have a nonnegative start point.z>Collection slices must have a definite, nonnegative end point.z!Do not support index lookups yet.) rirLslicestartstopr __getitem__int IndexError)rrrqrrr r r rTs      zFakeCollection.__getitem__)NNN)rr r r r rrrr r rr re:sre)r rcollections.abcr ImportError collectionssys version_infostrrra Exceptionrr*rr?rErFrrcrer r r r s* 1 [n