16
16
"""Tests for the gridfs package."""
17
17
from __future__ import annotations
18
18
19
+ import asyncio
19
20
import datetime
20
21
import itertools
21
22
import sys
22
23
import threading
23
24
import time
24
25
from io import BytesIO
26
+ from test .helpers import ConcurrentRunner
25
27
from unittest .mock import patch
26
28
27
29
sys .path [0 :0 ] = ["" ]
44
46
from pymongo .read_preferences import ReadPreference
45
47
from pymongo .synchronous .mongo_client import MongoClient
46
48
49
+ _IS_SYNC = True
47
50
48
- class JustWrite (threading .Thread ):
51
+
52
+ class JustWrite (ConcurrentRunner ):
49
53
def __init__ (self , gfs , num ):
50
- threading . Thread . __init__ (self )
54
+ super (). __init__ ()
51
55
self .gfs = gfs
52
56
self .num = num
53
57
self .daemon = True
@@ -59,9 +63,9 @@ def run(self):
59
63
file .close ()
60
64
61
65
62
- class JustRead (threading . Thread ):
66
+ class JustRead (ConcurrentRunner ):
63
67
def __init__ (self , gfs , num , results ):
64
- threading . Thread . __init__ (self )
68
+ super (). __init__ ()
65
69
self .gfs = gfs
66
70
self .num = num
67
71
self .results = results
@@ -89,12 +93,13 @@ def setUp(self):
89
93
90
94
def test_basic (self ):
91
95
oid = self .fs .upload_from_stream ("test_filename" , b"hello world" )
92
- self .assertEqual (b"hello world" , self .fs .open_download_stream (oid ).read ())
96
+ self .assertEqual (b"hello world" , ( self .fs .open_download_stream (oid ) ).read ())
93
97
self .assertEqual (1 , self .db .fs .files .count_documents ({}))
94
98
self .assertEqual (1 , self .db .fs .chunks .count_documents ({}))
95
99
96
100
self .fs .delete (oid )
97
- self .assertRaises (NoFile , self .fs .open_download_stream , oid )
101
+ with self .assertRaises (NoFile ):
102
+ self .fs .open_download_stream (oid )
98
103
self .assertEqual (0 , self .db .fs .files .count_documents ({}))
99
104
self .assertEqual (0 , self .db .fs .chunks .count_documents ({}))
100
105
@@ -111,7 +116,7 @@ def test_multi_chunk_delete(self):
111
116
112
117
def test_empty_file (self ):
113
118
oid = self .fs .upload_from_stream ("test_filename" , b"" )
114
- self .assertEqual (b"" , self .fs .open_download_stream (oid ).read ())
119
+ self .assertEqual (b"" , ( self .fs .open_download_stream (oid ) ).read ())
115
120
self .assertEqual (1 , self .db .fs .files .count_documents ({}))
116
121
self .assertEqual (0 , self .db .fs .chunks .count_documents ({}))
117
122
@@ -128,10 +133,12 @@ def test_corrupt_chunk(self):
128
133
self .db .fs .chunks .update_one ({"files_id" : files_id }, {"$set" : {"data" : Binary (b"foo" , 0 )}})
129
134
try :
130
135
out = self .fs .open_download_stream (files_id )
131
- self .assertRaises (CorruptGridFile , out .read )
136
+ with self .assertRaises (CorruptGridFile ):
137
+ out .read ()
132
138
133
139
out = self .fs .open_download_stream (files_id )
134
- self .assertRaises (CorruptGridFile , out .readline )
140
+ with self .assertRaises (CorruptGridFile ):
141
+ out .readline ()
135
142
finally :
136
143
self .fs .delete (files_id )
137
144
@@ -146,13 +153,13 @@ def test_upload_ensures_index(self):
146
153
self .assertTrue (
147
154
any (
148
155
info .get ("key" ) == [("files_id" , 1 ), ("n" , 1 )]
149
- for info in chunks .index_information ().values ()
156
+ for info in ( chunks .index_information () ).values ()
150
157
)
151
158
)
152
159
self .assertTrue (
153
160
any (
154
161
info .get ("key" ) == [("filename" , 1 ), ("uploadDate" , 1 )]
155
- for info in files .index_information ().values ()
162
+ for info in ( files .index_information () ).values ()
156
163
)
157
164
)
158
165
@@ -174,33 +181,35 @@ def test_ensure_index_shell_compat(self):
174
181
self .assertTrue (
175
182
any (
176
183
info .get ("key" ) == [("filename" , 1 ), ("uploadDate" , 1 )]
177
- for info in files .index_information ().values ()
184
+ for info in ( files .index_information () ).values ()
178
185
)
179
186
)
180
187
files .drop ()
181
188
182
189
def test_alt_collection (self ):
183
190
oid = self .alt .upload_from_stream ("test_filename" , b"hello world" )
184
- self .assertEqual (b"hello world" , self .alt .open_download_stream (oid ).read ())
191
+ self .assertEqual (b"hello world" , ( self .alt .open_download_stream (oid ) ).read ())
185
192
self .assertEqual (1 , self .db .alt .files .count_documents ({}))
186
193
self .assertEqual (1 , self .db .alt .chunks .count_documents ({}))
187
194
188
195
self .alt .delete (oid )
189
- self .assertRaises (NoFile , self .alt .open_download_stream , oid )
196
+ with self .assertRaises (NoFile ):
197
+ self .alt .open_download_stream (oid )
190
198
self .assertEqual (0 , self .db .alt .files .count_documents ({}))
191
199
self .assertEqual (0 , self .db .alt .chunks .count_documents ({}))
192
200
193
- self .assertRaises (NoFile , self .alt .open_download_stream , "foo" )
201
+ with self .assertRaises (NoFile ):
202
+ self .alt .open_download_stream ("foo" )
194
203
self .alt .upload_from_stream ("foo" , b"hello world" )
195
- self .assertEqual (b"hello world" , self .alt .open_download_stream_by_name ("foo" ).read ())
204
+ self .assertEqual (b"hello world" , ( self .alt .open_download_stream_by_name ("foo" ) ).read ())
196
205
197
206
self .alt .upload_from_stream ("mike" , b"" )
198
207
self .alt .upload_from_stream ("test" , b"foo" )
199
208
self .alt .upload_from_stream ("hello world" , b"" )
200
209
201
210
self .assertEqual (
202
211
{"mike" , "test" , "hello world" , "foo" },
203
- {k ["filename" ] for k in list ( self .db .alt .files .find ())},
212
+ {k ["filename" ] for k in self .db .alt .files .find (). to_list ( )},
204
213
)
205
214
206
215
def test_threaded_reads (self ):
@@ -240,13 +249,14 @@ def test_get_last_version(self):
240
249
two = two ._id
241
250
three = self .fs .upload_from_stream ("test" , b"baz" )
242
251
243
- self .assertEqual (b"baz" , self .fs .open_download_stream_by_name ("test" ).read ())
252
+ self .assertEqual (b"baz" , ( self .fs .open_download_stream_by_name ("test" ) ).read ())
244
253
self .fs .delete (three )
245
- self .assertEqual (b"bar" , self .fs .open_download_stream_by_name ("test" ).read ())
254
+ self .assertEqual (b"bar" , ( self .fs .open_download_stream_by_name ("test" ) ).read ())
246
255
self .fs .delete (two )
247
- self .assertEqual (b"foo" , self .fs .open_download_stream_by_name ("test" ).read ())
256
+ self .assertEqual (b"foo" , ( self .fs .open_download_stream_by_name ("test" ) ).read ())
248
257
self .fs .delete (one )
249
- self .assertRaises (NoFile , self .fs .open_download_stream_by_name , "test" )
258
+ with self .assertRaises (NoFile ):
259
+ self .fs .open_download_stream_by_name ("test" )
250
260
251
261
def test_get_version (self ):
252
262
self .fs .upload_from_stream ("test" , b"foo" )
@@ -256,28 +266,30 @@ def test_get_version(self):
256
266
self .fs .upload_from_stream ("test" , b"baz" )
257
267
time .sleep (0.01 )
258
268
259
- self .assertEqual (b"foo" , self .fs .open_download_stream_by_name ("test" , revision = 0 ).read ())
260
- self .assertEqual (b"bar" , self .fs .open_download_stream_by_name ("test" , revision = 1 ).read ())
261
- self .assertEqual (b"baz" , self .fs .open_download_stream_by_name ("test" , revision = 2 ).read ())
269
+ self .assertEqual (b"foo" , ( self .fs .open_download_stream_by_name ("test" , revision = 0 ) ).read ())
270
+ self .assertEqual (b"bar" , ( self .fs .open_download_stream_by_name ("test" , revision = 1 ) ).read ())
271
+ self .assertEqual (b"baz" , ( self .fs .open_download_stream_by_name ("test" , revision = 2 ) ).read ())
262
272
263
- self .assertEqual (b"baz" , self .fs .open_download_stream_by_name ("test" , revision = - 1 ).read ())
264
- self .assertEqual (b"bar" , self .fs .open_download_stream_by_name ("test" , revision = - 2 ).read ())
265
- self .assertEqual (b"foo" , self .fs .open_download_stream_by_name ("test" , revision = - 3 ).read ())
273
+ self .assertEqual (b"baz" , ( self .fs .open_download_stream_by_name ("test" , revision = - 1 ) ).read ())
274
+ self .assertEqual (b"bar" , ( self .fs .open_download_stream_by_name ("test" , revision = - 2 ) ).read ())
275
+ self .assertEqual (b"foo" , ( self .fs .open_download_stream_by_name ("test" , revision = - 3 ) ).read ())
266
276
267
- self .assertRaises (NoFile , self .fs .open_download_stream_by_name , "test" , revision = 3 )
268
- self .assertRaises (NoFile , self .fs .open_download_stream_by_name , "test" , revision = - 4 )
277
+ with self .assertRaises (NoFile ):
278
+ self .fs .open_download_stream_by_name ("test" , revision = 3 )
279
+ with self .assertRaises (NoFile ):
280
+ self .fs .open_download_stream_by_name ("test" , revision = - 4 )
269
281
270
282
def test_upload_from_stream (self ):
271
283
oid = self .fs .upload_from_stream ("test_file" , BytesIO (b"hello world" ), chunk_size_bytes = 1 )
272
284
self .assertEqual (11 , self .db .fs .chunks .count_documents ({}))
273
- self .assertEqual (b"hello world" , self .fs .open_download_stream (oid ).read ())
285
+ self .assertEqual (b"hello world" , ( self .fs .open_download_stream (oid ) ).read ())
274
286
275
287
def test_upload_from_stream_with_id (self ):
276
288
oid = ObjectId ()
277
289
self .fs .upload_from_stream_with_id (
278
290
oid , "test_file_custom_id" , BytesIO (b"custom id" ), chunk_size_bytes = 1
279
291
)
280
- self .assertEqual (b"custom id" , self .fs .open_download_stream (oid ).read ())
292
+ self .assertEqual (b"custom id" , ( self .fs .open_download_stream (oid ) ).read ())
281
293
282
294
@patch ("gridfs.synchronous.grid_file._UPLOAD_BUFFER_CHUNKS" , 3 )
283
295
@client_context .require_failCommand_fail_point
@@ -316,14 +328,14 @@ def test_open_upload_stream(self):
316
328
gin = self .fs .open_upload_stream ("from_stream" )
317
329
gin .write (b"from stream" )
318
330
gin .close ()
319
- self .assertEqual (b"from stream" , self .fs .open_download_stream (gin ._id ).read ())
331
+ self .assertEqual (b"from stream" , ( self .fs .open_download_stream (gin ._id ) ).read ())
320
332
321
333
def test_open_upload_stream_with_id (self ):
322
334
oid = ObjectId ()
323
335
gin = self .fs .open_upload_stream_with_id (oid , "from_stream_custom_id" )
324
336
gin .write (b"from stream with custom id" )
325
337
gin .close ()
326
- self .assertEqual (b"from stream with custom id" , self .fs .open_download_stream (oid ).read ())
338
+ self .assertEqual (b"from stream with custom id" , ( self .fs .open_download_stream (oid ) ).read ())
327
339
328
340
def test_missing_length_iter (self ):
329
341
# Test fix that guards against PHP-237
@@ -345,12 +357,12 @@ def test_gridfs_lazy_connect(self):
345
357
client = self .single_client ("badhost" , connect = False , serverSelectionTimeoutMS = 0 )
346
358
cdb = client .db
347
359
gfs = gridfs .GridFSBucket (cdb )
348
- self .assertRaises (ServerSelectionTimeoutError , gfs .delete , 0 )
360
+ with self .assertRaises (ServerSelectionTimeoutError ):
361
+ gfs .delete (0 )
349
362
350
363
gfs = gridfs .GridFSBucket (cdb )
351
- self .assertRaises (
352
- ServerSelectionTimeoutError , gfs .upload_from_stream , "test" , b""
353
- ) # Still no connection.
364
+ with self .assertRaises (ServerSelectionTimeoutError ):
365
+ gfs .upload_from_stream ("test" , b"" ) # Still no connection.
354
366
355
367
def test_gridfs_find (self ):
356
368
self .fs .upload_from_stream ("two" , b"test2" )
@@ -366,14 +378,15 @@ def test_gridfs_find(self):
366
378
cursor = self .fs .find (
367
379
{}, no_cursor_timeout = False , sort = [("uploadDate" , - 1 )], skip = 1 , limit = 2
368
380
)
369
- gout = next (cursor )
381
+ gout = cursor . next ()
370
382
self .assertEqual (b"test1" , gout .read ())
371
383
cursor .rewind ()
372
- gout = next (cursor )
384
+ gout = cursor . next ()
373
385
self .assertEqual (b"test1" , gout .read ())
374
- gout = next (cursor )
386
+ gout = cursor . next ()
375
387
self .assertEqual (b"test2+" , gout .read ())
376
- self .assertRaises (StopIteration , cursor .__next__ )
388
+ with self .assertRaises (StopIteration ):
389
+ cursor .next ()
377
390
cursor .close ()
378
391
self .assertRaises (TypeError , self .fs .find , {}, {"_id" : True })
379
392
@@ -383,20 +396,21 @@ def test_grid_in_non_int_chunksize(self):
383
396
self .fs .upload_from_stream ("f" , data )
384
397
self .db .fs .files .update_one ({"filename" : "f" }, {"$set" : {"chunkSize" : 100.0 }})
385
398
386
- self .assertEqual (data , self .fs .open_download_stream_by_name ("f" ).read ())
399
+ self .assertEqual (data , ( self .fs .open_download_stream_by_name ("f" ) ).read ())
387
400
388
401
def test_unacknowledged (self ):
389
402
# w=0 is prohibited.
390
403
with self .assertRaises (ConfigurationError ):
391
- gridfs .GridFSBucket (self .rs_or_single_client (w = 0 ).pymongo_test )
404
+ gridfs .GridFSBucket (( self .rs_or_single_client (w = 0 ) ).pymongo_test )
392
405
393
406
def test_rename (self ):
394
407
_id = self .fs .upload_from_stream ("first_name" , b"testing" )
395
- self .assertEqual (b"testing" , self .fs .open_download_stream_by_name ("first_name" ).read ())
408
+ self .assertEqual (b"testing" , ( self .fs .open_download_stream_by_name ("first_name" ) ).read ())
396
409
397
410
self .fs .rename (_id , "second_name" )
398
- self .assertRaises (NoFile , self .fs .open_download_stream_by_name , "first_name" )
399
- self .assertEqual (b"testing" , self .fs .open_download_stream_by_name ("second_name" ).read ())
411
+ with self .assertRaises (NoFile ):
412
+ self .fs .open_download_stream_by_name ("first_name" )
413
+ self .assertEqual (b"testing" , (self .fs .open_download_stream_by_name ("second_name" )).read ())
400
414
401
415
@patch ("gridfs.synchronous.grid_file._UPLOAD_BUFFER_SIZE" , 5 )
402
416
def test_abort (self ):
@@ -407,7 +421,8 @@ def test_abort(self):
407
421
self .assertEqual (3 , self .db .fs .chunks .count_documents ({"files_id" : gin ._id }))
408
422
gin .abort ()
409
423
self .assertTrue (gin .closed )
410
- self .assertRaises (ValueError , gin .write , b"test4" )
424
+ with self .assertRaises (ValueError ):
425
+ gin .write (b"test4" )
411
426
self .assertEqual (0 , self .db .fs .chunks .count_documents ({"files_id" : gin ._id }))
412
427
413
428
def test_download_to_stream (self ):
@@ -490,7 +505,7 @@ def test_gridfs_replica_set(self):
490
505
491
506
gfs = gridfs .GridFSBucket (rsc .gfsbucketreplica , "gfsbucketreplicatest" )
492
507
oid = gfs .upload_from_stream ("test_filename" , b"foo" )
493
- content = gfs .open_download_stream (oid ).read ()
508
+ content = ( gfs .open_download_stream (oid ) ).read ()
494
509
self .assertEqual (b"foo" , content )
495
510
496
511
def test_gridfs_secondary (self ):
@@ -504,7 +519,8 @@ def test_gridfs_secondary(self):
504
519
gfs = gridfs .GridFSBucket (secondary_connection .gfsbucketreplica , "gfsbucketsecondarytest" )
505
520
506
521
# This won't detect secondary, raises error
507
- self .assertRaises (NotPrimaryError , gfs .upload_from_stream , "test_filename" , b"foo" )
522
+ with self .assertRaises (NotPrimaryError ):
523
+ gfs .upload_from_stream ("test_filename" , b"foo" )
508
524
509
525
def test_gridfs_secondary_lazy (self ):
510
526
# Should detect it's connected to secondary and not attempt to
@@ -518,8 +534,10 @@ def test_gridfs_secondary_lazy(self):
518
534
gfs = gridfs .GridFSBucket (client .gfsbucketreplica , "gfsbucketsecondarylazytest" )
519
535
520
536
# Connects, doesn't create index.
521
- self .assertRaises (NoFile , gfs .open_download_stream_by_name , "test_filename" )
522
- self .assertRaises (NotPrimaryError , gfs .upload_from_stream , "test_filename" , b"data" )
537
+ with self .assertRaises (NoFile ):
538
+ gfs .open_download_stream_by_name ("test_filename" )
539
+ with self .assertRaises (NotPrimaryError ):
540
+ gfs .upload_from_stream ("test_filename" , b"data" )
523
541
524
542
525
543
if __name__ == "__main__" :
0 commit comments