1616"""Tests for the gridfs package."""
1717from __future__ import annotations
1818
19+ import asyncio
1920import datetime
2021import itertools
2122import sys
2223import threading
2324import time
2425from io import BytesIO
26+ from test .helpers import ConcurrentRunner
2527from unittest .mock import patch
2628
2729sys .path [0 :0 ] = ["" ]
4446from pymongo .read_preferences import ReadPreference
4547from pymongo .synchronous .mongo_client import MongoClient
4648
49+ _IS_SYNC = True
4750
48- class JustWrite (threading .Thread ):
51+
52+ class JustWrite (ConcurrentRunner ):
4953 def __init__ (self , gfs , num ):
50- threading . Thread . __init__ (self )
54+ super (). __init__ ()
5155 self .gfs = gfs
5256 self .num = num
5357 self .daemon = True
@@ -59,9 +63,9 @@ def run(self):
5963 file .close ()
6064
6165
62- class JustRead (threading . Thread ):
66+ class JustRead (ConcurrentRunner ):
6367 def __init__ (self , gfs , num , results ):
64- threading . Thread . __init__ (self )
68+ super (). __init__ ()
6569 self .gfs = gfs
6670 self .num = num
6771 self .results = results
@@ -89,12 +93,13 @@ def setUp(self):
8993
9094 def test_basic (self ):
9195 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 ())
9397 self .assertEqual (1 , self .db .fs .files .count_documents ({}))
9498 self .assertEqual (1 , self .db .fs .chunks .count_documents ({}))
9599
96100 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 )
98103 self .assertEqual (0 , self .db .fs .files .count_documents ({}))
99104 self .assertEqual (0 , self .db .fs .chunks .count_documents ({}))
100105
@@ -111,7 +116,7 @@ def test_multi_chunk_delete(self):
111116
112117 def test_empty_file (self ):
113118 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 ())
115120 self .assertEqual (1 , self .db .fs .files .count_documents ({}))
116121 self .assertEqual (0 , self .db .fs .chunks .count_documents ({}))
117122
@@ -128,10 +133,12 @@ def test_corrupt_chunk(self):
128133 self .db .fs .chunks .update_one ({"files_id" : files_id }, {"$set" : {"data" : Binary (b"foo" , 0 )}})
129134 try :
130135 out = self .fs .open_download_stream (files_id )
131- self .assertRaises (CorruptGridFile , out .read )
136+ with self .assertRaises (CorruptGridFile ):
137+ out .read ()
132138
133139 out = self .fs .open_download_stream (files_id )
134- self .assertRaises (CorruptGridFile , out .readline )
140+ with self .assertRaises (CorruptGridFile ):
141+ out .readline ()
135142 finally :
136143 self .fs .delete (files_id )
137144
@@ -146,13 +153,13 @@ def test_upload_ensures_index(self):
146153 self .assertTrue (
147154 any (
148155 info .get ("key" ) == [("files_id" , 1 ), ("n" , 1 )]
149- for info in chunks .index_information ().values ()
156+ for info in ( chunks .index_information () ).values ()
150157 )
151158 )
152159 self .assertTrue (
153160 any (
154161 info .get ("key" ) == [("filename" , 1 ), ("uploadDate" , 1 )]
155- for info in files .index_information ().values ()
162+ for info in ( files .index_information () ).values ()
156163 )
157164 )
158165
@@ -174,33 +181,35 @@ def test_ensure_index_shell_compat(self):
174181 self .assertTrue (
175182 any (
176183 info .get ("key" ) == [("filename" , 1 ), ("uploadDate" , 1 )]
177- for info in files .index_information ().values ()
184+ for info in ( files .index_information () ).values ()
178185 )
179186 )
180187 files .drop ()
181188
182189 def test_alt_collection (self ):
183190 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 ())
185192 self .assertEqual (1 , self .db .alt .files .count_documents ({}))
186193 self .assertEqual (1 , self .db .alt .chunks .count_documents ({}))
187194
188195 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 )
190198 self .assertEqual (0 , self .db .alt .files .count_documents ({}))
191199 self .assertEqual (0 , self .db .alt .chunks .count_documents ({}))
192200
193- self .assertRaises (NoFile , self .alt .open_download_stream , "foo" )
201+ with self .assertRaises (NoFile ):
202+ self .alt .open_download_stream ("foo" )
194203 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 ())
196205
197206 self .alt .upload_from_stream ("mike" , b"" )
198207 self .alt .upload_from_stream ("test" , b"foo" )
199208 self .alt .upload_from_stream ("hello world" , b"" )
200209
201210 self .assertEqual (
202211 {"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 ( )},
204213 )
205214
206215 def test_threaded_reads (self ):
@@ -240,13 +249,14 @@ def test_get_last_version(self):
240249 two = two ._id
241250 three = self .fs .upload_from_stream ("test" , b"baz" )
242251
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 ())
244253 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 ())
246255 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 ())
248257 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" )
250260
251261 def test_get_version (self ):
252262 self .fs .upload_from_stream ("test" , b"foo" )
@@ -256,28 +266,30 @@ def test_get_version(self):
256266 self .fs .upload_from_stream ("test" , b"baz" )
257267 time .sleep (0.01 )
258268
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 ())
262272
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 ())
266276
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 )
269281
270282 def test_upload_from_stream (self ):
271283 oid = self .fs .upload_from_stream ("test_file" , BytesIO (b"hello world" ), chunk_size_bytes = 1 )
272284 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 ())
274286
275287 def test_upload_from_stream_with_id (self ):
276288 oid = ObjectId ()
277289 self .fs .upload_from_stream_with_id (
278290 oid , "test_file_custom_id" , BytesIO (b"custom id" ), chunk_size_bytes = 1
279291 )
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 ())
281293
282294 @patch ("gridfs.synchronous.grid_file._UPLOAD_BUFFER_CHUNKS" , 3 )
283295 @client_context .require_failCommand_fail_point
@@ -316,14 +328,14 @@ def test_open_upload_stream(self):
316328 gin = self .fs .open_upload_stream ("from_stream" )
317329 gin .write (b"from stream" )
318330 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 ())
320332
321333 def test_open_upload_stream_with_id (self ):
322334 oid = ObjectId ()
323335 gin = self .fs .open_upload_stream_with_id (oid , "from_stream_custom_id" )
324336 gin .write (b"from stream with custom id" )
325337 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 ())
327339
328340 def test_missing_length_iter (self ):
329341 # Test fix that guards against PHP-237
@@ -345,12 +357,12 @@ def test_gridfs_lazy_connect(self):
345357 client = self .single_client ("badhost" , connect = False , serverSelectionTimeoutMS = 0 )
346358 cdb = client .db
347359 gfs = gridfs .GridFSBucket (cdb )
348- self .assertRaises (ServerSelectionTimeoutError , gfs .delete , 0 )
360+ with self .assertRaises (ServerSelectionTimeoutError ):
361+ gfs .delete (0 )
349362
350363 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.
354366
355367 def test_gridfs_find (self ):
356368 self .fs .upload_from_stream ("two" , b"test2" )
@@ -366,14 +378,15 @@ def test_gridfs_find(self):
366378 cursor = self .fs .find (
367379 {}, no_cursor_timeout = False , sort = [("uploadDate" , - 1 )], skip = 1 , limit = 2
368380 )
369- gout = next (cursor )
381+ gout = cursor . next ()
370382 self .assertEqual (b"test1" , gout .read ())
371383 cursor .rewind ()
372- gout = next (cursor )
384+ gout = cursor . next ()
373385 self .assertEqual (b"test1" , gout .read ())
374- gout = next (cursor )
386+ gout = cursor . next ()
375387 self .assertEqual (b"test2+" , gout .read ())
376- self .assertRaises (StopIteration , cursor .__next__ )
388+ with self .assertRaises (StopIteration ):
389+ cursor .next ()
377390 cursor .close ()
378391 self .assertRaises (TypeError , self .fs .find , {}, {"_id" : True })
379392
@@ -383,20 +396,21 @@ def test_grid_in_non_int_chunksize(self):
383396 self .fs .upload_from_stream ("f" , data )
384397 self .db .fs .files .update_one ({"filename" : "f" }, {"$set" : {"chunkSize" : 100.0 }})
385398
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 ())
387400
388401 def test_unacknowledged (self ):
389402 # w=0 is prohibited.
390403 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 )
392405
393406 def test_rename (self ):
394407 _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 ())
396409
397410 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 ())
400414
401415 @patch ("gridfs.synchronous.grid_file._UPLOAD_BUFFER_SIZE" , 5 )
402416 def test_abort (self ):
@@ -407,7 +421,8 @@ def test_abort(self):
407421 self .assertEqual (3 , self .db .fs .chunks .count_documents ({"files_id" : gin ._id }))
408422 gin .abort ()
409423 self .assertTrue (gin .closed )
410- self .assertRaises (ValueError , gin .write , b"test4" )
424+ with self .assertRaises (ValueError ):
425+ gin .write (b"test4" )
411426 self .assertEqual (0 , self .db .fs .chunks .count_documents ({"files_id" : gin ._id }))
412427
413428 def test_download_to_stream (self ):
@@ -490,7 +505,7 @@ def test_gridfs_replica_set(self):
490505
491506 gfs = gridfs .GridFSBucket (rsc .gfsbucketreplica , "gfsbucketreplicatest" )
492507 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 ()
494509 self .assertEqual (b"foo" , content )
495510
496511 def test_gridfs_secondary (self ):
@@ -504,7 +519,8 @@ def test_gridfs_secondary(self):
504519 gfs = gridfs .GridFSBucket (secondary_connection .gfsbucketreplica , "gfsbucketsecondarytest" )
505520
506521 # 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" )
508524
509525 def test_gridfs_secondary_lazy (self ):
510526 # Should detect it's connected to secondary and not attempt to
@@ -518,8 +534,10 @@ def test_gridfs_secondary_lazy(self):
518534 gfs = gridfs .GridFSBucket (client .gfsbucketreplica , "gfsbucketsecondarylazytest" )
519535
520536 # 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" )
523541
524542
525543if __name__ == "__main__" :
0 commit comments