-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
81 lines (62 loc) · 2.64 KB
/
test.py
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
import datetime
import unittest
from unittest.mock import patch
import backup
class TestMakeExcludeDir(unittest.TestCase):
def test_empty(self):
expected = ""
actual = backup.makeExcludeDir([])
self.assertEqual(expected, actual)
def test_single(self):
expected = "--exclude='path'"
actual = backup.makeExcludeDir(["path"])
self.assertEqual(expected, actual)
def test_multiple(self):
expected = "--exclude='path1' --exclude='path2'"
actual = backup.makeExcludeDir(["path1", "path2"])
self.assertEqual(expected, actual)
@patch("os.chdir")
@patch("os.system")
class TestAddFile(unittest.TestCase):
def test_normal_call(self, system_mock, chdir_mock):
expected_directory = "/home/someuser/somedir"
expected_cmd = "tar -r --exclude='path' --file=arch f1"
backup.addFile(expected_directory, "arch", "f1", "--exclude='path'")
system_mock.assert_called_once_with(expected_cmd)
chdir_mock.assert_called_once_with(expected_directory)
def test_no_exclude(self, system_mock, chdir_mock):
expected_directory = "/home/someuser/somedir"
expected_cmd = "tar -r --file=arch f1"
backup.addFile(expected_directory, "arch", "f1", "")
system_mock.assert_called_once_with(expected_cmd)
chdir_mock.assert_called_once_with(expected_directory)
@patch("os.path.exists")
@patch("backup.addFile")
@patch("backup.getFilesToCompress")
class TestCompress(unittest.TestCase):
def test_normal_call(self, getFilesToCompress_mock, addFile_mock, path_exists_mock):
bu_files = "bu_files"
target_file_name = "target"
to_compress = [("/somedir", "comp")]
to_exclude = ["excl"]
getFilesToCompress_mock.return_value = [to_compress, to_exclude]
to_exclude_str = backup.makeExcludeDir(to_exclude)
path_exists_mock.return_value = True
backup.Compress(bu_files, target_file_name)
addFile_mock.assert_called_once_with(
to_compress[0][0], target_file_name, to_compress[0][1], to_exclude_str
)
@patch("os.getcwd")
@patch("datetime.date")
class TestGetDefaultFileName(unittest.TestCase):
def test_normal_call(self, date_mock, getcwd_mock):
dir = "/some/dir"
day = datetime.date(1991, 3, 26)
date_str = str(day.year) + "_" + str(day.month) + "_" + str(day.day)
date_mock.today.return_value = day
getcwd_mock.return_value = dir
expected = "{}/backup_{}.tar".format(dir, date_str)
actual = backup.getDefaultFileName()
self.assertEqual(expected, actual)
if __name__ == "__main__":
unittest.main()