From aa1efd1079e291996853c65c92c96bd5b438cf54 Mon Sep 17 00:00:00 2001 From: bookfere Date: Mon, 4 Nov 2024 20:36:19 +0800 Subject: [PATCH] test: Resolve issues preventing test cases from running on Windows. --- tests/test_conversion.py | 49 ++++++++++++++++++++++------------------ tests/test_engine.py | 22 ++++++++++-------- tests/test_utils.py | 3 --- 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/tests/test_conversion.py b/tests/test_conversion.py index 7f86241..bd5b2c2 100644 --- a/tests/test_conversion.py +++ b/tests/test_conversion.py @@ -1,4 +1,5 @@ import unittest +from pathlib import Path from typing import Callable from unittest.mock import call, patch, Mock @@ -20,7 +21,7 @@ def setUp(self): self.ebook = Mock(Ebooks.Ebook) self.job = Mock() self.worker.working_jobs = { - self.job: (self.ebook, '/path/to/test.epub')} + self.job: (self.ebook, str(Path('/path/to/test.epub')))} def test_create_worker(self): self.assertIsInstance(self.worker, ConversionWorker) @@ -75,7 +76,8 @@ def test_translate_done_ebook_to_library( self.worker.translate_done(self.job) - mock_open.assert_called_once_with('/path/to/test.epub', 'r+b') + mock_open.assert_called_once_with( + str(Path('/path/to/test.epub')), 'r+b') mock_get_metadata.assert_called_once_with(file, 'epub') mock_set_metadata.assert_called_once_with(file, metadata, 'epub') self.assertEqual('test custom title [German]', metadata.title) @@ -86,7 +88,7 @@ def test_translate_done_ebook_to_library( self.worker.db.create_book_entry.assert_called_once_with(metadata) self.worker.api.add_format.assert_called_once_with( - 89, 'epub', '/path/to/test.epub', run_hooks=False) + 89, 'epub', str(Path('/path/to/test.epub')), run_hooks=False) self.worker.gui.library_view.model.assert_called_once() self.worker.gui.library_view.model().books_added \ .assert_called_once_with(1) @@ -126,7 +128,7 @@ def test_translate_done_ebook_to_path( mock_open_path, mock_open): self.job.failed = False self.job.description = 'test description' - self.job.log_path = '/path/to/log' + self.job.log_path = str(Path('/path/to/log')) metadata_config = { 'subjects': ['test subject 1', 'test subject 2'], 'lang_code': True, @@ -153,16 +155,17 @@ def test_translate_done_ebook_to_path( self.worker.translate_done(self.job) - mock_open.assert_called_once_with('/path/to/test.epub', 'r+b') - mock_os_rename.assert_called_once_with( - '/path/to/test.epub', - '/path/to/test_ custom title_ [German].epub') + original_path = str(Path('/path/to/test.epub')) + new_path = str(Path('/path/to/test_ custom title_ [German].epub')) + + mock_open.assert_called_once_with(original_path, 'r+b') + mock_os_rename.assert_called_once_with(original_path, new_path) self.worker.gui.status_bar.show_message.assert_called_once_with( 'test description ' + 'completed', 5000) arguments = self.worker.gui.proceed_question.mock_calls[0].args self.assertIsInstance(arguments[0], Callable) self.assertIs(self.worker.gui.job_manager.launch_gui_app, arguments[1]) - self.assertEqual('/path/to/log', arguments[2]) + self.assertEqual(str(Path('/path/to/log')), arguments[2]) self.assertEqual('Ebook Translation Log', arguments[3]) self.assertEqual('Translation Completed', arguments[4]) self.assertEqual( @@ -174,7 +177,8 @@ def test_translate_done_ebook_to_path( arguments[0](mock_payload) mock_payload.assert_called_once_with( 'ebook-viewer', kwargs={'args': [ - 'ebook-viewer', '/path/to/test_ custom title_ [German].epub']}) + 'ebook-viewer', + str(Path('/path/to/test_ custom title_ [German].epub'))]}) arguments = self.worker.gui.proceed_question.mock_calls[0].kwargs self.assertEqual(True, arguments.get('log_is_file')) @@ -188,7 +192,7 @@ def test_translate_done_other_to_library( self, mock_open, mock_os_rename, mock_open_path): self.job.failed = False self.job.description = 'test description' - self.job.log_path = 'C:\\path\\to\\log' + self.job.log_path = str(Path('/path/to/log')) metadata_config = {'lang_mark': True} self.worker.config = { 'ebook_metadata': metadata_config, @@ -203,11 +207,11 @@ def test_translate_done_other_to_library( self.ebook.custom_title = 'test custom title' self.ebook.target_lang = 'German' self.worker.working_jobs = { - self.job: (self.ebook, 'C:\\path\\to\\test.srt')} + self.job: (self.ebook, str(Path('/path/to/test.srt')))} metadata = Mock() self.worker.api.get_metadata.return_value = metadata self.worker.api.format_abspath.return_value = \ - 'C:\\path\\to\\test[m].srt' + str(Path('/path/to/test[m].srt')) self.worker.db.create_book_entry.return_value = 90 self.worker.translate_done(self.job) @@ -215,7 +219,7 @@ def test_translate_done_other_to_library( self.worker.api.get_metadata.assert_called_once_with(89) self.worker.db.create_book_entry.assert_called_once_with(metadata) self.worker.api.add_format.assert_called_once_with( - 90, 'srt', 'C:\\path\\to\\test.srt', run_hooks=False) + 90, 'srt', str(Path('/path/to/test.srt')), run_hooks=False) self.worker.gui.library_view.model.assert_called_once() self.worker.gui.library_view.model().books_added \ .assert_called_once_with(1) @@ -227,7 +231,7 @@ def test_translate_done_other_to_library( arguments = self.worker.gui.proceed_question.mock_calls[0].args self.assertIsInstance(arguments[0], Callable) self.assertIs(self.worker.gui.job_manager.launch_gui_app, arguments[1]) - self.assertEqual('C:\\path\\to\\log', arguments[2]) + self.assertEqual(str(Path('/path/to/log')), arguments[2]) self.assertEqual('Ebook Translation Log', arguments[3]) self.assertEqual('Translation Completed', arguments[4]) self.assertEqual( @@ -237,7 +241,8 @@ def test_translate_done_other_to_library( mock_payload = Mock() arguments[0](mock_payload) - mock_open_path.assert_called_once_with('C:\\path\\to\\test[m].srt') + mock_open_path.assert_called_once_with( + str(Path('/path/to/test[m].srt'))) arguments = self.worker.gui.proceed_question.mock_calls[0].kwargs self.assertEqual(True, arguments.get('log_is_file')) @@ -250,7 +255,7 @@ def test_translate_done_other_to_path( self, mock_open, mock_os_rename, mock_open_path): self.job.failed = False self.job.description = 'test description' - self.job.log_path = '/path/to/log' + self.job.log_path = str(Path('/path/to/log')) metadata_config = {'lang_mark': True} self.worker.config = { 'ebook_metadata': metadata_config, @@ -265,7 +270,7 @@ def test_translate_done_other_to_path( self.ebook.custom_title = 'test: custom title*' self.ebook.target_lang = 'German' self.worker.working_jobs = { - self.job: (self.ebook, '/path/to/test.srt')} + self.job: (self.ebook, str(Path('/path/to/test.srt')))} metadata = Mock() self.worker.api.get_metadata.return_value = metadata @@ -273,14 +278,14 @@ def test_translate_done_other_to_path( self.worker.api.get_metadata.assert_called_once_with(89) mock_os_rename.assert_called_once_with( - '/path/to/test.srt', - '/path/to/test_ custom title_ [German].srt') + str(Path('/path/to/test.srt')), + str(Path('/path/to/test_ custom title_ [German].srt'))) self.worker.gui.status_bar.show_message.assert_called_once_with( 'test description ' + 'completed', 5000) arguments = self.worker.gui.proceed_question.mock_calls[0].args self.assertIsInstance(arguments[0], Callable) self.assertIs(self.worker.gui.job_manager.launch_gui_app, arguments[1]) - self.assertEqual('/path/to/log', arguments[2]) + self.assertEqual(str(Path('/path/to/log')), arguments[2]) self.assertEqual('Ebook Translation Log', arguments[3]) self.assertEqual('Translation Completed', arguments[4]) self.assertEqual( @@ -291,7 +296,7 @@ def test_translate_done_other_to_path( mock_payload = Mock() arguments[0](mock_payload) mock_open_path.assert_called_once_with( - '/path/to/test_ custom title_ [German].srt') + str(Path('/path/to/test_ custom title_ [German].srt'))) arguments = self.worker.gui.proceed_question.mock_calls[0].kwargs self.assertEqual(True, arguments.get('log_is_file')) diff --git a/tests/test_engine.py b/tests/test_engine.py index b1784dc..08f25ee 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -2,6 +2,7 @@ import re import json import unittest +from pathlib import Path from types import GeneratorType from unittest.mock import call, patch, Mock @@ -161,23 +162,26 @@ def test_need_swap_api_key(self): @patch(module_name + '.base.os.path.isfile') def test_get_external_program(self, mock_os_path_isfile): - mock_os_path_isfile.side_effect = lambda p: p in [ - '/path/to/real', '/path/to/folder/real', '/path/to/specify/real'] + mock_os_path_isfile.side_effect = lambda path: path in [ + str(Path('/path/to/real')), str(Path('/path/to/folder/real')), + str(Path('/path/to/specify/real'))] - self.translator.search_paths = ['/path/to/real'] + self.translator.search_paths = [str(Path('/path/to/real'))] self.assertEqual( - '/path/to/real', self.translator.get_external_program('real')) + str(Path('/path/to/real')), + self.translator.get_external_program('real')) - self.translator.search_paths = ['/path/to/folder'] + self.translator.search_paths = [str(Path('/path/to/folder'))] self.assertEqual( - '/path/to/folder/real', + str(Path('/path/to/folder/real')), self.translator.get_external_program('real')) self.assertEqual( - '/path/to/specify/real', - self.translator.get_external_program('real', ['/path/to/specify'])) + str(Path('/path/to/specify/real')), + self.translator.get_external_program( + 'real', [str(Path('/path/to/specify'))])) self.assertIsNone( - self.translator.get_external_program('/path/to/fake')) + self.translator.get_external_program(str(Path('/path/to/fake')))) @patch(module_name + '.base.request') def test_translate(self, mock_request): diff --git a/tests/test_utils.py b/tests/test_utils.py index 696b074..24d6fb1 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -92,7 +92,6 @@ def test_request_output_as_string( cert_reqs=mock_ssl.CERT_NONE) browser.set_ca_data.assert_called_once_with( context=mock_ssl._create_unverified_context()) - browser.set_proxies.assert_not_called() mock_request.assert_called_once_with( 'https://example.com/api', 'test data', headers={}, timeout=30, @@ -115,7 +114,6 @@ def test_request_output_as_bytes( cert_reqs=mock_ssl.CERT_NONE) browser.set_ca_data.assert_called_once_with( context=mock_ssl._create_unverified_context()) - browser.set_proxies.assert_not_called() mock_request.assert_called_once_with( 'https://example.com/api', 'test data', headers={}, timeout=30, @@ -137,7 +135,6 @@ def test_request_with_stream(self, mock_browser, mock_request, mock_ssl): cert_reqs=mock_ssl.CERT_NONE) browser.set_ca_data.assert_called_once_with( context=mock_ssl._create_unverified_context()) - browser.set_proxies.assert_not_called() mock_request.assert_called_once_with( 'https://example.com/api', 'test data', headers={}, timeout=30,