Total Complexity | 56 |
Total Lines | 163 |
Duplicated Lines | 23.31 % |
Changes | 2 | ||
Bugs | 1 | Features | 1 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BucketTestCase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
112 | class BucketTestCase(unittest.TestCase): |
||
113 | q = Auth(access_key, secret_key) |
||
114 | bucket = BucketManager(q) |
||
115 | |||
116 | def test_list(self): |
||
117 | ret, eof, info = self.bucket.list(bucket_name, limit=4) |
||
118 | print(info) |
||
119 | assert eof is False |
||
120 | assert len(ret.get('items')) == 4 |
||
121 | ret, eof, info = self.bucket.list(bucket_name, limit=100) |
||
122 | print(info) |
||
123 | assert eof is True |
||
124 | |||
125 | def test_buckets(self): |
||
126 | ret, info = self.bucket.buckets() |
||
127 | print(info) |
||
128 | assert bucket_name in ret |
||
129 | |||
130 | def test_prefetch(self): |
||
131 | ret, info = self.bucket.prefetch(bucket_name, 'python-sdk.html') |
||
132 | print(info) |
||
133 | assert ret['key'] == 'python-sdk.html' |
||
134 | |||
135 | def test_fetch(self): |
||
136 | ret, info = self.bucket.fetch('http://developer.qiniu.com/docs/v6/sdk/python-sdk.html', bucket_name, 'fetch.html') |
||
137 | print(info) |
||
138 | assert ret['key'] == 'fetch.html' |
||
139 | assert 'hash' in ret |
||
140 | |||
141 | def test_fetch_without_key(self): |
||
142 | ret, info = self.bucket.fetch('http://developer.qiniu.com/docs/v6/sdk/python-sdk.html', bucket_name) |
||
143 | print(info) |
||
144 | assert ret['key'] == ret['hash'] |
||
145 | assert 'hash' in ret |
||
146 | |||
147 | def test_stat(self): |
||
148 | ret, info = self.bucket.stat(bucket_name, 'python-sdk.html') |
||
149 | print(info) |
||
150 | assert 'hash' in ret |
||
151 | |||
152 | def test_delete(self): |
||
153 | ret, info = self.bucket.delete(bucket_name, 'del') |
||
154 | print(info) |
||
155 | assert ret is None |
||
156 | assert info.status_code == 612 |
||
157 | |||
158 | def test_rename(self): |
||
159 | key = 'renameto'+rand_string(8) |
||
160 | self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key) |
||
161 | key2 = key + 'move' |
||
162 | ret, info = self.bucket.rename(bucket_name, key, key2) |
||
163 | print(info) |
||
164 | assert ret == {} |
||
165 | ret, info = self.bucket.delete(bucket_name, key2) |
||
166 | print(info) |
||
167 | assert ret == {} |
||
168 | |||
169 | def test_copy(self): |
||
170 | key = 'copyto'+rand_string(8) |
||
171 | ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key) |
||
172 | print(info) |
||
173 | assert ret == {} |
||
174 | ret, info = self.bucket.delete(bucket_name, key) |
||
175 | print(info) |
||
176 | assert ret == {} |
||
177 | |||
178 | def test_change_mime(self): |
||
179 | ret, info = self.bucket.change_mime(bucket_name, 'python-sdk.html', 'text/html') |
||
180 | print(info) |
||
181 | assert ret == {} |
||
182 | |||
183 | def test_copy(self): |
||
184 | key = 'copyto'+rand_string(8) |
||
185 | ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key) |
||
186 | print(info) |
||
187 | assert ret == {} |
||
188 | ret, info = self.bucket.delete(bucket_name, key) |
||
189 | print(info) |
||
190 | assert ret == {} |
||
191 | |||
192 | def test_copy_force(self): |
||
193 | ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, 'copyfrom', force='true') |
||
194 | print(info) |
||
195 | assert info.status_code == 200 |
||
196 | |||
197 | def test_change_mime(self): |
||
198 | ret, info = self.bucket.change_mime(bucket_name, 'python-sdk.html', 'text/html') |
||
199 | print(info) |
||
200 | assert ret == {} |
||
201 | |||
202 | def test_batch_copy(self): |
||
203 | key = 'copyto'+rand_string(8) |
||
204 | ops = build_batch_copy(bucket_name, {'copyfrom': key}, bucket_name) |
||
205 | ret, info = self.bucket.batch(ops) |
||
206 | print(info) |
||
207 | assert ret[0]['code'] == 200 |
||
208 | ops = build_batch_delete(bucket_name, [key]) |
||
209 | ret, info = self.bucket.batch(ops) |
||
210 | print(info) |
||
211 | assert ret[0]['code'] == 200 |
||
212 | |||
213 | def test_batch_copy_force(self): |
||
214 | ops = build_batch_copy(bucket_name, {'copyfrom': 'copyfrom'}, bucket_name, force='true') |
||
215 | ret, info = self.bucket.batch(ops) |
||
216 | print(info) |
||
217 | assert ret[0]['code'] == 200 |
||
218 | |||
219 | View Code Duplication | def test_batch_move(self): |
|
|
|||
220 | key = 'moveto'+rand_string(8) |
||
221 | self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key) |
||
222 | key2 = key + 'move' |
||
223 | ops = build_batch_move(bucket_name, {key: key2}, bucket_name) |
||
224 | ret, info = self.bucket.batch(ops) |
||
225 | print(info) |
||
226 | assert ret[0]['code'] == 200 |
||
227 | ret, info = self.bucket.delete(bucket_name, key2) |
||
228 | print(info) |
||
229 | assert ret == {} |
||
230 | |||
231 | View Code Duplication | def test_batch_move_force(self): |
|
232 | ret,info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, 'copyfrom', force='true') |
||
233 | print(info) |
||
234 | assert info.status_code == 200 |
||
235 | ops = build_batch_move(bucket_name, {'copyfrom':'copyfrom'}, bucket_name,force='true') |
||
236 | ret, info = self.bucket.batch(ops) |
||
237 | print(info) |
||
238 | assert ret[0]['code'] == 200 |
||
239 | |||
240 | View Code Duplication | def test_batch_rename(self): |
|
241 | key = 'rename'+rand_string(8) |
||
242 | self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key) |
||
243 | key2 = key + 'rename' |
||
244 | ops = build_batch_move(bucket_name, {key: key2}, bucket_name) |
||
245 | ret, info = self.bucket.batch(ops) |
||
246 | print(info) |
||
247 | assert ret[0]['code'] == 200 |
||
248 | ret, info = self.bucket.delete(bucket_name, key2) |
||
249 | print(info) |
||
250 | assert ret == {} |
||
251 | |||
252 | View Code Duplication | def test_batch_rename_force(self): |
|
253 | ret,info = self.bucket.rename(bucket_name, 'copyfrom', 'copyfrom', force='true') |
||
254 | print(info) |
||
255 | assert info.status_code == 200 |
||
256 | ops = build_batch_rename(bucket_name, {'copyfrom':'copyfrom'}, force='true') |
||
257 | ret, info = self.bucket.batch(ops) |
||
258 | print(info) |
||
259 | assert ret[0]['code'] == 200 |
||
260 | |||
261 | def test_batch_stat(self): |
||
262 | ops = build_batch_stat(bucket_name, ['python-sdk.html']) |
||
263 | ret, info = self.bucket.batch(ops) |
||
264 | print(info) |
||
265 | assert ret[0]['code'] == 200 |
||
266 | |||
267 | def test_delete_after_days(self): |
||
268 | days = '5' |
||
269 | ret, info = self.bucket.delete_after_days(bucket_name,'invaild.html', days) |
||
270 | assert info.status_code == 612 |
||
271 | key = 'copyto'+rand_string(8) |
||
272 | ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key) |
||
273 | ret, info = self.bucket.delete_after_days(bucket_name, key, days) |
||
274 | assert info.status_code == 200 |
||
275 | |||
498 |