Merge ~cjwatson/launchpadlib:fakelaunchpad-plain-methods into launchpadlib:main

Proposed by Colin Watson
Status: Merged
Merged at revision: ce8cee3126264ed7cdc7e43a2c32850eddadbb77
Proposed branch: ~cjwatson/launchpadlib:fakelaunchpad-plain-methods
Merge into: launchpadlib:main
Diff against target: 73 lines (+23/-1)
4 files modified
NEWS.rst (+2/-0)
src/launchpadlib/testing/launchpad.py (+4/-1)
src/launchpadlib/testing/testing-wadl.xml (+6/-0)
src/launchpadlib/testing/tests/test_launchpad.py (+11/-0)
Reviewer Review Type Date Requested Status
Jürgen Gmach Approve
Review via email: mp+435267@code.launchpad.net

Commit message

Fix handling of methods with no response representation

Description of the change

Some methods have no response representation defined in their WADL; these return plain data rather than an entry or a collection. Don't attempt to wrap the result of such fake methods in a `FakeResource`.

To post a comment you must log in.
Revision history for this message
Jürgen Gmach (jugmac00) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/NEWS.rst b/NEWS.rst
2index 29fc779..09feeb8 100644
3--- a/NEWS.rst
4+++ b/NEWS.rst
5@@ -9,6 +9,8 @@ NEWS for launchpadlib
6 ``launchpadlib.testing.launchpad.FakeLaunchpad``.
7 - Allow setting ``FakeLaunchpad`` sample data with attributes that are links
8 to other entries or collections.
9+- Fix handling of methods with no response representation in
10+ ``FakeLaunchpad``.
11
12 1.10.18 (2022-10-28)
13 ====================
14diff --git a/src/launchpadlib/testing/launchpad.py b/src/launchpadlib/testing/launchpad.py
15index 3b68798..aa2ee6d 100644
16--- a/src/launchpadlib/testing/launchpad.py
17+++ b/src/launchpadlib/testing/launchpad.py
18@@ -493,12 +493,15 @@ class FakeResource(object):
19 @param result: The result of calling the method.
20 @raises IntegrityError: Raised if C{result} is an invalid return value
21 for the method.
22- @return: A L{FakeResource} for C{result}.
23+ @return: A L{FakeResource} for C{result}, or just C{result} if no
24+ response representation is defined for the method.
25 """
26 resource_name = resource_type.tag.get("id")
27 if resource_name == name:
28 name = "get"
29 xml_id = self._find_representation_id(resource_type, name)
30+ if xml_id is None:
31+ return result
32 xml_id = strip_suffix(xml_id, "-full")
33 if xml_id not in self._application.resource_types:
34 xml_id += "-resource"
35diff --git a/src/launchpadlib/testing/testing-wadl.xml b/src/launchpadlib/testing/testing-wadl.xml
36index 650711d..720ac51 100644
37--- a/src/launchpadlib/testing/testing-wadl.xml
38+++ b/src/launchpadlib/testing/testing-wadl.xml
39@@ -161,6 +161,12 @@
40 </wadl:method>
41
42 <wadl:method name="DELETE" id="branch-delete"/>
43+
44+ <wadl:method id="branch-canBeDeleted" name="GET">
45+ <wadl:request>
46+ <wadl:param style="query" name="ws.op" required="true" fixed="canBeDeleted"/>
47+ </wadl:request>
48+ </wadl:method>
49 </wadl:resource_type>
50
51 <wadl:representation mediaType="application/json"
52diff --git a/src/launchpadlib/testing/tests/test_launchpad.py b/src/launchpadlib/testing/tests/test_launchpad.py
53index cad2f10..c988d2b 100644
54--- a/src/launchpadlib/testing/tests/test_launchpad.py
55+++ b/src/launchpadlib/testing/tests/test_launchpad.py
56@@ -261,6 +261,17 @@ class FakeLaunchpadTest(ResourcedTestCase):
57 self.launchpad.branches = dict(getByUniqueName=lambda name: None)
58 self.assertIsNone(self.launchpad.branches.getByUniqueName("foo"))
59
60+ def test_callable_object_no_response_representation(self):
61+ """
62+ If the WADL definition of a method does not include a response
63+ representation, then fake versions of that method just pass through
64+ the return value.
65+ """
66+ branch = dict(canBeDeleted=lambda: True)
67+ self.launchpad.branches = dict(getByUniqueName=lambda name: branch)
68+ branch = self.launchpad.branches.getByUniqueName("foo")
69+ self.assertTrue(branch.canBeDeleted())
70+
71 def test_entry_property(self):
72 """
73 Attributes that represent links to other objects are set using a

Subscribers

People subscribed via source and target branches