def test_autoresponds_case_insensitive(self): server = MockupDB() # Little M. Note this is only case-insensitive because it's a Command. server.autoresponds(Command('ismaster'), foo='bar') server.run() response = MongoClient(server.uri).admin.command('isMaster') # Big M. self.assertEqual('bar', response['foo'])
def test_list_indexes_command(self): server = MockupDB(auto_ismaster={'maxWireVersion': 6}) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) self.addCleanup(client.close) with going(client.test.collection.list_indexes) as cursor: request = server.receives( listIndexes='collection', namespace='test') request.reply({'cursor': { 'firstBatch': [{'name': 'index_0'}], 'id': 123}}) with going(list, cursor()) as indexes: request = server.receives(getMore=123, namespace='test', collection='collection') request.reply({'cursor': { 'nextBatch': [{'name': 'index_1'}], 'id': 0}}) self.assertEqual([{'name': 'index_0'}, {'name': 'index_1'}], indexes()) for index_info in indexes(): self.assertIsInstance(index_info, SON)
def test_ssl_uri(self): server = MockupDB(ssl=True) server.run() self.addCleanup(server.stop) self.assertEqual( 'mongodb://localhost:%d/?ssl=true' % server.port, server.uri)
class TestObjectCreation(unittest.TestCase): def setUp(self): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['DEBUG'] = False self.app = app.test_client() app.config['MONGO_URI'] = environ.get('MONGO_URI') self.app = app.test_client() self.server = MockupDB(auto_ismaster=True, verbose=True) self.server.run() def tearDown(self): pass #Check the valid values in Gender variable def test_valid_gender_values(self): stu_col1=stu_col.find({}) for i in stu_col1: self.assertIn(i['Gender'],'M|F') #Check the valid values in Predicted grade variable def test_valid_predicted_class_values(self): stu_col1=stu_col.find({}) for i in stu_col1: self.assertIn(i['Class'],'L|M|H') #Check the functioning of Home Page def test_home_page(self): response=self.app.get('/index') self.assertEqual(response.status_code,200) #Check the functioning of "All Students Page def test_view(self): response=self.app.get('/list') self.assertEqual(response.status_code,200)
def cluster_time_conversation(self, callback, replies): cluster_time = Timestamp(0, 0) server = MockupDB() # First test all commands include $clusterTime with wire version 6. responder = server.autoresponds( 'ismaster', {'minWireVersion': 0, 'maxWireVersion': 6, '$clusterTime': {'clusterTime': cluster_time}}) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) self.addCleanup(client.close) with going(callback, client): for reply in replies: request = server.receives() self.assertIn('$clusterTime', request) self.assertEqual(request['$clusterTime']['clusterTime'], cluster_time) cluster_time = Timestamp(cluster_time.time, cluster_time.inc + 1) reply['$clusterTime'] = {'clusterTime': cluster_time} request.reply(reply)
def test_iteration(self): server = MockupDB(auto_ismaster={'maxWireVersion': 3}) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) def send_three_docs(): for i in range(3): client.test.test.insert({'_id': i}) with going(send_three_docs): j = 0 # The "for request in server" statement is the point of this test. for request in server: self.assertTrue( request.matches({ 'insert': 'test', 'documents': [{ '_id': j }] })) request.ok() j += 1 if j == 3: break
def test_auth_recovering_member(self): # Test that we don't attempt auth against a recovering RS member. server = MockupDB() server.autoresponds( 'ismaster', { 'minWireVersion': 2, 'maxWireVersion': 6, 'ismaster': False, 'secondary': False, 'setName': 'rs' }) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri, replicaSet='rs', serverSelectionTimeoutMS=100, socketTimeoutMS=100) self.addCleanup(client.close) # Should see there's no primary or secondary and raise selection timeout # error. If it raises AutoReconnect we know it actually tried the # server, and that's wrong. with self.assertRaises(ServerSelectionTimeoutError): client.db.authenticate('user', 'password')
class TestGet(unittest.TestCase): @classmethod def setUp(self): self.server = MockupDB(auto_ismaster=True, verbose=True) self.server.run() app.testing = True app.config['MONGO_URI'] = self.server.uri self.client = app.test_client() @classmethod def tearDown(self): self.server.stop() # os.close(self.db_fd) # os.unlink(app.config['MONGO_URI']) @classmethod def test_getlist(self): document = DOCUMENT future = go(self.client.get, '/api/robot') request = self.server.receives() request.reply(document) http_response = future() data = http_response.get_data(as_text=True) self.assertIn('not_a_robot', data) self.assertIn('kawada-hironx', data)
def test_aggregate(self): server = MockupDB() server.autoresponds('ismaster', ismaster=True, msg='isdbgrid', minWireVersion=2, maxWireVersion=6) self.addCleanup(server.stop) server.run() client = MongoClient(server.uri) self.addCleanup(client.close) collection = client.test.collection with going(collection.aggregate, []): command = server.receives(aggregate='collection', pipeline=[]) self.assertFalse(command.slave_ok, 'SlaveOkay set') command.ok(result=[{}]) secondary_collection = collection.with_options( read_preference=ReadPreference.SECONDARY) with going(secondary_collection.aggregate, []): command = server.receives(OpMsg({"aggregate": "collection", "pipeline": [], '$readPreference': {'mode': 'secondary'}})) command.ok(result=[{}]) self.assertTrue(command.slave_ok, 'SlaveOkay not set')
def test_auto_dequeue(self): server = MockupDB(auto_ismaster=True) server.run() client = MongoClient(server.uri) future = go(client.admin.command, 'ping') server.autoresponds('ping') # Should dequeue the request. future()
def test_rsghost(self): rsother_response = { 'ok': 1.0, 'ismaster': False, 'secondary': False, 'info': 'Does not have a valid replica set config', 'isreplicaset': True, 'maxBsonObjectSize': 16777216, 'maxMessageSizeBytes': 48000000, 'maxWriteBatchSize': 100000, 'localTime': datetime.datetime(2021, 11, 30, 0, 53, 4, 99000), 'logicalSessionTimeoutMinutes': 30, 'connectionId': 3, 'minWireVersion': 0, 'maxWireVersion': 15, 'readOnly': False } server = MockupDB(auto_ismaster=rsother_response) server.run() self.addCleanup(server.stop) # Default auto discovery yields a server selection timeout. with MongoClient(server.uri, serverSelectionTimeoutMS=250) as client: with self.assertRaises(ServerSelectionTimeoutError): client.test.command('ping') # Direct connection succeeds. with MongoClient(server.uri, directConnection=True) as client: with going(client.test.command, 'ping'): request = server.receives(ping=1) request.reply()
def test_aggregate(self): server = MockupDB() server.autoresponds('ismaster', ismaster=True, msg='isdbgrid', minWireVersion=2, maxWireVersion=5) self.addCleanup(server.stop) server.run() client = MongoClient(server.uri) self.addCleanup(client.close) collection = client.test.collection with going(collection.aggregate, []): command = server.receives(aggregate='collection', pipeline=[]) self.assertFalse(command.slave_ok, 'SlaveOkay set') self.assertNotIn('$readPreference', command) command.ok(result=[{}]) secondary_collection = collection.with_options( read_preference=ReadPreference.SECONDARY) with going(secondary_collection.aggregate, []): command = server.receives({ '$query': SON([('aggregate', 'collection'), ('pipeline', []), ('cursor', {})]), '$readPreference': { 'mode': 'secondary' } }) command.ok(result=[{}]) self.assertTrue(command.slave_ok, 'SlaveOkay not set')
def test(self): server = MockupDB() self.addCleanup(server.stop) server.run() server.autoresponds('ismaster', ismaster=True, msg='isdbgrid', minWireVersion=2, maxWireVersion=6) pref = make_read_preference(read_pref_mode_from_name(mode), tag_sets=None) client = MongoClient(server.uri, read_preference=pref) self.addCleanup(client.close) with going(operation.function, client): request = server.receive() request.reply(operation.reply) if operation.op_type == 'always-use-secondary': self.assertEqual(ReadPreference.SECONDARY.document, request.doc.get('$readPreference')) slave_ok = mode != 'primary' elif operation.op_type == 'must-use-primary': slave_ok = False elif operation.op_type == 'may-use-secondary': slave_ok = mode != 'primary' self.assertEqual(pref.document, request.doc.get('$readPreference')) else: self.fail('unrecognized op_type %r' % operation.op_type) if slave_ok: self.assertTrue(request.slave_ok, 'SlaveOkay not set') else: self.assertFalse(request.slave_ok, 'SlaveOkay set')
def test_query_and_read_mode_sharded_op_msg(self): """Test OP_MSG sends non-primary $readPreference and never $query.""" server = MockupDB() server.autoresponds('ismaster', ismaster=True, msg='isdbgrid', minWireVersion=2, maxWireVersion=6) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) self.addCleanup(client.close) read_prefs = ( Primary(), SecondaryPreferred(), PrimaryPreferred(), Secondary(), Nearest(), SecondaryPreferred([{'tag': 'value'}]),) for query in ({'a': 1}, {'$query': {'a': 1}},): for mode in read_prefs: collection = client.db.get_collection('test', read_preference=mode) cursor = collection.find(query.copy()) with going(next, cursor): request = server.receives() # Command is not nested in $query. request.assert_matches(OpMsg( SON([('find', 'test'), ('filter', {'a': 1}), ('$readPreference', mode.document)]))) request.replies({'cursor': {'id': 0, 'firstBatch': [{}]}})
def test_projection(self): q = {} fields = {'foo': True} # OP_QUERY, server = MockupDB(auto_ismaster=True, min_wire_version=0, max_wire_version=3) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) cursor = client.test.collection.find(q, fields) with going(next, cursor): request = server.receives(OpQuery(q, fields=fields)) request.reply([], cursor_id=0) # "find" command. server = MockupDB(auto_ismaster=True, min_wire_version=0, max_wire_version=4) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) cursor = client.test.collection.find(q, fields) cmd = Command( SON([('find', 'collection'), ('filter', q), ('projection', fields)])) with going(next, cursor): request = server.receives(cmd) request.ok(cursor={'id': 0, 'firstBatch': []})
class TestUnacknowledgedWrites(unittest.TestCase): def setUp(self): self.server = MockupDB(auto_ismaster=True) self.server.run() self.addCleanup(self.server.stop) self.client = MongoClient(self.server.uri) self.collection = self.client.db.get_collection( 'collection', write_concern=WriteConcern(w=0)) def test_insert_one(self): with going(self.collection.insert_one, {'_id': 1}): # The moreToCome flag = 2. self.server.receives( OpMsg('insert', 'collection', writeConcern={'w': 0}, flags=2)) def test_insert_many(self): collection = self.collection.with_options( write_concern=WriteConcern(0)) docs = [{'_id': 1}, {'_id': 2}] with going(collection.insert_many, docs, ordered=False): self.server.receives( OpMsg(SON([('insert', 'collection'), ('ordered', False), ('writeConcern', { 'w': 0 })]), flags=2)) def test_replace_one(self): with going(self.collection.replace_one, {}, {}): self.server.receives( OpMsg(SON([('update', 'collection'), ('writeConcern', { 'w': 0 })]), flags=2)) def test_update_many(self): with going(self.collection.update_many, {}, {'$unset': 'a'}): self.server.receives( OpMsg(SON([('update', 'collection'), ('ordered', True), ('writeConcern', { 'w': 0 })]), flags=2)) def test_delete_one(self): with going(self.collection.delete_one, {}): self.server.receives( OpMsg(SON([('delete', 'collection'), ('writeConcern', { 'w': 0 })]), flags=2)) def test_delete_many(self): with going(self.collection.delete_many, {}): self.server.receives( OpMsg(SON([('delete', 'collection'), ('writeConcern', { 'w': 0 })]), flags=2))
def test_list_indexes_command(self): server = MockupDB(auto_ismaster={'maxWireVersion': 3}) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) self.addCleanup(client.close) with going(client.test.collection.list_indexes) as cursor: request = server.receives(listIndexes='collection', namespace='test') request.reply( {'cursor': { 'firstBatch': [{ 'name': 'index_0' }], 'id': 123 }}) with going(list, cursor()) as indexes: request = server.receives(OpGetMore, namespace='test.collection', cursor_id=123) request.reply([{'name': 'index_1'}], cursor_id=0) self.assertEqual([{'name': 'index_0'}, {'name': 'index_1'}], indexes()) self.check_indexes(indexes())
def test_autoresponds_case_insensitive(self): server = MockupDB(auto_ismaster=True) # Little M. Note this is only case-insensitive because it's a Command. server.autoresponds(Command('fooBar'), foo='bar') server.run() response = MongoClient(server.uri).admin.command('Foobar') self.assertEqual('bar', response['foo'])
def test_default_wire_version(self): server = MockupDB(auto_ismaster=True) server.run() self.addCleanup(server.stop) ismaster = MongoClient(server.uri).admin.command('isMaster') self.assertEqual(ismaster['minWireVersion'], 0) self.assertEqual(ismaster['maxWireVersion'], 6)
def client_server(self, *args, **kwargs): server = MockupDB(*args, **kwargs) server.run() self.addCleanup(server.stop) client = motor_asyncio.AsyncIOMotorClient(server.uri, io_loop=self.loop) return client, server
def client_server(self, *args, **kwargs): server = MockupDB(*args, **kwargs) server.run() self.addCleanup(server.stop) client = motor.motor_tornado.MotorClient(server.uri, io_loop=self.io_loop) return client, server
def test_datetime(self): server = MockupDB(auto_ismaster=True) server.run() client = MongoClient(server.uri) # Python datetimes have microsecond precision, BSON only millisecond. # Ensure this datetime matches itself despite the truncation. dt = datetime.datetime(2018, 12, 1, 6, 6, 6, 12345) doc = SON([('_id', 1), ('dt', dt)]) with going(client.db.collection.insert_one, doc): server.receives(OpMsg('insert', 'collection', documents=[doc])).ok()
def test_ok(self): server = MockupDB(auto_ismaster={'maxWireVersion': 3}) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) with going(client.test.command, {'foo': 1}) as future: server.receives().ok(3) response = future() self.assertEqual(3, response['ok'])
def test_query(self): server = MockupDB(auto_ismaster=True) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) cursor = client.test.collection.find() with going(list, cursor) as docs: request = server.receives(OpQuery) request.reply({'a': 1}, cursor_id=123, starting_from=-7) request = server.receives(OpGetMore, cursor_id=123) request.reply({'a': 2}, starting_from=-3, cursor_id=0) self.assertEqual([{'a': 1}, {'a': 2}], docs())
class BaseTestCase(TestCase): def create_app(self): app.config.from_object('project.config.TestingConfig') self.server = MockupDB(auto_ismaster=True) self.server.run() app.config['MONGO_URI'] = self.server.uri mongo.init_app(app) return app def setUp(self): pass def tearDown(self): self.server.stop()
def test_aggregate(self): server = MockupDB(auto_ismaster={'maxWireVersion': 3}) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) with going(client.test.collection.aggregate, []) as cursor: request = server.receives(Command) request.reply({'cursor': { 'id': 123, 'firstBatch': [{'a': 1}]}}) with going(list, cursor()) as docs: request = server.receives(OpGetMore, cursor_id=123) request.reply({'a': 2}, starting_from=-3, cursor_id=0) self.assertEqual([{'a': 1}, {'a': 2}], docs())
class BaseTestCase(unittest.TestCase): server = None @classmethod def setUpClass(self): self.server = MockupDB(auto_ismaster=True, verbose=True) self.server.run() # create mongo connection to mock server app.app.testing = True app.app.config['MONGO_URI'] = self.server.uri self.app = app.app.test_client() @classmethod def tearDownClass(self): self.server.stop() def test_getDataSources(self): # arrange future = go(self.app.get, '/dataSources') request = self.server.receives( Command({ 'find': 'dataSources', 'filter': {} }, flags=4, namespace='app')) request.ok( cursor={ 'id': 0, 'firstBatch': [{ 'name': 'Google', 'url': 'http://google.com/rest/api' }, { 'name': 'Rest', 'url': 'http://rest.com/rest/api' }] }) # act http_response = future() # assert data = http_response.get_data(as_text=True) self.assertIn('http://google.com/rest/api', data) self.assertIn('http://rest.com/rest/api', data)
def test_find_command(self): server = MockupDB(auto_ismaster={'maxWireVersion': 4}) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) with going(list, client.test.collection.find()) as docs: server.receives(Command).reply({'cursor': { 'id': 123, 'firstBatch': [{'a': 1}]}}) request = server.receives(Command("getMore", 123)) request.reply({'cursor': { 'id': 0, 'nextBatch': [{'a': 2}]}}, starting_from=-3) self.assertEqual([{'a': 1}, {'a': 2}], docs())
def client(): server = MockupDB(auto_ismaster=True) server.run() server.autoresponds(OpMsg("find", "todos"), cursor={ "id": 0, "firstBatch": items.json_data }) mongo_uri = f"{server.uri}/test" # Create the new app. os.environ["SECRET_KEY"] = "SECRET_KEY" os.environ["LOGIN_DISABLED"] = "1" test_app = app.create_app(mongo_uri) # Use the app to create a test_client that can be used in our tests. with test_app.test_client() as client: yield client
def test_indexes_query(self): server = MockupDB(auto_ismaster=True) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) self.addCleanup(client.close) with going(client.test.collection.list_indexes) as cursor: request = server.receives(OpQuery, namespace='test.system.indexes') request.reply([{'name': 'index_0'}], cursor_id=123) with going(list, cursor()) as indexes: request = server.receives(OpGetMore, namespace='test.system.indexes', cursor_id=123) request.reply([{'name': 'index_1'}], starting_from=1, cursor_id=0) self.check_indexes(indexes())
def test_initial_ismaster(self): server = MockupDB() server.run() self.addCleanup(server.stop) start = time.time() client = MongoClient(server.uri) self.addCleanup(client.close) # A single ismaster is enough for the client to be connected. self.assertFalse(client.nodes) server.receives('ismaster').ok(ismaster=True) wait_until(lambda: client.nodes, 'update nodes', timeout=1) # At least 10 seconds before next heartbeat. server.receives('ismaster').ok(ismaster=True) self.assertGreaterEqual(time.time() - start, 10)
def test_client_handshake_saslSupportedMechs(self): server = MockupDB() server.run() self.addCleanup(server.stop) primary_response = OpReply('ismaster', True, minWireVersion=2, maxWireVersion=6) client = MongoClient(server.uri, username='******', password='******') self.addCleanup(client.close) # New monitoring sockets send data during handshake. heartbeat = server.receives('ismaster') heartbeat.ok(primary_response) future = go(client.db.command, 'whatever') for request in server: if request.matches('ismaster'): if request.client_port == heartbeat.client_port: # This is the monitor again, keep going. request.ok(primary_response) else: # Handshaking a new application socket should send # saslSupportedMechs and speculativeAuthenticate. self.assertEqual(request['saslSupportedMechs'], 'admin.username') self.assertIn( 'saslStart', request['speculativeAuthenticate']) auth = {'conversationId': 1, 'done': False, 'payload': b'r=wPleNM8S5p8gMaffMDF7Py4ru9bnmmoqb0' b'1WNPsil6o=pAvr6B1garhlwc6MKNQ93ZfFky' b'tXdF9r,s=4dcxugMJq2P4hQaDbGXZR8uR3ei' b'PHrSmh4uhkg==,i=15000'} request.ok('ismaster', True, saslSupportedMechs=['SCRAM-SHA-256'], speculativeAuthenticate=auth, minWireVersion=2, maxWireVersion=6) # Authentication should immediately fail with: # OperationFailure: Server returned an invalid nonce. with self.assertRaises(OperationFailure): future() return
def test_iteration(self): server = MockupDB(auto_ismaster={'maxWireVersion': 3}) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri) def send_three_docs(): for i in range(3): client.test.test.insert({'_id': i}) with going(send_three_docs): j = 0 # The "for request in server" statement is the point of this test. for request in server: self.assertTrue(request.matches({'insert': 'test', 'documents': [{'_id': j}]})) request.ok() j += 1 if j == 3: break
def server(self, *args, **kwargs): server = MockupDB(*args, **kwargs) server.run() self.addCleanup(server.stop) return server
class TestLegacyWrites(unittest.TestCase): def setUp(self): self.server = MockupDB(auto_ismaster=True) self.server.run() self.addCleanup(self.server.stop) self.client = MongoClient(self.server.uri) self.collection = self.client.db.collection def test_insert_one(self): with going(self.collection.insert_one, {'_id': 1}) as future: self.server.receives(OpInsert({'_id': 1}, flags=0)) self.server.receives(Command('getlasterror')).replies_to_gle() self.assertEqual(1, future().inserted_id) def test_insert_many(self): collection = self.collection.with_options( write_concern=WriteConcern(0)) flags = INSERT_FLAGS['ContinueOnError'] docs = [{'_id': 1}, {'_id': 2}] with going(collection.insert_many, docs, ordered=False) as future: self.server.receives(OpInsert(docs, flags=flags)) self.assertEqual([1, 2], future().inserted_ids) def test_replace_one(self): with going(self.collection.replace_one, {}, {}) as future: self.server.receives(OpUpdate({}, {}, flags=0)) request = self.server.receives(Command('getlasterror')) request.replies_to_gle(upserted=1) self.assertEqual(1, future().upserted_id) def test_update_many(self): flags = UPDATE_FLAGS['MultiUpdate'] with going(self.collection.update_many, {}, {'$unset': 'a'}) as future: update = self.server.receives(OpUpdate({}, {}, flags=flags)) self.assertEqual(2, update.flags) gle = self.server.receives(Command('getlasterror')) gle.replies_to_gle(upserted=1) self.assertEqual(1, future().upserted_id) def test_delete_one(self): flags = DELETE_FLAGS['SingleRemove'] with going(self.collection.delete_one, {}) as future: delete = self.server.receives(OpDelete({}, flags=flags)) self.assertEqual(1, delete.flags) gle = self.server.receives(Command('getlasterror')) gle.replies_to_gle(n=1) self.assertEqual(1, future().deleted_count) def test_delete_many(self): with going(self.collection.delete_many, {}) as future: delete = self.server.receives(OpDelete({}, flags=0)) self.assertEqual(0, delete.flags) gle = self.server.receives(Command('getlasterror')) gle.replies_to_gle(n=2) self.assertEqual(2, future().deleted_count)
def test_ssl_basic(self): server = MockupDB(ssl=True, auto_ismaster=True) server.run() self.addCleanup(server.stop) client = MongoClient(server.uri, ssl_cert_reqs=ssl.CERT_NONE) client.db.command('ismaster')