From 7c39bcda973b902fd3d84f3cce423502604f57cd Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Mon, 18 Dec 2017 19:57:13 +0000 Subject: [PATCH] Finish tests --- .../openlp_core/widgets/test_views.py | 58 +++++++++++++++---- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/tests/functional/openlp_core/widgets/test_views.py b/tests/functional/openlp_core/widgets/test_views.py index fa4e4114e..d6817ffb3 100644 --- a/tests/functional/openlp_core/widgets/test_views.py +++ b/tests/functional/openlp_core/widgets/test_views.py @@ -38,19 +38,53 @@ class TestHandleMimeDataUrls(TestCase): """ Test the :func:`openlp.core.widgets.views.handle_mime_data_urls` function. """ - # TODO: Finish tests here!!!!! - mocked_path_instance_1 = MagicMock(**{'is_file.return_value': True}) - mocked_path_instance_2 = MagicMock(**{'is_file.return_value': True}) - with patch('openlp.core.widgets.views.Path', - side_effect=[mocked_path_instance_1, mocked_path_instance_2]) as mocked_path: - mocked_q_url_1 = MagicMock(**{'toLocalFile.return_value': os.path.join('file', 'test', 'path', '1.ext')}) - mocked_q_url_2 = MagicMock(**{'toLocalFile.return_value': os.path.join('file', 'test', 'path', '2.ext')}) - mocked_q_mime_data = MagicMock(**{'urls.return_value': [mocked_q_url_1, mocked_q_url_2]}) + def test_files(self): + """ + Test handle_mime_data_urls when the data points to some files. + """ + # GIVEN: Some mocked objects that return True when is_file is called, and some mocked mime data + mocked_path_instance_1 = MagicMock(**{'is_file.return_value': True}) + mocked_path_instance_2 = MagicMock(**{'is_file.return_value': True}) + with patch('openlp.core.widgets.views.Path', + side_effect=[mocked_path_instance_1, mocked_path_instance_2]) as mocked_path: + mocked_q_url_1 = MagicMock(**{'toLocalFile.return_value': os.path.join('file', 'test', 'path', '1.ext')}) + mocked_q_url_2 = MagicMock(**{'toLocalFile.return_value': os.path.join('file', 'test', 'path', '2.ext')}) + mocked_q_mime_data = MagicMock(**{'urls.return_value': [mocked_q_url_1, mocked_q_url_2]}) + + # WHEN: Calling handle_mime_data_urls with the mocked mime data + result = handle_mime_data_urls(mocked_q_mime_data) + + # THEN: Both mocked Path objects should be returned in the list + mocked_path.assert_has_calls([call(os.path.join('file', 'test', 'path', '1.ext')), + call(os.path.join('file', 'test', 'path', '2.ext'))]) + assert result == [mocked_path_instance_1, mocked_path_instance_2] + + def test_directory(self): + """ + Test handle_mime_data_urls when the data points to some directories. + """ + # GIVEN: Some mocked objects that return True when is_dir is called, and some mocked mime data + mocked_path_instance_1 = MagicMock() + mocked_path_instance_2 = MagicMock() + mocked_path_instance_3 = MagicMock() + mocked_path_instance_4 = MagicMock(**{'is_file.return_value': False, 'is_directory.return_value': True, + 'iterdir.return_value': [mocked_path_instance_1, mocked_path_instance_2]}) + mocked_path_instance_5 = MagicMock(**{'is_file.return_value': False, 'is_directory.return_value': True, + 'iterdir.return_value': [mocked_path_instance_3]}) + with patch('openlp.core.widgets.views.Path', + side_effect=[mocked_path_instance_4, mocked_path_instance_5]) as mocked_path: + mocked_q_url_1 = MagicMock(**{'toLocalFile.return_value': os.path.join('file', 'test', 'path')}) + mocked_q_url_2 = MagicMock(**{'toLocalFile.return_value': os.path.join('file', 'test', 'path')}) + mocked_q_mime_data = MagicMock(**{'urls.return_value': [mocked_q_url_1, mocked_q_url_2]}) + + # WHEN: Calling handle_mime_data_urls with the mocked mime data + result = handle_mime_data_urls(mocked_q_mime_data) + + # THEN: The three mocked Path file objects should be returned in the list + mocked_path.assert_has_calls([call(os.path.join('file', 'test', 'path')), + call(os.path.join('file', 'test', 'path'))]) + assert result == [mocked_path_instance_1, mocked_path_instance_2, mocked_path_instance_3] - result = handle_mime_data_urls(mocked_q_mime_data) - mocked_path.assert_has_calls([call(os.path.join('file', 'test', 'path', '1.ext')), - call(os.path.join('file', 'test', 'path', '2.ext'))]) - assert result == [mocked_path_instance_1, mocked_path_instance_2] class TestListPreviewWidget(TestCase):