def test_max_time_ms_getmore(self): # Cursor handles server timeout during getmore, also. yield self.collection.insert({} for _ in range(200)) try: # Send initial query. cursor = self.collection.find().max_time_ms(1) yield cursor.fetch_next cursor.next_object() # Test getmore timeout. yield self.enable_timeout() with assert_raises(ExecutionTimeout): while (yield cursor.fetch_next): cursor.next_object() # Send another initial query. yield self.disable_timeout() cursor = self.collection.find().max_time_ms(1) yield cursor.fetch_next cursor.next_object() # Test getmore timeout. yield self.enable_timeout() with assert_raises(ExecutionTimeout): yield cursor.to_list(None) # Avoid 'IOLoop is closing' warning. yield cursor.close() finally: # Cleanup. yield self.disable_timeout() yield self.collection.remove()
def test_strict(self): t = Template(""" % if x is UNDEFINED: undefined % else: x: ${x} % endif """, strict_undefined=True) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises( NameError, t.render, y=12 ) l = TemplateLookup(strict_undefined=True) l.put_string("a", "some template") l.put_string("b", """ <%namespace name='a' file='a' import='*'/> % if x is UNDEFINED: undefined % else: x: ${x} % endif """) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises( NameError, t.render, y=12 )
def test_def_operations(self): """test get/list/has def""" template = Template(""" this is the body <%def name="a()"> this is a </%def> <%def name="b(x, y)"> this is b, ${x} ${y} </%def> """) assert template.get_def("a") assert template.get_def("b") assert_raises(AttributeError, template.get_def, ("c")) assert template.has_def("a") assert template.has_def("b") assert not template.has_def("c") defs = template.list_defs() assert "a" in defs assert "b" in defs assert "body" in defs assert "c" not in defs
def test_alt_collection(self): db = self.db alt = motor.MotorGridFS(db, 'alt') oid = yield alt.put(b"hello world") gridout = yield alt.get(oid) self.assertEqual(b"hello world", (yield gridout.read())) self.assertEqual(1, (yield self.db.alt.files.count())) self.assertEqual(1, (yield self.db.alt.chunks.count())) yield alt.delete(oid) with assert_raises(NoFile): yield alt.get(oid) self.assertEqual(0, (yield self.db.alt.files.count())) self.assertEqual(0, (yield self.db.alt.chunks.count())) with assert_raises(NoFile): yield alt.get("foo") oid = yield alt.put(b"hello world", _id="foo") self.assertEqual("foo", oid) gridout = yield alt.get("foo") self.assertEqual(b"hello world", (yield gridout.read())) yield alt.put(b"", filename="mike") yield alt.put(b"foo", filename="test") yield alt.put(b"", filename="hello world") self.assertEqual(set(["mike", "test", "hello world"]), set((yield alt.list())))
def test_def_operations(self): """test get/list/has def""" template = Template(""" this is the body <%def name="a()"> this is a </%def> <%def name="b(x, y)"> this is b, ${x} ${y} </%def> """) assert template.get_def("a") assert template.get_def("b") assert_raises(AttributeError, template.get_def, ("c") ) assert template.has_def("a") assert template.has_def("b") assert not template.has_def("c") defs = template.list_defs() assert "a" in defs assert "b" in defs assert "body" in defs assert "c" not in defs
def test_recovering_member_triggers_refresh(self): # To test that find_one() and count() trigger immediate refreshes, # we'll create a separate client for each self.c_find_one, self.c_count = yield [ motor.MotorReplicaSetClient(self.seed, replicaSet=self.name, read_preference=SECONDARY).open() for _ in range(2) ] # We've started the primary and one secondary primary = ha_tools.get_primary() secondary = ha_tools.get_secondaries()[0] # Pre-condition: just make sure they all connected OK for c in self.c_find_one, self.c_count: self.assertEqual(one(c.secondaries), _partition_node(secondary)) ha_tools.set_maintenance(secondary, True) # Trigger a refresh in various ways with assert_raises(AutoReconnect): yield self.c_find_one.test.test.find_one() with assert_raises(AutoReconnect): yield self.c_count.test.test.count() # Wait for the immediate refresh to complete - we're not waiting for # the periodic refresh, which has been disabled yield self.pause(1) for c in self.c_find_one, self.c_count: self.assertFalse(c.secondaries) self.assertEqual(_partition_node(primary), c.primary)
def test_max_pool_size_validation(self): cx = motor.MotorClient(host=host, port=port, max_pool_size=-1, io_loop=self.io_loop) with assert_raises(ConfigurationError): yield cx.open() cx = motor.MotorClient(host=host, port=port, max_pool_size='foo', io_loop=self.io_loop) with assert_raises(ConfigurationError): yield cx.open() cx = motor.MotorClient(host=host, port=port, max_pool_size=100, io_loop=self.io_loop) yield cx.open() self.assertEqual(cx.max_pool_size, 100) cx.close()
def test_copy_db_auth(self): # See SERVER-6427. cx = self.get_client() if cx.is_mongos: raise SkipTest("Can't copy database with auth via mongos.") target_db_name = "motor_test_2" collection = cx.motor_test.test_collection yield collection.remove() yield collection.insert({"_id": 1}) yield cx.admin.add_user("admin", "password") yield cx.admin.authenticate("admin", "password") try: yield cx.motor_test.add_user("mike", "password") with assert_raises(pymongo.errors.OperationFailure): yield cx.copy_database("motor_test", target_db_name, username="******", password="******") with assert_raises(pymongo.errors.OperationFailure): yield cx.copy_database("motor_test", target_db_name, username="******", password="******") # Copy a database using name and password. yield cx.copy_database("motor_test", target_db_name, username="******", password="******") self.assertEqual({"_id": 1}, (yield cx[target_db_name].test_collection.find_one())) yield cx.drop_database(target_db_name) finally: yield remove_all_users(cx.motor_test) yield cx.admin.remove_user("admin")
def test_recovering_member_triggers_refresh(self): # To test that find_one() and count() trigger immediate refreshes, # we'll create a separate client for each self.c_find_one, self.c_count = yield [ motor.MotorReplicaSetClient( self.seed, replicaSet=self.name, read_preference=SECONDARY ).open() for _ in xrange(2)] # We've started the primary and one secondary primary = ha_tools.get_primary() secondary = ha_tools.get_secondaries()[0] # Pre-condition: just make sure they all connected OK for c in self.c_find_one, self.c_count: self.assertEqual(one(c.secondaries), _partition_node(secondary)) ha_tools.set_maintenance(secondary, True) # Trigger a refresh in various ways with assert_raises(AutoReconnect): yield self.c_find_one.test.test.find_one() with assert_raises(AutoReconnect): yield self.c_count.test.test.count() # Wait for the immediate refresh to complete - we're not waiting for # the periodic refresh, which has been disabled yield self.pause(1) for c in self.c_find_one, self.c_count: self.assertFalse(c.secondaries) self.assertEqual(_partition_node(primary), c.primary)
def test_max_time_ms_getmore(self): # Cursor handles server timeout during getmore, also. yield self.collection.insert({} for _ in range(200)) try: # Send initial query. cursor = self.collection.find().max_time_ms(100000) yield cursor.fetch_next cursor.next_object() # Test getmore timeout. yield self.enable_timeout() with assert_raises(ExecutionTimeout): while (yield cursor.fetch_next): cursor.next_object() yield cursor.close() # Send another initial query. yield self.disable_timeout() cursor = self.collection.find().max_time_ms(100000) yield cursor.fetch_next cursor.next_object() # Test getmore timeout. yield self.enable_timeout() with assert_raises(ExecutionTimeout): yield cursor.to_list(None) # Avoid 'IOLoop is closing' warning. yield cursor.close() finally: # Cleanup. yield self.disable_timeout() yield self.collection.remove()
def test_copy_db_auth(self): # SERVER-6427, can't copy database via mongos with auth. yield skip_if_mongos(self.cx) yield self.collection.insert({'_id': 1}) try: # self.cx is logged in as root. yield self.cx.motor_test.add_user('mike', 'password') client = self.get_client() target_db_name = 'motor_test_2' with assert_raises(pymongo.errors.OperationFailure): yield client.copy_database( 'motor_test', target_db_name, username='******', password='******') with assert_raises(pymongo.errors.OperationFailure): yield client.copy_database( 'motor_test', target_db_name, username='******', password='******') # Copy a database using name and password. yield client.copy_database( 'motor_test', target_db_name, username='******', password='******') self.assertEqual( {'_id': 1}, (yield client[target_db_name].test_collection.find_one())) yield client.drop_database(target_db_name) finally: yield remove_all_users(self.cx.motor_test)
def test_alt_collection(self): db = self.cx.pymongo_test alt = yield motor.MotorGridFS(db, 'alt').open() oid = yield alt.put(b("hello world")) gridout = yield alt.get(oid) self.assertEqual(b("hello world"), (yield gridout.read())) self.assertEqual(1, (yield db.alt.files.count())) self.assertEqual(1, (yield db.alt.chunks.count())) yield alt.delete(oid) with assert_raises(NoFile): yield alt.get(oid) self.assertEqual(0, (yield db.alt.files.count())) self.assertEqual(0, (yield db.alt.chunks.count())) with assert_raises(NoFile): yield alt.get("foo") oid = yield alt.put(b("hello world"), _id="foo") self.assertEqual("foo", oid) gridout = yield alt.get("foo") self.assertEqual(b("hello world"), (yield gridout.read())) yield alt.put(b(""), filename="mike") yield alt.put(b("foo"), filename="test") yield alt.put(b(""), filename="hello world") self.assertEqual(set(["mike", "test", "hello world"]), set((yield alt.list())))
def test_raise(self): template = Template(""" <% raise Exception("this is a test") %> """, format_errors=False) assert_raises(Exception, template.render)
def test_copy_db(self): # 1. Drop old test DBs # 2. Copy a test DB N times at once (we need to do it many times at # once to make sure the pool's start_request() is properly isolating # operations from each other) # 3. Create a username and password # 4. Copy a database using name and password ncopies = 10 test_db_names = ['pymongo_test%s' % i for i in range(ncopies)] def check_copydb_results(): db_names = self.sync_cx.database_names() for test_db_name in test_db_names: self.assertTrue(test_db_name in db_names) result = self.sync_cx[test_db_name].test_collection.find_one() self.assertTrue(result, "No results in %s" % test_db_name) self.assertEqual( "bar", result.get("foo"), "Wrong result from %s: %s" % (test_db_name, result)) # 1. Drop old test DBs yield self.cx.drop_database('pymongo_test') self.drop_databases(test_db_names) # 2. Copy a test DB N times at once yield self.cx.pymongo_test.test_collection.insert({"foo": "bar"}) yield [ self.cx.copy_database("pymongo_test", test_db_name) for test_db_name in test_db_names] check_copydb_results() self.drop_databases(test_db_names) # 3. Create a username and password yield self.cx.pymongo_test.add_user("mike", "password") with assert_raises(pymongo.errors.OperationFailure): yield self.cx.copy_database( "pymongo_test", "pymongo_test0", username="******", password="******") with assert_raises(pymongo.errors.OperationFailure): yield self.cx.copy_database( "pymongo_test", "pymongo_test0", username="******", password="******") # 4. Copy a database using name and password if not self.cx.is_mongos: # See SERVER-6427 yield [ self.cx.copy_database( "pymongo_test", test_db_name, username="******", password="******") for test_db_name in test_db_names] check_copydb_results() self.drop_databases(test_db_names)
def test_max_concurrent_validation(self): with assert_raises(ConfigurationError): yield self.motor_client(max_concurrent=-1) with assert_raises(TypeError): yield self.motor_client(max_concurrent=None) with assert_raises(TypeError): yield self.motor_client(max_concurrent=1.5)
def test_copy_db_argument_checking(self): with assert_raises(TypeError): yield self.cx.copy_database(4, "foo") with assert_raises(TypeError): yield self.cx.copy_database("foo", 4) with assert_raises(pymongo.errors.InvalidName): yield self.cx.copy_database("foo", "$foo")
def test_traditional_assignment_plus_undeclared(self): t = Template(""" t is: ${t} <% t = 12 %> """) assert_raises(UnboundLocalError, t.render, t="T")
def test_max_pool_size_validation(self): with assert_raises(ConfigurationError): motor.MotorClient(max_pool_size=-1) with assert_raises(ConfigurationError): motor.MotorClient(max_pool_size='foo') cx = self.motor_client(max_pool_size=100) self.assertEqual(cx.max_pool_size, 100) cx.close()
def test_copy_db_argument_checking(self): cx = self.get_client() with assert_raises(TypeError): yield cx.copy_database(4, 'foo') with assert_raises(TypeError): yield cx.copy_database('foo', 4) with assert_raises(pymongo.errors.InvalidName): yield cx.copy_database('foo', '$foo')
def test_basic(self): template = Template(""" <%page args="x, y, z=7"/> this is page, ${x}, ${y}, ${z} """) assert flatten_result(template.render(x=5, y=10)) == "this is page, 5, 10, 7" assert flatten_result(template.render(x=5, y=10, z=32)) == "this is page, 5, 10, 32" assert_raises(TypeError, template.render, y=10)
def test_raise(self): template = Template(""" <% raise Exception("this is a test") %> """, format_errors=False) assert_raises( Exception, template.render )
def test_max_pool_size_validation(self): with assert_raises(ConfigurationError): motor.MotorClient(max_pool_size=-1) with assert_raises(ConfigurationError): motor.MotorClient(max_pool_size="foo") cx = self.motor_client(max_pool_size=100) self.assertEqual(cx.max_pool_size, 100) cx.close()
def test_max_pool_size_validation(self): with assert_raises(ConfigurationError): motor.MotorClient(host=host, port=port, max_pool_size=-1) with assert_raises(ConfigurationError): motor.MotorClient(host=host, port=port, max_pool_size="foo") cx = motor.MotorClient(host=host, port=port, max_pool_size=100, io_loop=self.io_loop) self.assertEqual(cx.max_pool_size, 100) cx.close()
def test_traditional_assignment_plus_undeclared(self): t = Template( """ t is: ${t} <% t = 12 %> """ ) assert_raises(UnboundLocalError, t.render, t="T")
def test_to_list_argument_checking(self): # We need more than 10 documents so the cursor stays alive. yield self.make_test_data() coll = self.collection cursor = coll.find() yield self.check_optional_callback(cursor.to_list, 10) cursor = coll.find() with assert_raises(ValueError): yield cursor.to_list(-1) with assert_raises(TypeError): yield cursor.to_list('foo')
def test_max_time_ms_query(self): # Cursor parses server timeout error in response to initial query. yield self.enable_timeout() cursor = self.collection.find().max_time_ms(1) with assert_raises(ExecutionTimeout): yield cursor.fetch_next cursor = self.collection.find().max_time_ms(1) with assert_raises(ExecutionTimeout): yield cursor.to_list(10) with assert_raises(ExecutionTimeout): yield self.collection.find_one(max_time_ms=1)
def test_max_time_ms_query(self): # Cursor parses server timeout error in response to initial query. yield self.enable_timeout() cursor = self.collection.find().max_time_ms(100000) with assert_raises(ExecutionTimeout): yield cursor.fetch_next cursor = self.collection.find().max_time_ms(100000) with assert_raises(ExecutionTimeout): yield cursor.to_list(10) with assert_raises(ExecutionTimeout): yield self.collection.find_one(max_time_ms=100000)
def test_max_wait_validation(self): with assert_raises(ConfigurationError): yield self.motor_client(max_wait_time=-1) with assert_raises(ConfigurationError): yield self.motor_client(max_wait_time=0) with assert_raises(ConfigurationError): yield self.motor_client(max_wait_time='foo') # Ok self.motor_client_sync(max_wait_time=None) self.motor_client_sync(max_wait_time=100)
def test_validate_collection(self): db = self.cx.pymongo_test with assert_raises(TypeError): yield db.validate_collection(5) with assert_raises(TypeError): yield db.validate_collection(None) with assert_raises(OperationFailure): yield db.validate_collection("test.doesnotexist") with assert_raises(OperationFailure): yield db.validate_collection(db.test.doesnotexist) yield db.test.save({"dummy": u"object"}) self.assertTrue((yield db.validate_collection("test"))) self.assertTrue((yield db.validate_collection(db.test)))
def test_validate_collection(self): db = self.db with assert_raises(TypeError): yield db.validate_collection(5) with assert_raises(TypeError): yield db.validate_collection(None) with assert_raises(OperationFailure): yield db.validate_collection("test.doesnotexist") with assert_raises(OperationFailure): yield db.validate_collection(db.test.doesnotexist) yield db.test.save({"dummy": "object"}) self.assertTrue((yield db.validate_collection("test"))) self.assertTrue((yield db.validate_collection(db.test)))
def test_to_list_tailable(self): coll = self.collection cursor = coll.find(tailable=True) # Can't call to_list on tailable cursor. with assert_raises(InvalidOperation): yield cursor.to_list(10)
def test_grid_out_default_opts(self): self.assertRaises(TypeError, motor.MotorGridOut, "foo") db = self.cx.pymongo_test gout = motor.MotorGridOut(db.fs, 5) with assert_raises(NoFile): yield gout.open() a = yield motor.MotorGridIn(db.fs).open() yield a.close() b = yield motor.MotorGridOut(db.fs, a._id).open() self.assertEqual(a._id, b._id) self.assertEqual(0, b.length) self.assertEqual(None, b.content_type) self.assertEqual(256 * 1024, b.chunk_size) self.assertTrue(isinstance(b.upload_date, datetime.datetime)) self.assertEqual(None, b.aliases) self.assertEqual(None, b.metadata) self.assertEqual("d41d8cd98f00b204e9800998ecf8427e", b.md5) for attr in [ "_id", "name", "content_type", "length", "chunk_size", "upload_date", "aliases", "metadata", "md5" ]: self.assertRaises(AttributeError, setattr, b, attr, 5)
def test_auth_from_uri(self): if not test.env.auth: raise SkipTest('Authentication is not enabled on server') # self.db is logged in as root. yield remove_all_users(self.db) db = self.db try: yield db.add_user('mike', 'password', roles=['userAdmin', 'readWrite']) client = motor.MotorClient('mongodb://*****:*****@%s:%d' % (host, port), io_loop=self.io_loop) # Note: open() only calls ismaster, doesn't throw auth errors. yield client.open() with assert_raises(OperationFailure): yield client.db.collection.find_one() client = motor.MotorClient('mongodb://*****:*****@%s:%d/%s' % (host, port, db.name), io_loop=self.io_loop) yield client[db.name].collection.find_one() finally: yield db.remove_user('mike')
def test_gridfs_secondary(self): primary_host, primary_port = test.env.primary primary = self.motor_client(primary_host, primary_port) if test.env.auth: yield primary.admin.authenticate(test.db_user, test.db_password) secondary_host, secondary_port = test.env.secondaries[0] secondary = self.motor_client(secondary_host, secondary_port, read_preference=ReadPreference.SECONDARY) if test.env.auth: yield secondary.admin.authenticate(db_user, db_password) yield primary.motor_test.drop_collection("fs.files") yield primary.motor_test.drop_collection("fs.chunks") # Should detect it's connected to secondary and not attempt to # create index fs = motor.MotorGridFS(secondary.motor_test) # This won't detect secondary, raises error with assert_raises(AutoReconnect): yield fs.put(b'foo')
def test_wait_queue_timeout(self): # Do a find_one that takes 1 second, and set waitQueueTimeoutMS to 500, # 5000, and None. Verify timeout iff max_wait_time < 1 sec. where_delay = 1 yield from self.collection.insert({}) for waitQueueTimeoutMS in (500, 5000, None): cx = self.asyncio_client( max_pool_size=1, waitQueueTimeoutMS=waitQueueTimeoutMS) yield from cx.open() pool = cx._get_primary_pool() if waitQueueTimeoutMS: self.assertEqual( waitQueueTimeoutMS, pool.wait_queue_timeout * 1000) else: self.assertTrue(pool.wait_queue_timeout is None) collection = cx.motor_test.test_collection future = collection.find_one({'$where': delay(where_delay)}) if waitQueueTimeoutMS and waitQueueTimeoutMS < where_delay * 1000: with assert_raises(pymongo.errors.ConnectionFailure): yield from collection.find_one() else: # No error yield from collection.find_one() yield from future cx.close()
def test_stepdown_triggers_refresh(self): c_find_one = yield motor.MotorReplicaSetClient( self.seed, replicaSet=self.name).open() # We've started the primary and one secondary primary = ha_tools.get_primary() secondary = ha_tools.get_secondaries()[0] self.assertEqual( one(c_find_one.secondaries), _partition_node(secondary)) ha_tools.stepdown_primary() # Make sure the stepdown completes yield self.pause(1) # Trigger a refresh with assert_raises(AutoReconnect): yield c_find_one.test.test.find_one() # Wait for the immediate refresh to complete - we're not waiting for # the periodic refresh, which has been disabled yield self.pause(1) # We've detected the stepdown self.assertTrue( not c_find_one.primary or primary != _partition_node(c_find_one.primary))
def test_op(self): # motor.Op is deprecated in Motor 0.2, superseded by Tornado 3 Futures. # Just make sure it still works. collection = self.collection doc = {'_id': 'jesse'} with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # Op works. _id = yield motor.Op(collection.insert, doc) self.assertEqual('jesse', _id) # Raised a DeprecationWarning. self.assertEqual(1, len(w)) warning = w[-1] self.assertTrue(issubclass(warning.category, DeprecationWarning)) message = str(warning.message) self.assertTrue("deprecated" in message) self.assertTrue("insert" in message) result = yield motor.Op(collection.find_one, doc) self.assertEqual(doc, result) # Make sure it works with no args. result = yield motor.Op(collection.find_one) self.assertTrue(isinstance(result, dict)) with assert_raises(pymongo.errors.DuplicateKeyError): yield motor.Op(collection.insert, doc)
def test_max_time_ms_each_getmore(self): # Cursor.each() handles server timeout during getmore. yield self.collection.insert({} for _ in range(200)) try: # Send initial query. cursor = self.collection.find().max_time_ms(100000) yield cursor.fetch_next cursor.next_object() future = Future() def callback(result, error): if error: future.set_exception(error) elif not result: # Done. future.set_result(None) yield self.enable_timeout() with assert_raises(ExecutionTimeout): cursor.each(callback) yield future yield cursor.close() finally: # Cleanup. yield self.disable_timeout() yield self.collection.remove()
def test_create_collection(self): # Test creating collection, return val is wrapped in MotorCollection, # creating it again raises CollectionInvalid. db = self.db yield db.drop_collection('test_collection2') collection = yield db.create_collection('test_collection2') self.assertTrue(isinstance(collection, motor.MotorCollection)) self.assertTrue('test_collection2' in (yield db.collection_names())) with assert_raises(CollectionInvalid): yield db.create_collection('test_collection2') yield db.drop_collection('test_collection2') # Test creating capped collection collection = yield db.create_collection('test_capped', capped=True, size=4096) self.assertTrue(isinstance(collection, motor.MotorCollection)) self.assertEqual({ "capped": True, 'size': 4096 }, (yield db.test_capped.options())) yield db.drop_collection('test_capped')
def test_stepdown_triggers_refresh(self): c_find_one = yield motor.MotorReplicaSetClient( self.seed, replicaSet=self.name).open() # We've started the primary and one secondary primary = ha_tools.get_primary() secondary = ha_tools.get_secondaries()[0] self.assertEqual(one(c_find_one.secondaries), _partition_node(secondary)) ha_tools.stepdown_primary() # Make sure the stepdown completes yield self.pause(1) # Trigger a refresh with assert_raises(AutoReconnect): yield c_find_one.test.test.find_one() # Wait for the immediate refresh to complete - we're not waiting for # the periodic refresh, which has been disabled yield self.pause(1) # We've detected the stepdown self.assertTrue(not c_find_one.primary or primary != c_find_one.primary)
def test_auth_during_failover(self): c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) yield c.open() db = c.pymongo_ha_auth res = yield db.authenticate('user', 'userpass') self.assertTrue(res) yield db.foo.insert({'foo': 'bar'}, w=3, wtimeout=30000) yield db.logout() with assert_raises(OperationFailure): yield db.foo.find_one() primary = '%s:%d' % self.c.primary ha_tools.kill_members([primary], 2) # Let monitor notice primary's gone yield self.pause(2 * MONITOR_INTERVAL) # Make sure we can still authenticate res = yield db.authenticate('user', 'userpass') self.assertTrue(res) # And still query. db.read_preference = PRIMARY_PREFERRED res = yield db.foo.find_one() self.assertEqual('bar', res['foo']) c.close()
def test_auth_during_failover(self): c = motor.MotorReplicaSetClient(self.seed, replicaSet=self.name) yield c.open() db = c.pymongo_ha_auth res = yield db.authenticate('user', 'userpass') self.assertTrue(res) yield db.foo.insert({'foo': 'bar'}, w=3, wtimeout=1000) yield db.logout() with assert_raises(OperationFailure): yield db.foo.find_one() primary = '%s:%d' % self.c.primary ha_tools.kill_members([primary], 2) # Let monitor notice primary's gone yield self.pause(2 * MONITOR_INTERVAL) # Make sure we can still authenticate res = yield db.authenticate('user', 'userpass') self.assertTrue(res) # And still query. db.read_preference = PRIMARY_PREFERRED res = yield db.foo.find_one() self.assertEqual('bar', res['foo']) c.close()
def test_max_time_ms_each_getmore(self): # Cursor.each() handles server timeout during getmore. yield self.collection.insert({} for _ in range(200)) try: # Send initial query. cursor = self.collection.find().max_time_ms(1) yield cursor.fetch_next cursor.next_object() future = Future() def callback(result, error): if error: future.set_exception(error) elif not result: # Done. future.set_result(None) yield self.enable_timeout() with assert_raises(ExecutionTimeout): cursor.each(callback) yield future finally: # Cleanup. yield self.disable_timeout() yield self.collection.remove()
def test_connect(self): with assert_raises(pymongo.errors.ConnectionFailure): yield motor.MotorReplicaSetClient( '%s:%s' % (host, port), replicaSet='anything', io_loop=self.io_loop, connectTimeoutMS=600).test.test.find_one()
def test_unbound_scope(self): t = Template(""" <% y = 10 %> <%def name="a()"> y is: ${y} <% # should raise error ? y = 15 %> y is ${y} </%def> ${a()} """) assert_raises(UnboundLocalError, t.render)
def test_gridfs_secondary(self): primary_host, primary_port = test.env.primary primary = self.motor_client(primary_host, primary_port) if test.env.auth: yield primary.admin.authenticate(test.db_user, test.db_password) secondary_host, secondary_port = test.env.secondaries[0] secondary = self.motor_client( secondary_host, secondary_port, read_preference=ReadPreference.SECONDARY) if test.env.auth: yield secondary.admin.authenticate(db_user, db_password) yield primary.motor_test.drop_collection("fs.files") yield primary.motor_test.drop_collection("fs.chunks") # Should detect it's connected to secondary and not attempt to # create index fs = motor.MotorGridFS(secondary.motor_test) # This won't detect secondary, raises error with assert_raises(AutoReconnect): yield fs.put(b'foo')
def test_put_unacknowledged(self): client = self.motor_client(w=0) fs = motor.MotorGridFS(client.motor_test) with assert_raises(ConfigurationError): yield fs.put(b"hello") client.close()
def test_unix_socket(self): if not hasattr(socket, "AF_UNIX"): raise SkipTest("UNIX-sockets are not supported on this system") if (sys.platform == 'darwin' and server_started_with_auth(self.sync_cx)): raise SkipTest("SERVER-8492") mongodb_socket = '/tmp/mongodb-27017.sock' if not os.access(mongodb_socket, os.R_OK): raise SkipTest("Socket file is not accessible") yield motor.MotorClient( "mongodb://%s" % mongodb_socket, io_loop=self.io_loop).open() client = yield motor.MotorClient( "mongodb://%s" % mongodb_socket, io_loop=self.io_loop).open() yield client.pymongo_test.test.save({"dummy": "object"}) # Confirm we can read via the socket dbs = yield client.database_names() self.assertTrue("pymongo_test" in dbs) client.close() # Confirm it fails with a missing socket client = motor.MotorClient( "mongodb:///tmp/non-existent.sock", io_loop=self.io_loop) with assert_raises(ConnectionFailure): yield client.open()
def test_unix_socket(self): if not hasattr(socket, "AF_UNIX"): raise SkipTest("UNIX-sockets are not supported on this system") if (sys.platform == 'darwin' and server_started_with_auth(self.sync_cx)): raise SkipTest("SERVER-8492") mongodb_socket = '/tmp/mongodb-27017.sock' if not os.access(mongodb_socket, os.R_OK): raise SkipTest("Socket file is not accessible") yield motor.MotorClient("mongodb://%s" % mongodb_socket, io_loop=self.io_loop).open() client = yield motor.MotorClient("mongodb://%s" % mongodb_socket, io_loop=self.io_loop).open() yield client.pymongo_test.test.save({"dummy": "object"}) # Confirm we can read via the socket. dbs = yield client.database_names() self.assertTrue("pymongo_test" in dbs) client.close() # Confirm it fails with a missing socket. client = motor.MotorClient("mongodb:///tmp/non-existent.sock", io_loop=self.io_loop) with assert_raises(ConnectionFailure): yield client.open()
def test_grid_out_default_opts(self): self.assertRaises(TypeError, motor.MotorGridOut, "foo") db = self.cx.pymongo_test gout = motor.MotorGridOut(db.fs, 5) with assert_raises(NoFile): yield gout.open() a = yield motor.MotorGridIn(db.fs).open() yield a.close() b = yield motor.MotorGridOut(db.fs, a._id).open() self.assertEqual(a._id, b._id) self.assertEqual(0, b.length) self.assertEqual(None, b.content_type) self.assertEqual(256 * 1024, b.chunk_size) self.assertTrue(isinstance(b.upload_date, datetime.datetime)) self.assertEqual(None, b.aliases) self.assertEqual(None, b.metadata) self.assertEqual("d41d8cd98f00b204e9800998ecf8427e", b.md5) for attr in ["_id", "name", "content_type", "length", "chunk_size", "upload_date", "aliases", "metadata", "md5"]: self.assertRaises(AttributeError, setattr, b, attr, 5)
def test_wait_queue_timeout(self): # Do a find_one that takes 1 second, and set waitQueueTimeoutMS to 500, # 5000, and None. Verify timeout iff max_wait_time < 1 sec. where_delay = 1 for waitQueueTimeoutMS in [500]: #500, 5000, None: cx = yield self.motor_client(max_pool_size=1, waitQueueTimeoutMS=waitQueueTimeoutMS) pool = cx._get_pools()[0] if waitQueueTimeoutMS: self.assertEqual(waitQueueTimeoutMS, pool.wait_queue_timeout * 1000) else: self.assertTrue(pool.wait_queue_timeout is None) collection = cx.pymongo_test.test_collection cb = yield gen.Callback('find_one') collection.find_one({'$where': delay(where_delay)}, callback=cb) if waitQueueTimeoutMS and waitQueueTimeoutMS < where_delay * 1000: with assert_raises(pymongo.errors.ConnectionFailure): yield collection.find_one() else: # No error yield collection.find_one() yield gen.Wait('find_one') cx.close()
def test_op(self): # motor.Op is deprecated in Motor 0.2, superseded by Tornado 3 Futures. # Just make sure it still works. collection = self.cx.pymongo_test.test_collection doc = {'_id': 'jesse'} with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # Op works. _id = yield motor.Op(collection.insert, doc) self.assertEqual('jesse', _id) # Raised a DeprecationWarning. self.assertEqual(1, len(w)) warning = w[-1] self.assertTrue(issubclass(warning.category, DeprecationWarning)) message = str(warning.message) self.assertTrue("deprecated" in message) self.assertTrue("insert" in message) result = yield motor.Op(collection.find_one, doc) self.assertEqual(doc, result) # Make sure it works with no args. result = yield motor.Op(collection.find_one) self.assertTrue(isinstance(result, dict)) with assert_raises(pymongo.errors.DuplicateKeyError): yield motor.Op(collection.insert, doc)
def test_put_unacknowledged(self): client = yield self.motor_client(w=0) fs = yield motor.MotorGridFS(client.pymongo_test).open() with assert_raises(ConfigurationError): yield fs.put(b("hello")) client.close()