Dc] dZddlmZ ddlmZddlZejddk\reZ Gdde Z Gdd e Z d Zd Zd ZGd de ZGddeZGddeZGddeZy#e$r ddlmZYqwxYw)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)CallableNceZdZdZy)IntegrityErrorzERaised when bad sample data is used with a L{FakeLaunchpad} instance.N__name__ __module__ __qualname____doc__@/usr/lib/python3/dist-packages/launchpadlib/testing/launchpad.pyrrPsOrrc~eZdZdZ d dZdZdZe d dZe d dZ e d dZ y) 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. Ncv| ddlm}|}t|}|jj |||dy)Nrget_application) credentials _application _service_root)launchpadlib.testing.resourcesrFakeRoot__dict__update) selfr service_rootcachetimeout proxy_info applicationr root_resources r__init__zFakeLaunchpad.__init__[s?   F)+K -  * +!.  rc6|j}t|||y)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)rnamevaluesrs r __setattr__zFakeLaunchpad.__setattr__qs))  dF+rc.t|j|S)JGet sample data. @param name: The name of the attribute. )getattrr)rr&s r __getattr__zFakeLaunchpad.__getattr__{s t))400rc<ddlm}|t|S)z.Convenience for setting up access credentials.rrr!rrobject) cls consumer_name token_string access_secretrrrr rs rloginzFakeLaunchpad.logins C68):;;rc<ddlm}|t|S)z=Get credentials from Launchpad and log into the service root.rrr.r/)r1r2rrrr rs rget_token_and_loginz!FakeLaunchpad.get_token_and_login C68):;;rc<ddlm}|t|S)z5Log in to Launchpad with possibly cached credentials.rrr.r/)r1r2rlaunchpadlib_dirrr rs r login_withzFakeLaunchpad.login_withr8r)NNNNNN)NNNN) r r r r r#r(r, classmethodr5r7r;r rrrrTs ,,1  < < < < < >rceZdZdZdgZddZdZefdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZy) 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_saveNcL|i}|jj||i|dy)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's rr#zFakeResource.__init__s1 >F  +"/!   rct|tr |j|||j|<yi}|j |j |||<|j |j|||jd<y)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. rSN) isinstancedict_create_child_resourcerRrrS_check_resource_typerQr)rr&r@r's rr(zFakeResource.__setattr__sm eT "#'#>#>tU#KDNN4 F MM$,, ' F4L  % %d&9&96 B'-DMM) $rc|jj||}||ur>|jj||}t|tr|j ||S||j vrdS||urt|d|d|S)r*cy)NTr r rrz*FakeResource.__getattr__..srz has no attribute '')rRr>rSrVr _wrap_methodspecial_methodsAttributeError)rr&_markerresults rr,zFakeResource.__getattr__s ##D'2 W \\%%dG4F&(+((v66 4'' '  W  tT!JK K rcfd}|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. c2jg|i|SrD) _run_method)argskwargsmethodr&rs rwrapperz*FakeResource._wrap_method..wrapper s"#4##D&B4B6B Brr )rr&rhris``` rr^zFakeResource._wrap_methods Crc|j|jd}|jj|}|j |Dcic]}|j |}}d}|j |dz}|d}|j |dz}|t|d|j|} |r)|j| |t|j| |S|j| |\}} t|j| ||| Scc}w)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. r>F_collection_linkT_linkz isn't a valid property.) _find_representation_idrQrrepresentation_definitionsparamsr&r>r_get_resource_typerY FakeEntry_check_collection_typeFakeCollection) rr&r'xml_idrepresentationrArois_linkparamrTchild_resource_types rrXz#FakeResource._create_child_resources+--d.A.A5I**EEfM(..~>  JJ    4"445 =GJJtg~.E = !GH H//6   % %mV <T.. vF F(,(C(Cv) %D%"!!#  ' s Dc|jjtd}|jd}|jj |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. linkrT)tagfindrLr>rget_resource_type)rrwrzr&s rrpzFakeResource._get_resource_type>s@yy~~hv./xx(  22488rc|jD];\}}t|tr|j||)|j |||=y)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)itemsrVr _get_method_check_attribute)rrTpartial_objectr&r@s rrYz!FakeResource._check_resource_typeJsN*//1 BKD%%*  5%%mT5A  Brcd}d}|jD]V\}}|dk(r|j||\}}!t|tr|j ||D|j |||X||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)r_check_entriesrVrrr)rrTrr&rxr@s rrrz#FakeResource._check_collection_type[s")//1 BKD%y ,0,?,?!5-))E8,  5%%mT5A B(((rc|j||}|D];}|D]4}|jd}||jj|ccS=y)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)rr>r lookup_xml_id)rrTr& get_methodresponserurepresentation_urls rrmz$FakeResource._find_representation_idwsg%%mT: " OH"* O%3%7%7%?"%1,,::;MNN O OrcN|j|d}|j|||y)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. r>N)rm_check_attribute_representation)rrTr&r@rts rrzFakeResource._check_attributes(--mUC ,,VT5ArcX|jj|}|j|Dcic]}|j|}}|dz|vr-|j ||dz}|j ||\}} y|dz|vr*|j ||dz}|j ||y|j|} | td|z| j!t|tst|d|y| jdk(r!t|tst|d|yycc}w)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. rkrlNz %s not foundz is not a str or unicode for z xsd:dateTimez is not a datetime for ) rrnror&rprrrYr>rtyperV basestringr) rrtr&r@rurArorT child_namerxrws rrz,FakeResource._check_attribute_representations\**EEfM(..~>  JJ    $ $ . 33t001M/3.I.Iu/ +J+G^v % 33F4'>4JKM  % %mU ;JJt$E}$^d%:;;zz!!%4(?DdK5~-!%2(9>E3./ sD'c||jvry|jjd}|d|} t|jd|\}|S#t$rt |d|wxYw)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. Nid-z is not a method of )r_r{r>rB ValueErrorr)rrTr& resource_namertrs rrzFakeResource._get_methods~ 4'' ' %))--d3 )40 ,]->->fMLZ    .2MB  s AA(cp||i|}||jvs||S|j|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. )r__create_resourcerQ)rr&rhrfrgrbs rrezFakeResource._run_methodsE(( 4'' '6>M(()<)z-fullz -resourcez-page-resource) r{r>rmrIrresource_typesrErrrsrYrqrr%) rrTr&rbrrtresult_resource_typerxresourcer child_values rrzFakeResource._create_resources!&))--d3 D D--mTB >Mfg. **99 9 k !F#00??G ??+ ,(,(C(C$f) %D%"!!$#    % %&:F C !2!24HIH+1<<> ;' K*k: ;Orc(|j|d}|jj|}t|jdd\}t |\}|j d}|jdd}||jj|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. r>r& entry_linksrT#) rmrrnrBr{listr>splitr})rrTrtrepresentation_definitionrresource_type_urlresource_type_names r_get_child_resource_typez%FakeResource._get_child_resource_types--mUC    8 8 @ "* % ) )6= {+)--o>.44S9!<     / /0A B  rcd|j|\}}|D]}|j||||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. )rrY)rrTrr&rxentrys rrzFakeResource._check_entries'sM%)$A$A % !! BE  % %&95 A B(((rc *|jjjd}d}|jjd|}|jjd|}d|jj d|d|dt t|d S)z The resource type, identifier if available, and memory address are used to generate a representation of this fake resource. rr0r&< z at >)rQr{r>rS __class__r hexr)rr&keys r__repr__zFakeResource.__repr__9sy ""&&**40lltS)llvs+ NN # #   4M   rrD)r r r r r_r#r(r0r,r^rXrprYrrrmrrrrerrrrr rrrNrNss !kO (.&)/ *X 9B")8 O B(T*L(%N 0)$ rrNc"eZdZdZfdZxZS)rz$Fake root object for an application.cj|j|jdz}tt|||y)zCreate a L{FakeResource} for the service root of C{application}. @param application: A C{wadllib.application.Application} instance. z #service-rootN)r} markup_urlsuperrr#)rr!rTrs rr#zFakeRoot.__init__Ms6 $55  " "_ 4  h&{MBr)r r r r r# __classcell__rs@rrrJs.CCrrceZdZdZy)rqzA fake resource for an entry.Nrr rrrqrqXs'rrqc6eZdZdZ dfd ZdZdZxZS)rsz!A fake resource for a collection.cltt| ||||jj ||dy)N)_name_child_resource_type)rrsr#rr)rr!rTr'r&rxrs rr#zFakeCollection.__init___s8 nd,   4G H rc#K|jjdd}|D]+}|j|j|j|-yw)z;Iterate items if this resource has an C{entries} attribute.rr N)rSr>rrr)rrrs r__iter__zFakeCollection.__iter__nsO,,""9b1 E''))4::u  sAAc*t|}t|trM|jxsd}|j}|dkr t d|dkr t d|j |St|tr|j |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.) rrVslicestartstopr __getitem__int IndexError)rrrrrs rrzFakeCollection.__getitem__vst* c5 !IINE88Dqy Oax -&&s+ + S !&&s+ +@A Ar)NNN)r r r r r#rrrrs@rrsrs\s!+    Brrs)r rcollections.abcr ImportError collectionssys version_infostrr Exceptionrr0rrBrIrLrNrrqrsr rrrs&/b%( A!JPYPX