@@ -98,6 +98,10 @@ class IpfsApiTest(unittest.TestCase):
98
98
fake_file = 'fake_dir/fsdfgh'
99
99
fake_file_only_res = {'Name' : 'fsdfgh' ,
100
100
'Hash' : 'QmQcCtMgLVwvMQGu6mvsRYLjwqrZJcYtH4mboM9urWW9vX' }
101
+ fake_file_dir_res = [
102
+ {'Name' : 'fsdfgh' , 'Hash' : 'QmQcCtMgLVwvMQGu6mvsRYLjwqrZJcYtH4mboM9urWW9vX' },
103
+ {'Name' : '' , 'Hash' : 'Qme7vmxd4LAAYL7vpho3suQeT3gvMeLLtPdp7myCb9Db55' }
104
+ ]
101
105
fake_file2 = 'fake_dir/popoiopiu'
102
106
fake_files_res = [
103
107
{'Name' : 'fsdfgh' , 'Hash' : 'QmQcCtMgLVwvMQGu6mvsRYLjwqrZJcYtH4mboM9urWW9vX' },
@@ -162,10 +166,21 @@ def setUp(self):
162
166
163
167
# Makes all of the diff visible if the hashes change for some reason
164
168
self .maxDiff = None
169
+
170
+ self .pinned = set (self .api .pin_ls (type = "recursive" )["Keys" ])
165
171
166
172
def tearDown (self ):
167
173
os .chdir (self ._olddir )
168
174
175
+ def _clean_up_pins (self ):
176
+ for multihash in self .api .pin_ls (type = "recursive" )["Keys" ]:
177
+ if multihash not in self .pinned :
178
+ self .api .pin_rm (multihash )
179
+
180
+ @staticmethod
181
+ def _sort_by_key (items , key = "Name" ):
182
+ return sorted (items , key = lambda x : x [key ])
183
+
169
184
#########
170
185
# TESTS #
171
186
#########
@@ -174,125 +189,178 @@ def test_version(self):
174
189
expected = ['Repo' , 'Commit' , 'Version' ]
175
190
resp_version = self .api .version ()
176
191
for key in expected :
177
- self . assertTrue ( key in resp_version )
192
+ assert key in resp_version
178
193
179
194
def test_id (self ):
180
195
expected = ['PublicKey' , 'ProtocolVersion' ,
181
196
'ID' , 'AgentVersion' , 'Addresses' ]
182
197
resp_id = self .api .id ()
183
198
for key in expected :
184
- self . assertTrue ( key in resp_id )
199
+ assert key in resp_id
185
200
186
201
def test_add_single_from_str (self ):
187
- res = self .api .add (self .fake_file )
188
- self .assertEqual (self .fake_file_only_res , res )
202
+ res = self .api .add (self .fake_file , pin = False )
203
+
204
+ assert self .fake_file_only_res == res
205
+
206
+ assert res ["Hash" ] not in self .api .pin_ls (type = "recursive" )
207
+ assert res ["Hash" ] in list (map (lambda i : i ["Ref" ], self .api .refs_local ()))
189
208
190
209
def test_add_single_from_fp (self ):
191
210
with open (self .fake_file , 'rb' ) as fp :
192
- res = self .api .add (fp )
193
- self .assertEqual (self .fake_file_only_res , res )
211
+ res = self .api .add (fp , pin = False )
212
+
213
+ assert self .fake_file_only_res == res
214
+
215
+ assert res ["Hash" ] not in self .api .pin_ls (type = "recursive" )
216
+ assert res ["Hash" ] in list (map (lambda i : i ["Ref" ], self .api .refs_local ()))
217
+
218
+ def test_add_single_from_str_with_dir (self ):
219
+ res = self .api .add (self .fake_file , wrap_with_directory = True )
220
+
221
+ try :
222
+ assert self .fake_file_dir_res == res
223
+
224
+ dir_hash = None
225
+ for item in res :
226
+ if item ["Name" ] == "" :
227
+ dir_hash = item ["Hash" ]
228
+ assert dir_hash in self .api .pin_ls (type = "recursive" )["Keys" ]
229
+ finally :
230
+ self ._clean_up_pins ()
231
+
232
+ def test_only_hash_file (self ):
233
+ self .api .repo_gc ()
234
+
235
+ res = self .api .add (self .fake_file , only_hash = True )
236
+
237
+ assert self .fake_file_only_res == res
238
+
239
+ assert res ["Hash" ] not in self .api .pin_ls (type = "recursive" )
240
+ assert res ["Hash" ] not in list (map (lambda i : i ["Ref" ], self .api .refs_local ()))
194
241
195
242
def test_add_multiple_from_list (self ):
196
243
res = self .api .add ([self .fake_file , self .fake_file2 ])
197
- self .assertEqual (self .fake_files_res , res )
244
+
245
+ try :
246
+ assert self .fake_files_res == res
247
+ finally :
248
+ self ._clean_up_pins ()
198
249
199
250
def test_add_multiple_from_dirname (self ):
200
251
res = self .api .add (self .fake_dir_test2 )
201
- self .assertEqual (sorted (self .fake_dir_res ,
202
- key = lambda x : x ['Name' ]),
203
- sorted (res ,
204
- key = lambda x : x ['Name' ]))
252
+
253
+ try :
254
+ assert self ._sort_by_key (self .fake_dir_res ) == self ._sort_by_key (res )
255
+ finally :
256
+ self ._clean_up_pins ()
205
257
206
258
def test_add_filepattern_from_dirname (self ):
207
259
res = self .api .add (self .fake_dir , pattern = self .pattern )
208
- self .assertEqual (sorted (self .fake_dir_fnpattern_res ,
209
- key = lambda x : x ['Name' ]),
210
- sorted (res ,
211
- key = lambda x : x ['Name' ]))
260
+
261
+ try :
262
+ assert self ._sort_by_key (self .fake_dir_fnpattern_res ) == self ._sort_by_key (res )
263
+ finally :
264
+ self ._clean_up_pins ()
265
+
212
266
213
267
def test_add_filepattern_subdir_wildcard (self ):
214
268
res = self .api .add (self .fake_dir , pattern = self .pattern2 )
215
- self .assertEqual (sorted (self .fake_dir_fnpattern2_res ,
216
- key = lambda x : x ['Name' ]),
217
- sorted (res ,
218
- key = lambda x : x ['Name' ]))
269
+
270
+ try :
271
+ assert self ._sort_by_key (self .fake_dir_fnpattern2_res ) == self ._sort_by_key (res )
272
+ finally :
273
+ self ._clean_up_pins ()
219
274
220
275
def test_add_recursive (self ):
221
276
res = self .api .add (self .fake_dir , recursive = True )
222
- self .assertEqual (sorted (self .fake_dir_recursive_res ,
223
- key = lambda x : x ['Name' ]),
224
- sorted (res ,
225
- key = lambda x : x ['Name' ]))
277
+
278
+ try :
279
+ assert self ._sort_by_key (self .fake_dir_recursive_res ) == self ._sort_by_key (res )
280
+ finally :
281
+ self ._clean_up_pins ()
226
282
227
283
def test_add_json (self ):
228
284
data = {'Action' : 'Open' , 'Type' : 'PR' , 'Name' : 'IPFS' , 'Pubkey' : 7 }
229
285
res = self .api .add_json (data )
230
- self .assertEqual (data ,
231
- self .api .get_json (res ))
286
+
287
+ try :
288
+ assert data == self .api .get_json (res )
232
289
233
- # have to test the string added to IPFS, deserializing JSON will not
234
- # test order of keys
235
- self .assertEqual (
236
- '{"Action":"Open","Name":"IPFS","Pubkey":7,"Type":"PR"}' ,
237
- self .api .cat (res ).decode ('utf-8' )
238
- )
290
+ # have to test the string added to IPFS, deserializing JSON will not
291
+ # test order of keys
292
+ assert '{"Action":"Open","Name":"IPFS","Pubkey":7,"Type":"PR"}' == self .api .cat (res ).decode ('utf-8' )
293
+ finally :
294
+ self ._clean_up_pins ()
239
295
240
296
def test_add_get_pyobject (self ):
241
297
data = [- 1 , 3.14 , u'Hän€' , b'23' ]
242
298
res = self .api .add_pyobj (data )
243
- self .assertEqual (data ,
244
- self .api .get_pyobj (res ))
299
+
300
+ try :
301
+ assert data == self .api .get_pyobj (res )
302
+ finally :
303
+ self ._clean_up_pins ()
245
304
246
305
def test_get_file (self ):
247
306
self .api .add (self .fake_file )
307
+
308
+ try :
309
+ test_hash = self .fake [0 ]['Hash' ]
248
310
249
- test_hash = self .fake [0 ]['Hash' ]
250
-
251
- self .api .get (test_hash )
252
- self .assertIn (test_hash , os .listdir (os .getcwd ()))
311
+ self .api .get (test_hash )
312
+ assert test_hash in os .listdir (os .getcwd ())
253
313
254
- os .remove (test_hash )
255
- self .assertNotIn (test_hash , os .listdir (os .getcwd ()))
314
+ os .remove (test_hash )
315
+ assert test_hash not in os .listdir (os .getcwd ())
316
+ finally :
317
+ self ._clean_up_pins ()
256
318
257
319
def test_get_dir (self ):
258
320
self .api .add (self .fake_dir , recursive = True )
259
-
260
- test_hash = self .fake [8 ]['Hash' ]
261
-
262
- self .api .get (test_hash )
263
- self .assertIn (test_hash , os .listdir (os .getcwd ()))
264
-
265
- shutil .rmtree (test_hash )
266
- self .assertNotIn (test_hash , os .listdir (os .getcwd ()))
321
+
322
+ try :
323
+ test_hash = self .fake [8 ]['Hash' ]
324
+
325
+ self .api .get (test_hash )
326
+ assert test_hash in os .listdir (os .getcwd ())
327
+
328
+ shutil .rmtree (test_hash )
329
+ assert test_hash not in os .listdir (os .getcwd ())
330
+ finally :
331
+ self ._clean_up_pins ()
267
332
268
333
def test_get_path (self ):
269
334
self .api .add (self .fake_file )
270
-
271
- test_hash = self .fake [8 ]['Hash' ] + '/fsdfgh'
272
-
273
- self .api .get (test_hash )
274
- self .assertIn ('fsdfgh' , os .listdir (os .getcwd ()))
275
-
276
- os .remove ('fsdfgh' )
277
- self .assertNotIn ('fsdfgh' , os .listdir (os .getcwd ()))
335
+
336
+ try :
337
+ test_hash = self .fake [8 ]['Hash' ] + '/fsdfgh'
338
+
339
+ self .api .get (test_hash )
340
+ assert 'fsdfgh' in os .listdir (os .getcwd ())
341
+
342
+ os .remove ('fsdfgh' )
343
+ assert 'fsdfgh' not in os .listdir (os .getcwd ())
344
+ finally :
345
+ self ._clean_up_pins ()
278
346
279
347
def test_refs (self ):
280
348
self .api .add (self .fake_dir , recursive = True )
281
-
282
- refs = self .api .refs (self .fake [8 ]['Hash' ])
283
-
284
- self .assertEqual (sorted (self .refs_res , key = lambda x : x ['Ref' ]),
285
- sorted (refs , key = lambda x : x ['Ref' ]))
286
-
287
- def test_refs_local (self ):
288
- refs = self .api .refs_local ()
289
-
290
- self .assertEqual (sorted (refs [0 ].keys ()), ['Err' , 'Ref' ])
349
+
350
+ try :
351
+ refs = self .api .refs (self .fake [8 ]['Hash' ])
352
+ assert self ._sort_by_key (self .refs_res , "Ref" ) == self ._sort_by_key (refs , "Ref" )
353
+ finally :
354
+ self ._clean_up_pins ()
291
355
292
356
def test_cat_single_file_str (self ):
293
357
self .api .add (self .fake_file )
294
- res = self .api .cat ('QmQcCtMgLVwvMQGu6mvsRYLjwqrZJcYtH4mboM9urWW9vX' )
295
- self .assertEqual (b"dsadsad\n " , res )
358
+
359
+ try :
360
+ content = self .api .cat ('QmQcCtMgLVwvMQGu6mvsRYLjwqrZJcYtH4mboM9urWW9vX' )
361
+ assert content == b"dsadsad\n "
362
+ finally :
363
+ self ._clean_up_pins ()
296
364
297
365
298
366
@skipIfOffline ()
0 commit comments