本文整理汇总了Python中tests.functional.patch.object函数的典型用法代码示例。如果您正苦于以下问题:Python object函数的具体用法?Python object怎么用?Python object使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了object函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: play_media_wait_state_not_playing_test
def play_media_wait_state_not_playing_test(self, mocked_get_vlc, mocked_threading):
"""
Test the play() method when media_wait_state() returns False
"""
# GIVEN: A bunch of mocked out things
mocked_thread = MagicMock()
mocked_threading.Thread.return_value = mocked_thread
mocked_vlc = MagicMock()
mocked_get_vlc.return_value = mocked_vlc
mocked_controller = MagicMock()
mocked_controller.media_info.start_time = 0
mocked_display = MagicMock()
mocked_display.controller = mocked_controller
vlc_player = VlcPlayer(None)
vlc_player.state = MediaState.Paused
# WHEN: play() is called
with patch.object(vlc_player, 'media_state_wait') as mocked_media_state_wait, \
patch.object(vlc_player, 'volume') as mocked_volume:
mocked_media_state_wait.return_value = False
result = vlc_player.play(mocked_display)
# THEN: A thread should be started, but the method should return False
mocked_thread.start.assert_called_with()
self.assertFalse(result)
示例2: test_do_import_breaks_on_stop
def test_do_import_breaks_on_stop(self, mocked_pyodbc):
"""
Test the MediaShoutImport do_import stops when the user presses the cancel button
"""
SongRecord = namedtuple('SongRecord', 'Record, Title, Author, Copyright, SongID, CCLI, Notes')
song = SongRecord(1, 'Amazing Grace', 'William Wilberforce', 'Public Domain', 1, '654321', '')
# GIVEN: A MediaShoutImport instance and a bunch of stuff mocked out
importer = MediaShoutImport(MagicMock(), filename='mediashout.db')
mocked_cursor = MagicMock()
mocked_cursor.fetchall.return_value = [song]
mocked_connection = MagicMock()
mocked_connection.cursor.return_value = mocked_cursor
mocked_pyodbc.connect.return_value = mocked_connection
# WHEN: do_import is called, but cancelled
with patch.object(importer, 'import_wizard') as mocked_import_wizard, \
patch.object(importer, 'process_song') as mocked_process_song:
importer.stop_import_flag = True
importer.do_import()
# THEN: The songs should have been imported
mocked_cursor.execute.assert_called_once_with(
'SELECT Record, Title, Author, Copyright, SongID, CCLI, Notes FROM Songs ORDER BY Title')
mocked_process_song.assert_not_called()
示例3: update_ui_dvd_test
def update_ui_dvd_test(self, mocked_get_vlc):
"""
Test updating the UI for a CD or DVD
"""
# GIVEN: A whole bunch of mocks
mocked_vlc = MagicMock()
mocked_vlc.State.Ended = 1
mocked_get_vlc.return_value = mocked_vlc
mocked_controller = MagicMock()
mocked_controller.media_info.start_time = 100
mocked_controller.media_info.end_time = 300
mocked_controller.seek_slider.isSliderDown.return_value = False
mocked_display = MagicMock()
mocked_display.controller = mocked_controller
mocked_display.vlc_media.get_state.return_value = 1
mocked_display.vlc_media_player.get_time.return_value = 400000
mocked_display.controller.media_info.media_type = MediaType.DVD
vlc_player = VlcPlayer(None)
# WHEN: update_ui() is called
with patch.object(vlc_player, 'stop') as mocked_stop, \
patch.object(vlc_player, 'set_visible') as mocked_set_visible:
vlc_player.update_ui(mocked_display)
# THEN: Certain methods should be called
mocked_stop.assert_called_with(mocked_display)
self.assertEqual(2, mocked_stop.call_count)
mocked_display.vlc_media_player.get_time.assert_called_with()
mocked_set_visible.assert_called_with(mocked_display, False)
mocked_controller.seek_slider.setSliderPosition.assert_called_with(300000)
expected_calls = [call(True), call(False)]
self.assertEqual(expected_calls, mocked_controller.seek_slider.blockSignals.call_args_list)
示例4: 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
示例5: test_update_ui
def test_update_ui(self):
"""
Test the update_ui() method on the SystemPlayer
"""
# GIVEN: A SystemPlayer instance
player = SystemPlayer(self)
player.state = MediaState.Playing
mocked_display = MagicMock()
mocked_display.media_player.state.return_value = QtMultimedia.QMediaPlayer.PausedState
mocked_display.controller.media_info.end_time = 1
mocked_display.media_player.position.return_value = 2
mocked_display.controller.seek_slider.isSliderDown.return_value = False
# WHEN: update_ui() is called
with patch.object(player, 'stop') as mocked_stop, \
patch.object(player, 'set_visible') as mocked_set_visible:
player.update_ui(mocked_display)
# THEN: The UI is updated
expected_stop_calls = [call(mocked_display), call(mocked_display)]
expected_position_calls = [call(), call()]
expected_block_signals_calls = [call(True), call(False)]
mocked_display.media_player.state.assert_called_once_with()
self.assertEqual(2, mocked_stop.call_count)
self.assertEqual(expected_stop_calls, mocked_stop.call_args_list)
self.assertEqual(2, mocked_display.media_player.position.call_count)
self.assertEqual(expected_position_calls, mocked_display.media_player.position.call_args_list)
mocked_set_visible.assert_called_once_with(mocked_display, False)
mocked_display.controller.seek_slider.isSliderDown.assert_called_once_with()
self.assertEqual(expected_block_signals_calls,
mocked_display.controller.seek_slider.blockSignals.call_args_list)
mocked_display.controller.seek_slider.setSliderPosition.assert_called_once_with(2)
示例6: get_language_accepted_test
def get_language_accepted_test(self):
"""
Test the BibleImport.get_language method when the user accepts the dialog box
"""
# GIVEN: A mocked LanguageForm with an exec method which returns QtDialog.Accepted an instance of BibleDB and
# a combobox with the selected item data as 10
with patch.object(BibleDB, 'save_meta'), patch.object(BibleDB, '_setup'), \
patch('openlp.plugins.bibles.forms.LanguageForm') as mocked_language_form:
# The integer value of QtDialog.Accepted is 1. Using the enumeration causes a seg fault for some reason
mocked_language_form_instance = MagicMock(**{'exec.return_value': 1,
'language_combo_box.itemData.return_value': 10})
mocked_language_form.return_value = mocked_language_form_instance
instance = BibleImport(MagicMock())
mocked_wizard = MagicMock()
instance.wizard = mocked_wizard
# WHEN: Calling get_language()
result = instance.get_language('Bible Name')
# THEN: get_language() should return the id of the selected language in the combo box
mocked_language_form.assert_called_once_with(mocked_wizard)
mocked_language_form_instance.exec.assert_called_once_with('Bible Name')
self.assertEqual(result, 10, 'get_language() should return the id of the language the user has chosen when '
'they accept the dialog box')
示例7: test_play_is_preview
def test_play_is_preview(self, mocked_functools):
"""
Test the play() method of the SystemPlayer on the preview display
"""
# GIVEN: A SystemPlayer instance and a mocked display
mocked_functools.partial.return_value = 'function'
player = SystemPlayer(self)
mocked_display = MagicMock()
mocked_display.controller.is_live = False
mocked_display.controller.media_info.start_time = 1
mocked_display.controller.media_info.volume = 1
# WHEN: play() is called
with patch.object(player, 'get_preview_state') as mocked_get_preview_state, \
patch.object(player, 'seek') as mocked_seek, \
patch.object(player, 'volume') as mocked_volume, \
patch.object(player, 'set_state') as mocked_set_state:
mocked_get_preview_state.return_value = QtMultimedia.QMediaPlayer.PlayingState
result = player.play(mocked_display)
# THEN: the media file is played
mocked_get_preview_state.assert_called_once_with()
mocked_display.media_player.play.assert_called_once_with()
mocked_seek.assert_called_once_with(mocked_display, 1000)
mocked_volume.assert_called_once_with(mocked_display, 1)
mocked_display.media_player.durationChanged.connect.assert_called_once_with('function')
mocked_set_state.assert_called_once_with(MediaState.Playing, mocked_display)
mocked_display.video_widget.raise_.assert_called_once_with()
self.assertTrue(result)
示例8: play_test
def play_test(self, mocked_get_vlc, mocked_threading):
"""
Test the play() method
"""
# GIVEN: A bunch of mocked out things
mocked_thread = MagicMock()
mocked_threading.Thread.return_value = mocked_thread
mocked_vlc = MagicMock()
mocked_get_vlc.return_value = mocked_vlc
mocked_controller = MagicMock()
mocked_controller.media_info.start_time = 0
mocked_controller.media_info.media_type = MediaType.Video
mocked_controller.media_info.volume = 100
mocked_media = MagicMock()
mocked_media.get_duration.return_value = 50000
mocked_display = MagicMock()
mocked_display.controller = mocked_controller
mocked_display.vlc_media_player.get_media.return_value = mocked_media
vlc_player = VlcPlayer(None)
vlc_player.state = MediaState.Paused
# WHEN: play() is called
with patch.object(vlc_player, 'media_state_wait') as mocked_media_state_wait, \
patch.object(vlc_player, 'volume') as mocked_volume:
mocked_media_state_wait.return_value = True
result = vlc_player.play(mocked_display)
# THEN: A bunch of things should happen to play the media
mocked_thread.start.assert_called_with()
self.assertEqual(50, mocked_controller.media_info.length)
mocked_volume.assert_called_with(mocked_display, 100)
mocked_controller.seek_slider.setMaximum.assert_called_with(50000)
self.assertEqual(MediaState.Playing, vlc_player.state)
mocked_display.vlc_widget.raise_.assert_called_with()
self.assertTrue(result, 'The value returned from play() should be True')
示例9: validate_xml_file_success_test
def validate_xml_file_success_test(self):
"""
Test that validate_xml_file returns True with valid XML
"""
# GIVEN: Some test data with an OpenSong Bible "bible" root tag
with patch.object(BibleImport, 'parse_xml', return_value=objectify.fromstring('<bible></bible>')), \
patch.object(BibleImport, 'is_compressed', return_value=False):
importer = BibleImport(MagicMock(), path='.', name='.', filename='')
# WHEN: Calling validate_xml_file
result = importer.validate_xml_file('file.name', 'bible')
# THEN: True should be returned
self.assertTrue(result)
示例10: test_on_search_results_widget_selection_changed
def test_on_search_results_widget_selection_changed(self):
"""
Test that the view button is updated when the search results list is changed
"""
# GIVEN: A SongSelect form
ssform = SongSelectForm(None, MagicMock(), MagicMock())
# WHEN: There is at least 1 item selected
with patch.object(ssform, 'search_results_widget') as mocked_search_results_widget, \
patch.object(ssform, 'view_button') as mocked_view_button:
mocked_search_results_widget.selectedItems.return_value = [1]
ssform.on_search_results_widget_selection_changed()
# THEN: The view button should be enabled
mocked_view_button.setEnabled.assert_called_with(True)
示例11: test_on_back_button_clicked
def test_on_back_button_clicked(self):
"""
Test that when the back button is clicked, the stacked widget is set back one page
"""
# GIVEN: A SongSelect form
ssform = SongSelectForm(None, MagicMock(), MagicMock())
# WHEN: The back button is clicked
with patch.object(ssform, 'stacked_widget') as mocked_stacked_widget, \
patch.object(ssform, 'search_combobox') as mocked_search_combobox:
ssform.on_back_button_clicked()
# THEN: The stacked widget should be set back one page
mocked_stacked_widget.setCurrentIndex.assert_called_with(1)
mocked_search_combobox.setFocus.assert_called_with()
示例12: validate_xml_file_parse_xml_fails_test
def validate_xml_file_parse_xml_fails_test(self):
"""
Test that validate_xml_file raises a ValidationError when parse_xml returns None
"""
# GIVEN: A mocked parse_xml which returns None
with patch.object(BibleImport, 'parse_xml', return_value=None), \
patch.object(BibleImport, 'is_compressed', return_value=False):
importer = BibleImport(MagicMock(), path='.', name='.', filename='')
# WHEN: Calling validate_xml_file
# THEN: ValidationError should be raised, with the message 'Error when opening file'
# the user that an OpenSong bible was found
with self.assertRaises(ValidationError) as context:
importer.validate_xml_file('file.name', 'xbible')
self.assertEqual(context.exception.msg, 'Error when opening file')
示例13: test_on_search_shortcut_triggered_shows_media_manager
def test_on_search_shortcut_triggered_shows_media_manager(self):
"""
Test that the media manager is made visible when the search shortcut is triggered
"""
# GIVEN: A build main window set up for testing
with patch.object(self.main_window, 'media_manager_dock') as mocked_media_manager_dock, \
patch.object(self.main_window, 'media_tool_box') as mocked_media_tool_box:
mocked_media_manager_dock.isVisible.return_value = False
mocked_media_tool_box.currentWidget.return_value = None
# WHEN: The search shortcut is triggered
self.main_window.on_search_shortcut_triggered()
# THEN: The media manager dock is made visible
mocked_media_manager_dock.setVisible.assert_called_with(True)
示例14: load_test
def load_test(self, mocked_normcase, mocked_get_vlc):
"""
Test loading a video into VLC
"""
# GIVEN: A mocked out get_vlc() method
media_path = '/path/to/media.mp4'
mocked_normcase.side_effect = lambda x: x
mocked_vlc = MagicMock()
mocked_get_vlc.return_value = mocked_vlc
mocked_controller = MagicMock()
mocked_controller.media_info.volume = 100
mocked_controller.media_info.media_type = MediaType.Video
mocked_controller.media_info.file_info.absoluteFilePath.return_value = media_path
mocked_vlc_media = MagicMock()
mocked_media = MagicMock()
mocked_media.get_duration.return_value = 10000
mocked_display = MagicMock()
mocked_display.controller = mocked_controller
mocked_display.vlc_instance.media_new_path.return_value = mocked_vlc_media
mocked_display.vlc_media_player.get_media.return_value = mocked_media
vlc_player = VlcPlayer(None)
# WHEN: A video is loaded into VLC
with patch.object(vlc_player, 'volume') as mocked_volume:
result = vlc_player.load(mocked_display)
# THEN: The video should be loaded
mocked_normcase.assert_called_with(media_path)
mocked_display.vlc_instance.media_new_path.assert_called_with(media_path)
self.assertEqual(mocked_vlc_media, mocked_display.vlc_media)
mocked_display.vlc_media_player.set_media.assert_called_with(mocked_vlc_media)
mocked_vlc_media.parse.assert_called_with()
mocked_volume.assert_called_with(mocked_display, 100)
self.assertEqual(10, mocked_controller.media_info.length)
self.assertTrue(result)
示例15: test_on_view_button_clicked
def test_on_view_button_clicked(self):
"""
Test that a song is retrieved when the view button is clicked
"""
# GIVEN: A SongSelect form
ssform = SongSelectForm(None, MagicMock(), MagicMock())
expected_song = {'title': 'Amazing Grace'}
# WHEN: A song result is double-clicked
with patch.object(ssform, '_view_song') as mocked_view_song, \
patch.object(ssform, 'search_results_widget') as mocked_search_results_widget:
mocked_search_results_widget.currentItem.return_value = expected_song
ssform.on_view_button_clicked()
# THEN: The song is fetched and shown to the user
mocked_view_song.assert_called_with(expected_song)