Skip to content

Commit

Permalink
Merge pull request #15 from MrTyton/testing_updates
Browse files Browse the repository at this point in the history
make the check system agnostic
  • Loading branch information
MrTyton authored Sep 26, 2024
2 parents f9f48ec + 2bc7f6f commit 1a5a217
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 92 deletions.
51 changes: 39 additions & 12 deletions root/app/calibre_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest.mock import MagicMock, mock_open, patch
from parameterized import parameterized
import unittest
import os

from calibre_info import CalibreInfo

Expand Down Expand Up @@ -30,29 +31,39 @@ class ConfigCase(NamedTuple):
"path": "test_path",
"username": "test_username",
"password": "test_password",
"default_ini": "test_default_ini\\defaults.ini",
"personal_ini": "test_personal_ini\\personal.ini",
"default_ini": os.path.join(
"test_default_ini", "defaults.ini"
),
"personal_ini": os.path.join(
"test_personal_ini", "personal.ini"
),
}
},
),
# Test case: default_ini and personal_ini already end with "/defaults.ini" and "/personal.ini"
ConfigCase(
toml_path="path/to/yet_another_config.toml",
config="""
config=f"""
[calibre]
path = "yet_another_test_path"
username = "yet_another_test_username"
password = "yet_another_test_password"
default_ini = "yet_another_test_default_ini/defaults.ini"
personal_ini = "yet_another_test_personal_ini/personal.ini"
""",
default_ini = "{os.path.join("yet_another_test_default_ini", "defaults.ini")}"
personal_ini = "{os.path.join("yet_another_test_personal_ini","personal.ini")}"
""".replace(
"\\", "\\\\"
), # Replace backslashes with double backslashes
expected_config={
"calibre": {
"path": "yet_another_test_path",
"username": "yet_another_test_username",
"password": "yet_another_test_password",
"default_ini": "yet_another_test_default_ini/defaults.ini",
"personal_ini": "yet_another_test_personal_ini/personal.ini",
"default_ini": os.path.join(
"yet_another_test_default_ini", "defaults.ini"
),
"personal_ini": os.path.join(
"yet_another_test_personal_ini", "personal.ini"
),
}
},
),
Expand All @@ -75,23 +86,33 @@ class ConfigCase(NamedTuple):
@patch("multiprocessing.Manager")
@patch("calibre_info.ff_logging.log_failure")
def test_calibre_info_init(
self, toml_path, config, expected_config, mock_log, mock_manager, mock_file, mock_isfile
self,
toml_path,
config,
expected_config,
mock_log,
mock_manager,
mock_file,
mock_isfile,
):
mock_file.return_value.read.return_value = str(config).encode()
mock_manager.return_value = MagicMock()
# TODO: Actually test this.
mock_isfile.return_value = True
if isinstance(expected_config, dict):
calibre_info = CalibreInfo(toml_path, mock_manager())
self.assertEqual(calibre_info.location, expected_config["calibre"]["path"])
self.assertEqual(
calibre_info.location, expected_config["calibre"]["path"]
)
self.assertEqual(
calibre_info.username, expected_config["calibre"]["username"]
)
self.assertEqual(
calibre_info.password, expected_config["calibre"]["password"]
)
self.assertEqual(
calibre_info.default_ini, expected_config["calibre"]["default_ini"]
calibre_info.default_ini,
expected_config["calibre"]["default_ini"],
)
self.assertEqual(
calibre_info.personal_ini,
Expand Down Expand Up @@ -177,7 +198,13 @@ class StrRepresentationCase(NamedTuple):
@patch("multiprocessing.Manager")
@patch("builtins.open", new_callable=mock_open)
def test_str_representation(
self, location, username, password, expected_result, mock_file, mock_manager
self,
location,
username,
password,
expected_result,
mock_file,
mock_manager,
):
mock_manager.return_value = MagicMock()

Expand Down
146 changes: 81 additions & 65 deletions root/app/regex_parsing_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re
import unittest
from typing import NamedTuple, Optional
Expand All @@ -17,13 +18,24 @@ class CheckFilenameExtractionTestCase(NamedTuple):
@parameterized.expand(
[
# Test case: Extract 'story' from 'story-name-1234'
CheckFilenameExtractionTestCase(input="story-name-1234", expected="story"),
CheckFilenameExtractionTestCase(
input="story-name-1234", expected="story"
),
# Test case: Extract 'author' from 'author-name'
CheckFilenameExtractionTestCase(input="author-name", expected="author"),
CheckFilenameExtractionTestCase(
input="author-name", expected="author"
),
# Test case: Extract 'story' from '/path/story-name-1234.epub'
CheckFilenameExtractionTestCase(input="/path/story-name-1234.epub", expected="story"),
CheckFilenameExtractionTestCase(
input=os.path.join("path", "story-name-1234.epub"),
expected="story",
),
# Test case: Extract 'story' from '\\path\\to\\story-name-1234.epub'
CheckFilenameExtractionTestCase(input="\\path\\to\\story-name-1234.epub", expected="story"), ]
CheckFilenameExtractionTestCase(
input=os.path.join("path", "to", "story-name-1234.epub"),
expected="story",
),
]
)
def test_extract_filename(self, input, expected):
self.assertEqual(regex_parsing.extract_filename(input), expected)
Expand Down Expand Up @@ -54,7 +66,12 @@ class CheckRegexesTestCase(NamedTuple):
)
@patch("regex_parsing.ff_logging.log_failure")
def test_check_regexes(
self, test_output, test_pattern, test_message, expected_result, mock_log_failure
self,
test_output,
test_pattern,
test_message,
expected_result,
mock_log_failure,
):
self.assertEqual(
regex_parsing.check_regexes(
Expand All @@ -72,28 +89,26 @@ class CheckRegexFailuresTestCase(NamedTuple):
expected: bool
message: Optional[str]

@parameterized.expand([
# Test case: Output contains 5 chapters, expected failure, with a specific message
CheckRegexFailuresTestCase(
output="test output already contains 5 chapters.",
expected=False,
message="Issue with story, site is broken. Story likely hasn't updated on site yet."
),
# Test case: Output doesn't contain any recognizable chapters, expected failure, with a specific message
CheckRegexFailuresTestCase(
output="test output doesn't contain any recognizable chapters, probably from a different source. Not updating.",
expected=False,
message="Something is messed up with the site or the epub. No chapters found."
),
# Test case: Generic output, expected success, no specific message
CheckRegexFailuresTestCase(
output="test output",
expected=True,
message=None
),
])
@parameterized.expand(
[
# Test case: Output contains 5 chapters, expected failure, with a specific message
CheckRegexFailuresTestCase(
output="test output already contains 5 chapters.",
expected=False,
message="Issue with story, site is broken. Story likely hasn't updated on site yet.",
),
# Test case: Output doesn't contain any recognizable chapters, expected failure, with a specific message
CheckRegexFailuresTestCase(
output="test output doesn't contain any recognizable chapters, probably from a different source. Not updating.",
expected=False,
message="Something is messed up with the site or the epub. No chapters found.",
),
# Test case: Generic output, expected success, no specific message
CheckRegexFailuresTestCase(
output="test output", expected=True, message=None
),
]
)
@patch("regex_parsing.ff_logging.log_failure")
def test_check_failure_regexes(
self, input, expected, log_message, mock_log_failure
Expand All @@ -109,28 +124,26 @@ class CheckForceableRegexTestCase(NamedTuple):
expected: bool
message: Optional[str]

@parameterized.expand([
# Test case: Output contains 5 chapters, more than source: 3, expected True, with a specific message
CheckForceableRegexTestCase(
output="test output contains 5 chapters, more than source: 3.",
expected=True,
message="Chapter difference between source and destination. Forcing update."
),
# Test case: File has been updated more recently than the story, expected True, with a specific message
CheckForceableRegexTestCase(
output="File(test.epub) Updated(2022-01-01) more recently than Story(2021-12-31) - Skipping",
expected=True,
message="File has been updated more recently than the story, this is likely a metadata bug. Forcing update."
),
# Test case: Generic output, expected False, no specific message
CheckForceableRegexTestCase(
output="test output",
expected=False,
message=None
),
])
@parameterized.expand(
[
# Test case: Output contains 5 chapters, more than source: 3, expected True, with a specific message
CheckForceableRegexTestCase(
output="test output contains 5 chapters, more than source: 3.",
expected=True,
message="Chapter difference between source and destination. Forcing update.",
),
# Test case: File has been updated more recently than the story, expected True, with a specific message
CheckForceableRegexTestCase(
output="File(test.epub) Updated(2022-01-01) more recently than Story(2021-12-31) - Skipping",
expected=True,
message="File has been updated more recently than the story, this is likely a metadata bug. Forcing update.",
),
# Test case: Generic output, expected False, no specific message
CheckForceableRegexTestCase(
output="test output", expected=False, message=None
),
]
)
@patch("regex_parsing.ff_logging.log_failure")
def test_check_forceable_regexes(
self, input, expected, log_message, mock_log_failure
Expand All @@ -146,22 +159,25 @@ class CheckGenerateFanficInfoTestCase(NamedTuple):
expected_url: str
expected_site: str

@parameterized.expand([
# Test case: Fanfiction.net URL
CheckGenerateFanficInfoTestCase(
url="https://www.fanfiction.net/s/1234",
expected_url="www.fanfiction.net/s/1234",
expected_site="ffnet"
),
# Test case: Archive of Our Own URL
CheckGenerateFanficInfoTestCase(
url="https://archiveofourown.org/works/5678",
expected_url="archiveofourown.org/works/5678",
expected_site="ao3"
),
])
def test_generate_FanficInfo_from_url(self, input_url, expected_url, expected_site):
@parameterized.expand(
[
# Test case: Fanfiction.net URL
CheckGenerateFanficInfoTestCase(
url="https://www.fanfiction.net/s/1234",
expected_url="www.fanfiction.net/s/1234",
expected_site="ffnet",
),
# Test case: Archive of Our Own URL
CheckGenerateFanficInfoTestCase(
url="https://archiveofourown.org/works/5678",
expected_url="archiveofourown.org/works/5678",
expected_site="ao3",
),
]
)
def test_generate_FanficInfo_from_url(
self, input_url, expected_url, expected_site
):
fanfic = regex_parsing.generate_FanficInfo_from_url(input_url)
self.assertIsInstance(fanfic, fanfic_info.FanficInfo)
self.assertEqual(fanfic.url, expected_url)
Expand Down
43 changes: 28 additions & 15 deletions root/app/system_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
get_files,
copy_configs_to_temp_dir,
)
import os
from typing import NamedTuple, Optional


Expand All @@ -28,25 +29,25 @@ class GetFilesTestCase(NamedTuple):
@parameterized.expand(
[
GetFilesTestCase(
directory_path="/fake/dir",
directory_path=os.path.join("fake", "dir"),
file_extension=None,
return_full_path=False,
expected_files=["file1.txt", "file2.py", "file3.txt"],
),
GetFilesTestCase(
directory_path="/fake/dir",
directory_path=os.path.join("fake", "dir"),
file_extension=".txt",
return_full_path=False,
expected_files=["file1.txt", "file3.txt"],
),
GetFilesTestCase(
directory_path="/fake/dir",
directory_path=os.path.join("fake", "dir"),
file_extension=None,
return_full_path=True,
expected_files=[
"/fake/dir\\file1.txt",
"/fake/dir\\file2.py",
"/fake/dir\\file3.txt",
os.path.join("fake", "dir", "file1.txt"),
os.path.join("fake", "dir", "file2.py"),
os.path.join("fake", "dir", "file3.txt"),
],
),
]
Expand Down Expand Up @@ -76,25 +77,37 @@ class CopyConfigsTestCase(NamedTuple):
@parameterized.expand(
[
CopyConfigsTestCase(
default_ini="/fake/path/defaults.ini",
personal_ini="/fake/path/personal.ini",
default_ini=os.path.join("fake", "path", "defaults.ini"),
personal_ini=os.path.join("fake", "path", "personal.ini"),
expected_calls=[
("/fake/path/defaults.ini", "/fake/temp/dir\\defaults.ini"),
("/fake/path/personal.ini", "/fake/temp/dir\\personal.ini"),
(
os.path.join("fake", "path", "defaults.ini"),
os.path.join("fake", "temp", "dir", "defaults.ini"),
),
(
os.path.join("fake", "path", "personal.ini"),
os.path.join("fake", "temp", "dir", "personal.ini"),
),
],
),
CopyConfigsTestCase(
default_ini=None,
personal_ini="/fake/path/personal.ini",
personal_ini=os.path.join("fake", "path", "personal.ini"),
expected_calls=[
("/fake/path/personal.ini", "/fake/temp/dir\\personal.ini"),
(
os.path.join("fake", "path", "personal.ini"),
os.path.join("fake", "temp", "dir", "personal.ini"),
),
],
),
CopyConfigsTestCase(
default_ini="/fake/path/defaults.ini",
default_ini=os.path.join("fake", "path", "defaults.ini"),
personal_ini=None,
expected_calls=[
("/fake/path/defaults.ini", "/fake/temp/dir\\defaults.ini"),
(
os.path.join("fake", "path", "defaults.ini"),
os.path.join("fake", "temp", "dir", "defaults.ini"),
),
],
),
]
Expand All @@ -111,7 +124,7 @@ def test_copy_configs_to_temp_dir(
cdb = MagicMock()
cdb.default_ini = default_ini
cdb.personal_ini = personal_ini
copy_configs_to_temp_dir(cdb, "/fake/temp/dir")
copy_configs_to_temp_dir(cdb, os.path.join("fake", "temp", "dir"))
for call_args in expected_calls:
mock_copyfile.assert_any_call(*call_args)
self.assertEqual(mock_copyfile.call_count, len(expected_calls))
Expand Down

0 comments on commit 1a5a217

Please sign in to comment.