2
2
3
3
import os
4
4
import shutil
5
- import unittest
6
5
7
6
from dups import backup , exceptions , utils
8
7
8
+ import pytest
9
9
import utils as test_utils
10
- from ddt import data , ddt
11
10
12
11
13
- @ddt
14
- class Test_Backup (unittest .TestCase ):
12
+ class Test_Backup :
15
13
missing_backup = '19900100000000'
16
14
valid_backup = '19900101000000'
17
15
invalid_backup = '19900102000000'
18
16
19
- def tearDown (self ):
17
+ def teardown_method (self , method ):
20
18
for dir_ in os .listdir (context .BACKUP_DIR ):
21
19
if dir_ in [self .valid_backup , self .invalid_backup ]:
22
20
continue
@@ -41,7 +39,7 @@ def get_invalid(self, io):
41
39
return backup .Backup .from_name (io , self .invalid_backup ,
42
40
context .BACKUP_DIR )
43
41
44
- @data ( ' local' , 'remote' )
42
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
45
43
def test_new (self , target ):
46
44
io = self .get_io (target )
47
45
@@ -53,7 +51,7 @@ def test_new(self, target):
53
51
54
52
io .close ()
55
53
56
- @data ( ' local' , 'remote' )
54
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
57
55
def test_from_label (self , target ):
58
56
io = self .get_io (target )
59
57
@@ -62,53 +60,52 @@ def test_from_label(self, target):
62
60
self .get_valid (io )
63
61
64
62
except Exception as e :
65
- self .fail (str (e ))
63
+ pytest .fail (str (e ))
66
64
67
- self . assertRaises (exceptions .BackupNotFoundException ,
68
- backup .Backup .from_name , io , self .missing_backup ,
69
- context .BACKUP_DIR )
65
+ with pytest . raises (exceptions .BackupNotFoundException ):
66
+ backup .Backup .from_name ( io , self .missing_backup ,
67
+ context .BACKUP_DIR )
70
68
71
69
io .close ()
72
70
73
- @data ( ' local' , 'remote' )
71
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
74
72
def test_all_backups (self , target ):
75
73
io = self .get_io (target )
76
74
77
75
# Get a list of valid backups
78
76
backups = backup .Backup .all_backups (io , context .BACKUP_DIR )
79
- self . assertCountEqual ([ self . get_valid ( io )], backups )
77
+ assert len ( backups ) == 1
80
78
81
79
# Get a list of all (valid and invalid) backups
82
80
backups = backup .Backup .all_backups (io , context .BACKUP_DIR , True , True )
83
- self .assertCountEqual (
84
- [self .get_valid (io ), self .get_invalid (io )], backups )
81
+ assert len (backups ) == 2
85
82
86
83
io .close ()
87
84
88
- @data ( ' local' , 'remote' )
85
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
89
86
def test_latest (self , target ):
90
87
io = self .get_io (target )
91
88
92
89
# Get the latest valid backup
93
90
bak = backup .Backup .latest (io , context .BACKUP_DIR )
94
- self .assertEqual ( self . get_valid (io ), bak )
91
+ assert self .get_valid (io ) == bak
95
92
96
93
# Get the latest backup including invalid ones
97
94
bak = backup .Backup .latest (io , context .BACKUP_DIR , True , True )
98
- self .assertEqual ( self . get_invalid (io ), bak )
95
+ assert self .get_invalid (io ) == bak
99
96
100
97
io .close ()
101
98
102
- @data ( ' local' , 'remote' )
99
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
103
100
def test_backup (self , target ):
104
101
io = self .get_io (target )
105
102
106
103
bak = backup .Backup .new (io , context .BACKUP_DIR )
107
104
status = bak .backup ([context .TEST_DIR , context .TEST_FILE ],
108
105
excludes = ['**/dir2/.gitkeep' ])
109
106
110
- self . assertEqual ( status .exit_code , 0 )
111
- self . assertTrue ( os .path .exists (bak .backup_data_dir ) )
107
+ assert status .exit_code == 0
108
+ assert os .path .exists (bak .backup_data_dir )
112
109
113
110
# Define the structure we expect after the backup
114
111
expected_data = test_utils .get_dir_struct (context .DATA_DIR )
@@ -119,19 +116,19 @@ def test_backup(self, target):
119
116
context .DATA_DIR .lstrip ('/' ))
120
117
121
118
synced_data = test_utils .get_dir_struct (real_data_dir )
122
- self . assertEqual ( expected_data , synced_data )
119
+ assert expected_data == synced_data
123
120
124
121
io .close ()
125
122
126
- @data ( ' local' , 'remote' )
123
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
127
124
def test_backup_pattern (self , target ):
128
125
io = self .get_io (target )
129
126
130
127
bak = backup .Backup .new (io , context .BACKUP_DIR )
131
128
status = bak .backup ([os .path .join (context .DATA_DIR , 'test*' )],
132
129
excludes = ['**/dir2' ])
133
130
134
- self . assertEqual ( status .exit_code , 0 )
131
+ assert status .exit_code == 0
135
132
136
133
# Define the structure we expect after the backup
137
134
expected_data = test_utils .get_dir_struct (context .DATA_DIR )
@@ -141,81 +138,77 @@ def test_backup_pattern(self, target):
141
138
context .DATA_DIR .lstrip ('/' ))
142
139
143
140
synced_data = test_utils .get_dir_struct (real_data_dir )
144
- self . assertEqual ( expected_data , synced_data )
141
+ assert expected_data == synced_data
145
142
146
143
io .close ()
147
144
148
- @data ( ' local' , 'remote' )
145
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
149
146
def test_backup_dry_run (self , target ):
150
147
io = self .get_io (target )
151
148
152
149
bak = backup .Backup .new (io , context .BACKUP_DIR )
153
150
status = bak .backup ([context .TEST_DIR , context .TEST_FILE ],
154
151
dry_run = True )
155
152
156
- self . assertEqual ( status .exit_code , 0 )
157
- self . assertTrue ( not os .path .exists (bak .backup_dir ) )
153
+ assert status .exit_code == 0
154
+ assert not os .path .exists (bak .backup_dir )
158
155
159
156
io .close ()
160
157
161
- @data ( ' local' , 'remote' )
158
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
162
159
def test_restore_items (self , target ):
163
160
io = self .get_io (target )
164
161
165
162
bak = self .get_valid (io )
166
163
status = bak .restore (context .TMP_DIR , ['/tmp/test1.file' ])
167
164
168
- self . assertEqual ( status .exit_code , 0 )
165
+ assert status .exit_code == 0
169
166
170
167
expected_data = test_utils .get_dir_struct (bak .backup_data_dir )
171
168
del expected_data ['tmp' ]['test2.file' ]
172
169
del expected_data ['tmp' ]['test3.file' ]
173
170
174
171
synced_data = test_utils .get_dir_struct (context .TMP_DIR )
175
- self . assertEqual ( expected_data , synced_data )
172
+ assert expected_data == synced_data
176
173
177
174
io .close ()
178
175
179
- @data ( ' local' , 'remote' )
176
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
180
177
def test_restore_full (self , target ):
181
178
io = self .get_io (target )
182
179
183
180
bak = self .get_valid (io )
184
181
status = bak .restore (context .TMP_DIR )
185
182
186
- self . assertEqual ( status .exit_code , 0 )
183
+ assert status .exit_code == 0
187
184
188
185
expected_data = test_utils .get_dir_struct (bak .backup_data_dir )
189
186
synced_data = test_utils .get_dir_struct (context .TMP_DIR )
190
- self . assertEqual ( expected_data , synced_data )
187
+ assert expected_data == synced_data
191
188
192
189
io .close ()
193
190
194
- @data ( ' local' , 'remote' )
191
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
195
192
def test_restore_dry_run (self , target ):
196
193
io = self .get_io (target )
197
194
198
195
bak = self .get_valid (io )
199
196
status = bak .restore (context .TMP_DIR , dry_run = True )
200
197
201
- self . assertEqual ( status .exit_code , 0 )
202
- self . assertTrue ( not os .path .exists (context .TMP_DIR ) )
198
+ assert status .exit_code == 0
199
+ assert not os .path .exists (context .TMP_DIR )
203
200
204
201
io .close ()
205
202
206
- @data ( ' local' , 'remote' )
203
+ @pytest . mark . parametrize ( 'target' , [ ' local' , 'remote' ] )
207
204
def test_remove (self , target ):
208
205
io = self .get_io (target )
209
206
210
207
bak = backup .Backup .new (io , context .BACKUP_DIR )
211
208
bak .backup ([context .TEST_FILE ])
212
209
213
- self . assertTrue ( os .path .exists (bak .backup_data_dir ) )
210
+ assert os .path .exists (bak .backup_data_dir )
214
211
bak .remove ()
215
- self . assertTrue ( not os .path .exists (bak .backup_data_dir ) )
212
+ assert not os .path .exists (bak .backup_data_dir )
216
213
217
214
io .close ()
218
-
219
-
220
- if __name__ == '__main__' :
221
- unittest .main (exit = False )
0 commit comments