Merge ~cjwatson/launchpadlib:fakelaunchpad-methods-returning-None into launchpadlib:main

Proposed by Colin Watson
Status: Merged
Merged at revision: abef23b17f0fdd6b2091bc7c6784b24072d326c0
Proposed branch: ~cjwatson/launchpadlib:fakelaunchpad-methods-returning-None
Merge into: launchpadlib:main
Diff against target: 58 lines (+12/-2)
4 files modified
NEWS.rst (+2/-0)
src/launchpadlib/testing/launchpad.py (+1/-1)
src/launchpadlib/testing/tests/test_launchpad.py (+8/-0)
tox.ini (+1/-1)
Reviewer Review Type Date Requested Status
Jürgen Gmach Approve
Review via email: mp+435156@code.launchpad.net

Commit message

Support fake methods that return None

Description of the change

`FakeLaunchpad` is useful for writing test suites for packages that use `launchpadlib`. When trying to do so, I noticed a problem: attempting to register a fake method that returns None caused a crash. Real Launchpad webservice methods may return None, so fix this.

I also ran into a couple of issues with this package's `pytest` setup, and fixed those in passing.

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 7d7b2f4..b3475eb 100644
3--- a/NEWS.rst
4+++ b/NEWS.rst
5@@ -5,6 +5,8 @@ NEWS for launchpadlib
6 1.11.0 (unreleased)
7 ===================
8 - Move the ``keyring`` dependency to a new ``keyring`` extra.
9+- Support setting fake methods that return None on instances of
10+ ``launchpadlib.testing.launchpad.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 9a8db06..a079da1 100644
16--- a/src/launchpadlib/testing/launchpad.py
17+++ b/src/launchpadlib/testing/launchpad.py
18@@ -463,7 +463,7 @@ class FakeResource(object):
19 isn't valid.
20 """
21 result = method(*args, **kwargs)
22- if name in self.special_methods:
23+ if name in self.special_methods or result is None:
24 return result
25 else:
26 return self._create_resource(self._resource_type, name, result)
27diff --git a/src/launchpadlib/testing/tests/test_launchpad.py b/src/launchpadlib/testing/tests/test_launchpad.py
28index c118e17..956fc06 100644
29--- a/src/launchpadlib/testing/tests/test_launchpad.py
30+++ b/src/launchpadlib/testing/tests/test_launchpad.py
31@@ -246,6 +246,14 @@ class FakeLaunchpadTest(ResourcedTestCase):
32 self.launchpad.me = dict(getBranches=lambda statuses: branches)
33 self.assertRaises(IntegrityError, self.launchpad.me.getBranches, [])
34
35+ def test_callable_object_return_None(self):
36+ """
37+ A fake method passes through a return value of None rather than
38+ trying to create a L{FakeResource}.
39+ """
40+ self.launchpad.branches = dict(getByUniqueName=lambda name: None)
41+ self.assertIsNone(self.launchpad.branches.getByUniqueName("foo"))
42+
43 def test_collection_property(self):
44 """
45 Sample collections can be set on L{FakeLaunchpad} instances. They are
46diff --git a/tox.ini b/tox.ini
47index be0759a..d71e61d 100644
48--- a/tox.ini
49+++ b/tox.ini
50@@ -6,7 +6,7 @@ envlist =
51 deps =
52 .[test,testing]
53 commands =
54- coverage run -m pytest src/launchpadlib/tests {posargs}
55+ coverage run -m pytest src {posargs}
56
57 [testenv:lint]
58 # necessary to build the woke linter

Subscribers

People subscribed via source and target branches