Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import datetime 

2import unittest 

3from unittest import mock 

4from pi3bar.plugins.base import Plugin 

5 

6 

7class PluginInitTestCase(unittest.TestCase): 

8 """Instantiate ``Plugin``.""" 

9 def test_default_values(self): 

10 p = Plugin() 

11 self.assertEqual('Plugin', p.full_text) 

12 self.assertIsNone(p.short_text) 

13 self.assertIsNone(p.color) 

14 self.assertIsNone(p.background) 

15 self.assertIsNone(p.min_width) 

16 self.assertIsNone(p.align) 

17 self.assertIsNone(p.instance) 

18 self.assertIsNone(p.urgent) 

19 self.assertIsNone(p.separator) 

20 self.assertIsNone(p.separator_block_width) 

21 self.assertEqual(60, p.ticks) 

22 self.assertIsNone(p.timer) 

23 self.assertDictEqual({}, p.click_commands) 

24 

25 def test_kwargs(self): 

26 p = Plugin( 

27 full_text='TestPlugin', 

28 short_text='TP', 

29 color='#00ff00', 

30 background='#111111', 

31 min_width=200, 

32 align='right', 

33 instance='first', 

34 urgent=False, 

35 separator=True, 

36 separator_block_width=9, 

37 ticks=120, 

38 on_click={ 

39 1: 'xterm', 

40 3: 'network-manager' 

41 } 

42 ) 

43 self.assertEqual('TestPlugin', p.full_text) 

44 self.assertEqual('TP', p.short_text) 

45 self.assertEqual('#00ff00', p.color) 

46 self.assertEqual('#111111', p.background) 

47 self.assertEqual(200, p.min_width) 

48 self.assertEqual('right', p.align) 

49 self.assertEqual('first', p.instance) 

50 self.assertFalse(p.urgent) 

51 self.assertTrue(p.separator) 

52 self.assertEqual(9, p.separator_block_width) 

53 self.assertEqual(120, p.ticks) 

54 self.assertDictEqual( 

55 { 

56 1: 'xterm', 

57 3: 'network-manager' 

58 }, 

59 p.click_commands 

60 ) 

61 

62 def test_unknown_kwarg_raises_attribute_error(self): 

63 with self.assertRaises(AttributeError) as e: 

64 Plugin(test=True) 

65 self.assertEqual('Got invalid kwarg(s): {\'test\': True}', str(e.exception)) 

66 

67 

68class PluginComparisonTestCase(unittest.TestCase): 

69 def setUp(self): 

70 self.p1 = Plugin() 

71 self.p2 = Plugin() 

72 self.p3 = Plugin(instance='other') 

73 

74 def test_eq(self): 

75 self.assertTrue(self.p1 == self.p2) 

76 self.assertFalse(self.p1 == self.p3) 

77 

78 def test_ne(self): 

79 self.assertTrue(self.p1 != self.p3) 

80 self.assertFalse(self.p1 != self.p2) 

81 

82 

83class PluginCycleTestCase(unittest.TestCase): 

84 def test_cycle_passes(self): 

85 p = Plugin() 

86 p.cycle() 

87 

88 

89@mock.patch('pi3bar.plugins.base.threading') 

90@mock.patch.object(Plugin, 'debug') 

91class PluginTickTestCase(unittest.TestCase): 

92 @mock.patch.object(Plugin, 'cycle', return_value=None) 

93 def test_cycle_is_called(self, mock_cycle, mock_debug, mock_threading): 

94 plugin = Plugin() 

95 plugin.run() 

96 mock_cycle.assert_called_once_with() 

97 

98 def test_sets_timer(self, mock_debug, mock_threading): 

99 plugin = Plugin() 

100 

101 with mock.patch.object(Plugin, 'cycle'): 

102 plugin.run() 

103 mock_threading.Timer.assert_called_once_with(60, plugin.run) 

104 

105 def test_rerun_cancel(self, mock_debug, mock_threading): 

106 plugin = Plugin() 

107 _timer_mock = mock.MagicMock() 

108 plugin.timer = _timer_mock 

109 

110 with mock.patch.object(Plugin, 'run') as mock_run: 

111 plugin.rerun() 

112 

113 self.assertIsNone(plugin.timer) 

114 mock_run.assert_called_once_with() 

115 

116 def test_rerun(self, mock_debug, mock_threading): 

117 plugin = Plugin() 

118 plugin.timer = None 

119 

120 with mock.patch.object(Plugin, 'run') as mock_run: 

121 plugin.rerun() 

122 

123 mock_run.assert_called_once_with() 

124 

125 @mock.patch.object(Plugin, 'cycle', side_effect=ValueError) 

126 def test_cycle_exception_is_logged(self, mock_cycle, mock_debug, mock_threading): 

127 plugin = Plugin() 

128 plugin.logger = mock.MagicMock() 

129 plugin.run() 

130 plugin.logger.error.assert_called_once_with('[%s] Exception in cycle:' % plugin.name, exc_info=1) 

131 

132 @mock.patch('pi3bar.plugins.base.datetime') 

133 def test_cycle_duration_debug(self, mock_datetime, mock_debug, mock_threading): 

134 d1 = datetime.datetime(2014, 1, 1) 

135 d2 = datetime.datetime(2014, 1, 1, 4, 3, 2, 1) 

136 

137 def now_side_effect(): 

138 mock_datetime.datetime.now.side_effect = lambda: d2 # second call 

139 return d1 # first call 

140 mock_datetime.datetime.now.side_effect = now_side_effect 

141 

142 plugin = Plugin() 

143 plugin.run() 

144 

145 self.assertEqual(2, mock_datetime.datetime.now.call_count) 

146 mock_debug.assert_called_once_with('cycle took 04:03:02:000001.') 

147 

148 @mock.patch.object(Plugin, 'error') 

149 def test_cycle_duration_warning(self, mock_error, mock_debug, mock_threading): 

150 plugin = Plugin() 

151 plugin.timer = True 

152 plugin.run() 

153 mock_error.assert_called_once_with('cycle took longer than ticks. Cancelled.') 

154 

155 

156@mock.patch('subprocess.Popen') 

157class PluginOnClickTestCase(unittest.TestCase): 

158 def test_unknown_command(self, mock_subprocess): 

159 plugin = Plugin() 

160 plugin.on_click(1, 1919, 1079) 

161 self.assertEqual(0, mock_subprocess.Popen.call_count) 

162 

163 def test_command(self, mock_subprocess): 

164 plugin = Plugin(on_click={ 

165 1: 'xterm -hold -e echo Hallo' 

166 }) 

167 plugin.on_click(1, 1919, 1079) 

168 from subprocess import PIPE 

169 mock_subprocess.assert_called_once_with(['xterm', '-hold', '-e', 'echo', 'Hallo'], 

170 stdout=PIPE, 

171 stdin=PIPE, 

172 stderr=PIPE) 

173 

174 

175class PluginGetOutputTestCase(unittest.TestCase): 

176 def test_none_attributes_are_not_assigned(self): 

177 plugin = Plugin(full_text='TestPlugin') 

178 output = plugin.get_output() 

179 self.assertDictEqual( 

180 { 

181 'name': 'Plugin', 

182 'full_text': 'TestPlugin' 

183 }, 

184 output 

185 ) 

186 

187 def test_all_attributes(self): 

188 plugin = Plugin( 

189 full_text='TestPlugin', 

190 short_text='TP', 

191 color='#00ff00', 

192 background='#111111', 

193 min_width=200, 

194 align='right', 

195 instance='first', 

196 urgent=False, 

197 separator=True, 

198 separator_block_width=9, 

199 ticks=120, 

200 on_click={ 

201 1: 'xterm', 

202 3: 'network-manager' 

203 } 

204 ) 

205 output = plugin.get_output() 

206 self.assertDictEqual( 

207 { 

208 'name': 'Plugin', 

209 'full_text': 'TestPlugin', 

210 'short_text': 'TP', 

211 'color': '#00ff00', 

212 'background': '#111111', 

213 'min_width': 200, 

214 'align': 'right', 

215 'instance': 'first', 

216 'urgent': False, 

217 'separator': True, 

218 'separator_block_width': 9 

219 }, 

220 output 

221 ) 

222 

223 

224class PluginFormatLogTestCase(unittest.TestCase): 

225 def test_format_log(self): 

226 plugin = Plugin() 

227 formatted = plugin._format_log(['test']) 

228 self.assertEqual('[Plugin] test', formatted) 

229 

230 def test_args_are_joined(self): 

231 plugin = Plugin() 

232 formatted = plugin._format_log(['test', '1', '2', '3']) 

233 self.assertEqual('[Plugin] test 1 2 3', formatted) 

234 

235 def test_args_to_string(self): 

236 plugin = Plugin() 

237 some_var = 5 

238 formatted = plugin._format_log([None, some_var]) 

239 self.assertEqual('[Plugin] None 5', formatted) 

240 

241 def test_with_instance(self): 

242 plugin = Plugin(instance='B') 

243 formatted = plugin._format_log(['test']) 

244 self.assertEqual('[Plugin] [B] test', formatted) 

245 

246 

247class PluginLoggingTestCase(unittest.TestCase): 

248 def setUp(self): 

249 self.plugin = Plugin() 

250 self.plugin.logger = mock.MagicMock() 

251 

252 def test_info(self): 

253 self.plugin.info('test') 

254 self.plugin.logger.info.assert_called_once_with('[Plugin] test') 

255 

256 def test_debug(self): 

257 self.plugin.debug('test') 

258 self.plugin.logger.debug.assert_called_once_with('[Plugin] test') 

259 

260 def test_error(self): 

261 self.plugin.error('test') 

262 self.plugin.logger.error.assert_called_once_with('[Plugin] test') 

263 

264 def test_warning(self): 

265 self.plugin.warning('test') 

266 self.plugin.logger.warning.assert_called_once_with('[Plugin] test') 

267 

268 def test_kwargs_are_passed_to_logger(self): 

269 self.plugin.error('test', exc_info=1) 

270 self.plugin.logger.error.assert_called_once_with('[Plugin] test', exc_info=1)