def test_build_song_footer_base_songbook(self): """ Test build songs footer with basic song and multiple songbooks """ # GIVEN: A Song and a Service Item song = Song() song.title = 'My Song' song.copyright = 'My copyright' song.authors_songs = [] song.songbook_entries = [] song.ccli_number = '' book1 = MagicMock() book1.name = "My songbook" book2 = MagicMock() book2.name = "Thy songbook" song.songbookentries = [] song.add_songbook_entry(book1, '12') song.add_songbook_entry(book2, '502A') service_item = ServiceItem(None) # WHEN: I generate the Footer with default settings self.media_item.generate_footer(service_item, song) # THEN: The songbook should not be in the footer self.assertEqual(service_item.raw_footer, ['My Song', 'My copyright']) # WHEN: I activate the "display songbook" option self.media_item.display_songbook = True self.media_item.generate_footer(service_item, song) # THEN: The songbook should be in the footer self.assertEqual(service_item.raw_footer, ['My Song', 'My copyright', 'My songbook #12, Thy songbook #502A'])
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_topic(self): """ Test displaying song search results grouped by topic with basic song """ # GIVEN: Search results grouped by topic, 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_topic = MagicMock() mock_song = MagicMock() mock_song_temp = MagicMock() mock_topic.name = 'My Topic' mock_topic.songs = [] mock_song.id = 1 mock_song.title = 'My Song' mock_song.sort_key = 'My Song' 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.temporary = True mock_topic.songs.append(mock_song) mock_topic.songs.append(mock_song_temp) mock_search_results.append(mock_topic) mock_qlist_widget = MagicMock() MockedQListWidgetItem.return_value = mock_qlist_widget # WHEN: I display song search results grouped by topic self.media_item.display_results_topic(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('My Topic (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 on_slide_selected_index_service_item_not_command_test(self, mocked_execute): """ Test that when there is a service item but it's not a command, the preview widget is updated """ # GIVEN: A mocked service item and a slide controller with a service item mocked_item = MagicMock() mocked_item.is_command.return_value = False mocked_item.name = 'Mocked Item' mocked_update_preview = MagicMock() mocked_preview_widget = MagicMock() mocked_slide_selected = MagicMock() Registry.create() slide_controller = SlideController(None) slide_controller.service_item = mocked_item slide_controller.update_preview = mocked_update_preview slide_controller.preview_widget = mocked_preview_widget slide_controller.slide_selected = mocked_slide_selected # WHEN: The method is called slide_controller.on_slide_selected_index([7]) # THEN: It should have sent a notification mocked_item.is_command.assert_called_once_with() self.assertEqual(0, mocked_execute.call_count, 'Execute should not have been called') self.assertEqual(0, mocked_update_preview.call_count, 'Update preview should not have been called') mocked_preview_widget.change_slide.assert_called_once_with(7) mocked_slide_selected.assert_called_once_with()
def on_slide_selected_index_service_item_command_test(self, mocked_execute): """ Test that when there is a command service item, the command is executed """ # GIVEN: A mocked service item and a slide controller with a service item mocked_item = MagicMock() mocked_item.is_command.return_value = True mocked_item.name = 'Mocked Item' mocked_update_preview = MagicMock() mocked_preview_widget = MagicMock() mocked_slide_selected = MagicMock() Registry.create() slide_controller = SlideController(None) slide_controller.service_item = mocked_item slide_controller.update_preview = mocked_update_preview slide_controller.preview_widget = mocked_preview_widget slide_controller.slide_selected = mocked_slide_selected slide_controller.is_live = True # WHEN: The method is called slide_controller.on_slide_selected_index([9]) # THEN: It should have sent a notification mocked_item.is_command.assert_called_once_with() mocked_execute.assert_called_once_with('mocked item_slide', [mocked_item, True, 9]) mocked_update_preview.assert_called_once_with() self.assertEqual(0, mocked_preview_widget.change_slide.call_count, 'Change slide should not have been called') self.assertEqual(0, mocked_slide_selected.call_count, 'slide_selected should not have been called')
def process_chapters_completes_test(self, mocked_parse_chapter_number): """ Test process_chapters when it completes """ # GIVEN: An instance of OpenSongBible importer = OpenSongBible(MagicMock(), path='.', name='.', filename='') importer.wizard = MagicMock() # WHEN: called with some valid data book = MagicMock() book.name = "Book" chapter1 = MagicMock() chapter1.attrib = {'n': '1'} chapter1.c = 'Chapter1' chapter1.v = ['Chapter1 Verses'] chapter2 = MagicMock() chapter2.attrib = {'n': '2'} chapter2.c = 'Chapter2' chapter2.v = ['Chapter2 Verses'] importer.process_verses = MagicMock() importer.stop_import_flag = False importer.process_chapters(book, [chapter1, chapter2]) # THEN: parse_chapter_number, process_verses and increment_process_bar should have been called self.assertEqual(mocked_parse_chapter_number.call_args_list, [call('1', 0), call('2', 1)]) self.assertEqual(importer.process_verses.call_args_list, [ call(book, 1, ['Chapter1 Verses']), call(book, 2, ['Chapter2 Verses']) ]) self.assertEqual( importer.wizard.increment_progress_bar.call_args_list, [call('Importing Book 1...'), call('Importing Book 2...')])
def test_process_chapters(self, mocked_open, mocked_os): """ Test the process_chapters() method """ # GIVEN: A WordProject importer and a bunch of mocked things importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer.base_dir = '' importer.stop_import_flag = False importer.language_id = 'en' mocked_open.return_value.__enter__.return_value.read.return_value = CHAPTER_PAGE mocked_os.path.join.side_effect = lambda *x: ''.join(x) mocked_os.path.normpath.side_effect = lambda x: x mocked_db_book = MagicMock() mocked_db_book.name = 'Genesis' book_id = 1 book_link = '01/1.htm' # WHEN: process_chapters() is called with patch.object(importer, 'set_current_chapter') as mocked_set_current_chapter, \ patch.object(importer, 'process_verses') as mocked_process_verses: importer.process_chapters(mocked_db_book, book_id, book_link) # THEN: The right methods should have been called expected_set_current_chapter_calls = [call('Genesis', ch) for ch in range(1, 51)] expected_process_verses_calls = [call(mocked_db_book, 1, ch) for ch in range(1, 51)] mocked_os.path.join.assert_called_once_with('', '01/1.htm') mocked_open.assert_called_once_with('01/1.htm', encoding='utf-8', errors='ignore') assert mocked_set_current_chapter.call_args_list == expected_set_current_chapter_calls assert mocked_process_verses.call_args_list == expected_process_verses_calls
def get_plugin_by_name_exists_test(self): """ Test running the get_plugin_by_name() method to find a plugin that exists """ # GIVEN: A PluginManager instance and a list with a mocked up plugin whose status is set to Active mocked_plugin = MagicMock() mocked_plugin.name = 'Mocked Plugin' plugin_manager = PluginManager() plugin_manager.plugins = [mocked_plugin] # WHEN: We run finalise_plugins() result = plugin_manager.get_plugin_by_name('Mocked Plugin') # THEN: The is_active() and finalise() methods should have been called self.assertEqual(result, mocked_plugin, 'The result for get_plugin_by_name should be the mocked plugin')
def get_plugin_by_name_does_not_exist_test(self): """ Test running the get_plugin_by_name() method to find a plugin that does not exist """ # GIVEN: A PluginManager instance and a list with a mocked up plugin whose status is set to Active mocked_plugin = MagicMock() mocked_plugin.name = 'Mocked Plugin' plugin_manager = PluginManager() plugin_manager.plugins = [mocked_plugin] # WHEN: We run finalise_plugins() result = plugin_manager.get_plugin_by_name('Missing Plugin') # THEN: The is_active() and finalise() methods should have been called self.assertIsNone(result, 'The result for get_plugin_by_name should be None')
def test_simple_import(self, mocked_languages, mocked_pysword_modules, mocked_application): """ Test that a simple SWORD import works """ # GIVEN: Test files with a mocked out "manager", "import_wizard", and mocked functions # get_book_ref_id_by_name, create_verse, create_book, session and get_language. # Also mocked pysword structures mocked_manager = MagicMock() mocked_import_wizard = MagicMock() importer = SwordBible(mocked_manager, path='.', name='.', filename='', sword_key='', sword_path='') result_file = open(os.path.join(TEST_PATH, 'dk1933.json'), 'rb') test_data = json.loads(result_file.read().decode()) importer.wizard = mocked_import_wizard importer.get_book_ref_id_by_name = MagicMock() importer.create_verse = MagicMock() importer.create_book = MagicMock() importer.session = MagicMock() mocked_languages.get_language.return_value = 'Danish' mocked_bible = MagicMock() mocked_genesis = MagicMock() mocked_genesis.name = 'Genesis' mocked_genesis.num_chapters = 1 books = {'ot': [mocked_genesis]} mocked_structure = MagicMock() mocked_structure.get_books.return_value = books mocked_bible.get_structure.return_value = mocked_structure mocked_bible.get_iter.return_value = [ verse[1] for verse in test_data['verses'] ] mocked_module = MagicMock() mocked_module.get_bible_from_module.return_value = mocked_bible mocked_pysword_modules.SwordModules.return_value = mocked_module # WHEN: Importing bible file importer.do_import() # THEN: The create_verse() method should have been called with each verse in the file. self.assertTrue(importer.create_verse.called) for verse_tag, verse_text in test_data['verses']: importer.create_verse.assert_any_call(importer.create_book().id, 1, int(verse_tag), verse_text)
def test_simple_import(self, mocked_pysword_modules, mocked_application): """ Test that a simple SWORD import works """ # GIVEN: Test files with a mocked out "manager", "import_wizard", and mocked functions # get_book_ref_id_by_name, create_verse, create_book, session and get_language. # Also mocked pysword structures mocked_manager = MagicMock() mocked_import_wizard = MagicMock() importer = SwordBible(mocked_manager, path='.', name='.', filename='', sword_key='', sword_path='') result_file = open(os.path.join(TEST_PATH, 'dk1933.json'), 'rb') test_data = json.loads(result_file.read().decode()) importer.wizard = mocked_import_wizard importer.get_book_ref_id_by_name = MagicMock() importer.create_verse = MagicMock() importer.create_book = MagicMock() importer.session = MagicMock() importer.get_language = MagicMock(return_value='Danish') mocked_bible = MagicMock() mocked_genesis = MagicMock() mocked_genesis.name = 'Genesis' mocked_genesis.num_chapters = 1 books = {'ot': [mocked_genesis]} mocked_structure = MagicMock() mocked_structure.get_books.return_value = books mocked_bible.get_structure.return_value = mocked_structure mocked_bible.get_iter.return_value = [verse[1] for verse in test_data['verses']] mocked_module = MagicMock() mocked_module.get_bible_from_module.return_value = mocked_bible mocked_pysword_modules.SwordModules.return_value = mocked_module # WHEN: Importing bible file importer.do_import() # THEN: The create_verse() method should have been called with each verse in the file. self.assertTrue(importer.create_verse.called) for verse_tag, verse_text in test_data['verses']: importer.create_verse.assert_any_call(importer.create_book().id, 1, int(verse_tag), verse_text)
def test_process_verses(self, mocked_open, mocked_os): """ Test the process_verses() method """ # GIVEN: A WordProject importer and a bunch of mocked things importer = WordProjectBible(MagicMock(), path='.', name='.', filename='kj.zip') importer.base_dir = '' importer.stop_import_flag = False importer.language_id = 'en' mocked_open.return_value.__enter__.return_value.read.return_value = CHAPTER_PAGE mocked_os.path.join.side_effect = lambda *x: '/'.join(x) mocked_db_book = MagicMock() mocked_db_book.name = 'Genesis' book_number = 1 chapter_number = 1 # WHEN: process_verses() is called with patch.object(importer, 'process_verse') as mocked_process_verse: importer.process_verses(mocked_db_book, book_number, chapter_number) # THEN: All the right methods should have been called mocked_os.path.join.assert_called_once_with('', '01', '1.htm') mocked_open.assert_called_once_with('/01/1.htm', encoding='utf-8', errors='ignore') assert mocked_process_verse.call_count == 31