Merge lp:~gandelman-a/charm-helpers/test_helpers into lp:charm-helpers

Proposed by Adam Gandelman
Status: Merged
Merged at revision: 80
Proposed branch: lp:~gandelman-a/charm-helpers/test_helpers
Merge into: lp:charm-helpers
Diff against target: 179 lines (+10/-92)
4 files modified
tests/contrib/network/test_ovs.py (+3/-19)
tests/contrib/openstack/test_os_contexts.py (+2/-19)
tests/core/test_host.py (+4/-35)
tests/fetch/test_fetch.py (+1/-19)
To merge this branch: bzr merge lp:~gandelman-a/charm-helpers/test_helpers
Reviewer Review Type Date Requested Status
James Page Approve
Review via email: mp+187074@code.launchpad.net

Description of the change

Removes the redefinition of patch_open() and mock_open() across tests and uses those defined in tests/helpers.py

To post a comment you must log in.
Revision history for this message
James Page (james-page) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/contrib/network/test_ovs.py'
2--- tests/contrib/network/test_ovs.py 2013-07-01 09:53:08 +0000
3+++ tests/contrib/network/test_ovs.py 2013-09-23 17:30:00 +0000
4@@ -1,27 +1,11 @@
5-from contextlib import contextmanager
6-from mock import patch, call, MagicMock
7+from mock import patch, call
8 from testtools import TestCase
9
10+from tests.helpers import patch_open
11+
12 import charmhelpers.contrib.network.ovs as ovs
13
14
15-@contextmanager
16-def patch_open():
17- '''Patch open() to allow mocking both open() itself and the file that is
18- yielded.
19-
20- Yields the mock for "open" and "file", respectively.'''
21- mock_open = MagicMock(spec=open)
22- mock_file = MagicMock(spec=file)
23-
24- @contextmanager
25- def stub_open(*args, **kwargs):
26- mock_open(*args, **kwargs)
27- yield mock_file
28-
29- with patch('__builtin__.open', stub_open):
30- yield mock_open, mock_file
31-
32 GOOD_CERT = '''Certificate:
33 Data:
34 Version: 1 (0x0)
35
36=== modified file 'tests/contrib/openstack/test_os_contexts.py'
37--- tests/contrib/openstack/test_os_contexts.py 2013-07-19 23:29:59 +0000
38+++ tests/contrib/openstack/test_os_contexts.py 2013-09-23 17:30:00 +0000
39@@ -1,31 +1,14 @@
40 import unittest
41
42+from tests.helpers import patch_open
43+
44 from mock import patch, MagicMock, call
45
46-from contextlib import contextmanager
47 from copy import copy
48
49 import charmhelpers.contrib.openstack.context as context
50
51
52-@contextmanager
53-def patch_open():
54- '''Patch open() to allow mocking both open() itself and the file that is
55- yielded.
56-
57- Yields the mock for "open" and "file", respectively.'''
58- mock_open = MagicMock(spec=open)
59- mock_file = MagicMock(spec=file)
60-
61- @contextmanager
62- def stub_open(*args, **kwargs):
63- mock_open(*args, **kwargs)
64- yield mock_file
65-
66- with patch('__builtin__.open', stub_open):
67- yield mock_open, mock_file
68-
69-
70 class FakeRelation(object):
71 '''
72 A fake relation class. Lets tests specify simple relation data
73
74=== modified file 'tests/core/test_host.py'
75--- tests/core/test_host.py 2013-09-23 10:50:53 +0000
76+++ tests/core/test_host.py 2013-09-23 17:30:00 +0000
77@@ -1,10 +1,10 @@
78 from collections import OrderedDict
79-from contextlib import contextmanager
80 import subprocess
81-import io
82
83-from mock import patch, call, MagicMock
84+from mock import patch, call
85 from testtools import TestCase
86+from tests.helpers import patch_open
87+from tests.helpers import mock_open as mocked_open
88
89 from charmhelpers.core import host
90
91@@ -25,36 +25,6 @@
92 '''
93
94
95-@contextmanager
96-def patch_open():
97- '''Patch open() to allow mocking both open() itself and the file that is
98- yielded.
99-
100- Yields the mock for "open" and "file", respectively.'''
101- mock_open = MagicMock(spec=open)
102- mock_file = MagicMock(spec=file)
103-
104- @contextmanager
105- def stub_open(*args, **kwargs):
106- mock_open(*args, **kwargs)
107- yield mock_file
108-
109- with patch('__builtin__.open', stub_open):
110- yield mock_open, mock_file
111-
112-
113-@contextmanager
114-def mock_open(filename, contents=None):
115- ''' Slightly simpler mock of open to return contents for filename '''
116- def mock_file(*args):
117- if args[0] == filename:
118- return io.StringIO(contents)
119- else:
120- return open(*args)
121- with patch('__builtin__.open', mock_file):
122- yield
123-
124-
125 class HelpersTest(TestCase):
126 @patch('subprocess.call')
127 def test_runs_service_action(self, mock_call):
128@@ -130,7 +100,6 @@
129
130 service.assert_called_with('reload', service_name)
131
132-
133 @patch.object(host, 'service')
134 def test_start_a_service_fails(self, service):
135 service_name = 'foo-service'
136@@ -661,7 +630,7 @@
137 "DISTRIB_DESCRIPTION": "\"Ubuntu Saucy Salamander "
138 "(development branch)\""
139 }
140- with mock_open('/etc/lsb-release', LSB_RELEASE):
141+ with mocked_open('/etc/lsb-release', LSB_RELEASE):
142 lsb_release = host.lsb_release()
143 for key in result:
144 print lsb_release
145
146=== modified file 'tests/fetch/test_fetch.py'
147--- tests/fetch/test_fetch.py 2013-08-21 15:45:53 +0000
148+++ tests/fetch/test_fetch.py 2013-09-23 17:30:00 +0000
149@@ -1,4 +1,4 @@
150-from contextlib import contextmanager
151+from tests.helpers import patch_open
152 from testtools import TestCase
153 from mock import (
154 patch,
155@@ -36,24 +36,6 @@
156 return cache
157
158
159-@contextmanager
160-def patch_open():
161- '''Patch open() to allow mocking both open() itself and the file that is
162- yielded.
163-
164- Yields the mock for "open" and "file", respectively.'''
165- mock_open = MagicMock(spec=open)
166- mock_file = MagicMock(spec=file)
167-
168- @contextmanager
169- def stub_open(*args, **kwargs):
170- mock_open(*args, **kwargs)
171- yield mock_file
172-
173- with patch('__builtin__.open', stub_open):
174- yield mock_open, mock_file
175-
176-
177 class FetchTest(TestCase):
178 @patch('apt_pkg.Cache')
179 def test_filter_packages_missing(self, cache):

Subscribers

People subscribed via source and target branches