def test_display_results_cclinumber(self): """ Test displaying song search results sorted by CCLI number with basic song """ # GIVEN: Search results sorted by CCLI number, plus a mocked QtListWidgetItem with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \ patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole: mock_search_results = [] mock_song = MagicMock() mock_song_temp = MagicMock() mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.ccli_number = '12345' mock_song.temporary = False mock_song_temp.id = 2 mock_song_temp.title = 'My Temporary' mock_song_temp.sort_key = 'My Temporary' mock_song_temp.ccli_number = '12346' mock_song_temp.temporary = True mock_search_results.append(mock_song) mock_search_results.append(mock_song_temp) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget # WHEN: I display song search results sorted by CCLI number self.media_item.display_results_cclinumber(mock_search_results) # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() MockedQListWidgetItem.assert_called_once_with('12345 (My Song)') mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id) self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget)
def test_display_results_cclinumber(self): """ Test displaying song search results sorted by CCLI number with basic song """ # GIVEN: Search results sorted by CCLI number, plus a mocked QtListWidgetItem with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \ patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole: mock_search_results = [] mock_song = MagicMock() mock_song_temp = MagicMock() mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.ccli_number = '12345' mock_song.temporary = False mock_song_temp.id = 2 mock_song_temp.title = 'My Temporary' mock_song_temp.sort_key = 'My Temporary' mock_song_temp.ccli_number = '12346' mock_song_temp.temporary = True mock_search_results.append(mock_song) mock_search_results.append(mock_song_temp) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget # WHEN: I display song search results sorted by CCLI number self.media_item.display_results_cclinumber(mock_search_results) # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() MockedQListWidgetItem.assert_called_once_with('12345 (My Song)') mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id) self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget)
def test_load_objects(self, mocked_set_case_insensitive_completer): """ Test the _load_objects() method """ # GIVEN: A song edit form and some mocked stuff mocked_class = MagicMock() mocked_class.name = 'Author' mocked_combo = MagicMock() mocked_combo.count.return_value = 0 mocked_cache = MagicMock() mocked_object = MagicMock() mocked_object.name = 'Charles' mocked_object.id = 1 mocked_manager = MagicMock() mocked_manager.get_all_objects.return_value = [mocked_object] self.edit_song_form.manager = mocked_manager # WHEN: _load_objects() is called self.edit_song_form._load_objects(mocked_class, mocked_combo, mocked_cache) # THEN: All the correct methods should have been called self.edit_song_form.manager.get_all_objects.assert_called_once_with(mocked_class) mocked_combo.clear.assert_called_once_with() mocked_combo.count.assert_called_once_with() mocked_combo.addItem.assert_called_once_with('Charles') mocked_cache.append.assert_called_once_with('Charles') mocked_combo.setItemData.assert_called_once_with(0, 1) mocked_set_case_insensitive_completer.assert_called_once_with(mocked_cache, mocked_combo) mocked_combo.setCurrentIndex.assert_called_once_with(-1) mocked_combo.setCurrentText.assert_called_once_with('')
def test_display_results_song(self): """ Test displaying song search results with basic song """ # GIVEN: Search results, plus a mocked QtListWidgetItem with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \ patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole: mock_search_results = [] mock_song = MagicMock() mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.authors = [] mock_song_temp = MagicMock() mock_song_temp.id = 2 mock_song_temp.title = 'My Temporary' mock_song_temp.sort_key = 'My Temporary' mock_song_temp.authors = [] mock_author = MagicMock() mock_author.display_name = 'My Author' mock_song.authors.append(mock_author) mock_song_temp.authors.append(mock_author) mock_song.temporary = False mock_song_temp.temporary = True mock_search_results.append(mock_song) mock_search_results.append(mock_song_temp) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget self.media_item.auto_select_id = 1 # WHEN: I display song search results self.media_item.display_results_song(mock_search_results) # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() self.media_item.save_auto_select_id.assert_called_with() MockedQListWidgetItem.assert_called_once_with('My Song (My Author)') mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id) self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget) self.media_item.list_view.setCurrentItem.assert_called_with(mock_qlist_widget)
def test_display_results_song(self): """ Test displaying song search results with basic song """ # GIVEN: Search results, plus a mocked QtListWidgetItem with patch('openlp.core.lib.QtWidgets.QListWidgetItem') as MockedQListWidgetItem, \ patch('openlp.core.lib.QtCore.Qt.UserRole') as MockedUserRole: mock_search_results = [] mock_song = MagicMock() mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' mock_song.authors = [] mock_song_temp = MagicMock() mock_song_temp.id = 2 mock_song_temp.title = 'My Temporary' mock_song_temp.sort_key = 'My Temporary' mock_song_temp.authors = [] mock_author = MagicMock() mock_author.display_name = 'My Author' mock_song.authors.append(mock_author) mock_song_temp.authors.append(mock_author) mock_song.temporary = False mock_song_temp.temporary = True mock_search_results.append(mock_song) mock_search_results.append(mock_song_temp) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget self.media_item.auto_select_id = 1 # WHEN: I display song search results self.media_item.display_results_song(mock_search_results) # THEN: The current list view is cleared, the widget is created, and the relevant attributes set self.media_item.list_view.clear.assert_called_with() self.media_item.save_auto_select_id.assert_called_with() MockedQListWidgetItem.assert_called_once_with('My Song (My Author)') mock_qlist_widget.setData.assert_called_once_with(MockedUserRole, mock_song.id) self.media_item.list_view.addItem.assert_called_once_with(mock_qlist_widget) self.media_item.list_view.setCurrentItem.assert_called_with(mock_qlist_widget)
def test_build_remote_search(self): """ Test results for the remote search api """ # GIVEN: A Song and a search a JSON array should be returned. mock_song = MagicMock() mock_song.id = 123 mock_song.title = 'My Song' mock_song.search_title = 'My Song' mock_song.alternate_title = 'My alternative' self.media_item.search_entire = MagicMock() self.media_item.search_entire.return_value = [mock_song] # WHEN: I process a search search_results = self.media_item.search('My Song', False) # THEN: The correct formatted results are returned self.assertEqual(search_results, [[123, 'My Song', 'My alternative']])
def test_build_remote_search(self): """ Test results for the remote search api """ # GIVEN: A Song and a search a JSON array should be returned. mock_song = MagicMock() mock_song.id = 123 mock_song.title = 'My Song' mock_song.search_title = 'My Song' mock_song.alternate_title = 'My alternative' self.media_item.search_entire = MagicMock() self.media_item.search_entire.return_value = [mock_song] # WHEN: I process a search search_results = self.media_item.search('My Song', False) # THEN: The correct formatted results are returned self.assertEqual(search_results, [[123, 'My Song', 'My alternative']])
def test_process_verse_no_text(self): """ Test the process_verse() method when there's no text """ # GIVEN: An importer and a mocked method importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') mocked_db_book = MagicMock() mocked_db_book.id = 1 chapter_number = 1 verse_number = 1 verse_text = '' # WHEN: process_verse() is called with patch.object(importer, 'create_verse') as mocked_create_verse: importer.process_verse(mocked_db_book, chapter_number, verse_number, verse_text) # THEN: The create_verse() method should NOT have been called assert mocked_create_verse.call_count == 0
def test_process_verse(self): """ Test the process_verse() method """ # GIVEN: An importer and a mocked method importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') mocked_db_book = MagicMock() mocked_db_book.id = 1 chapter_number = 1 verse_number = 1 verse_text = ' In the beginning, God created the heavens and the earth ' # WHEN: process_verse() is called with patch.object(importer, 'create_verse') as mocked_create_verse: importer.process_verse(mocked_db_book, chapter_number, verse_number, verse_text) # THEN: The create_verse() method should have been called mocked_create_verse.assert_called_once_with(1, 1, 1, 'In the beginning, God created the heavens and the earth')
def process_verse_success_test(self): """ Test process_verse when called with an element with text set """ # GIVEN: An instance of OSISBible, and some mocked test data test_book = MagicMock() test_book.id = 1 test_verse = MagicMock() test_verse.tail = '\n ' # Whitespace test_verse.text = 'Verse Text' test_verse.get.side_effect = lambda x: { 'osisID': '1.2.4', 'sID': '999' }.get(x) importer = OSISBible(MagicMock(), path='.', name='.', filename='') # WHEN: Calling process_verse with the test data importer.process_verse(test_book, 2, test_verse) # THEN: create_verse should have been called with the test data self.mocked_create_verse.assert_called_once_with(1, 2, 4, 'Verse Text')
def process_verse_no_text_test(self): """ Test process_verse when called with an empty verse element """ # GIVEN: An instance of OSISBible, and some mocked test data test_book = MagicMock() test_book.id = 1 test_verse = MagicMock() test_verse.tail = '\n ' # Whitespace test_verse.text = None test_verse.get.side_effect = lambda x: { 'osisID': '1.2.4', 'sID': '999' }.get(x) importer = OSISBible(MagicMock(), path='.', name='.', filename='') # WHEN: Calling process_verse with the test data importer.process_verse(test_book, 2, test_verse) # THEN: create_verse should not have been called self.assertFalse(self.mocked_create_verse.called)
def process_verses_completes_test(self): """ Test process_verses when it completes """ with patch('openlp.plugins.bibles.lib.importers.opensong.get_text', **{'side_effect': ['Verse1 Text', 'Verse2 Text']}) as mocked_get_text, \ patch.object(OpenSongBible, 'parse_verse_number', **{'side_effect': [1, 2]}) as mocked_parse_verse_number: # GIVEN: An instance of OpenSongBible importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer.wizard = MagicMock() # WHEN: called with some valid data book = MagicMock() book.id = 1 verse1 = MagicMock() verse1.attrib = {'n': '1'} verse1.c = 'Chapter1' verse1.v = ['Chapter1 Verses'] verse2 = MagicMock() verse2.attrib = {'n': '2'} verse2.c = 'Chapter2' verse2.v = ['Chapter2 Verses'] importer.create_verse = MagicMock() importer.stop_import_flag = False importer.process_verses(book, 1, [verse1, verse2]) # THEN: parse_chapter_number, process_verses and increment_process_bar should have been called self.assertEqual(mocked_parse_verse_number.call_args_list, [call('1', 0), call('2', 1)]) self.assertEqual(mocked_get_text.call_args_list, [call(verse1), call(verse2)]) self.assertEqual( importer.create_verse.call_args_list, [call(1, 1, 1, 'Verse1 Text'), call(1, 1, 2, 'Verse2 Text')])