3232from pymongo .mongo_client import MongoClient
3333from pymongo .read_preferences import ReadPreference
3434from pymongo .server_description import ServerDescription
35+ from pymongo .write_concern import WriteConcern
3536from test import SkipTest , unittest , utils , client_knobs
3637from test .utils import one , wait_until , connected
3738from test .version import Version
@@ -96,12 +97,13 @@ def setUp(self):
9697 def test_secondary_connection (self ):
9798 self .c = MongoClient (self .seed , replicaSet = self .name )
9899 wait_until (lambda : len (self .c .secondaries ), "discover secondary" )
99- db = self .c .pymongo_test
100-
101100 # Wait for replication...
102101 w = len (self .c .secondaries ) + 1
103- db .test .remove ({}, w = w )
104- db .test .insert ({'foo' : 'bar' }, w = w )
102+ db = self .c .get_database ("pymongo_test" ,
103+ write_concern = WriteConcern (w = w ))
104+
105+ db .test .delete_many ({})
106+ db .test .insert_one ({'foo' : 'bar' })
105107
106108 # Test direct connection to a primary or secondary
107109 primary_host , primary_port = ha_tools .get_primary ().split (':' )
@@ -149,7 +151,9 @@ def test_secondary_connection(self):
149151 # direct connection raises AutoReconnect('not master'), MongoClient
150152 # should do the same for unacknowledged writes.
151153 try :
152- client .pymongo_test .test .insert ({}, w = 0 )
154+ client .get_database (
155+ "pymongo_test" ,
156+ write_concern = WriteConcern (w = 0 )).test .insert_one ({})
153157 except AutoReconnect as e :
154158 self .assertEqual ('not master' , e .args [0 ])
155159 else :
@@ -167,7 +171,9 @@ def test_secondary_connection(self):
167171
168172 # See explanation above
169173 try :
170- client .pymongo_test .test .insert ({}, w = 0 )
174+ client .get_database (
175+ "pymongo_test" ,
176+ write_concern = WriteConcern (w = 0 )).test .insert_one ({})
171177 except AutoReconnect as e :
172178 self .assertEqual ('not master' , e .args [0 ])
173179 else :
@@ -389,10 +395,11 @@ def test_writes_with_failover(self):
389395 wait_until (lambda : c .primary , "discover primary" )
390396 wait_until (lambda : len (c .secondaries ) == 2 , "discover secondaries" )
391397 primary = c .primary
392- db = c .pymongo_test
393398 w = len (c .secondaries ) + 1
394- db .test .remove ({}, w = w )
395- db .test .insert ({'foo' : 'bar' }, w = w )
399+ db = c .get_database ("pymongo_test" ,
400+ write_concern = WriteConcern (w = w ))
401+ db .test .delete_many ({})
402+ db .test .insert_one ({'foo' : 'bar' })
396403 self .assertEqual ('bar' , db .test .find_one ()['foo' ])
397404
398405 killed = ha_tools .kill_primary (9 )
@@ -406,7 +413,7 @@ def test_writes_with_failover(self):
406413 # while we wait for new primary.
407414 for _ in xrange (10000 ):
408415 try :
409- db .test .insert ({'bar' : 'baz' })
416+ db .test .insert_one ({'bar' : 'baz' })
410417
411418 # No error, found primary.
412419 break
@@ -438,11 +445,12 @@ def iter_cursor(cursor):
438445 pass
439446 return True
440447
441- db = c .pymongo_test
442448 w = len (c .secondaries ) + 1
443- db .test .remove ({}, w = w )
449+ db = c .get_database ("pymongo_test" ,
450+ write_concern = WriteConcern (w = w ))
451+ db .test .delete_many ({})
444452 # Force replication
445- db .test .insert ([{'foo' : i } for i in xrange (10 )], w = w )
453+ db .test .insert_many ([{'foo' : i } for i in xrange (10 )])
446454 self .assertEqual (10 , db .test .count ())
447455
448456 db .read_preference = SECONDARY_PREFERRED
@@ -505,11 +513,11 @@ def setUp(self):
505513 self .other_secondary_dc = {'dc' : self .other_secondary_tags ['dc' ]}
506514
507515 self .c = MongoClient (self .seed , replicaSet = self .name )
508- self .db = self .c .pymongo_test
509516 self .w = len (self .c .secondaries ) + 1
510- self .db .test .remove ({}, w = self .w )
511- self .db .test .insert (
512- [{'foo' : i } for i in xrange (10 )], w = self .w )
517+ self .db = self .c .get_database ("pymongo_test" ,
518+ write_concern = WriteConcern (w = self .w ))
519+ self .db .test .delete_many ({})
520+ self .db .test .insert_many ([{'foo' : i } for i in xrange (10 )])
513521
514522 self .clear_ping_times ()
515523
@@ -754,8 +762,9 @@ def setUp(self):
754762
755763 def test_auth_during_failover (self ):
756764 self .assertTrue (self .db .authenticate ('user' , 'userpass' ))
757- self .assertTrue (self .db .foo .insert ({'foo' : 'bar' },
758- w = 3 , wtimeout = 3000 ))
765+ db = self .db .connection .get_database (
766+ self .db .name , write_concern = WriteConcern (w = 3 , wtimeout = 3000 ))
767+ self .assertTrue (db .foo .insert_one ({'foo' : 'bar' })
759768 self .db .logout ()
760769 self .assertRaises (OperationFailure , self .db .foo .find_one )
761770
@@ -820,7 +829,7 @@ def setUp(self):
820829
821830 def test_mongos_ha (self ):
822831 coll = self .client [self .dbname ].test
823- self . assertTrue ( coll .insert ({'foo' : 'bar' }))
832+ coll .insert_one ({'foo' : 'bar' }))
824833
825834 first = '%s:%d' % (self .client .host , self .client .port )
826835 ha_tools .kill_mongos (first )
@@ -873,13 +882,11 @@ def test_get_last_error_defaults(self):
873882
874883 self .c .admin .command ("replSetReconfig" , replset )
875884
876- self .assertRaises (WTimeoutError , self .c .pymongo_test .test .insert ,
885+ self .assertRaises (WTimeoutError , self .c .pymongo_test .test .insert_one ,
877886 {'_id' : 0 })
878- self .assertRaises (WTimeoutError , self .c .pymongo_test .test .save ,
879- {'_id' : 0 , "a" : 5 })
880- self .assertRaises (WTimeoutError , self .c .pymongo_test .test .update ,
887+ self .assertRaises (WTimeoutError , self .c .pymongo_test .test .update_one ,
881888 {'_id' : 0 }, {"$set" : {"a" : 10 }})
882- self .assertRaises (WTimeoutError , self .c .pymongo_test .test .remove ,
889+ self .assertRaises (WTimeoutError , self .c .pymongo_test .test .delete_one ,
883890 {'_id' : 0 })
884891
885892
@@ -893,8 +900,10 @@ def setUp(self):
893900
894901 def test_ship_of_theseus (self ):
895902 c = MongoClient (self .seed , replicaSet = self .name )
896- db = c .pymongo_test
897- db .test .insert ({}, w = len (c .secondaries ) + 1 )
903+ db = c .get_database (
904+ "pymongo_test" ,
905+ write_concern = WriteConcern (w = len (c .secondaries ) + 1 ))
906+ db .test .insert_one ({})
898907 find_one = db .test .find_one
899908
900909 primary = ha_tools .get_primary ()
@@ -985,9 +994,10 @@ def test_last_error(self):
985994 wait_until (lambda : c .primary , "discover primary" )
986995 wait_until (lambda : c .secondaries , "discover secondary" )
987996 ha_tools .stepdown_primary ()
988- db = c .pymongo_test
997+ db = c .get_database (
998+ "pymongo_test" , write_concern = WriteConcern (w = 0 ))
989999
990- db .test .insert ({}, w = 0 )
1000+ db .test .insert_one ({})
9911001 response = db .error ()
9921002 self .assertTrue ('err' in response and 'not master' in response ['err' ])
9931003 wait_until (lambda : len (c .secondaries ) == 2 , "discover two secondaries" )
0 commit comments