~homeworkprod/byceps

ref: ee44f550bd6c9f49e72424cd5826669dd1f57c1c byceps/tests/unit/util/test_upload.py -rw-r--r-- 1.5 KiB
ee44f550 — Jochen Kupperschmidt Avoid same German translation for different meanings of "cancel" 1 year, 6 months ago
                                                                                
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
"""
:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from io import BytesIO

import pytest

from byceps.util.upload import delete, store


SOURCE_BYTES = BytesIO(b'\x04\x08\x15\x16\x23\x42')


def test_store(tmp_path):
    target_path = tmp_path / 'famous-words.txt'
    assert not target_path.exists()

    store(SOURCE_BYTES, target_path)

    assert target_path.exists()


def test_store_with_existent_target_path(tmp_path):
    target_path = tmp_path / 'famous-words.txt'
    target_path.touch(exist_ok=False)
    assert target_path.exists()

    with pytest.raises(FileExistsError):
        store(SOURCE_BYTES, target_path)


def test_store_with_nonexistent_parent_path1(tmp_path):
    target_path = tmp_path / 'nonexistent' / 'famous-words.txt'

    with pytest.raises(FileNotFoundError):
        store(SOURCE_BYTES, target_path)


def test_store_with_nonexistent_parent_path2(tmp_path):
    target_path = tmp_path / 'nonexistent' / 'famous-words.txt'

    store(SOURCE_BYTES, target_path, create_parent_path_if_nonexistent=True)


# -------------------------------------------------------------------- #


def test_delete_with_existent_path(tmp_path):
    path = tmp_path / 'to-be-removed.png'
    path.touch(exist_ok=False)
    assert path.exists()

    delete(path)

    assert not path.exists()


def test_delete_with_nonexistent_path(tmp_path):
    path = tmp_path / 'fake.png'
    assert not path.exists()

    delete(path)