test_files.py 5.82 KB
Newer Older
David Runge's avatar
David Runge committed
1
import os
2
import shutil
David Runge's avatar
David Runge committed
3
import tarfile
4
import tempfile
David Runge's avatar
David Runge committed
5
6
from contextlib import nullcontext as does_not_raise
from pathlib import Path
7
from typing import Iterator
David Runge's avatar
David Runge committed
8

9
from pytest import fixture, mark, raises
David Runge's avatar
David Runge committed
10

11
from repo_management import convert, defaults, errors, files, models
David Runge's avatar
David Runge committed
12

13
from .fixtures import create_db_file, create_empty_json_files
David Runge's avatar
David Runge committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34


@fixture(scope="function")
def create_gz_db_file() -> Iterator[Path]:
    db_file = create_db_file()
    yield db_file
    os.remove(db_file)


@fixture(scope="function")
def create_bzip_db_file() -> Iterator[Path]:
    db_file = create_db_file(compression="bz2")
    yield db_file
    os.remove(db_file)


@fixture(scope="function")
def create_null_db_file() -> Iterator[Path]:
    yield create_db_file(remove_db=True)


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
@fixture(scope="function")
def empty_json_files_in_dir() -> Iterator[Path]:
    directory = create_empty_json_files()
    yield directory
    shutil.rmtree(directory)


@fixture(scope="function")
def empty_dir() -> Iterator[Path]:
    directory = tempfile.mkdtemp()
    yield Path(directory)
    shutil.rmtree(directory)


@fixture(scope="function")
def empty_file() -> Iterator[Path]:
    [foo, file_name] = tempfile.mkstemp()
    yield Path(file_name)


@fixture(scope="function")
def broken_json_file() -> Iterator[Path]:
    [foo, json_file] = tempfile.mkstemp(suffix=".json")
    with open(json_file, "w") as input_file:
        input_file.write("garbage")
    yield Path(json_file)


@fixture(scope="function")
def invalid_json_file() -> Iterator[Path]:
    [foo, json_file] = tempfile.mkstemp(suffix=".json")
    with open(json_file, "w") as input_file:
        input_file.write('{"foo": "bar"}')
    yield Path(json_file)


David Runge's avatar
David Runge committed
71
72
def test__read_db_file(create_gz_db_file: Path) -> None:
    with does_not_raise():
David Runge's avatar
David Runge committed
73
        assert files._read_db_file(create_gz_db_file)
David Runge's avatar
David Runge committed
74
75
76
77


def test__read_db_file_wrong_compression(create_gz_db_file: Path) -> None:
    with raises(tarfile.CompressionError):
David Runge's avatar
David Runge committed
78
        assert files._read_db_file(create_gz_db_file, compression="foo")
David Runge's avatar
David Runge committed
79
80
81
82


def test__read_db_file_does_not_exist(create_null_db_file: Path) -> None:
    with raises(FileNotFoundError):
David Runge's avatar
David Runge committed
83
        assert files._read_db_file(create_null_db_file)
David Runge's avatar
David Runge committed
84
85
86
87


def test__read_db_file_wrong_db_compression(create_bzip_db_file: Path) -> None:
    with raises(tarfile.ReadError):
David Runge's avatar
David Runge committed
88
89
90
        assert files._read_db_file(create_bzip_db_file)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
def test__read_db_file_member_as_model(create_gz_db_file: Path) -> None:
    for member in files._db_file_member_as_model(db_file=files._read_db_file(create_gz_db_file)):
        assert isinstance(member, models.RepoDbMemberData)


@mark.parametrize(
    "member_name, result",
    [
        ("foo-bar-1.0.0-42", "foo-bar"),
        ("foobar-1.0.0-42", "foobar"),
        ("foo-bar-1.0.0-42/desc", "foo-bar"),
        ("foo-bar-1.0.0-42/files", "foo-bar"),
        ("foobar-1.0.0-42/desc", "foobar"),
        ("foobar-1.0.0-42/files", "foobar"),
    ],
)
def test__extract_db_member_package_name(
    member_name: str,
    result: str,
) -> None:
    assert files._extract_db_member_package_name(name=member_name) == result
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150


def test__json_files_in_directory(empty_json_files_in_dir: Path, empty_dir: Path) -> None:
    for json_file in files._json_files_in_directory(path=empty_json_files_in_dir):
        assert isinstance(json_file, Path)

    with raises(errors.RepoManagementFileNotFoundError):
        for json_file in files._json_files_in_directory(path=empty_dir):
            assert isinstance(json_file, Path)


def test__read_pkgbase_json_file(broken_json_file: Path, invalid_json_file: Path) -> None:
    with raises(errors.RepoManagementFileError):
        files._read_pkgbase_json_file(path=broken_json_file)
    with raises(errors.RepoManagementValidationError):
        files._read_pkgbase_json_file(path=invalid_json_file)


def test__write_db_file(empty_dir: Path) -> None:
    assert isinstance(files._write_db_file(empty_dir / Path("foo.db")), tarfile.TarFile)


@mark.parametrize(
    "model, db_type",
    [
        (
            models.OutputPackageBase(
                base="foo",
                packager="foobar",
                version="1.0.0-1",
                packages=[
                    models.OutputPackage(
                        arch="foo",
                        builddate=1,
                        csize=1,
                        desc="foo",
                        filename="foo",
                        files=["foo", "bar"],
                        isize=1,
151
                        license=["foo"],
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
                        md5sum="foo",
                        name="foo",
                        pgpsig="foo",
                        sha256sum="foo",
                        url="foo",
                    ),
                ],
            ),
            defaults.RepoDbType.DEFAULT,
        ),
        (
            models.OutputPackageBase(
                base="foo",
                packager="foobar",
                version="1.0.0-1",
                packages=[
                    models.OutputPackage(
                        arch="foo",
                        builddate=1,
                        csize=1,
                        desc="foo",
                        filename="foo",
                        files=["foo", "bar"],
                        isize=1,
176
                        license=["foo"],
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
                        md5sum="foo",
                        name="foo",
                        pgpsig="foo",
                        sha256sum="foo",
                        url="foo",
                    ),
                ],
            ),
            defaults.RepoDbType.FILES,
        ),
    ],
)
def test__stream_package_base_to_db(
    model: models.OutputPackageBase,
    db_type: defaults.RepoDbType,
    empty_file: Path,
) -> None:
    files._stream_package_base_to_db(
        db=files._write_db_file(path=empty_file),
        model=model,
        repodbfile=convert.RepoDbFile(),
        db_type=db_type,
    )