Skip to content

Commit 9a46cbb

Browse files
committed
tests: fix tests and add new for the function
1 parent 98b33e6 commit 9a46cbb

File tree

1 file changed

+159
-3
lines changed

1 file changed

+159
-3
lines changed

tests/archive_tests.py

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
from mdbackup.actions.builtin._register import register
66
from mdbackup.actions.container import _clean_actions
7-
from mdbackup.archive import archive_folder
7+
from mdbackup.archive import archive_file, archive_folder
88
from mdbackup.config import CloudConfig
99

1010

11-
class ArchiveTests(TestCase):
11+
class ArchiveFolderTests(TestCase):
1212
_actions = None
1313

1414
def _run_task_actions(self, _, actions):
@@ -57,7 +57,7 @@ def test_no_compression_no_encrypt_should_run_actions_to_create_a_tar_file(self,
5757

5858
@patch('mdbackup.archive.run_task_actions')
5959
def test_compression_no_encrypt_should_run_actions_to_create_a_tar_file(self, mock: Mock):
60-
config = CloudConfig({'providers': [], 'compression': {'method': 'br'}})
60+
config = CloudConfig({'providers': [], 'compression': {'method': 'br', 'cpus': 2}})
6161
backup_path = Path()
6262
folder = backup_path / 'folder'
6363
mock.side_effect = self._run_task_actions
@@ -71,6 +71,7 @@ def test_compression_no_encrypt_should_run_actions_to_create_a_tar_file(self, mo
7171
self.assertDictEqual({
7272
'compress-br': {
7373
'level': 6,
74+
'cpus': 2,
7475
},
7576
}, self._actions[2])
7677
self._check_to_file(folder, backup_path, '.br', 3)
@@ -117,6 +118,7 @@ def test_compression_encrypt_should_run_actions_to_create_a_tar_file(self, mock:
117118
self.assertDictEqual({
118119
'compress-gz': {
119120
'level': 6,
121+
'cpus': None,
120122
},
121123
}, self._actions[2])
122124
self.assertDictEqual({
@@ -127,3 +129,157 @@ def test_compression_encrypt_should_run_actions_to_create_a_tar_file(self, mock:
127129
},
128130
}, self._actions[3])
129131
self._check_to_file(folder, backup_path, '.gz.asc', 4)
132+
133+
134+
class ArchiveFileTests(TestCase):
135+
_actions = None
136+
137+
def _run_task_actions(self, _, actions):
138+
self._actions = actions
139+
140+
def _generate_actions(self, compressed=False, encrypted=False):
141+
actions = [{'from-file': None}]
142+
if compressed:
143+
actions.append({'compress-xd': None})
144+
if encrypted:
145+
actions.append({'encrypt-asd': None})
146+
actions.append({'to-file': None})
147+
return {'actions': actions}
148+
149+
def _check_from_file(self, file_path):
150+
self.assertDictEqual({
151+
'from-file': str(file_path),
152+
}, self._actions[0])
153+
154+
def _check_to_file(self, file_path, backup_path, ext='', num=2):
155+
self.assertDictEqual({
156+
'to-file': {
157+
'to': file_path.parts[-1] + ext,
158+
'_backup_path': backup_path,
159+
},
160+
}, self._actions[num])
161+
162+
def setUp(self):
163+
super().setUp()
164+
register()
165+
166+
def tearDown(self):
167+
super().tearDown()
168+
_clean_actions()
169+
170+
@patch('mdbackup.archive.run_task_actions')
171+
def test_no_compression_no_encrypt_should_not_run_actions_and_return_none(self, mock: Mock):
172+
config = CloudConfig({'providers': []})
173+
task = self._generate_actions()
174+
backup_path = Path()
175+
path = backup_path / 'file.txt'
176+
mock.side_effect = self._run_task_actions
177+
178+
filename = archive_file(backup_path, path, task, config)
179+
180+
self.assertIsNone(self._actions)
181+
self.assertIsNone(filename)
182+
183+
@patch('mdbackup.archive.run_task_actions')
184+
def test_compression_no_encrypt_with_no_compressed_file_should_run_actions_to_create_file(self, mock: Mock):
185+
config = CloudConfig({'providers': [], 'compression': {'method': 'br', 'cpus': 2}})
186+
task = self._generate_actions()
187+
backup_path = Path()
188+
path = backup_path / 'file.txt'
189+
mock.side_effect = self._run_task_actions
190+
191+
filename = archive_file(backup_path, path, task, config)
192+
193+
self.assertIsNotNone(filename)
194+
self.assertIsNotNone(self._actions)
195+
self.assertEqual(3, len(self._actions))
196+
self._check_from_file(path)
197+
self.assertDictEqual({
198+
'compress-br': {
199+
'level': 6,
200+
'cpus': 2,
201+
},
202+
}, self._actions[1])
203+
self._check_to_file(path, backup_path, '.br', 2)
204+
205+
@patch('mdbackup.archive.run_task_actions')
206+
def test_compression_no_encrypt_with_compressed_file_should_do_nothing(self, mock: Mock):
207+
config = CloudConfig({'providers': [], 'compression': {'method': 'br', 'cpus': 2}})
208+
task = self._generate_actions(compressed=True)
209+
backup_path = Path()
210+
path = backup_path / 'file.txt'
211+
mock.side_effect = self._run_task_actions
212+
213+
filename = archive_file(backup_path, path, task, config)
214+
215+
self.assertIsNone(filename)
216+
self.assertIsNone(self._actions)
217+
218+
@patch('mdbackup.archive.run_task_actions')
219+
def test_no_compression_encrypt_with_no_encrypted_file_should_run_actions_to_create_file(self, mock: Mock):
220+
config = CloudConfig({'providers': [], 'encrypt': {'strategy': 'gpg-passphrase', 'passphrase': '1'}})
221+
task = self._generate_actions()
222+
backup_path = Path()
223+
path = backup_path / 'file.txt'
224+
mock.side_effect = self._run_task_actions
225+
226+
filename = archive_file(backup_path, path, task, config)
227+
228+
self.assertIsNotNone(filename)
229+
self.assertIsNotNone(self._actions)
230+
self.assertEqual(3, len(self._actions))
231+
self._check_from_file(path)
232+
self.assertDictEqual({
233+
'encrypt-gpg': {
234+
'passphrase': '1',
235+
'recipients': [],
236+
'algorithm': None,
237+
},
238+
}, self._actions[1])
239+
self._check_to_file(path, backup_path, '.asc', 2)
240+
241+
@patch('mdbackup.archive.run_task_actions')
242+
def test_no_compression_encrypt_with_encrypted_file_should_run_actions_to_create_file(self, mock: Mock):
243+
config = CloudConfig({'providers': [], 'encrypt': {'strategy': 'gpg-passphrase', 'passphrase': '1'}})
244+
task = self._generate_actions(encrypted=True)
245+
backup_path = Path()
246+
path = backup_path / 'file.txt'
247+
mock.side_effect = self._run_task_actions
248+
249+
filename = archive_file(backup_path, path, task, config)
250+
251+
self.assertIsNone(filename)
252+
self.assertIsNone(self._actions)
253+
254+
@patch('mdbackup.archive.run_task_actions')
255+
def test_compression_encrypt_should_run_actions_to_create_file(self, mock: Mock):
256+
config = CloudConfig({
257+
'providers': [],
258+
'encrypt': {'strategy': 'gpg-passphrase', 'passphrase': '1'},
259+
'compression': {'method': 'gz'},
260+
})
261+
task = self._generate_actions()
262+
backup_path = Path()
263+
path = backup_path / 'file.txt'
264+
mock.side_effect = self._run_task_actions
265+
266+
filename = archive_file(backup_path, path, task, config)
267+
268+
self.assertIsNotNone(filename)
269+
self.assertIsNotNone(self._actions)
270+
self.assertEqual(4, len(self._actions))
271+
self._check_from_file(path)
272+
self.assertDictEqual({
273+
'compress-gz': {
274+
'level': 6,
275+
'cpus': None,
276+
},
277+
}, self._actions[1])
278+
self.assertDictEqual({
279+
'encrypt-gpg': {
280+
'passphrase': '1',
281+
'recipients': [],
282+
'algorithm': None,
283+
},
284+
}, self._actions[2])
285+
self._check_to_file(path, backup_path, '.gz.asc', 3)

0 commit comments

Comments
 (0)