aboutsummaryrefslogtreecommitdiff
path: root/tests/conftest.py
diff options
context:
space:
mode:
authorCody Logan <cody@lokken.dev>2023-11-14 16:44:34 -0800
committerCody Logan <cody@lokken.dev>2023-11-14 16:44:34 -0800
commit605085c95fe5ab6af337aefc723794f78cf3dfd7 (patch)
treef0dfba6e4fe1fa71a0ba48ecd6f098c66c03f30e /tests/conftest.py
parent733eeb27581ee6fc2a9c2d79d7b002127bde85f1 (diff)
downloadwikiget-605085c95fe5ab6af337aefc723794f78cf3dfd7.tar.gz
wikiget-605085c95fe5ab6af337aefc723794f78cf3dfd7.zip
Use fixtures to create test files
This reduces the number of temporary folders and files created during testing. Additionally, an actual JPEG is created for a couple tests instead of using random text for the contents.
Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py73
1 files changed, 63 insertions, 10 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 8b1fea9..3eb99cd 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -25,15 +25,68 @@ import requests_mock as rm
from wikiget.file import File
+# 2x2 JPEG
+TEST_FILE_BYTES = (
+ b"\xff\xd8\xff\xdb\x00C\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03\x03\x03\x03"
+ b"\x04\x06\x04\x04\x04\x04\x04\x08\x06\x06\x05\x06\t\x08\n\n\t\x08\t\t\n\x0c\x0f"
+ b"\x0c\n\x0b\x0e\x0b\t\t\r\x11\r\x0e\x0f\x10\x10\x11\x10\n\x0c\x12\x13\x12\x10\x13"
+ b"\x0f\x10\x10\x10\xff\xc0\x00\x0b\x08\x00\x02\x00\x02\x01\x01\x11\x00\xff\xc4\x00"
+ b"\x14\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\t\xff"
+ b"\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
+ b"\x00\xff\xda\x00\x08\x01\x01\x00\x00?\x00T\xdf\xff\xd9"
+)
+
+
+@pytest.fixture(autouse=True, scope="session")
+def _chdir_to_tmp_dir(tmp_path_factory: pytest.TempPathFactory) -> None:
+ """Change to the base temporary directory before running tests.
+
+ :param tmp_path_factory: temporary path generator
+ :type tmp_path_factory: pytest.TempPathFactory
+ """
+ chdir(tmp_path_factory.getbasetemp())
+
+
+@pytest.fixture(scope="session")
+def batch_file(tmp_path_factory: pytest.TempPathFactory) -> Path:
+ """Create a temporary batch file for testing.
+
+ :param tmp_path_factory: temporary path generator
+ :type tmp_path_factory: pytest.TempPathFactory
+ :return: test batch file
+ :rtype: pathlib.Path
+ """
+ tmp_file = tmp_path_factory.getbasetemp() / "batch.txt"
+ tmp_file.write_text("File:Foo.jpg\nFile:Bar.jpg\nFile:Baz.jpg\n")
+ return tmp_file
+
+
+@pytest.fixture(scope="session")
+def batch_file_with_comment(tmp_path_factory: pytest.TempPathFactory) -> Path:
+ """Create a temporary batch file with comments for testing.
+
+ :param tmp_path_factory: temporary path generator
+ :type tmp_path_factory: pytest.TempPathFactory
+ :return: test batch file
+ :rtype: pathlib.Path
+ """
+ tmp_file = tmp_path_factory.getbasetemp() / "batch_with_comment.txt"
+ tmp_file.write_text("File:Foo.jpg\n\n#File:Bar.jpg\nFile:Baz.jpg\n")
+ return tmp_file
+
-@pytest.fixture(autouse=True)
-def _chdir_to_tmp_dir(tmp_path: Path) -> None:
- """Change to a temporary directory before running tests.
+@pytest.fixture(scope="session")
+def test_file(tmp_path_factory: pytest.TempPathFactory) -> Path:
+ """Create a fake downloaded file with known contents.
- :param tmp_path: temporary path object
- :type tmp_path: Path
+ :param tmp_path_factory: temporary path generator
+ :type tmp_path_factory: pytest.TempPathFactory
+ :return: test file
+ :rtype: pathlib.Path
"""
- chdir(tmp_path)
+ tmp_file = tmp_path_factory.getbasetemp() / "Testfile.jpg"
+ tmp_file.write_bytes(TEST_FILE_BYTES)
+ return tmp_file
@pytest.fixture()
@@ -44,7 +97,7 @@ def file_with_name() -> File:
the same value and its site property to the program's default site.
:return: File object created using a filename
- :rtype: File
+ :rtype: wikiget.file.File
"""
return File("foobar.jpg")
@@ -54,7 +107,7 @@ def file_with_name_and_dest() -> File:
"""Create a test File with a name and destination.
:return: File object created with name and dest
- :rtype: File
+ :rtype: wikiget.file.File
"""
return File(name="foobar.jpg", dest="bazqux.jpg")
@@ -64,9 +117,9 @@ def _mock_get(requests_mock: rm.Mocker) -> None:
"""Fake the download request for the true URL of File:Example.jpg.
:param requests_mock: a requests_mock Mocker object
- :type requests_mock: rm.Mocker
+ :type requests_mock: requests_mock.Mocker
"""
requests_mock.get(
"https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg",
- text="data",
+ content=TEST_FILE_BYTES,
)