本文整理汇总了Python中tests.functional.MagicMock类的典型用法代码示例。如果您正苦于以下问题:Python MagicMock类的具体用法?Python MagicMock怎么用?Python MagicMock使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MagicMock类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: paint_event_text_fits_test
def paint_event_text_fits_test(self):
"""
Test the paintEvent method when text fits the label
"""
font = QtGui.QFont()
metrics = QtGui.QFontMetrics(font)
with patch('openlp.core.ui.slidecontroller.QtGui.QLabel'), \
patch('openlp.core.ui.slidecontroller.QtGui.QPainter') as mocked_qpainter:
# GIVEN: An instance of InfoLabel, with mocked text return, width and rect methods
info_label = InfoLabel()
test_string = 'Label Text'
mocked_rect = MagicMock()
mocked_text = MagicMock()
mocked_width = MagicMock()
mocked_text.return_value = test_string
info_label.rect = mocked_rect
info_label.text = mocked_text
info_label.width = mocked_width
# WHEN: The instance is wider than its text, and the paintEvent method is called
info_label.width.return_value = metrics.boundingRect(test_string).width() + 10
info_label.paintEvent(MagicMock())
# THEN: The text should be drawn centered with the complete test_string
mocked_qpainter().drawText.assert_called_once_with(mocked_rect(), QtCore.Qt.AlignCenter, test_string)
示例2: on_slide_selected_index_service_item_command_test
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')
示例3: on_slide_selected_index_service_item_not_command_test
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()
示例4: test_load_objects
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('')
示例5: on_go_live_service_manager_test
def on_go_live_service_manager_test(self):
"""
Test that when the on_go_live() method is called the message is sent to the live controller and focus is
set correctly.
"""
# GIVEN: A new SlideController instance and service manager preview then pressing go live should respond
mocked_display = MagicMock()
mocked_service_manager = MagicMock()
mocked_live_controller = MagicMock()
mocked_preview_widget = MagicMock()
mocked_service_item = MagicMock()
mocked_service_item.from_service = True
mocked_service_item.unique_identifier = 42
mocked_preview_widget.current_slide_number.return_value = 1
mocked_preview_widget.slide_count.return_value = 2
mocked_live_controller.preview_widget = MagicMock()
Registry.create()
Registry().register('live_controller', mocked_live_controller)
Registry().register('service_manager', mocked_service_manager)
slide_controller = SlideController(None)
slide_controller.service_item = mocked_service_item
slide_controller.preview_widget = mocked_preview_widget
slide_controller.display = mocked_display
# WHEN: on_go_live() is called
slide_controller.on_go_live()
# THEN: the service manager should have the service item and the focus set to live
mocked_service_manager.preview_live.assert_called_once_with(42, 1)
mocked_live_controller.preview_widget.setFocus.assert_called_once_with()
示例6: test_build_song_footer_base_ccli
def test_build_song_footer_base_ccli(self):
"""
Test build songs footer with basic song and a CCLI number
"""
# GIVEN: A Song and a Service Item and a configured CCLI license
mock_song = MagicMock()
mock_song.title = 'My Song'
mock_song.copyright = 'My copyright'
service_item = ServiceItem(None)
Settings().setValue('core/ccli number', '1234')
# WHEN: I generate the Footer with default settings
self.media_item.generate_footer(service_item, mock_song)
# THEN: I get the following Array returned
self.assertEqual(service_item.raw_footer, ['My Song', 'My copyright', 'CCLI License: 1234'],
'The array should be returned correctly with a song, an author, copyright and ccli')
# WHEN: I amend the CCLI value
Settings().setValue('core/ccli number', '4321')
self.media_item.generate_footer(service_item, mock_song)
# THEN: I would get an amended footer string
self.assertEqual(service_item.raw_footer, ['My Song', 'My copyright', 'CCLI License: 4321'],
'The array should be returned correctly with a song, an author, copyright and amended ccli')
示例7: test_build_song_footer_base_songbook
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'])
示例8: test_check_media
def test_check_media(self, MockQThread, MockCheckMediaWorker):
"""
Test the check_media() method of the SystemPlayer
"""
# GIVEN: A SystemPlayer instance and a mocked thread
valid_file = '/path/to/video.ogv'
mocked_application = MagicMock()
Registry().create()
Registry().register('application', mocked_application)
player = SystemPlayer(self)
mocked_thread = MagicMock()
mocked_thread.isRunning.side_effect = [True, False]
mocked_thread.quit = 'quit' # actually supposed to be a slot, but it's all mocked out anyway
MockQThread.return_value = mocked_thread
mocked_check_media_worker = MagicMock()
mocked_check_media_worker.play = 'play'
mocked_check_media_worker.result = True
MockCheckMediaWorker.return_value = mocked_check_media_worker
# WHEN: check_media() is called with a valid media file
result = player.check_media(valid_file)
# THEN: It should return True
MockQThread.assert_called_once_with()
MockCheckMediaWorker.assert_called_once_with(valid_file)
mocked_check_media_worker.setVolume.assert_called_once_with(0)
mocked_check_media_worker.moveToThread.assert_called_once_with(mocked_thread)
mocked_check_media_worker.finished.connect.assert_called_once_with('quit')
mocked_thread.started.connect.assert_called_once_with('play')
mocked_thread.start.assert_called_once_with()
self.assertEqual(2, mocked_thread.isRunning.call_count)
mocked_application.processEvents.assert_called_once_with()
self.assertTrue(result)
示例9: test_save_file_raises_permission_error
def test_save_file_raises_permission_error(self, mocked_save_file_as, mocked_zipfile, mocked_shutil_copy):
"""
Test that when a PermissionError is raised when trying to save a file, it is handled correctly
"""
# GIVEN: A service manager, a service to save
mocked_main_window = MagicMock()
mocked_main_window.service_manager_settings_section = 'servicemanager'
Registry().register('main_window', mocked_main_window)
Registry().register('application', MagicMock())
service_manager = ServiceManager(None)
service_manager._file_name = os.path.join('temp', 'filename.osz')
service_manager._save_lite = False
service_manager.service_items = []
service_manager.service_theme = 'Default'
service_manager.service_manager_list = MagicMock()
mocked_save_file_as.return_value = True
mocked_zipfile.ZipFile.return_value = MagicMock()
mocked_shutil_copy.side_effect = PermissionError
# WHEN: The service is saved and a PermissionError is thrown
result = service_manager.save_file()
# THEN: The "save_as" method is called to save the service
self.assertTrue(result)
mocked_save_file_as.assert_called_with()
示例10: build_song_footer_base_songbook_test
def build_song_footer_base_songbook_test(self):
"""
Test build songs footer with basic song and a songbook
"""
# GIVEN: A Song and a Service Item
mock_song = MagicMock()
mock_song.title = 'My Song'
mock_song.copyright = 'My copyright'
mock_song.book = MagicMock()
mock_song.book.name = "My songbook"
mock_song.song_number = 12
service_item = ServiceItem(None)
# WHEN: I generate the Footer with default settings
self.media_item.generate_footer(service_item, mock_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, mock_song)
# THEN: The songbook should be in the footer
self.assertEqual(service_item.raw_footer, ['My Song', 'My copyright', 'My songbook #12'])
示例11: test_build_html_video
def test_build_html_video(self, MockedSettings, Mocked_build_html):
# GIVEN: Mocked display
display = MagicMock()
mocked_media_controller = MagicMock()
Registry.create()
Registry().register('media_controller', mocked_media_controller)
main_display = MainDisplay(display)
main_display.frame = MagicMock()
mocked_settings = MagicMock()
mocked_settings.value.return_value = False
MockedSettings.return_value = mocked_settings
main_display.shake_web_view = MagicMock()
service_item = MagicMock()
service_item.theme_data = MagicMock()
service_item.theme_data.background_type = 'video'
service_item.theme_data.theme_name = 'name'
service_item.theme_data.background_filename = 'background_filename'
mocked_plugin = MagicMock()
display.plugin_manager = PluginManager()
display.plugin_manager.plugins = [mocked_plugin]
main_display.web_view = MagicMock()
# WHEN: build_html is called with a normal service item and a video theme.
main_display.build_html(service_item)
# THEN: the following should had not been called
self.assertEquals(main_display.web_view.setHtml.call_count, 1, 'setHTML should be called once')
self.assertEquals(main_display.media_controller.video.call_count, 1,
'Media Controller video should have been called once')
示例12: test_process_chapters
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
示例13: paint_event_text_doesnt_fit_test
def paint_event_text_doesnt_fit_test(self):
"""
Test the paintEvent method when text fits the label
"""
font = QtGui.QFont()
metrics = QtGui.QFontMetrics(font)
with patch('openlp.core.ui.slidecontroller.QtGui.QLabel'), \
patch('openlp.core.ui.slidecontroller.QtGui.QPainter') as mocked_qpainter:
# GIVEN: An instance of InfoLabel, with mocked text return, width and rect methods
info_label = InfoLabel()
test_string = 'Label Text'
mocked_rect = MagicMock()
mocked_text = MagicMock()
mocked_width = MagicMock()
mocked_text.return_value = test_string
info_label.rect = mocked_rect
info_label.text = mocked_text
info_label.width = mocked_width
# WHEN: The instance is narrower than its text, and the paintEvent method is called
label_width = metrics.boundingRect(test_string).width() - 10
info_label.width.return_value = label_width
info_label.paintEvent(MagicMock())
# THEN: The text should be drawn aligned left with an elided test_string
elided_test_string = metrics.elidedText(test_string, QtCore.Qt.ElideRight, label_width)
mocked_qpainter().drawText.assert_called_once_with(mocked_rect(), QtCore.Qt.AlignLeft, elided_test_string)
示例14: setUp
def setUp(self):
mocked_plugin = MagicMock()
mocked_plugin.settings_section = 'presentations'
Settings().extend_default_settings(__default_settings__)
self.file_name = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.pptx')
self.ppc = ImpressController(mocked_plugin)
self.doc = ImpressDocument(self.ppc, self.file_name)
示例15: test_write_theme_same_image
def test_write_theme_same_image(self):
"""
Test that we don't try to overwrite a theme background image with itself
"""
# GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
# theme, check_directory_exists and thememanager-attributes.
with patch('builtins.open') as mocked_open, \
patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
mocked_open.return_value = MagicMock()
theme_manager = ThemeManager(None)
theme_manager.old_background_image = None
theme_manager.generate_and_save_image = MagicMock()
theme_manager.path = ''
mocked_theme = MagicMock()
mocked_theme.theme_name = 'themename'
mocked_theme.extract_formatted_xml = MagicMock()
mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode()
# WHEN: Calling _write_theme with path to the same image, but the path written slightly different
file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
# Do replacement from end of string to avoid problems with path start
file_name2 = file_name1[::-1].replace(os.sep, os.sep + os.sep, 2)[::-1]
theme_manager._write_theme(mocked_theme, file_name1, file_name2)
# THEN: The mocked_copyfile should not have been called
self.assertFalse(mocked_copyfile.called, 'shutil.copyfile should not be called')