aboutsummaryrefslogtreecommitdiff
path: root/tests/conftest.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py77
1 files changed, 73 insertions, 4 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 5fccfc0..6088029 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -17,11 +17,80 @@
"""Define fixtures used across all tests in this folder."""
+from pathlib import Path
+
import pytest
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)
+def _chdir_to_tmp_dir(
+ tmp_path_factory: pytest.TempPathFactory, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """Change to the base temporary directory before running tests.
+
+ :param tmp_path_factory: temporary path generator
+ :type tmp_path_factory: pytest.TempPathFactory
+ :param tmp_path_factory: Pytest monkeypatch helper
+ :type tmp_path_factory: pytest.MonkeyPatch
+ """
+ monkeypatch.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(scope="session")
+def test_file(tmp_path_factory: pytest.TempPathFactory) -> Path:
+ """Create a fake downloaded file with known contents.
+
+ :param tmp_path_factory: temporary path generator
+ :type tmp_path_factory: pytest.TempPathFactory
+ :return: test file
+ :rtype: pathlib.Path
+ """
+ tmp_file = tmp_path_factory.getbasetemp() / "Testfile.jpg"
+ tmp_file.write_bytes(TEST_FILE_BYTES)
+ return tmp_file
+
@pytest.fixture()
def file_with_name() -> File:
@@ -31,7 +100,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")
@@ -41,7 +110,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")
@@ -51,9 +120,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,
)