Debian Codegraph @

src/launchpadlib/testing/tests/test_launchpad.py

raw
1 # Copyright 2008 Canonical Ltd.
2
3 # This file is part of launchpadlib.
4 #
5 # launchpadlib is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # launchpadlib is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with launchpadlib. If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 from datetime import datetime, timezone
20
21 from testresources import ResourcedTestCase
22
23 from launchpadlib.testing.launchpad import (
24 FakeLaunchpad,
25 FakeResource,
26 FakeRoot,
27 IntegrityError,
28 )
29 from launchpadlib.testing.resources import (
30 FakeLaunchpadResource,
31 get_application,
32 )
33
34
35 class FakeRootTest(ResourcedTestCase):
36 def test_create_root_resource(self):
37 root_resource = FakeRoot(get_application())
38 self.assertTrue(isinstance(root_resource, FakeResource))
39
40
41 class FakeResourceTest(ResourcedTestCase):
42
43 resources = [("launchpad", FakeLaunchpadResource())]
44
45 def test_repr_entry(self):
46 """A custom C{__repr__} is provided for L{FakeEntry}s."""
47 bug = dict()
48 self.launchpad.bugs = dict(entries=[bug])
49 [bug] = list(self.launchpad.bugs)
50 self.assertEqual(
51 "<FakeEntry bug object at %s>" % hex(id(bug)), repr(bug)
52 )
53
54 def test_repr_collection(self):
55 """A custom C{__repr__} is provided for L{FakeCollection}s."""
56 branches = dict(total_size="test-branch")
57 self.launchpad.me = dict(getBranches=lambda statuses: branches)
58 branches = self.launchpad.me.getBranches([])
59 obj_id = hex(id(branches))
60 self.assertEqual(
61 "<FakeCollection branch-page-resource object at %s>" % obj_id,
62 repr(branches),
63 )
64
65 def test_repr_with_name(self):
66 """
67 If the fake has a C{name} property it's included in the repr string to
68 make it easier to figure out what it is.
69 """
70 self.launchpad.me = dict(name="foo")
71 person = self.launchpad.me
72 self.assertEqual(
73 "<FakeEntry person foo at %s>" % hex(id(person)), repr(person)
74 )
75
76 def test_repr_with_id(self):
77 """
78 If the fake has an C{id} property it's included in the repr string to
79 make it easier to figure out what it is.
80 """
81 bug = dict(id="1", title="Bug #1")
82 self.launchpad.bugs = dict(entries=[bug])
83 [bug] = list(self.launchpad.bugs)
84 self.assertEqual("<FakeEntry bug 1 at %s>" % hex(id(bug)), repr(bug))
85
86
87 class FakeLaunchpadTest(ResourcedTestCase):
88
89 resources = [("launchpad", FakeLaunchpadResource())]
90
91 def test_wb_instantiate_without_application(self):
92 """
93 The builtin WADL definition is used if the C{application} is not
94 provided during instantiation.
95 """
96 credentials = object()
97 launchpad = FakeLaunchpad(credentials)
98 self.assertEqual(credentials, launchpad.credentials)
99 self.assertEqual(get_application(), launchpad._application)
100
101 def test_instantiate_with_everything(self):
102 """
103 L{FakeLaunchpad} takes the same parameters as L{Launchpad} during
104 instantiation, with the addition of an C{application} parameter. The
105 optional parameters are discarded when the object is instantiated.
106 """
107 credentials = object()
108 launchpad = FakeLaunchpad(
109 credentials,
110 service_root=None,
111 cache=None,
112 timeout=None,
113 proxy_info=None,
114 application=get_application(),
115 )
116 self.assertEqual(credentials, launchpad.credentials)
117
118 def test_instantiate_with_credentials(self):
119 """A L{FakeLaunchpad} can be instantiated with credentials."""
120 credentials = object()
121 launchpad = FakeLaunchpad(credentials, application=get_application())
122 self.assertEqual(credentials, launchpad.credentials)
123
124 def test_instantiate_without_credentials(self):
125 """
126 A L{FakeLaunchpad} instantiated without credentials has its
127 C{credentials} attribute set to C{None}.
128 """
129 self.assertIsNone(self.launchpad.credentials)
130
131 def test_set_undefined_property(self):
132 """
133 An L{IntegrityError} is raised if an attribute is set on a
134 L{FakeLaunchpad} instance that isn't present in the WADL definition.
135 """
136 self.assertRaises(
137 IntegrityError, setattr, self.launchpad, "foo", "bar"
138 )
139
140 def test_get_undefined_resource(self):
141 """
142 An L{AttributeError} is raised if an attribute is accessed on a
143 L{FakeLaunchpad} instance that doesn't exist.
144 """
145 self.launchpad.me = dict(display_name="Foo")
146 self.assertRaises(AttributeError, getattr, self.launchpad.me, "name")
147
148 def test_string_property(self):
149 """
150 Sample data can be created by setting L{FakeLaunchpad} attributes with
151 dicts that represent objects. Plain string values can be represented
152 as C{str} values.
153 """
154 self.launchpad.me = dict(name="foo")
155 self.assertEqual("foo", self.launchpad.me.name)
156
157 def test_unicode_property(self):
158 """
159 Sample data can be created by setting L{FakeLaunchpad} attributes with
160 dicts that represent objects. Plain string values can be represented
161 as C{unicode} strings.
162 """
163 self.launchpad.me = dict(name="foo")
164 self.assertEqual("foo", self.launchpad.me.name)
165
166 def test_datetime_property(self):
167 """
168 Attributes that represent dates are set with C{datetime} instances.
169 """
170 now = datetime.now(timezone.utc)
171 self.launchpad.me = dict(date_created=now)
172 self.assertEqual(now, self.launchpad.me.date_created)
173
174 def test_invalid_datetime_property(self):
175 """
176 Only C{datetime} values can be set on L{FakeLaunchpad} instances for
177 attributes that represent dates.
178 """
179 self.assertRaises(
180 IntegrityError,
181 setattr,
182 self.launchpad,
183 "me",
184 dict(date_created="now"),
185 )
186
187 def test_multiple_string_properties(self):
188 """
189 Sample data can be created by setting L{FakeLaunchpad} attributes with
190 dicts that represent objects.
191 """
192 self.launchpad.me = dict(name="foo", display_name="Foo")
193 self.assertEqual("foo", self.launchpad.me.name)
194 self.assertEqual("Foo", self.launchpad.me.display_name)
195
196 def test_invalid_property_name(self):
197 """
198 Sample data set on a L{FakeLaunchpad} instance is validated against
199 the WADL definition. If a key is defined on a resource that doesn't
200 match a related parameter, an L{IntegrityError} is raised.
201 """
202 self.assertRaises(
203 IntegrityError, setattr, self.launchpad, "me", dict(foo="bar")
204 )
205
206 def test_invalid_property_value(self):
207 """
208 The types of sample data values set on L{FakeLaunchpad} instances are
209 validated against types defined in the WADL definition.
210 """
211 self.assertRaises(
212 IntegrityError, setattr, self.launchpad, "me", dict(name=102)
213 )
214
215 def test_callable(self):
216 """
217 A callable set on a L{FakeLaunchpad} instance is validated against the
218 WADL definition, to make sure a matching method exists.
219 """
220 branches = dict(total_size="test-branch")
221 self.launchpad.me = dict(getBranches=lambda statuses: branches)
222 self.assertNotEqual(None, self.launchpad.me.getBranches([]))
223
224 def test_invalid_callable_name(self):
225 """
226 An L{IntegrityError} is raised if a method is defined on a resource
227 that doesn't match a method defined in the WADL definition.
228 """
229 self.assertRaises(
230 IntegrityError,
231 setattr,
232 self.launchpad,
233 "me",
234 dict(foo=lambda: None),
235 )
236
237 def test_callable_object_return_type(self):
238 """
239 The result of a fake method is a L{FakeResource}, automatically
240 created from the object used to define the return object.
241 """
242 branches = dict(total_size="8")
243 self.launchpad.me = dict(getBranches=lambda statuses: branches)
244 branches = self.launchpad.me.getBranches([])
245 self.assertTrue(isinstance(branches, FakeResource))
246 self.assertEqual("8", branches.total_size)
247
248 def test_invalid_callable_object_return_type(self):
249 """
250 An L{IntegrityError} is raised if a method returns an invalid result.
251 """
252 branches = dict(total_size=8)
253 self.launchpad.me = dict(getBranches=lambda statuses: branches)
254 self.assertRaises(IntegrityError, self.launchpad.me.getBranches, [])
255
256 def test_callable_object_return_None(self):
257 """
258 A fake method passes through a return value of None rather than
259 trying to create a L{FakeResource}.
260 """
261 self.launchpad.branches = dict(getByUniqueName=lambda name: None)
262 self.assertIsNone(self.launchpad.branches.getByUniqueName("foo"))
263
264 def test_callable_object_no_response_representation(self):
265 """
266 If the WADL definition of a method does not include a response
267 representation, then fake versions of that method just pass through
268 the return value.
269 """
270 branch = dict(canBeDeleted=lambda: True)
271 self.launchpad.branches = dict(getByUniqueName=lambda name: branch)
272 branch = self.launchpad.branches.getByUniqueName("foo")
273 self.assertTrue(branch.canBeDeleted())
274
275 def test_entry_property(self):
276 """
277 Attributes that represent links to other objects are set using a
278 dict representing the object.
279 """
280 bug = dict(owner=dict(name="test-person"))
281 self.launchpad.bugs = dict(entries=[bug])
282 bug = self.launchpad.bugs[0]
283 self.assertEqual("test-person", bug.owner.name)
284
285 def test_invalid_entry_property(self):
286 """
287 Sample data for linked entries is validated.
288 """
289 bug = dict(owner=dict(foo="bar"))
290 self.assertRaises(
291 IntegrityError,
292 setattr,
293 self.launchpad,
294 "bugs",
295 dict(entries=[bug]),
296 )
297
298 def test_top_level_collection_property(self):
299 """
300 Sample top-level collections can be set on L{FakeLaunchpad}
301 instances. They are validated the same way other sample data is
302 validated.
303 """
304 branch = dict(name="foo")
305 self.launchpad.branches = dict(getByUniqueName=lambda name: branch)
306 branch = self.launchpad.branches.getByUniqueName("foo")
307 self.assertEqual("foo", branch.name)
308
309 def test_collection_property(self):
310 """
311 Attributes that represent links to collections of other objects are
312 set using a dict representing the collection.
313 """
314 bug = dict(id="1")
315 branch = dict(linked_bugs=dict(entries=[bug]))
316 self.launchpad.branches = dict(getByUniqueName=lambda name: branch)
317 branch = self.launchpad.branches.getByUniqueName("foo")
318 [bug] = list(branch.linked_bugs)
319 self.assertEqual("1", bug.id)
320
321 def test_iterate_collection(self):
322 """
323 Data for a sample collection set on a L{FakeLaunchpad} instance can be
324 iterated over if an C{entries} key is defined.
325 """
326 bug = dict(id="1", title="Bug #1")
327 self.launchpad.bugs = dict(entries=[bug])
328 bugs = list(self.launchpad.bugs)
329 self.assertEqual(1, len(bugs))
330 bug = bugs[0]
331 self.assertEqual("1", bug.id)
332 self.assertEqual("Bug #1", bug.title)
333
334 def test_top_level_collection_with_invalid_entries(self):
335 """
336 Sample data for each entry in a collection is validated when it's set
337 on a L{FakeLaunchpad} instance.
338 """
339 bug = dict(foo="bar")
340 self.assertRaises(
341 IntegrityError,
342 setattr,
343 self.launchpad,
344 "bugs",
345 dict(entries=[bug]),
346 )
347
348 def test_collection_with_invalid_entries(self):
349 """
350 Sample data for each entry in a collection is validated when it's set
351 on an attribute representing a link to a collection of objects.
352 """
353 bug = dict(foo="bar")
354 branch = dict(linked_bugs=dict(entries=[bug]))
355 self.launchpad.branches = dict(getByUniqueName=lambda name: branch)
356 self.assertRaises(
357 IntegrityError,
358 self.launchpad.branches.getByUniqueName,
359 "foo",
360 )
361
362 def test_slice_collection(self):
363 """
364 Data for a sample collection set on a L{FakeLaunchpad} instance can be
365 sliced if an C{entries} key is defined.
366 """
367 bug1 = dict(id="1", title="Bug #1")
368 bug2 = dict(id="2", title="Bug #2")
369 bug3 = dict(id="3", title="Bug #3")
370 self.launchpad.bugs = dict(entries=[bug1, bug2, bug3])
371 bugs = self.launchpad.bugs[1:3]
372 self.assertEqual(2, len(bugs))
373 self.assertEqual("2", bugs[0].id)
374 self.assertEqual("3", bugs[1].id)
375
376 def test_slice_collection_with_negative_start(self):
377 """
378 A C{ValueError} is raised if a negative start value is used when
379 slicing a sample collection set on a L{FakeLaunchpad} instance.
380 """
381 bug1 = dict(id="1", title="Bug #1")
382 bug2 = dict(id="2", title="Bug #2")
383 self.launchpad.bugs = dict(entries=[bug1, bug2])
384 self.assertRaises(ValueError, lambda: self.launchpad.bugs[-1:])
385 self.assertRaises(ValueError, lambda: self.launchpad.bugs[-1:2])
386
387 def test_slice_collection_with_negative_stop(self):
388 """
389 A C{ValueError} is raised if a negative stop value is used when
390 slicing a sample collection set on a L{FakeLaunchpad} instance.
391 """
392 bug1 = dict(id="1", title="Bug #1")
393 bug2 = dict(id="2", title="Bug #2")
394 self.launchpad.bugs = dict(entries=[bug1, bug2])
395 self.assertRaises(ValueError, lambda: self.launchpad.bugs[:-1])
396 self.assertRaises(ValueError, lambda: self.launchpad.bugs[0:-1])
397
398 def test_subscript_operator_out_of_range(self):
399 """
400 An C{IndexError} is raised if an invalid index is used when retrieving
401 data from a sample collection.
402 """
403 bug1 = dict(id="1", title="Bug #1")
404 self.launchpad.bugs = dict(entries=[bug1])
405 self.assertRaises(IndexError, lambda: self.launchpad.bugs[2])
406
407 def test_replace_property(self):
408 """Values already set on fake resource objects can be replaced."""
409 self.launchpad.me = dict(name="foo")
410 person = self.launchpad.me
411 self.assertEqual("foo", person.name)
412 person.name = "bar"
413 self.assertEqual("bar", person.name)
414 self.assertEqual("bar", self.launchpad.me.name)
415
416 def test_replace_method(self):
417 """Methods already set on fake resource objects can be replaced."""
418 branch1 = dict(name="foo", bzr_identity="lp:~user/project/branch1")
419 branch2 = dict(name="foo", bzr_identity="lp:~user/project/branch2")
420 self.launchpad.branches = dict(getByUniqueName=lambda name: branch1)
421 self.launchpad.branches.getByUniqueName = lambda name: branch2
422 branch = self.launchpad.branches.getByUniqueName("foo")
423 self.assertEqual("lp:~user/project/branch2", branch.bzr_identity)
424
425 def test_replace_property_with_invalid_value(self):
426 """Values set on fake resource objects are validated."""
427 self.launchpad.me = dict(name="foo")
428 person = self.launchpad.me
429 self.assertRaises(IntegrityError, setattr, person, "name", 1)
430
431 def test_replace_resource(self):
432 """Resources already set on L{FakeLaunchpad} can be replaced."""
433 self.launchpad.me = dict(name="foo")
434 self.assertEqual("foo", self.launchpad.me.name)
435 self.launchpad.me = dict(name="bar")
436 self.assertEqual("bar", self.launchpad.me.name)
437
438 def test_add_property(self):
439 """Sample data set on a L{FakeLaunchpad} instance can be added to."""
440 self.launchpad.me = dict(name="foo")
441 person = self.launchpad.me
442 person.display_name = "Foo"
443 self.assertEqual("foo", person.name)
444 self.assertEqual("Foo", person.display_name)
445 self.assertEqual("foo", self.launchpad.me.name)
446 self.assertEqual("Foo", self.launchpad.me.display_name)
447
448 def test_add_property_to_empty_object(self):
449 """An empty object can be used when creating sample data."""
450 self.launchpad.me = dict()
451 self.assertRaises(AttributeError, getattr, self.launchpad.me, "name")
452 self.launchpad.me.name = "foo"
453 self.assertEqual("foo", self.launchpad.me.name)
454
455 def test_login(self):
456 """
457 L{FakeLaunchpad.login} ignores all parameters and returns a new
458 instance using the builtin WADL definition.
459 """
460 launchpad = FakeLaunchpad.login("name", "token", "secret")
461 self.assertTrue(isinstance(launchpad, FakeLaunchpad))
462
463 def test_get_token_and_login(self):
464 """
465 L{FakeLaunchpad.get_token_and_login} ignores all parameters and
466 returns a new instance using the builtin WADL definition.
467 """
468 launchpad = FakeLaunchpad.get_token_and_login("name")
469 self.assertTrue(isinstance(launchpad, FakeLaunchpad))
470
471 def test_login_with(self):
472 """
473 L{FakeLaunchpad.login_with} ignores all parameters and returns a new
474 instance using the builtin WADL definition.
475 """
476 launchpad = FakeLaunchpad.login_with("name")
477 self.assertTrue(isinstance(launchpad, FakeLaunchpad))
478
479 def test_lp_save(self):
480 """
481 Sample object have an C{lp_save} method that is a no-op by default.
482 """
483 self.launchpad.me = dict(name="foo")
484 self.assertTrue(self.launchpad.me.lp_save())
485
486 def test_custom_lp_save(self):
487 """A custom C{lp_save} method can be set on a L{FakeResource}."""
488 self.launchpad.me = dict(name="foo", lp_save=lambda: "custom")
489 self.assertEqual("custom", self.launchpad.me.lp_save())
490
491 def test_set_custom_lp_save(self):
492 """
493 A custom C{lp_save} method can be set on a L{FakeResource} after its
494 been created.
495 """
496 self.launchpad.me = dict(name="foo")
497 self.launchpad.me.lp_save = lambda: "custom"
498 self.assertEqual("custom", self.launchpad.me.lp_save())