def test_connect_lazily_with_index(self): heartbeat_listener = HeartbeatStartedListener() create_indexes_listener = WhiteListEventListener('createIndexes') connect('mongodb://localhost:27017/foo', 'foo-connection', connect=False, event_listeners=[heartbeat_listener, create_indexes_listener]) client = _get_connection('foo-connection').database.client class Article(MongoModel): title = CharField() class Meta: connection_alias = 'foo-connection' indexes = [IndexModel([('title', 1)])] # Creating the class didn't create a connection, or any indexes. self.assertEqual(len(heartbeat_listener.results), 0) self.assertEqual(len(create_indexes_listener.results['started']), 0) # The connection and indexes are created on the first query. self.assertEqual(Article.objects.count(), 0) self.assertGreaterEqual(len(heartbeat_listener.results), 1) self.assertGreaterEqual( len(create_indexes_listener.results['started']), 1)
def test_connect_with_kwargs(self): connect('mongodb://localhost:27017/foo?maxPoolSize=42', 'foo-connection', minpoolsize=10) client = _get_connection('foo-connection').database.client self.assertEqual(42, client.max_pool_size) self.assertEqual(10, client.min_pool_size)
def test_connect_lazily_with_index(self): heartbeat_listener = HeartbeatStartedListener() create_indexes_listener = WhiteListEventListener('createIndexes') connect('mongodb://localhost:27017/foo', 'foo-connection', connect=False, event_listeners=[heartbeat_listener, create_indexes_listener]) client = _get_connection('foo-connection').database.client class Article(MongoModel): title = CharField() class Meta: connection_alias = 'foo-connection' indexes = [ IndexModel([('title', 1)]) ] # Creating the class didn't create a connection, or any indexes. self.assertEqual(len(heartbeat_listener.results), 0) self.assertEqual(len(create_indexes_listener.results['started']), 0) # The connection and indexes are created on the first query. self.assertEqual(Article.objects.count(), 0) self.assertGreaterEqual(len(heartbeat_listener.results), 1) self.assertGreaterEqual(len(create_indexes_listener.results['started']), 1)
def custom_connection(self, **kwargs): # mix kwargs with variables variables = self.variables for key, value in kwargs.items(): variables[key] = value db = variables.get('database', self._defaultdb) uri = "mongodb://%s:%s/%s" % ( variables.get('host'), variables.get('port'), db) # if db == self._defaultdb: # mongodb.connect(uri) # obj = mongodb._get_connection() # else: mongodb.connect(uri, alias=db) link = mongodb._get_connection(alias=db) log.debug("Connected to db %s", db) class obj: connection = link return obj
def test_connect_with_kwargs(self): connect('mongodb://localhost:27017/foo?maxPoolSize=42', 'foo-connection', minpoolsize=10) client = _get_connection('foo-connection').database.client self.assertEqual(42, client.max_pool_size) self.assertEqual(10, client.min_pool_size)
def custom_connection(self, **kwargs): ################## # mix kwargs with variables variables = self.variables for key, value in kwargs.items(): variables[key] = value ################## # connect for authentication if required uri = "mongodb://{}:{}/{}".format( variables.get('host'), variables.get('port'), AUTH_DB, ) mongodb.connect(uri, alias=AUTH_DB) ################## db = variables.get('database', 'UNKNOWN') uri = "mongodb://{}:{}/{}".format(variables.get('host'), variables.get('port'), db) mongodb.connect(uri, alias=db) link = mongodb._get_connection(alias=db) log.verbose("Connected to db {}", db) class obj: connection = link return obj
def tst(session): os.environ["APPLICATION_ENV"] = "testing" application = make_app() session.client = application.test_client() session.context = application.test_request_context() with application.app_context(): connection.connect(application.config["MONGO_URL"]) yield session client = connection._get_connection().database.client client.drop_database(application.config["MONGO_DB"])
def connect(self, **kwargs): variables = self.variables.copy() variables.update(kwargs) MongoExt.DATABASE = variables.get("database", "rapydo") uri = self._get_uri(variables) mongodb.connect(uri, alias=MongoExt.DATABASE) self.connection = mongodb._get_connection(alias=MongoExt.DATABASE) TopLevelMongoModel.save = catch_db_exceptions(TopLevelMongoModel.save) return self
def test_connect_lazily(self): heartbeat_listener = HeartbeatStartedListener() connect('mongodb://localhost:27017/foo', 'foo-connection', connect=False, event_listeners=[heartbeat_listener]) client = _get_connection('foo-connection').database.client class Article(MongoModel): title = CharField() class Meta: connection_alias = 'foo-connection' # Creating the class didn't create a connection. self.assertEqual(len(heartbeat_listener.results), 0) # The connection is created on the first query. self.assertEqual(Article.objects.count(), 0) self.assertGreaterEqual(len(heartbeat_listener.results), 1)
def test_connect_lazily(self): heartbeat_listener = HeartbeatStartedListener() connect('mongodb://localhost:27017/foo', 'foo-connection', connect=False, event_listeners=[heartbeat_listener]) client = _get_connection('foo-connection').database.client class Article(MongoModel): title = CharField() class Meta: connection_alias = 'foo-connection' # Creating the class didn't create a connection. self.assertEqual(len(heartbeat_listener.results), 0) # The connection is created on the first query. self.assertEqual(Article.objects.count(), 0) self.assertGreaterEqual(len(heartbeat_listener.results), 1)