Merge lp:~free.ekanayaka/python-zope-fixtures/utility-fixture into lp:python-zope-fixtures

Proposed by Free Ekanayaka
Status: Merged
Merged at revision: 3
Proposed branch: lp:~free.ekanayaka/python-zope-fixtures/utility-fixture
Merge into: lp:python-zope-fixtures
Diff against target: 155 lines (+55/-12)
3 files modified
lib/zope_fixtures/__init__.py (+3/-2)
lib/zope_fixtures/components.py (+23/-4)
lib/zope_fixtures/tests/test_components.py (+29/-6)
To merge this branch: bzr merge lp:~free.ekanayaka/python-zope-fixtures/utility-fixture
Reviewer Review Type Date Requested Status
python-fixtures committers Pending
Review via email: mp+83124@code.launchpad.net

Description of the change

This branch:

- Renames zope_fixtures.zoperegistry.ZopeRegistryFixture to zope_fixtures.components.ComponentsFixture, to make it more clear that the APIs of interest are the ones provided by zope.components, and because the Zope prefix seems now redundant.

- Adds a new UtilityFixture, which is merely a convenience around ComponentsFixture for overriding a single utility, reducing boilerplate code for this common use case.

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

This needs some docs in README; I'll add that as I merge.

Revision history for this message
Robert Collins (lifeless) wrote :

Oh, and __all__ updated.

Revision history for this message
Robert Collins (lifeless) wrote :

Oh, and as you're stacking on a fixture, useFixture is handy :)

    def setUp(self):
        super(UtilityFixture, self).setUp()
        components = self.useFixture(ComponentsFixture())
        components.registry.registerUtility(*self.args, **self.kwargs)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/zope_fixtures/__init__.py'
2--- lib/zope_fixtures/__init__.py 2011-11-22 12:31:11 +0000
3+++ lib/zope_fixtures/__init__.py 2011-11-23 09:17:39 +0000
4@@ -34,10 +34,11 @@
5 __version__ = (0, 0, 1, 'beta', 0)
6
7 __all__ = [
8- 'ZopeRegistryFixture',
9+ 'ComponentsFixture',
10+ 'UtilityFixture',
11 ]
12
13-from zope_fixtures.zoperegistry import ZopeRegistryFixture
14+from zope_fixtures.components import ComponentsFixture, UtilityFixture
15
16
17 def test_suite():
18
19=== renamed file 'lib/zope_fixtures/zoperegistry.py' => 'lib/zope_fixtures/components.py'
20--- lib/zope_fixtures/zoperegistry.py 2011-11-22 12:31:11 +0000
21+++ lib/zope_fixtures/components.py 2011-11-23 09:17:39 +0000
22@@ -14,7 +14,7 @@
23 # limitations under that license.
24
25 __all__ = [
26- 'ZopeRegistryFixture'
27+ 'ComponentsFixture'
28 ]
29
30 from fixtures import Fixture
31@@ -24,7 +24,7 @@
32 getSiteManager = try_import("zope.component.getSiteManager")
33
34
35-class ZopeRegistryFixture(Fixture):
36+class ComponentsFixture(Fixture):
37 """Overlay the global Zope registry so that tests don't change it.
38
39 :ivar registry: An alternate Zope registry that can be used to override
40@@ -37,15 +37,34 @@
41 class TestSomething(TestCase):
42
43 def test_registry(self):
44- self.useFixture(ZopeRegistryFixture())
45+ self.useFixture(ComponentsFixture())
46 getSiteManager().registerUtility(...)
47 # more code here
48 """
49
50 def setUp(self):
51- super(ZopeRegistryFixture, self).setUp()
52+ super(ComponentsFixture, self).setUp()
53 self.registry = Components(bases=(getSiteManager(),))
54 getSiteManager.sethook(lambda context=None: self.registry)
55 self.addCleanup(getSiteManager.reset)
56
57
58+class UtilityFixture(Fixture):
59+ """Convenience around ComponentsFixture to override a single utility."""
60+
61+ def __init__(self, *args, **kwargs):
62+ """Create an UtilityFixture.
63+
64+ The args and kwargs parameters are the same supported by
65+ registerUtility and will be passed to it.
66+ """
67+ super(UtilityFixture, self).__init__()
68+ self.args = args
69+ self.kwargs = kwargs
70+
71+ def setUp(self):
72+ super(UtilityFixture, self).setUp()
73+ components = ComponentsFixture()
74+ components.setUp()
75+ components.registry.registerUtility(*self.args, **self.kwargs)
76+ self.addCleanup(components.cleanUp)
77
78=== renamed file 'lib/zope_fixtures/tests/test_zoperegistry.py' => 'lib/zope_fixtures/tests/test_components.py'
79--- lib/zope_fixtures/tests/test_zoperegistry.py 2011-11-22 12:31:11 +0000
80+++ lib/zope_fixtures/tests/test_components.py 2011-11-23 09:17:39 +0000
81@@ -18,7 +18,7 @@
82 from zope.interface import Interface, implements
83 from zope.component import provideUtility, queryUtility, getSiteManager
84
85-from zope_fixtures import ZopeRegistryFixture
86+from zope_fixtures import ComponentsFixture, UtilityFixture
87
88
89 class ITestUtility(Interface):
90@@ -29,25 +29,25 @@
91 implements(ITestUtility)
92
93
94-class TestZopeRegistry(testtools.TestCase):
95+class TestComponentsFixture(testtools.TestCase):
96
97 def test_overlays_site_manager(self):
98 # A custom site manager is installed.
99- fixture = ZopeRegistryFixture()
100+ fixture = ComponentsFixture()
101 original_registry = getSiteManager()
102 self.useFixture(fixture)
103 self.assertIs(fixture.registry, getSiteManager())
104 self.assertIn(original_registry, fixture.registry.__bases__)
105
106 def test_restores_site_manager(self):
107- fixture = ZopeRegistryFixture()
108+ fixture = ComponentsFixture()
109 original_registry = getSiteManager()
110 with fixture:
111 self.assertIs(fixture.registry, getSiteManager())
112 self.assertIs(original_registry, getSiteManager())
113
114 def test_provide_utility_against_fixture_registry(self):
115- fixture = ZopeRegistryFixture()
116+ fixture = ComponentsFixture()
117 original_registry = getSiteManager()
118 self.useFixture(fixture)
119 utility = TestUtility()
120@@ -57,11 +57,34 @@
121 self.assertIs(None, original_registry.queryUtility(ITestUtility))
122
123 def test_restores_original_registry(self):
124- fixture = ZopeRegistryFixture()
125+ fixture = ComponentsFixture()
126+ original_registry = getSiteManager()
127 original_utility = TestUtility()
128 provideUtility(original_utility)
129+ self.addCleanup(original_registry.unregisterUtility, original_utility)
130 with fixture:
131 overridden_utility = TestUtility()
132 fixture.registry.registerUtility(overridden_utility)
133 self.assertIs(overridden_utility, queryUtility(ITestUtility))
134 self.assertIs(original_utility, queryUtility(ITestUtility))
135+
136+
137+class TestUtilityFixture(testtools.TestCase):
138+
139+ def test_provide_utility(self):
140+ utility = TestUtility()
141+ fixture = UtilityFixture(utility)
142+ self.useFixture(fixture)
143+ self.assertIs(utility, queryUtility(ITestUtility))
144+
145+ def test_restore_utility(self):
146+ utility = TestUtility()
147+ with UtilityFixture(utility):
148+ self.assertIs(utility, queryUtility(ITestUtility))
149+ self.assertIs(None, queryUtility(ITestUtility))
150+
151+ def test_arguments_passthrough(self):
152+ utility = TestUtility()
153+ fixture = UtilityFixture(utility, name="foo")
154+ self.useFixture(fixture)
155+ self.assertIs(utility, queryUtility(ITestUtility, name="foo"))

Subscribers

People subscribed via source and target branches