summaryrefslogtreecommitdiff
path: root/tests/todo/todo_rename.py
blob: 89fe7d2fbef0898933141f4f22c58462bf01edef (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
import os
from pathlib import Path

from organize.actions import Rename

USER_DIR = os.path.expanduser("~")

ARGS = {"basedir": Path.home(), "path": Path.home() / "test.py", "simulate": False}


def test_tilde_expansion(mock_exists, mock_samefile, mock_rename, mock_trash):
    mock_exists.return_value = False
    mock_samefile.return_value = False
    rename = Rename(name="newname.py", overwrite=False)
    new_path = rename.run(**ARGS)
    assert mock_exists.call_count > 0
    mock_trash.assert_not_called()
    expected_path = (Path.home() / "newname.py").expanduser()
    mock_rename.assert_called_with(expected_path)
    assert new_path == {"path": expected_path}


def test_overwrite(mock_exists, mock_samefile, mock_rename, mock_trash):
    mock_exists.return_value = True
    mock_samefile.return_value = False
    rename = Rename(name="{path.stem} Kopie.py", overwrite=True)
    new_path = rename.run(**ARGS)
    assert mock_exists.call_count > 0
    mock_trash.assert_called_with(os.path.join(USER_DIR, "test Kopie.py"))
    mock_rename.assert_called_with(Path("~/test Kopie.py").expanduser())
    assert new_path is not None


def test_already_exists(mock_exists, mock_samefile, mock_rename, mock_trash):
    mock_exists.side_effect = [True, False]
    mock_samefile.return_value = False
    rename = Rename(name="asd.txt", overwrite=False)
    new_path = rename.run(**ARGS)
    assert mock_exists.call_count > 0
    mock_trash.assert_not_called()
    mock_rename.assert_called_with(Path("~/asd 2.txt").expanduser())
    assert new_path is not None


def test_overwrite_samefile(mock_exists, mock_samefile, mock_rename, mock_trash):
    args = {"basedir": Path.home(), "path": Path.home() / "test.PDF", "simulate": False}
    mock_exists.return_value = True
    mock_samefile.return_value = True
    rename = Rename(name="{path.stem}.pdf", overwrite=False)
    new_path = rename.run(**args)
    assert mock_exists.call_count > 0
    mock_trash.assert_not_called()
    mock_rename.assert_called_with((Path.home() / "test.pdf").expanduser())
    assert new_path is not None


def test_keep_name(mock_exists, mock_samefile, mock_rename, mock_trash):
    args = {"basedir": Path.home(), "path": Path.home() / "test.pdf", "simulate": False}
    mock_exists.return_value = True
    mock_samefile.return_value = True
    rename = Rename(name="{path.stem}.pdf", overwrite=False)
    new_path = rename.run(**args)
    assert mock_exists.call_count > 0
    mock_trash.assert_not_called()
    mock_rename.assert_not_called()
    assert new_path is not None


def test_already_exists_multiple(mock_exists, mock_samefile, mock_rename, mock_trash):
    mock_exists.side_effect = [True, True, True, False]
    mock_samefile.return_value = False
    rename = Rename(name="asd.txt", overwrite=False)
    new_path = rename.run(**ARGS)
    assert mock_exists.call_count > 0
    mock_trash.assert_not_called()
    mock_rename.assert_called_with(Path("~/asd 4.txt").expanduser())
    assert new_path is not None


def test_already_exists_multiple_separator(
    mock_exists, mock_samefile, mock_rename, mock_trash
):
    mock_exists.side_effect = [True, True, True, False]
    mock_samefile.return_value = False
    rename = Rename(name="asd.txt", overwrite=False, counter_separator="-")
    new_path = rename.run(**ARGS)
    assert mock_exists.call_count > 0
    mock_trash.assert_not_called()
    mock_rename.assert_called_with(Path("~/asd-4.txt").expanduser())
    assert new_path is not None


def test_args(mock_exists, mock_samefile, mock_rename, mock_trash):
    args = {
        "basedir": Path.home(),
        "path": Path.home() / "test.py",
        "nr": {"upper": 1},
        "simulate": False,
    }

    mock_exists.return_value = False
    mock_samefile.return_value = False
    rename = Rename(name="{nr.upper}-{path.stem} Kopie.py")
    new_path = rename.run(**args)
    assert mock_exists.call_count > 0
    mock_trash.assert_not_called()
    mock_rename.assert_called_with(Path("~/1-test Kopie.py").expanduser())
    assert new_path is not None