def test__get_connection_with_uri(self): """Test the `_get_connection()` method with URIs.""" with mock.patch('simon.connection.MongoClient') as mock_conn: url1 = ('mongodb://*****:*****@simon.mongo.com:27017/' 'simon') conn, settings = connection._get_connection(host=url1, port=None, replica_set=None) mock_conn.assert_called_with(host='simon.mongo.com', port=27017) self.assertEqual(settings['name'], 'simon') self.assertEqual(settings['username'], 'simonuser') self.assertEqual(settings['password'], 'simonpassword') with mock.patch('simon.connection.MongoReplicaSetClient') as mock_conn: url2 = ('mongodb://*****:*****@simon.m0.mongo.com:' '27017/simon-rs') conn, settings = connection._get_connection(host=url2, port=None, replica_set='simonrs') mock_conn.assert_called_with(hosts_or_uri='simon.m0.mongo.com', replicaSet='simonrs') self.assertEqual(settings['name'], 'simon-rs') self.assertEqual(settings['username'], 'simonuser') self.assertEqual(settings['password'], 'simonpassword') url3 = ('mongodb://*****:*****@' 'simon.m0.mongo.com:27017,simon.m1.mongo.com:27017/' 'simon-rs') conn, settings = connection._get_connection(host=url3, port=None, replica_set='simonrs') mock_conn.assert_called_with(hosts_or_uri=url3, replicaSet='simonrs') self.assertEqual(settings['name'], 'simon-rs') self.assertEqual(settings['username'], 'simonuser') self.assertEqual(settings['password'], 'simonpassword') url4 = ('mongodb://*****:*****@' 'simon.m1.mongo.com:27017/simon-rsh?replicaSet=simon-rs') conn, settings = connection._get_connection(host=url4, port=None, replica_set=None) mock_conn.assert_called_with(hosts_or_uri='simon.m1.mongo.com', replicaSet='simon-rs') self.assertEqual(settings['name'], 'simon-rsh') self.assertEqual(settings['username'], 'simonuser') self.assertEqual(settings['password'], 'simonpassword') self.assertIn('simon.mongo.com:27017', connection._connections) self.assertIn('simon.m0.mongo.com:simonrs', connection._connections) self.assertIn('{0}:simonrs'.format(url3), connection._connections)
def test__get_connection_with_replica_set(self): """Test the `_get_connection()` method with replica sets.""" with mock.patch('simon.connection.MongoReplicaSetClient') as mock_conn: connection._get_connection(host='localhost', port=None, replica_set='simonrs') mock_conn.assert_called_with(hosts_or_uri='localhost', replicaSet='simonrs')
def test__get_connection_with_remote_host(self): """Test the `_get_connection()` method with a remote host.""" # The import is safe if the method passed the skip check from .integration_settings import REMOTE_HOST username = REMOTE_HOST.get('MONGODB_USERNAME') password = REMOTE_HOST.get('MONGODB_PASSWORD') host = REMOTE_HOST.get('MONGODB_HOST') port = REMOTE_HOST.get('MONGODB_PORT', 27017) dbname = REMOTE_HOST.get('MONGODB_DBNAME') url = 'mongodb://{0}:{1}@{2}:{3}/{4}'.format(username, password, host, port, dbname) connection._get_connection(host=url, port=None) self.assertIsInstance( connection._connections['{0}:{1}'.format(host, port)], connection.MongoClient)
def test__get_connection_with_connection(self): """Test the `_get_connection()` method with a `Connection`.""" conn = mock.Mock() conn.database_names = mock.Mock(return_value=True) conn2, __ = connection._get_connection(host=conn, port=None) self.assertEqual(conn, conn2)
def test__get_connection_with_remote_host_replica_set(self): ("Test the `_get_connection()` method with a remote replica " "set.") # The import is safe if the method passed the skip check from .integration_settings import REMOTE_REPLICA_SET username = REMOTE_REPLICA_SET.get('MONGODB_USERNAME') password = REMOTE_REPLICA_SET.get('MONGODB_PASSWORD') host = REMOTE_REPLICA_SET.get('MONGODB_HOST') dbname = REMOTE_REPLICA_SET.get('MONGODB_DBNAME') replica_set = REMOTE_REPLICA_SET.get('MONGODB_REPLICA_SET') url = 'mongodb://{0}:{1}@{2}/{3}'.format(username, password, host, dbname) connection._get_connection(host=url, port=None, replica_set=replica_set) self.assertIsInstance( connection._connections['{0}:{1}'.format(url, replica_set)], connection.MongoReplicaSetClient)
def test__get_connection(self): """Test the `_get_connection()` method.""" with mock.patch('simon.connection.MongoClient') as mock_conn: connection._get_connection(host='localhost', port=None, replica_set=None) mock_conn.assert_called_with(host='localhost', port=None) self.assertIn('localhost:27017', connection._connections) # When calling _get_connection() the second time, the # connection should be returned right from _connections # so mock_conn() should still have the same call parameters # and the length of __connections should be 1 connection._get_connection(host='localhost', port=27017, replica_set=None) mock_conn.assert_called_with(host='localhost', port=None) self.assertEqual(len(connection._connections), 1)
def test__get_connection_with_localhost(self): """Test the `_get_connection()` method with localhost.""" connection._get_connection(host='localhost', port=None) self.assertIsInstance(connection._connections['localhost:27017'], connection.MongoClient)
def test__get_connection_connectionerror(self): """Test that `_get_connection()` raises `ConnectionError`.""" with self.assertRaises(connection.ConnectionError): connection._get_connection(host='/dev/null', port=None)