Skip to content

Commit ee3cce3

Browse files
committed
Enhance connection settings.
Settings will accept: - MONGODB_ALIAS or alias - MONGODB_DB or db - MONGODB_HOST or host - MONGODB_PORT or port - MONGODB_USERNAME or username - MONGODB_PASSWORD or password
1 parent fce79d8 commit ee3cce3

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

flask_mongoengine/connection.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,29 @@ def _register_test_connection(port, db_alias, preserved):
241241
def _resolve_settings(conn_setting, removePass=True):
242242
if conn_setting and isinstance(conn_setting, dict):
243243
read_preference = False
244+
alias = conn_setting.get('MONGODB_ALIAS',
245+
conn_setting.get('alias', DEFAULT_CONNECTION_NAME))
246+
db = conn_setting.get('MONGODB_DB', conn_setting.get('db', 'test'))
247+
host = conn_setting.get('MONGODB_HOST', conn_setting.get('host', 'localhost'))
248+
port = conn_setting.get('MONGODB_PORT', conn_setting.get('port', 27017))
249+
username = conn_setting.get('MONGODB_USERNAME', conn_setting.get('username', None))
250+
password = conn_setting.get('MONGODB_PASSWORD', conn_setting.get('password', None))
251+
252+
if (not current_app.config.get('TESTING', False)
253+
and alias == DEFAULT_CONNECTION_NAME):
254+
alias = "{0}_{1}".format(db, port)
255+
244256
if IS_PYMONGO_3:
245257
read_preference = ReadPreference.PRIMARY
246258

247259
resolved = {}
248260
resolved['read_preference'] = read_preference
249-
resolved['alias'] = conn_setting.get('MONGODB_ALIAS', DEFAULT_CONNECTION_NAME)
250-
resolved['name'] = conn_setting.get('MONGODB_DB', 'test')
251-
resolved['host'] = conn_setting.get('MONGODB_HOST', 'localhost')
252-
resolved['password'] = conn_setting.get('MONGODB_PASSWORD', None)
253-
resolved['port'] = conn_setting.get('MONGODB_PORT', 27017)
254-
resolved['username'] = conn_setting.get('MONGODB_USERNAME', None)
261+
resolved['alias'] = alias
262+
resolved['name'] = db
263+
resolved['host'] = host
264+
resolved['password'] = password
265+
resolved['port'] = port
266+
resolved['username'] = username
255267
resolved['replicaSet'] = conn_setting.pop('replicaset', None)
256268

257269
host = resolved['host']

tests/test_connection.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ def test_mongomock_connection_request_on_most_old_mongoengine(self):
3030
if mongoengine.VERSION < (0, 10, 6):
3131
self.ensure_mongomock_connection()
3232

33+
def test_live_connection(self):
34+
db = MongoEngine()
35+
self.app.config['TEMP_DB'] = True
36+
self.app.config['MONGODB_SETTINGS'] = {
37+
'host' : 'localhost',
38+
'port' : 27017
39+
}
40+
class Todo(db.Document):
41+
title = db.StringField(max_length=60)
42+
text = db.StringField()
43+
done = db.BooleanField(default=False)
44+
45+
db.init_app(self.app)
46+
47+
# Test persist
48+
todo = Todo()
49+
todo.text = "Sample"
50+
todo.title = "Testing"
51+
todo.done = True
52+
s_todo = todo.save()
53+
54+
f_to = Todo.objects().first()
55+
self.assertEqual(s_todo.title, f_to.title)
56+
3357
def test_mongodb_temp_instance(self):
3458
# String value used instead of boolean
3559
self.app.config['TESTING'] = True

0 commit comments

Comments
 (0)