39
39
from bson .regex import Regex
40
40
from bson .son import SON
41
41
from pymongo import helpers_shared
42
- from pymongo .asynchronous import auth
43
42
from pymongo .errors import (
44
43
CollectionInvalid ,
45
44
ExecutionTimeout ,
50
49
)
51
50
from pymongo .read_concern import ReadConcern
52
51
from pymongo .read_preferences import ReadPreference
52
+ from pymongo .synchronous import auth
53
53
from pymongo .synchronous .collection import Collection
54
54
from pymongo .synchronous .database import Database
55
+ from pymongo .synchronous .helpers import next
55
56
from pymongo .synchronous .mongo_client import MongoClient
56
57
from pymongo .write_concern import WriteConcern
57
58
59
+ _IS_SYNC = True
60
+
58
61
59
62
class TestDatabaseNoConnect (unittest .TestCase ):
60
63
"""Test Database features on a client that does not connect."""
@@ -140,32 +143,38 @@ def test_get_coll(self):
140
143
self .assertEqual (db .test .mike , db ["test.mike" ])
141
144
142
145
def test_repr (self ):
146
+ name = "Database"
143
147
self .assertEqual (
144
148
repr (Database (self .client , "pymongo_test" )),
145
- "Database ({!r}, {})" .format (self .client , repr ("pymongo_test" )),
149
+ "{} ({!r}, {})" .format (name , self .client , repr ("pymongo_test" )),
146
150
)
147
151
148
152
def test_create_collection (self ):
149
153
db = Database (self .client , "pymongo_test" )
150
154
151
155
db .test .insert_one ({"hello" : "world" })
152
- self .assertRaises (CollectionInvalid , db .create_collection , "test" )
156
+ with self .assertRaises (CollectionInvalid ):
157
+ db .create_collection ("test" )
153
158
154
159
db .drop_collection ("test" )
155
160
156
- self .assertRaises (TypeError , db .create_collection , 5 )
157
- self .assertRaises (TypeError , db .create_collection , None )
158
- self .assertRaises (InvalidName , db .create_collection , "coll..ection" )
161
+ with self .assertRaises (TypeError ):
162
+ db .create_collection (5 ) # type: ignore[arg-type]
163
+ with self .assertRaises (TypeError ):
164
+ db .create_collection (None ) # type: ignore[arg-type]
165
+ with self .assertRaises (InvalidName ):
166
+ db .create_collection ("coll..ection" ) # type: ignore[arg-type]
159
167
160
168
test = db .create_collection ("test" )
161
169
self .assertTrue ("test" in db .list_collection_names ())
162
170
test .insert_one ({"hello" : "world" })
163
- self .assertEqual (db .test .find_one ()["hello" ], "world" ) # type: ignore
171
+ self .assertEqual (( db .test .find_one ()) ["hello" ], "world" )
164
172
165
173
db .drop_collection ("test.foo" )
166
174
db .create_collection ("test.foo" )
167
175
self .assertTrue ("test.foo" in db .list_collection_names ())
168
- self .assertRaises (CollectionInvalid , db .create_collection , "test.foo" )
176
+ with self .assertRaises (CollectionInvalid ):
177
+ db .create_collection ("test.foo" )
169
178
170
179
def test_list_collection_names (self ):
171
180
db = Database (self .client , "pymongo_test" )
@@ -274,11 +283,11 @@ def test_list_collections(self):
274
283
else :
275
284
self .assertTrue (False )
276
285
277
- colls = db .list_collections (filter = {"name" : {"$regex" : "^test$" }})
278
- self .assertEqual (1 , len (list ( colls ) ))
286
+ colls = ( db .list_collections (filter = {"name" : {"$regex" : "^test$" }})). to_list ( )
287
+ self .assertEqual (1 , len (colls ))
279
288
280
- colls = db .list_collections (filter = {"name" : {"$regex" : "^test.mike$" }})
281
- self .assertEqual (1 , len (list ( colls ) ))
289
+ colls = ( db .list_collections (filter = {"name" : {"$regex" : "^test.mike$" }})). to_list ( )
290
+ self .assertEqual (1 , len (colls ))
282
291
283
292
db .drop_collection ("test" )
284
293
@@ -326,8 +335,10 @@ def test_list_collection_names_single_socket(self):
326
335
def test_drop_collection (self ):
327
336
db = Database (self .client , "pymongo_test" )
328
337
329
- self .assertRaises (TypeError , db .drop_collection , 5 )
330
- self .assertRaises (TypeError , db .drop_collection , None )
338
+ with self .assertRaises (TypeError ):
339
+ db .drop_collection (5 ) # type: ignore[arg-type]
340
+ with self .assertRaises (TypeError ):
341
+ db .drop_collection (None ) # type: ignore[arg-type]
331
342
332
343
db .test .insert_one ({"dummy" : "object" })
333
344
self .assertTrue ("test" in db .list_collection_names ())
@@ -360,13 +371,17 @@ def test_drop_collection(self):
360
371
def test_validate_collection (self ):
361
372
db = self .client .pymongo_test
362
373
363
- self .assertRaises (TypeError , db .validate_collection , 5 )
364
- self .assertRaises (TypeError , db .validate_collection , None )
374
+ with self .assertRaises (TypeError ):
375
+ db .validate_collection (5 ) # type: ignore[arg-type]
376
+ with self .assertRaises (TypeError ):
377
+ db .validate_collection (None ) # type: ignore[arg-type]
365
378
366
379
db .test .insert_one ({"dummy" : "object" })
367
380
368
- self .assertRaises (OperationFailure , db .validate_collection , "test.doesnotexist" )
369
- self .assertRaises (OperationFailure , db .validate_collection , db .test .doesnotexist )
381
+ with self .assertRaises (OperationFailure ):
382
+ db .validate_collection ("test.doesnotexist" )
383
+ with self .assertRaises (OperationFailure ):
384
+ db .validate_collection (db .test .doesnotexist )
370
385
371
386
self .assertTrue (db .validate_collection ("test" ))
372
387
self .assertTrue (db .validate_collection (db .test ))
@@ -426,17 +441,21 @@ def test_cursor_command(self):
426
441
427
442
self .assertIsInstance (cursor , CommandCursor )
428
443
429
- result_docs = list ( cursor )
444
+ result_docs = cursor . to_list ( )
430
445
self .assertEqual (docs , result_docs )
431
446
432
447
def test_cursor_command_invalid (self ):
433
- self .assertRaises (InvalidOperation , self .db .cursor_command , "usersInfo" , "test" )
448
+ with self .assertRaises (InvalidOperation ):
449
+ self .db .cursor_command ("usersInfo" , "test" )
434
450
435
451
@client_context .require_no_fips
436
452
def test_password_digest (self ):
437
- self .assertRaises (TypeError , auth ._password_digest , 5 )
438
- self .assertRaises (TypeError , auth ._password_digest , True )
439
- self .assertRaises (TypeError , auth ._password_digest , None )
453
+ with self .assertRaises (TypeError ):
454
+ auth ._password_digest (5 ) # type: ignore[arg-type, call-arg]
455
+ with self .assertRaises (TypeError ):
456
+ auth ._password_digest (True ) # type: ignore[arg-type, call-arg]
457
+ with self .assertRaises (TypeError ):
458
+ auth ._password_digest (None ) # type: ignore[arg-type, call-arg]
440
459
441
460
self .assertTrue (isinstance (auth ._password_digest ("mike" , "password" ), str ))
442
461
self .assertEqual (
@@ -470,16 +489,20 @@ def test_deref(self):
470
489
db = self .client .pymongo_test
471
490
db .test .drop ()
472
491
473
- self .assertRaises (TypeError , db .dereference , 5 )
474
- self .assertRaises (TypeError , db .dereference , "hello" )
475
- self .assertRaises (TypeError , db .dereference , None )
492
+ with self .assertRaises (TypeError ):
493
+ db .dereference (5 ) # type: ignore[arg-type]
494
+ with self .assertRaises (TypeError ):
495
+ db .dereference ("hello" ) # type: ignore[arg-type]
496
+ with self .assertRaises (TypeError ):
497
+ db .dereference (None ) # type: ignore[arg-type]
476
498
477
499
self .assertEqual (None , db .dereference (DBRef ("test" , ObjectId ())))
478
500
obj : dict [str , Any ] = {"x" : True }
479
- key = db .test .insert_one (obj ).inserted_id
501
+ key = ( db .test .insert_one (obj ) ).inserted_id
480
502
self .assertEqual (obj , db .dereference (DBRef ("test" , key )))
481
503
self .assertEqual (obj , db .dereference (DBRef ("test" , key , "pymongo_test" )))
482
- self .assertRaises (ValueError , db .dereference , DBRef ("test" , key , "foo" ))
504
+ with self .assertRaises (ValueError ):
505
+ db .dereference (DBRef ("test" , key , "foo" ))
483
506
484
507
self .assertEqual (None , db .dereference (DBRef ("test" , 4 )))
485
508
obj = {"_id" : 4 }
@@ -504,7 +527,7 @@ def test_insert_find_one(self):
504
527
db .test .drop ()
505
528
506
529
a_doc = SON ({"hello" : "world" })
507
- a_key = db .test .insert_one (a_doc ).inserted_id
530
+ a_key = ( db .test .insert_one (a_doc ) ).inserted_id
508
531
self .assertTrue (isinstance (a_doc ["_id" ], ObjectId ))
509
532
self .assertEqual (a_doc ["_id" ], a_key )
510
533
self .assertEqual (a_doc , db .test .find_one ({"_id" : a_doc ["_id" ]}))
@@ -531,12 +554,12 @@ def test_long(self):
531
554
db = self .client .pymongo_test
532
555
db .test .drop ()
533
556
db .test .insert_one ({"x" : 9223372036854775807 })
534
- retrieved = db .test .find_one ()["x" ] # type: ignore
557
+ retrieved = ( db .test .find_one ()) ["x" ]
535
558
self .assertEqual (Int64 (9223372036854775807 ), retrieved )
536
559
self .assertIsInstance (retrieved , Int64 )
537
560
db .test .delete_many ({})
538
561
db .test .insert_one ({"x" : Int64 (1 )})
539
- retrieved = db .test .find_one ()["x" ] # type: ignore
562
+ retrieved = ( db .test .find_one ()) ["x" ]
540
563
self .assertEqual (Int64 (1 ), retrieved )
541
564
self .assertIsInstance (retrieved , Int64 )
542
565
@@ -578,7 +601,8 @@ def test_command_response_without_ok(self):
578
601
# Sometimes (SERVER-10891) the server's response to a badly-formatted
579
602
# command document will have no 'ok' field. We should raise
580
603
# OperationFailure instead of KeyError.
581
- self .assertRaises (OperationFailure , helpers_shared ._check_command_response , {}, None )
604
+ with self .assertRaises (OperationFailure ):
605
+ helpers_shared ._check_command_response ({}, None )
582
606
583
607
try :
584
608
helpers_shared ._check_command_response ({"$err" : "foo" }, None )
@@ -624,22 +648,23 @@ def test_command_max_time_ms(self):
624
648
try :
625
649
db = self .client .pymongo_test
626
650
db .command ("count" , "test" )
627
- self .assertRaises (ExecutionTimeout , db .command , "count" , "test" , maxTimeMS = 1 )
651
+ with self .assertRaises (ExecutionTimeout ):
652
+ db .command ("count" , "test" , maxTimeMS = 1 )
628
653
pipeline = [{"$project" : {"name" : 1 , "count" : 1 }}]
629
654
# Database command helper.
630
655
db .command ("aggregate" , "test" , pipeline = pipeline , cursor = {})
631
- self .assertRaises (
632
- ExecutionTimeout ,
633
- db .command ,
634
- "aggregate" ,
635
- "test" ,
636
- pipeline = pipeline ,
637
- cursor = {},
638
- maxTimeMS = 1 ,
639
- )
656
+ with self .assertRaises (ExecutionTimeout ):
657
+ db .command (
658
+ "aggregate" ,
659
+ "test" ,
660
+ pipeline = pipeline ,
661
+ cursor = {},
662
+ maxTimeMS = 1 ,
663
+ )
640
664
# Collection helper.
641
665
db .test .aggregate (pipeline = pipeline )
642
- self .assertRaises (ExecutionTimeout , db .test .aggregate , pipeline , maxTimeMS = 1 )
666
+ with self .assertRaises (ExecutionTimeout ):
667
+ db .test .aggregate (pipeline , maxTimeMS = 1 )
643
668
finally :
644
669
self .client .admin .command ("configureFailPoint" , "maxTimeAlwaysTimeOut" , mode = "off" )
645
670
@@ -723,7 +748,10 @@ def test_database_aggregation_fake_cursor(self):
723
748
with self .assertRaises (StopIteration ):
724
749
next (cursor )
725
750
726
- result = wait_until (output_coll .find_one , "read unacknowledged write" )
751
+ def lambda_fn ():
752
+ return output_coll .find_one ()
753
+
754
+ result = wait_until (lambda_fn , "read unacknowledged write" )
727
755
self .assertEqual (result ["dummy" ], self .result ["dummy" ])
728
756
729
757
def test_bool (self ):
0 commit comments