aboutsummaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: f8d2c7e3861796cb9d9d784de953294611f24097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# wikiget - CLI tool for downloading files from Wikimedia sites
# Copyright (C) 2023 Cody Logan
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Wikiget is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Wikiget is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Wikiget. If not, see <https://www.gnu.org/licenses/>.

"""Define fixtures used across all tests in this folder."""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from wikiget.file import File

if TYPE_CHECKING:
    from pathlib import Path

    import requests_mock as rm

# 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.

    Args:
        tmp_path_factory (pytest.TempPathFactor): temporary path generator
        monkeypatch (pytest.MonkeyPatch): Pytest monkeypatch helper
    """
    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.

    Args:
        tmp_path_factory (pytest.TempPathFactor): temporary path generator

    Returns:
        pathlib.Path: test batch file
    """
    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.

    Args:
        tmp_path_factory (pytest.TempPathFactor): temporary path generator

    Returns:
        pathlib.Path: test batch file
    """
    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.

    Args:
        tmp_path_factory (pytest.TempPathFactor): temporary path generator

    Returns:
        pathlib.Path: test file
    """
    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:
    """Create a test File with only a filename.

    A File object created with only a name should set its destination property to
    the same value and its site property to the program's default site.

    Returns:
        wikiget.file.File: File object created using a filename
    """
    return File("foobar.jpg")


@pytest.fixture()
def file_with_name_and_dest() -> File:
    """Create a test File with a name and destination.

    Returns:
        wikiget.file.File: File object created with name and dest
    """
    return File(name="foobar.jpg", dest="bazqux.jpg")


@pytest.fixture()
def _mock_get(requests_mock: rm.Mocker) -> None:
    """Fake the download request for the true URL of File:Example.jpg.

    Args:
        requests_mock (requests_mock.Mocker): a requests_mock Mocker object
    """
    requests_mock.get(
        "https://upload.wikimedia.org/wikipedia/commons/a/a9/Example.jpg",
        content=TEST_FILE_BYTES,
    )