Merge lp:~verterok/txstatsd/use-mock-instead-of-mocker into lp:txstatsd

Proposed by Guillermo Gonzalez on 2013-03-07
Status: Merged
Approved by: Sidnei da Silva on 2013-03-07
Approved revision: 105
Merged at revision: 105
Proposed branch: lp:~verterok/txstatsd/use-mock-instead-of-mocker
Merge into: lp:txstatsd
Diff against target: 339 lines (+82/-98)
3 files modified
requirements.txt (+1/-1)
txstatsd/tests/test_client.py (+24/-28)
txstatsd/tests/test_process.py (+57/-69)
To merge this branch: bzr merge lp:~verterok/txstatsd/use-mock-instead-of-mocker
Reviewer Review Type Date Requested Status
Sidnei da Silva 2013-03-07 Approve on 2013-03-07
Review via email: mp+152206@code.launchpad.net

Commit message

Use mock instead of mocker in the tests

Description of the change

Use mock (python-mock) instead of mocker (python-mocker) as there is no py3k package for the later.

To post a comment you must log in.
Sidnei da Silva (sidnei) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'requirements.txt'
2--- requirements.txt 2012-05-08 16:23:00 +0000
3+++ requirements.txt 2013-03-07 15:53:38 +0000
4@@ -1,7 +1,7 @@
5 Twisted==11.1.0
6 carbon==0.9.9
7 whisper==0.9.9
8-mocker==1.1
9+mock==1.0
10 psutil==0.4.1
11 wsgiref==0.1.2
12 zope.interface==3.8.0
13
14=== modified file 'txstatsd/tests/test_client.py'
15--- txstatsd/tests/test_client.py 2012-12-27 20:49:06 +0000
16+++ txstatsd/tests/test_client.py 2013-03-07 15:53:38 +0000
17@@ -22,7 +22,7 @@
18
19 import sys
20
21-from mocker import Mocker, expect, ANY
22+from mock import Mock, call
23 from twisted.internet import reactor
24 from twisted.internet.defer import inlineCallbacks, Deferred
25 from twisted.python import log
26@@ -67,7 +67,6 @@
27 super(TestClient, self).setUp()
28 self.client = None
29 self.exception = None
30- self.mocker = Mocker()
31
32 def tearDown(self):
33 if self.client:
34@@ -250,11 +249,11 @@
35 self.client = TwistedStatsDClient('localhost', 8000)
36 self.build_protocol()
37
38- self.client.connect_callback = self.mocker.mock()
39- expect(self.client.connect_callback())
40+ self.client.connect_callback = Mock()
41
42- with self.mocker:
43- self.client.host_resolved('127.0.0.1')
44+ self.client.host_resolved('127.0.0.1')
45+ self.assertTrue(self.client.connect_callback.called)
46+ self.client.connect_callback.assert_called_once_with()
47
48 def test_sends_messages_to_gateway_after_host_resolves(self):
49 """After the host is resolved, send messages to the
50@@ -265,14 +264,12 @@
51
52 message = 'some data'
53 bytes_sent = len(message)
54- self.client.data_queue = self.mocker.mock(spec=DataQueue) # not called
55- self.client.transport_gateway = self.mocker.mock(spec=TransportGateway)
56- callback = self.mocker.mock()
57- expect(self.client.transport_gateway.write(message, callback)).result(
58- bytes_sent)
59-
60- with self.mocker:
61- self.assertEqual(self.client.write(message, callback), bytes_sent)
62+ self.client.data_queue = Mock(spec=DataQueue)
63+ self.client.transport_gateway = Mock(spec=TransportGateway)
64+ callback = Mock()
65+ self.client.transport_gateway.write.return_value = bytes_sent
66+ self.assertEqual(self.client.write(message, callback), bytes_sent)
67+ self.client.transport_gateway.write.assert_called_once_with(message, callback)
68
69 def test_sends_messages_to_queue_before_host_resolves(self):
70 """Before the host is resolved, send messages to the DataQueue."""
71@@ -280,13 +277,12 @@
72 self.build_protocol()
73
74 message = 'some data'
75- bytes_sent = len(message)
76- self.client.data_queue = self.mocker.mock(spec=DataQueue)
77- callback = self.mocker.mock()
78- expect(self.client.data_queue.write(message, callback)).result(None)
79-
80- with self.mocker:
81- self.assertEqual(self.client.write(message, callback), None)
82+ self.client.data_queue = Mock(spec=DataQueue)
83+ callback = Mock()
84+ self.client.data_queue.write.return_value = None
85+ result = self.client.write(message, callback)
86+ self.client.data_queue.write.assert_called_once_with(message, callback)
87+ self.assertEqual(result, None)
88
89 def test_flushes_queued_messages_to_the_gateway_when_host_resolves(self):
90 """As soon as the host is resolved, flush all messages to the
91@@ -298,14 +294,14 @@
92 self.client.data_queue.write('data 2', 'callback 2')
93 self.client.data_queue.write('data 3', 'callback 3')
94
95- mock_gateway_write = self.mocker.mock()
96+ mock_gateway_write = Mock()
97 self.patch(TransportGateway, 'write', mock_gateway_write)
98- expect(mock_gateway_write('data 1', 'callback 1'))
99- expect(mock_gateway_write('data 2', 'callback 2'))
100- expect(mock_gateway_write('data 3', 'callback 3'))
101-
102- with self.mocker:
103- self.client.host_resolved('127.0.0.1')
104+ self.client.host_resolved('127.0.0.1')
105+ self.assertTrue(mock_gateway_write.call_count, 3)
106+ expected = [call('data 1', 'callback 1'),
107+ call('data 2', 'callback 2'),
108+ call('data 3', 'callback 3')]
109+ self.assertEqual(mock_gateway_write.call_args_list, expected)
110
111 def test_sets_client_transport_when_connected(self):
112 """Set the transport as an attribute of the client."""
113
114=== modified file 'txstatsd/tests/test_process.py'
115--- txstatsd/tests/test_process.py 2012-06-28 17:29:26 +0000
116+++ txstatsd/tests/test_process.py 2013-03-07 15:53:38 +0000
117@@ -23,7 +23,7 @@
118 import psutil
119 import sys
120
121-from mocker import MockerTestCase
122+import mock
123 from twisted.trial.unittest import TestCase
124
125 from txstatsd.process import (
126@@ -82,7 +82,7 @@
127 tun0: 5138313 24837 0 0 0 0 0 0 5226635 26986 0 0 0 0 0 0"""
128
129
130-class TestSystemPerformance(TestCase, MockerTestCase):
131+class TestSystemPerformance(TestCase):
132 """Test system performance monitoring."""
133
134 def test_loadinfo(self):
135@@ -102,11 +102,10 @@
136 def test_cpu_counters(self):
137 """System cpu counters are collected through psutil."""
138 cpu_times = psutil.cpu_times()
139- mock = self.mocker.replace("psutil.cpu_times")
140- self.expect(mock(percpu=False)).result(cpu_times)
141- self.mocker.replay()
142-
143- result = report_system_stats()
144+ with mock.patch("psutil.cpu_times"):
145+ psutil.cpu_times.return_value = cpu_times
146+ result = report_system_stats()
147+ psutil.cpu_times.assert_called_once_with(percpu=False)
148 # cpu_times is platform-dependent
149 if sys.platform.lower().startswith("linux"):
150 self.assertEqual(cpu_times.user, result["sys.cpu.user"])
151@@ -132,11 +131,10 @@
152 def test_per_cpu_counters(self):
153 """System percpu counters are collected through psutil."""
154 cpu_times = psutil.cpu_times()
155- mock = self.mocker.replace("psutil.cpu_times")
156- self.expect(mock(percpu=True)).result([cpu_times, cpu_times])
157- self.mocker.replay()
158-
159- result = report_system_stats(percpu=True)
160+ with mock.patch("psutil.cpu_times"):
161+ psutil.cpu_times.return_value = [cpu_times, cpu_times]
162+ result = report_system_stats(percpu=True)
163+ psutil.cpu_times.assert_called_once_with(percpu=True)
164 # cpu_times is platform-dependent
165 if sys.platform.lower().startswith("linux"):
166 self.assertEqual(cpu_times.user, result["sys.cpu.000.user"])
167@@ -187,14 +185,12 @@
168 cpu_percent = process.get_cpu_percent()
169 memory_percent = process.get_memory_percent()
170
171- mock = self.mocker.mock()
172- self.expect(mock.get_memory_info()).result((vsize, rss))
173- self.expect(mock.get_cpu_percent()).result(cpu_percent)
174- self.expect(mock.get_memory_percent()).result(memory_percent)
175- self.expect(mock.get_num_threads).result(None)
176- self.mocker.replay()
177-
178- result = ProcessReport(process=mock).get_memory_and_cpu()
179+ proc = mock.Mock()
180+ proc.get_memory_info.return_value = (vsize, rss)
181+ proc.get_cpu_percent.return_value = cpu_percent
182+ proc.get_memory_percent.return_value = memory_percent
183+ proc.get_num_threads = None
184+ result = ProcessReport(process=proc).get_memory_and_cpu()
185 self.assertEqual(cpu_percent, result["proc.cpu.percent"])
186 self.assertEqual(vsize, result["proc.memory.vsize"])
187 self.assertEqual(rss, result["proc.memory.rss"])
188@@ -208,11 +204,10 @@
189 process = psutil.Process(os.getpid())
190 utime, stime = process.get_cpu_times()
191
192- mock = self.mocker.mock()
193- self.expect(mock.get_cpu_times()).result((utime, stime))
194- self.mocker.replay()
195-
196- result = ProcessReport(process=mock).get_cpu_counters()
197+ proc = mock.Mock()
198+ proc.get_cpu_times.return_value = (utime, stime)
199+ result = ProcessReport(process=proc).get_cpu_counters()
200+ proc.get_cpu_times.assert_called_once_with()
201 self.assertEqual(utime, result["proc.cpu.user"])
202 self.assertEqual(stime, result["proc.cpu.system"])
203
204@@ -229,14 +224,18 @@
205 cpu_percent = process.get_cpu_percent()
206 memory_percent = process.get_memory_percent()
207
208- mock = self.mocker.mock()
209- self.expect(mock.get_memory_info()).result((vsize, rss))
210- self.expect(mock.get_cpu_percent()).result(cpu_percent)
211- self.expect(mock.get_memory_percent()).result(memory_percent)
212- self.expect(mock.get_num_threads()).result(1)
213- self.mocker.replay()
214-
215- result = ProcessReport(process=mock).get_memory_and_cpu()
216+ proc = mock.Mock()
217+ proc.get_memory_info.return_value = (vsize, rss)
218+ proc.get_cpu_percent.return_value = cpu_percent
219+ proc.get_memory_percent.return_value = memory_percent
220+ proc.get_num_threads.return_value = 1
221+ result = ProcessReport(process=proc).get_memory_and_cpu()
222+ proc.get_memory_info.assert_called_once_with()
223+ proc.get_cpu_percent.assert_called_once_with()
224+ proc.get_memory_percent.assert_called_once_with()
225+ proc.get_num_threads.assert_called_once_with()
226+
227+
228 self.assertEqual(cpu_percent, result["proc.cpu.percent"])
229 self.assertEqual(vsize, result["proc.memory.vsize"])
230 self.assertEqual(rss, result["proc.memory.rss"])
231@@ -245,13 +244,11 @@
232
233 def test_ioinfo(self):
234 """Process IO info is collected through psutil."""
235- mock = self.mocker.mock()
236- self.expect(mock.get_io_counters).result(None)
237- self.mocker.replay()
238-
239 # If the version of psutil doesn't have the C{get_io_counters},
240 # then io stats are not included in the output.
241- result = ProcessReport(process=mock).get_io_counters()
242+ proc = mock.Mock()
243+ proc.get_io_counters = None
244+ result = ProcessReport(process=proc).get_io_counters()
245 self.failIf("proc.io.read.count" in result)
246 self.failIf("proc.io.write.count" in result)
247 self.failIf("proc.io.read.bytes" in result)
248@@ -266,12 +263,10 @@
249 """
250 io_counters = (10, 42, 125, 16)
251
252- mock = self.mocker.mock()
253- self.expect(mock.get_io_counters).result(mock)
254- self.expect(mock.get_io_counters()).result(io_counters)
255- self.mocker.replay()
256-
257- result = ProcessReport(process=mock).get_io_counters()
258+ proc = mock.Mock()
259+ proc.get_io_counters.return_value = io_counters
260+ result = ProcessReport(process=proc).get_io_counters()
261+ proc.get_io_counters.assert_called_once_with()
262 self.assertEqual(10, result["proc.io.read.count"])
263 self.assertEqual(42, result["proc.io.write.count"])
264 self.assertEqual(125, result["proc.io.read.bytes"])
265@@ -284,13 +279,11 @@
266 If the version of psutil doesn't implement C{get_connections} for
267 L{Process}, then no information is returned.
268 """
269- mock = self.mocker.mock()
270- self.expect(mock.get_connections).result(None)
271- self.mocker.replay()
272-
273 # If the version of psutil doesn't have the C{get_io_counters},
274 # then io stats are not included in the output.
275- result = ProcessReport(process=mock).get_net_stats()
276+ proc = mock.Mock()
277+ proc.get_connections = None
278+ result = ProcessReport(process=proc).get_net_stats()
279 self.failIf("proc.net.status.established" in result)
280
281 def test_netinfo_with_get_connections(self):
282@@ -311,37 +304,32 @@
283 ("72.14.234.83", 443), "SYN_SENT")
284 ]
285
286- mock = self.mocker.mock()
287- self.expect(mock.get_connections).result(mock)
288- self.expect(mock.get_connections()).result(connections)
289- self.mocker.replay()
290-
291- result = ProcessReport(process=mock).get_net_stats()
292+ proc = mock.Mock()
293+ proc.get_connections.return_value = connections
294+ result = ProcessReport(process=proc).get_net_stats()
295+ proc.get_connections.assert_called_once_with()
296 self.assertEqual(2, result["proc.net.status.established"])
297 self.assertEqual(1, result["proc.net.status.closing"])
298 self.assertEqual(1, result["proc.net.status.syn_sent"])
299
300 def test_reactor_stats(self):
301 """Given a twisted reactor, pull out some stats from it."""
302- mock = self.mocker.mock()
303- self.expect(mock.getReaders()).result([None, None, None])
304- self.expect(mock.getWriters()).result([None, None])
305- self.mocker.replay()
306-
307- result = report_reactor_stats(mock)()
308+ mock_reactor = mock.Mock()
309+ mock_reactor.getReaders.return_value = [None, None, None]
310+ mock_reactor.getWriters.return_value = [None, None]
311+ result = report_reactor_stats(mock_reactor)()
312 self.assertEqual(3, result["reactor.readers"])
313 self.assertEqual(2, result["reactor.writers"])
314
315 def test_threadpool_stats(self):
316 """Given a twisted threadpool, pull out some stats from it."""
317- mock = self.mocker.mock()
318- self.expect(mock.q.qsize()).result(42)
319- self.expect(mock.threads).result(6 * [None])
320- self.expect(mock.waiters).result(2 * [None])
321- self.expect(mock.working).result(4 * [None])
322- self.mocker.replay()
323+ mock_reactor = mock.Mock()
324+ mock_reactor.q.qsize.return_value = 42
325+ mock_reactor.threads = 6 * [None]
326+ mock_reactor.waiters = 2 * [None]
327+ mock_reactor.working = 4 * [None]
328
329- result = report_threadpool_stats(mock)()
330+ result = report_threadpool_stats(mock_reactor)()
331 self.assertEqual(42, result["threadpool.queue"])
332 self.assertEqual(6, result["threadpool.threads"])
333 self.assertEqual(2, result["threadpool.waiters"])
334@@ -385,5 +373,5 @@
335 self.assertEqual({"foo": 4}, wrapped())
336 self.assertEqual({"foo": 5}, wrapped())
337 self.assertEqual({"foo": 7}, wrapped())
338-
339+
340

Subscribers

People subscribed via source and target branches