~openlp-core/openlp/trunk

« back to all changes in this revision

Viewing changes to tests/interfaces/openlp_plugins/songs/forms/test_authorsform.py

  • Committer: Tim Bentley
  • Author(s): raoul at snyman
  • Date: 2017-03-02 17:58:46 UTC
  • mfrom: (2723.2.2 off-by-one)
  • Revision ID: tim.bentley@gmail.com-20170302175846-4jbdgunpa5olhqoj
Fix bug #1666005 and bug #1668994

Add this to your merge proposal:
--------------------------------
lp:~raoul-snyman/openlp/off-by-one (revision 2725)
[SUCCESS] https://ci.openlp.io/job/Branch-01-Pull/1914/
[SUCCESS] https://ci.openlp.io/job/Branch-02-Functional-Tests/1825/
[SUCCESS] https://ci.openlp.io/job/Branch-03-Interface-Tests/1766/
[SUCCESS] https://ci.openlp.io/job/Branch-04a-Windows_Functional_Tests/1499/
[SUCCESS] https://ci.openlp.io/job/Branch-04b-Windows_Interface_Tests/1089/
[...

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
Package to test the openlp.plugins.songs.forms.authorsform package.
24
24
"""
25
25
from unittest import TestCase
 
26
from unittest.mock import patch
26
27
 
27
28
from PyQt5 import QtWidgets
28
29
 
138
139
 
139
140
        # THEN: The display_name_edit should have the correct value
140
141
        self.assertEqual(self.form.display_edit.text(), display_name, 'The display name should be set correctly')
 
142
 
 
143
    @patch('openlp.plugins.songs.forms.authorsform.QtWidgets.QDialog.exec')
 
144
    def test_exec(self, mocked_exec):
 
145
        """
 
146
        Test the exec() method
 
147
        """
 
148
        # GIVEN: An authors for and various mocked objects
 
149
        with patch.object(self.form.first_name_edit, 'clear') as mocked_first_name_edit_clear, \
 
150
                patch.object(self.form.last_name_edit, 'clear') as mocked_last_name_edit_clear, \
 
151
                patch.object(self.form.display_edit, 'clear') as mocked_display_edit_clear, \
 
152
                patch.object(self.form.first_name_edit, 'setFocus') as mocked_first_name_edit_setFocus:
 
153
            # WHEN: The exec() method is called
 
154
            self.form.exec(clear=True)
 
155
 
 
156
            # THEN: The clear and exec() methods should have been called
 
157
        mocked_first_name_edit_clear.assert_called_once_with()
 
158
        mocked_last_name_edit_clear.assert_called_once_with()
 
159
        mocked_display_edit_clear.assert_called_once_with()
 
160
        mocked_first_name_edit_setFocus.assert_called_once_with()
 
161
        mocked_exec.assert_called_once_with(self.form)
 
162
 
 
163
    def test_first_name_edited(self):
 
164
        """
 
165
        Test the on_first_name_edited() method
 
166
        """
 
167
        # GIVEN: An author form
 
168
        self.form.auto_display_name = True
 
169
 
 
170
        with patch.object(self.form.last_name_edit, 'text') as mocked_last_name_edit_text, \
 
171
                patch.object(self.form.display_edit, 'setText') as mocked_display_edit_setText:
 
172
            mocked_last_name_edit_text.return_value = 'Newton'
 
173
 
 
174
            # WHEN: on_first_name_edited() is called
 
175
            self.form.on_first_name_edited('John')
 
176
 
 
177
        # THEN: The display name should be updated
 
178
        assert mocked_last_name_edit_text.call_count == 2
 
179
        mocked_display_edit_setText.assert_called_once_with('John Newton')
 
180
 
 
181
    def test_first_name_edited_no_auto(self):
 
182
        """
 
183
        Test the on_first_name_edited() method without auto_display_name
 
184
        """
 
185
        # GIVEN: An author form
 
186
        self.form.auto_display_name = False
 
187
 
 
188
        with patch.object(self.form.last_name_edit, 'text') as mocked_last_name_edit_text, \
 
189
                patch.object(self.form.display_edit, 'setText') as mocked_display_edit_setText:
 
190
 
 
191
            # WHEN: on_first_name_edited() is called
 
192
            self.form.on_first_name_edited('John')
 
193
 
 
194
        # THEN: The display name should not be updated
 
195
        assert mocked_last_name_edit_text.call_count == 0
 
196
        assert mocked_display_edit_setText.call_count == 0
 
197
 
 
198
    def test_last_name_edited(self):
 
199
        """
 
200
        Test the on_last_name_edited() method
 
201
        """
 
202
        # GIVEN: An author form
 
203
        self.form.auto_display_name = True
 
204
 
 
205
        with patch.object(self.form.first_name_edit, 'text') as mocked_first_name_edit_text, \
 
206
                patch.object(self.form.display_edit, 'setText') as mocked_display_edit_setText:
 
207
            mocked_first_name_edit_text.return_value = 'John'
 
208
 
 
209
            # WHEN: on_last_name_edited() is called
 
210
            self.form.on_last_name_edited('Newton')
 
211
 
 
212
        # THEN: The display name should be updated
 
213
        assert mocked_first_name_edit_text.call_count == 2
 
214
        mocked_display_edit_setText.assert_called_once_with('John Newton')
 
215
 
 
216
    def test_last_name_edited_no_auto(self):
 
217
        """
 
218
        Test the on_last_name_edited() method without auto_display_name
 
219
        """
 
220
        # GIVEN: An author form
 
221
        self.form.auto_display_name = False
 
222
 
 
223
        with patch.object(self.form.first_name_edit, 'text') as mocked_first_name_edit_text, \
 
224
                patch.object(self.form.display_edit, 'setText') as mocked_display_edit_setText:
 
225
 
 
226
            # WHEN: on_last_name_edited() is called
 
227
            self.form.on_last_name_edited('Newton')
 
228
 
 
229
        # THEN: The display name should not be updated
 
230
        assert mocked_first_name_edit_text.call_count == 0
 
231
        assert mocked_display_edit_setText.call_count == 0
 
232
 
 
233
    @patch('openlp.plugins.songs.forms.authorsform.critical_error_message_box')
 
234
    def test_accept_no_first_name(self, mocked_critical_error):
 
235
        """
 
236
        Test the accept() method with no first name
 
237
        """
 
238
        # GIVEN: A form and no text in thefirst name edit
 
239
        with patch.object(self.form.first_name_edit, 'text') as mocked_first_name_edit_text, \
 
240
                patch.object(self.form.first_name_edit, 'setFocus') as mocked_first_name_edit_setFocus:
 
241
            mocked_first_name_edit_text.return_value = ''
 
242
 
 
243
            # WHEN: accept() is called
 
244
            result = self.form.accept()
 
245
 
 
246
        # THEN: The result should be false and a critical error displayed
 
247
        assert result is False
 
248
        mocked_critical_error.assert_called_once_with(message='You need to type in the first name of the author.')
 
249
        mocked_first_name_edit_text.assert_called_once_with()
 
250
        mocked_first_name_edit_setFocus.assert_called_once_with()
 
251
 
 
252
    @patch('openlp.plugins.songs.forms.authorsform.critical_error_message_box')
 
253
    def test_accept_no_last_name(self, mocked_critical_error):
 
254
        """
 
255
        Test the accept() method with no last name
 
256
        """
 
257
        # GIVEN: A form and no text in the last name edit
 
258
        with patch.object(self.form.first_name_edit, 'text') as mocked_first_name_edit_text, \
 
259
                patch.object(self.form.last_name_edit, 'text') as mocked_last_name_edit_text, \
 
260
                patch.object(self.form.last_name_edit, 'setFocus') as mocked_last_name_edit_setFocus:
 
261
            mocked_first_name_edit_text.return_value = 'John'
 
262
            mocked_last_name_edit_text.return_value = ''
 
263
 
 
264
            # WHEN: accept() is called
 
265
            result = self.form.accept()
 
266
 
 
267
        # THEN: The result should be false and a critical error displayed
 
268
        assert result is False
 
269
        mocked_critical_error.assert_called_once_with(message='You need to type in the last name of the author.')
 
270
        mocked_first_name_edit_text.assert_called_once_with()
 
271
        mocked_last_name_edit_text.assert_called_once_with()
 
272
        mocked_last_name_edit_setFocus.assert_called_once_with()
 
273
 
 
274
    @patch('openlp.plugins.songs.forms.authorsform.critical_error_message_box')
 
275
    def test_accept_no_display_name_no_combine(self, mocked_critical_error):
 
276
        """
 
277
        Test the accept() method with no display name and no combining
 
278
        """
 
279
        # GIVEN: A form and no text in the display name edit
 
280
        mocked_critical_error.return_value = QtWidgets.QMessageBox.No
 
281
        with patch.object(self.form.first_name_edit, 'text') as mocked_first_name_edit_text, \
 
282
                patch.object(self.form.last_name_edit, 'text') as mocked_last_name_edit_text, \
 
283
                patch.object(self.form.display_edit, 'text') as mocked_display_edit_text, \
 
284
                patch.object(self.form.display_edit, 'setFocus') as mocked_display_edit_setFocus:
 
285
            mocked_first_name_edit_text.return_value = 'John'
 
286
            mocked_last_name_edit_text.return_value = 'Newton'
 
287
            mocked_display_edit_text.return_value = ''
 
288
 
 
289
            # WHEN: accept() is called
 
290
            result = self.form.accept()
 
291
 
 
292
        # THEN: The result should be false and a critical error displayed
 
293
        assert result is False
 
294
        mocked_critical_error.assert_called_once_with(
 
295
            message='You have not set a display name for the author, combine the first and last names?',
 
296
            parent=self.form, question=True)
 
297
        mocked_first_name_edit_text.assert_called_once_with()
 
298
        mocked_last_name_edit_text.assert_called_once_with()
 
299
        mocked_display_edit_text.assert_called_once_with()
 
300
        mocked_display_edit_setFocus.assert_called_once_with()
 
301
 
 
302
    @patch('openlp.plugins.songs.forms.authorsform.critical_error_message_box')
 
303
    @patch('openlp.plugins.songs.forms.authorsform.QtWidgets.QDialog.accept')
 
304
    def test_accept_no_display_name(self, mocked_accept, mocked_critical_error):
 
305
        """
 
306
        Test the accept() method with no display name and auto-combine
 
307
        """
 
308
        # GIVEN: A form and no text in the display name edit
 
309
        mocked_accept.return_value = True
 
310
        mocked_critical_error.return_value = QtWidgets.QMessageBox.Yes
 
311
        with patch.object(self.form.first_name_edit, 'text') as mocked_first_name_edit_text, \
 
312
                patch.object(self.form.last_name_edit, 'text') as mocked_last_name_edit_text, \
 
313
                patch.object(self.form.display_edit, 'text') as mocked_display_edit_text, \
 
314
                patch.object(self.form.display_edit, 'setText') as mocked_display_edit_setText:
 
315
            mocked_first_name_edit_text.return_value = 'John'
 
316
            mocked_last_name_edit_text.return_value = 'Newton'
 
317
            mocked_display_edit_text.return_value = ''
 
318
 
 
319
            # WHEN: accept() is called
 
320
            result = self.form.accept()
 
321
 
 
322
        # THEN: The result should be false and a critical error displayed
 
323
        assert result is True
 
324
        mocked_critical_error.assert_called_once_with(
 
325
            message='You have not set a display name for the author, combine the first and last names?',
 
326
            parent=self.form, question=True)
 
327
        assert mocked_first_name_edit_text.call_count == 2
 
328
        assert mocked_last_name_edit_text.call_count == 2
 
329
        mocked_display_edit_text.assert_called_once_with()
 
330
        mocked_display_edit_setText.assert_called_once_with('John Newton')
 
331
        mocked_accept.assert_called_once_with(self.form)
 
332
 
 
333
    @patch('openlp.plugins.songs.forms.authorsform.QtWidgets.QDialog.accept')
 
334
    def test_accept(self, mocked_accept):
 
335
        """
 
336
        Test the accept() method
 
337
        """
 
338
        # GIVEN: A form and text in the right places
 
339
        mocked_accept.return_value = True
 
340
        with patch.object(self.form.first_name_edit, 'text') as mocked_first_name_edit_text, \
 
341
                patch.object(self.form.last_name_edit, 'text') as mocked_last_name_edit_text, \
 
342
                patch.object(self.form.display_edit, 'text') as mocked_display_edit_text:
 
343
            mocked_first_name_edit_text.return_value = 'John'
 
344
            mocked_last_name_edit_text.return_value = 'Newton'
 
345
            mocked_display_edit_text.return_value = 'John Newton'
 
346
 
 
347
            # WHEN: accept() is called
 
348
            result = self.form.accept()
 
349
 
 
350
        # THEN: The result should be false and a critical error displayed
 
351
        assert result is True
 
352
        mocked_first_name_edit_text.assert_called_once_with()
 
353
        mocked_last_name_edit_text.assert_called_once_with()
 
354
        mocked_display_edit_text.assert_called_once_with()
 
355
        mocked_accept.assert_called_once_with(self.form)