�
D�c�] � �
� d Z ddlmZ ddlmZ ddlZej d dk\ reZ G d� de
� Z G d� d e� Z
d
� Zd� Zd� Z G d
� de� Z G d� de� Z G d� de� Z G d� de� Zy# e$ r ddlmZ Y �qw xY w)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 � � e Zd ZdZy)�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.pyr r P s � �Or r c �~ � e Zd ZdZ 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 � |�
ddl m} |� }t |� }| j j |||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� ��-�
��
�
���*� +�!.�
�
r c �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�valuesr s r �__setattr__zFakeLaunchpad.__setattr__q s � � �)�)����d�F�+r c �. � t | j |� S )�JGet sample data.
@param name: The name of the attribute.
)�getattrr )r r& |