%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python3/dist-packages/launchpadlib/testing/__pycache__/
Upload File :
Create Path :
Current File : //usr/lib/python3/dist-packages/launchpadlib/testing/__pycache__/launchpad.cpython-312.pyc

�

D�c�]��
�dZddlmZ	ddlmZddlZejddk\reZ	Gd�de
�ZGd�d	e�Z
d
�Zd�Zd�ZGd
�de�ZGd�de�ZGd�de�ZGd�de�Zy#e$r	ddlmZY�qwxYw)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.
�)�datetime)�CallableN�c��eZdZdZy)�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.pyrrPs��Orrc�~�eZdZdZ						d	d�Zd�Zd�Ze				d
d��Ze				d
d��Z	e				d
d��Z
y)�
FakeLaunchpadz�A 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�|�
ddlm}|�}t|�}|jj	|||d��y)Nr��get_application)�credentials�_application�
_service_root)�launchpadlib.testing.resourcesr�FakeRoot�__dict__�update)	�selfr�service_root�cache�timeout�
proxy_info�applicationr�
root_resources	         r�__init__zFakeLaunchpad.__init__[s?����F�)�+�K� ��-�
��
�
���*� +�!.�
�	
rc�6�|j}t|||�y)z�Set 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�valuesrs    r�__setattr__zFakeLaunchpad.__setattr__qs���)�)����d�F�+rc�.�t|j|�S)�JGet sample data.

        @param name: The name of the attribute.
        )�getattrr)rr&s  r�__getattr__zFakeLaunchpad.__getattr__{s��
�t�)�)�4�0�0rc�<�ddlm}|t�|���S)z.Convenience for setting up access credentials.rr�r!�rr�object)	�cls�
consumer_name�token_string�
access_secretrrrr rs	         r�loginzFakeLaunchpad.login�s��	C��6�8��):�;�;rc�<�ddlm}|t�|���S)z=Get credentials from Launchpad and log into the service root.rrr.r/)r1r2rrrr rs       r�get_token_and_loginz!FakeLaunchpad.get_token_and_login����	C��6�8��):�;�;rc�<�ddlm}|t�|���S)z5Log in to Launchpad with possibly cached credentials.rrr.r/)r1r2r�launchpadlib_dirrr rs       r�
login_withzFakeLaunchpad.login_with�r8r)NNNNNN)NNNN)r	r
rrr#r(r,�classmethodr5r7r;r
rrrrTs����������
�,,�1������
<��
<������
<��<������
<��<rrc�V�|D�cgc]}|j|�|k(s�|��c}Scc}w)z@Find children of element where attribute name is equal to value.)�get)�elementr&�value�childs    r�find_by_attributerB�s%��&�C�e�%�)�)�D�/�U�*B�E�C�C��Cs�&�&c�F�|j|�r|dt|�S|S�N)�endswith�len)�string�suffixs  r�strip_suffixrI�s&��
���v���n��V���%�%��Mrc��d|zS)z)Scope a tag name with the WADL namespace.z&{http://research.sun.com/wadl/2006/10}r
)�tag_names r�wadl_tagrL�s
��3�h�>�>rc��eZdZdZdgZdd�Zd�Ze�fd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zy)�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�L�|�i}|jj||i|d��y)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��
�
��� +�"/��!�	
�	
rc��t|t�r |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)	�
isinstance�dict�_create_child_resourcerRrrS�_check_resource_typerQr)rr&r@r's    rr(zFakeResource.__setattr__�sm���e�T�"�#'�#>�#>�t�U�#K�D�N�N�4� ��F��M�M�$�,�,�'� �F�4�L�
�%�%�d�&9�&9�6�B�'-�D�M�M�)�$rc��|jj||�}||ur>|jj||�}t|t�r|j||�S||jvrd�S||urt|�d|�d���|S)r*c��y)NTr
r
rr�<lambda>z*FakeResource.__getattr__.<locals>.<lambda>s�rz has no attribute '�')rRr>rSrVr�_wrap_method�special_methods�AttributeError)rr&�_marker�results    rr,zFakeResource.__getattr__�s���
���#�#�D�'�2���W���\�\�%�%�d�G�4�F��&�(�+��(�(��v�6�6��4�'�'�'����W�� �t�T�!J�K�K��
rc��������fd�}|S)z�Wrapper 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���j��g|��i|��SrD)�_run_method)�args�kwargs�methodr&rs  ���r�wrapperz*FakeResource._wrap_method.<locals>.wrapper
s"���#�4�#�#�D�&�B�4�B�6�B�Brr
)rr&rhris``` rr^zFakeResource._wrap_methods���	C��rc��|j|jd�}|jj|}|j	|�D�cic]}|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_idrQr�representation_definitions�paramsr&r>r�_get_resource_typerY�	FakeEntry�_check_collection_type�FakeCollection)rr&r'�xml_id�representationrAro�is_link�paramrT�child_resource_types           rrXz#FakeResource._create_child_resources+���-�-�d�.A�.A�5�I���*�*�E�E�f�M��(�.�.�~�>�
��
�J�J���
��
����
�
�4�"4�4�5���=��G��J�J�t�g�~�.�E��=� ��!G�H�H��/�/��6�
���%�%�m�V�<��T�.�.�
�v�F�F�(,�(C�(C��v�)�%�D�%�"��!�!����#��
��'
s�	Dc��|jjtd��}|jd�}|jj|�S)z�Get 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)�tag�findrLr>r�get_resource_type)rrwrzr&s    rrpzFakeResource._get_resource_type>s@���y�y�~�~�h�v�.�/���x�x��(��� � �2�2�4�8�8rc��|j�D];\}}t|t�r|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)rrT�partial_objectr&r@s     rrYz!FakeResource._check_resource_typeJsN��*�/�/�1�	B�K�D�%��%��*�� � ���5��%�%�m�T�5�A�	Brc���d}d}|j�D]V\}}|dk(r|j||�\}}�!t|t�r|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.
        N�entries)r�_check_entriesrVrr�r�)rrTr�r&rxr@s      rrrz#FakeResource._check_collection_type[s�����"��)�/�/�1�		B�K�D�%��y� �,0�,?�,?�!�5�-�)��)��E�8�,�� � ���5��%�%�m�T�5�A�		B��(�(�(rc��|j||�}|D];}|D]4}|jd�}|��|jj|�ccS�=y)z�Find 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�r>r�
lookup_xml_id)rrTr&�
get_method�responseru�representation_urls       rrmz$FakeResource._find_representation_idwsg���%�%�m�T�:�
�"�	O�H�"*�
O��%3�%7�%7��%?�"�%�1��,�,�:�:�;M�N�N�
O�	Orc�N�|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     rr�zFakeResource._check_attribute�s(���-�-�m�U�C���,�,�V�T�5�Arc�X�|jj|}|j|�D�cic]}|j|��}}|dz|vr-|j	||dz�}|j||�\}}	y|dz|vr*|j	||dz�}|j
||�y|j|�}
|
�td|z��|
j�!t|t�st|�d|����y|
jdk(r!t|t�st|�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>r�typerV�
basestringr)rrtr&r@rurArorT�
child_namerxrws           rr�z,FakeResource._check_attribute_representation�s\���*�*�E�E�f�M��(�.�.�~�>�
��
�J�J���
��
��$�$��.� �3�3��t�0�0�1��M�/3�.I�.I��u�/�+�J�+��G�^�v�
%� �3�3�F�4�'�>�4J�K�M��%�%�m�U�;��J�J�t�$�E��}�$�^�d�%:�;�;��z�z�!�!�%��4�(�?D�d�K���5����~�-�!�%��2�(�9>��E���3�.��/
s�D'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.
        N�id�-z is not a method of )r_r{r>rB�
ValueErrorr)rrTr&�
resource_namertr�s      rr�zFakeResource._get_method�s~���4�'�'�'��%�)�)�-�-�d�3�
�)�4�0��	�,�]�->�->��f�M�L�Z�
���	�	� �.2�M�B��
�	�s�A�A(c�p�||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_method�sE����(��(���4�'�'�'�6�>��M��(�(��)<�)<�d�F�K�Krc��|jjd�}||k(rd}|j||�}|�|St|d�}||jj
vr|dz
}|jj
|}|j
d�r.|j||�\}}t|j||||�S|j||�t|j|�}|j�D]\}	}
t||	|
��|S)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�r>z-fullz	-resourcez-page-resource)
r{r>rmrIr�resource_typesrErrrsrYrqrr%)rrTr&rbr�rt�result_resource_typerx�resourcer��child_values           rr�zFakeResource._create_resource�s!��&�)�)�-�-�d�3�
��D� ��D��-�-�m�T�B���>��M��f�g�.����*�*�9�9�9��k�!�F�#�0�0�?�?��G���?�?�+�,�(,�(C�(C�$�f�)�%�D�%�"��!�!�$���#��
�
�%�%�&:�F�C� ��!2�!2�4H�I�H�+1�<�<�>�
;�'�
�K���*�k�:�
;��Orc�(�|j|d�}|jj|}t|jdd�\}t|�\}|j
d�}|jd�d}||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})rrTrt�representation_definitionr��resource_type_url�resource_type_names       r�_get_child_resource_typez%FakeResource._get_child_resource_types����-�-�m�U�C�����8�8��@�	"�*�%�)�)�6�=�
�
���{�+���)�-�-�o�>��.�4�4�S�9�!�<������/�/�0A�B�
�	
rc�d�|j|�\}}|D]}|j||��||fS)a�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�rY)rrTr�r&rx�entrys      rr�zFakeResource._check_entries'sM��%)�$A�$A��%
�!��!��	B�E��%�%�&9�5�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.
        r�r0r&�<� z at �>)rQr{r>rS�	__class__r	�hexr�)rr&�keys   r�__repr__zFakeResource.__repr__9sy��
�"�"�&�&�*�*�4�0�����l�l���t�S�)���l�l���v�s�+���N�N�#�#�����4��M�	
�	
rrD)r	r
rrr_r#r(r0r,r^rXrprYrrrmr�r�r�rer�r�r�r�r
rrrNrN�ss���!�k�O�
�(.�&)/��� 
�*�X
9�B�")�8
O�
B�(�T�*L�(%�N
�0)�$
rrNc�"��eZdZdZ�fd�Z�xZS)rz$Fake root object for an application.c�j��|j|jdz�}tt|�||�y)z�Create a L{FakeResource} for the service root of C{application}.

        @param application: A C{wadllib.application.Application} instance.
        z
#service-rootN)r}�
markup_url�superrr#)rr!rTr�s   �rr#zFakeRoot.__init__Ms6���
$�5�5��"�"�_�4�
�
�	�h��&�{�M�Br)r	r
rrr#�
__classcell__�r�s@rrrJs���.�C�Crrc��eZdZdZy)rqzA fake resource for an entry.Nrr
rrrqrqXs��'rrqc�6��eZdZdZ			d�fd�	Zd�Zd�Z�xZS)rsz!A fake resource for a collection.c�l��tt|�|||�|jj	||d��y)N)�_name�_child_resource_type)r�rsr#rr)rr!rTr'r&rxr�s      �rr#zFakeCollection.__init___s8���	�n�d�,����	
�	
�
�
����4G�H�	
rc#�K�|jjdd�}|D]+}|j|j|j|����-y�w)z;Iterate items if this resource has an C{entries} attribute.r�r
N)rSr>r�r�r�)rr�r�s   r�__iter__zFakeCollection.__iter__nsO�����,�,�"�"�9�b�1���	�E��'�'��)�)�4�:�:�u��
�	�s�AAc�*�t|�}t|t�rM|jxsd}|j}|dkrtd��|dkrtd��|j
|�St|t�r|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.)	r�rV�slice�start�stopr��__getitem__�int�
IndexError)rr�r�r�r�s     rr�zFakeCollection.__getitem__vs����t�*���c�5�!��I�I�N��E��8�8�D��q�y� �O����a�x� �-����&�&�s�+�+�
��S�
!��&�&�s�+�+��@�A�Ar)NNN)r	r
rrr#r�r�r�r�s@rrsrs\s!���+��
� �


��Brrs)rr�collections.abcr�ImportError�collections�sys�version_info�strr��	Exceptionrr0rrBrIrLrNrrqrsr
rr�<module>r�s���&/�b�%�(�����A��!���J�P�Y�P�X<�F�X<�vD�
�?�
H
�6�H
�VC�|�C�(��(�2B�\�2B��i�%�$�%�s�A4�4B�B

Zerion Mini Shell 1.0