Skip to content

Commit

Permalink
test: Resolve issues preventing test cases from running on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
bookfere committed Nov 4, 2024
1 parent c0d7204 commit aa1efd1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
49 changes: 27 additions & 22 deletions tests/test_conversion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from pathlib import Path
from typing import Callable
from unittest.mock import call, patch, Mock

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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'))
Expand All @@ -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,
Expand All @@ -203,19 +207,19 @@ 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)

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)
Expand All @@ -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(
Expand All @@ -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'))
Expand All @@ -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,
Expand All @@ -265,22 +270,22 @@ 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

self.worker.translate_done(self.job)

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(
Expand All @@ -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'))
Expand Down
22 changes: 13 additions & 9 deletions tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
3 changes: 0 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit aa1efd1

Please sign in to comment.