Repository navigation
Expand file tree
/
Copy pathtest_duplicate_scanner.py
More file actions
160 lines (138 loc) · 11.2 KB
/
Copy pathtest_duplicate_scanner.py
File metadata and controls
160 lines (138 loc) · 11.2 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import sys
import unittest
from pathlib import Path
from unittest.mock import Mock, MagicMock
# Add current directory to sys.path
sys.path.insert(0, str(Path(__file__).resolve().parent))
# Ensure google modules can be imported even if not installed in current environment
for mod in [
"google_auth_oauthlib",
"google_auth_oauthlib.flow",
"google.auth",
"google.auth.transport",
"google.auth.transport.requests",
"google.oauth2",
"google.oauth2.credentials",
"googleapiclient",
"googleapiclient.discovery",
"googleapiclient.errors",
"httplib2",
]:
if mod not in sys.modules:
sys.modules[mod] = MagicMock()
class MockHttpError(Exception):
def __init__(self, resp=None, content=b""):
self.resp = resp
self.content = content
import duplicate_scanner
if not isinstance(duplicate_scanner.HttpError, type) or not issubclass(duplicate_scanner.HttpError, BaseException):
duplicate_scanner.HttpError = MockHttpError
from duplicate_scanner import SelectionAssistant
class TestSelectionAssistant(unittest.TestCase):
def setUp(self):
self.mock_service = Mock()
self.duplicate_groups = {
"md5_1": [
{"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]},
{"id": "file1_newest", "name": "b_longer_name.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]},
{"id": "file1_middle", "name": "c.txt", "modifiedTime": "2023-01-01T11:00:00Z", "size": "150", "parents": ["folderA"]}
],
"md5_2": [
{"id": "file2_short", "name": "short.doc", "modifiedTime": "2023-03-01T10:00:00Z", "size": "500", "parents": ["folderC"]},
{"id": "file2_long", "name": "very_long_name.doc", "modifiedTime": "2023-03-02T10:00:00Z", "size": "400", "parents": ["folderD"]}
]
}
def test_mark_all_but_one_oldest(self):
assistant = SelectionAssistant(self.mock_service, self.duplicate_groups)
assistant.mark_all_but_one(keep_strategy='oldest')
files_to_trash = assistant.get_files_to_trash()
self.assertIn({"id": "file1_newest", "name": "b_longer_name.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]}, files_to_trash)
self.assertIn({"id": "file1_middle", "name": "c.txt", "modifiedTime": "2023-01-01T11:00:00Z", "size": "150", "parents": ["folderA"]}, files_to_trash)
self.assertNotIn({"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]}, files_to_trash)
self.assertIn({"id": "file2_long", "name": "very_long_name.doc", "modifiedTime": "2023-03-02T10:00:00Z", "size": "400", "parents": ["folderD"]}, files_to_trash)
self.assertNotIn({"id": "file2_short", "name": "short.doc", "modifiedTime": "2023-03-01T10:00:00Z", "size": "500", "parents": ["folderC"]}, files_to_trash)
self.assertEqual(len(files_to_trash), 3)
def test_mark_all_but_one_newest(self):
assistant = SelectionAssistant(self.mock_service, self.duplicate_groups)
assistant.mark_all_but_one(keep_strategy='newest')
files_to_trash = assistant.get_files_to_trash()
self.assertIn({"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]}, files_to_trash)
self.assertIn({"id": "file1_middle", "name": "c.txt", "modifiedTime": "2023-01-01T11:00:00Z", "size": "150", "parents": ["folderA"]}, files_to_trash)
self.assertNotIn({"id": "file1_newest", "name": "b.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]}, files_to_trash)
self.assertIn({"id": "file2_short", "name": "short.doc", "modifiedTime": "2023-03-01T10:00:00Z", "size": "500", "parents": ["folderC"]}, files_to_trash)
self.assertNotIn({"id": "file2_long", "name": "very_long_name.doc", "modifiedTime": "2023-03-02T10:00:00Z", "size": "400", "parents": ["folderD"]}, files_to_trash)
self.assertEqual(len(files_to_trash), 3)
def test_mark_all_but_one_smallest(self):
assistant = SelectionAssistant(self.mock_service, self.duplicate_groups)
assistant.mark_all_but_one(keep_strategy='smallest')
files_to_trash = assistant.get_files_to_trash()
self.assertIn({"id": "file1_newest", "name": "b_longer_name.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]}, files_to_trash)
self.assertIn({"id": "file1_middle", "name": "c.txt", "modifiedTime": "2023-01-01T11:00:00Z", "size": "150", "parents": ["folderA"]}, files_to_trash)
self.assertNotIn({"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]}, files_to_trash)
self.assertIn({"id": "file2_short", "name": "short.doc", "modifiedTime": "2023-03-01T10:00:00Z", "size": "500", "parents": ["folderC"]}, files_to_trash)
self.assertNotIn({"id": "file2_long", "name": "very_long_name.doc", "modifiedTime": "2023-03-02T10:00:00Z", "size": "400", "parents": ["folderD"]}, files_to_trash)
self.assertEqual(len(files_to_trash), 3)
def test_mark_all_but_one_largest(self):
assistant = SelectionAssistant(self.mock_service, self.duplicate_groups)
assistant.mark_all_but_one(keep_strategy='largest')
files_to_trash = assistant.get_files_to_trash()
self.assertIn({"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]}, files_to_trash)
self.assertIn({"id": "file1_middle", "name": "c.txt", "modifiedTime": "2023-01-01T11:00:00Z", "size": "150", "parents": ["folderA"]}, files_to_trash)
self.assertNotIn({"id": "file1_newest", "name": "b.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]}, files_to_trash)
self.assertIn({"id": "file2_long", "name": "very_long_name.doc", "modifiedTime": "2023-03-02T10:00:00Z", "size": "400", "parents": ["folderD"]}, files_to_trash)
self.assertNotIn({"id": "file2_short", "name": "short.doc", "modifiedTime": "2023-03-01T10:00:00Z", "size": "500", "parents": ["folderC"]}, files_to_trash)
self.assertEqual(len(files_to_trash), 3)
def test_mark_all_but_one_shortest_name(self):
assistant = SelectionAssistant(self.mock_service, self.duplicate_groups)
assistant.mark_all_but_one(keep_strategy='shortest_name')
files_to_trash = assistant.get_files_to_trash()
self.assertIn({"id": "file1_newest", "name": "b_longer_name.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]}, files_to_trash)
self.assertIn({"id": "file1_middle", "name": "c.txt", "modifiedTime": "2023-01-01T11:00:00Z", "size": "150", "parents": ["folderA"]}, files_to_trash)
self.assertNotIn({"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]}, files_to_trash)
self.assertIn({"id": "file2_long", "name": "very_long_name.doc", "modifiedTime": "2023-03-02T10:00:00Z", "size": "400", "parents": ["folderD"]}, files_to_trash)
self.assertNotIn({"id": "file2_short", "name": "short.doc", "modifiedTime": "2023-03-01T10:00:00Z", "size": "500", "parents": ["folderC"]}, files_to_trash)
self.assertEqual(len(files_to_trash), 3)
def test_mark_all_but_one_longest_name(self):
assistant = SelectionAssistant(self.mock_service, self.duplicate_groups)
assistant.mark_all_but_one(keep_strategy='longest_name')
files_to_trash = assistant.get_files_to_trash()
self.assertIn({"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]}, files_to_trash)
self.assertIn({"id": "file1_middle", "name": "c.txt", "modifiedTime": "2023-01-01T11:00:00Z", "size": "150", "parents": ["folderA"]}, files_to_trash)
self.assertNotIn({"id": "file1_newest", "name": "b.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]}, files_to_trash)
self.assertIn({"id": "file2_short", "name": "short.doc", "modifiedTime": "2023-03-01T10:00:00Z", "size": "500", "parents": ["folderC"]}, files_to_trash)
self.assertNotIn({"id": "file2_long", "name": "very_long_name.doc", "modifiedTime": "2023-03-02T10:00:00Z", "size": "400", "parents": ["folderD"]}, files_to_trash)
self.assertEqual(len(files_to_trash), 3)
def test_mark_by_folder(self):
assistant = SelectionAssistant(self.mock_service, self.duplicate_groups)
assistant.mark_by_folder(folder_id_to_trash="folderA")
files_to_trash = assistant.get_files_to_trash()
self.assertIn({"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]}, files_to_trash)
self.assertIn({"id": "file1_middle", "name": "c.txt", "modifiedTime": "2023-01-01T11:00:00Z", "size": "150", "parents": ["folderA"]}, files_to_trash)
self.assertNotIn({"id": "file1_newest", "name": "b.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]}, files_to_trash)
self.assertNotIn({"id": "file2_short", "name": "short.doc", "modifiedTime": "2023-03-01T10:00:00Z", "size": "500", "parents": ["folderC"]}, files_to_trash)
self.assertNotIn({"id": "file2_long", "name": "very_long_name.doc", "modifiedTime": "2023-03-02T10:00:00Z", "size": "400", "parents": ["folderD"]}, files_to_trash)
self.assertEqual(len(files_to_trash), 2)
def test_get_files_to_trash_uniqueness(self):
assistant = SelectionAssistant(self.mock_service, self.duplicate_groups)
assistant.files_to_trash.append({"id": "file1_newest", "name": "b_longer_name.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]})
assistant.files_to_trash.append({"id": "file1_newest", "name": "b_longer_name.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]})
assistant.files_to_trash.append({"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]})
files_to_trash = assistant.get_files_to_trash()
self.assertEqual(len(files_to_trash), 2)
self.assertIn({"id": "file1_newest", "name": "b_longer_name.txt", "modifiedTime": "2023-01-02T10:00:00Z", "size": "200", "parents": ["folderB"]}, files_to_trash)
self.assertIn({"id": "file1_oldest", "name": "a.txt", "modifiedTime": "2023-01-01T10:00:00Z", "size": "100", "parents": ["folderA"]}, files_to_trash)
class TestFetchAllFiles(unittest.TestCase):
def test_fetch_all_files_http_error(self):
import duplicate_scanner
from duplicate_scanner import fetch_all_files
mock_service = Mock()
mock_execute = Mock(side_effect=duplicate_scanner.HttpError(resp=Mock(status=500), content=b'{"error": "Simulated error"}'))
mock_list = Mock()
mock_list.execute = mock_execute
mock_files = Mock()
mock_files.list.return_value = mock_list
mock_service.files.return_value = mock_files
result = fetch_all_files(mock_service)
self.assertEqual(result, [])
if __name__ == '__main__':
unittest.main()