Skip to content

Commit 9ffe4fe

Browse files
authored
Merge pull request #277 from BarrusInvestments/fix/connection_regressions
Fix for connection regressions
2 parents 38b1606 + 8efcb10 commit 9ffe4fe

File tree

8 files changed

+66
-35
lines changed

8 files changed

+66
-35
lines changed

flask_mongoengine/connection.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -254,28 +254,44 @@ def _register_test_connection(port, db_alias, preserved):
254254
return _conn
255255

256256

257-
def _resolve_settings(conn_setting, remove_pass=True):
258-
259-
if conn_setting and isinstance(conn_setting, dict):
260-
conn_setting = dict(((k[8:] if k.startswith("MONGODB_") else k), v) for k, v in conn_setting.items() if v is not None)
261-
conn_setting = dict((k.lower(), v) for k, v in conn_setting.items())
262-
263-
resolved = {
264-
'alias': conn_setting.get('alias', DEFAULT_CONNECTION_NAME),
265-
'name': conn_setting.get('db', 'test'),
266-
'host': conn_setting.get('host', 'localhost'),
267-
'port': conn_setting.get('port', 27017),
268-
'username': conn_setting.get('username'),
269-
# default to ReadPreference.PRIMARY if no read_preference is supplied
270-
'read_preference': conn_setting.get('read_preference', ReadPreference.PRIMARY),
271-
}
272-
if 'replicaset' in conn_setting:
273-
resolved['replicaSet'] = conn_setting.pop('replicaset')
274-
if not remove_pass:
275-
resolved['password'] = conn_setting.get('password')
276-
return resolved
277-
278-
return conn_setting
257+
def _resolve_settings(settings, settings_prefix=None, remove_pass=True):
258+
259+
if settings and isinstance(settings, dict):
260+
resolved_settings = dict()
261+
for k, v in settings.items():
262+
if settings_prefix:
263+
# Only resolve parameters that contain the prefix, ignoring the rest.
264+
if k.startswith(settings_prefix):
265+
resolved_settings[k[len(settings_prefix):].lower()] = v
266+
else:
267+
# If no prefix is provided then we assume that all parameters are relevant for the DB connection string.
268+
resolved_settings[k.lower()] = v
269+
270+
# Add various default values.
271+
resolved_settings['alias'] = resolved_settings.get('alias', DEFAULT_CONNECTION_NAME)
272+
if 'db' in resolved_settings:
273+
resolved_settings['name'] = resolved_settings.pop('db')
274+
else:
275+
resolved_settings['name'] = 'test'
276+
277+
resolved_settings['host'] = resolved_settings.get('host', 'localhost')
278+
resolved_settings['port'] = resolved_settings.get('port', 27017)
279+
resolved_settings['username'] = resolved_settings.get('username', None)
280+
281+
# default to ReadPreference.PRIMARY if no read_preference is supplied
282+
resolved_settings['read_preference'] = resolved_settings.get('read_preference', ReadPreference.PRIMARY)
283+
if 'replicaset' in resolved_settings:
284+
resolved_settings['replicaSet'] = resolved_settings.pop('replicaset')
285+
if remove_pass:
286+
try:
287+
del resolved_settings['password']
288+
except KeyError:
289+
# Password not specified, ignore.
290+
pass
291+
292+
return resolved_settings
293+
294+
return settings
279295

280296

281297
def fetch_connection_settings(config, remove_pass=True):
@@ -306,14 +322,14 @@ def fetch_connection_settings(config, remove_pass=True):
306322
# List of connection settings.
307323
settings_list = []
308324
for setting in settings:
309-
settings_list.append(_resolve_settings(setting, remove_pass))
325+
settings_list.append(_resolve_settings(setting, remove_pass=remove_pass))
310326
return settings_list
311327
else:
312328
# Connection settings provided as a dictionary.
313-
return _resolve_settings(settings, remove_pass)
329+
return _resolve_settings(settings, remove_pass=remove_pass)
314330
else:
315331
# Connection settings provided in standard format.
316-
return _resolve_settings(config, remove_pass)
332+
return _resolve_settings(config, settings_prefix='MONGODB_', remove_pass=remove_pass)
317333

318334

319335
def create_connection(config, app):

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class FlaskMongoEngineTestCase(unittest.TestCase):
99

1010
def setUp(self):
1111
self.app = flask.Flask(__name__)
12-
self.app.config['MONGODB_DB'] = 'testing'
12+
self.app.config['MONGODB_DB'] = 'test_db'
1313
self.app.config['TESTING'] = True
1414
self.ctx = self.app.app_context()
1515
self.ctx.push()

tests/test_connection.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def test_live_connection(self):
4545

4646
self._do_persist(db)
4747

48+
def test_uri_connection_string(self):
49+
db = MongoEngine()
50+
self.app.config['TEMP_DB'] = True
51+
self.app.config['MONGO_URI'] = 'mongodb://localhost:27017/test_uri'
52+
53+
self._do_persist(db)
54+
4855
def _do_persist(self, db):
4956
class Todo(db.Document):
5057
title = db.StringField(max_length=60)
@@ -67,16 +74,17 @@ class Todo(db.Document):
6774
def test_multiple_connections(self):
6875
db = MongoEngine()
6976
self.app.config['TESTING'] = True
77+
self.app.config['TEMP_DB'] = True
7078
self.app.config['MONGODB_SETTINGS'] = [
7179
{
7280
'ALIAS': 'default',
73-
'DB': 'my_db1',
81+
'DB': 'testing_db1',
7482
'HOST': 'localhost',
7583
'PORT': 27017
7684
},
7785
{
78-
"ALIAS": "my_db2",
79-
"DB": 'my_db2',
86+
"ALIAS": "testing_db2",
87+
"DB": 'testing_db2',
8088
"HOST": 'localhost',
8189
"PORT": 27017
8290
},
@@ -86,7 +94,7 @@ class Todo(db.Document):
8694
title = db.StringField(max_length=60)
8795
text = db.StringField()
8896
done = db.BooleanField(default=False)
89-
meta = {"db_alias": "my_db2"}
97+
meta = {"db_alias": "testing_db2"}
9098

9199
db.init_app(self.app)
92100
Todo.drop_collection()
@@ -133,3 +141,10 @@ def test_temp_db_with_false_testing(self):
133141
self.app.config['TEMP_DB'] = True
134142
self.app.config['MONGODB_ALIAS'] = 'unittest_4'
135143
self.assertRaises(InvalidSettingsError, MongoEngine, self.app)
144+
145+
def test_connection_kwargs(self):
146+
self.app.config['MONGODB_SETTINGS'] = {'DB': 'testing_tz_aware', 'alias': 'tz_aware_true', 'TZ_AWARE': True}
147+
self.app.config['TESTING'] = True
148+
db = MongoEngine()
149+
db.init_app(self.app)
150+
self.assertTrue(db.connection.client.codec_options.tz_aware)

tests/test_forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class WTFormsAppTestCase(FlaskMongoEngineTestCase):
1717

1818
def setUp(self):
1919
super(WTFormsAppTestCase, self).setUp()
20-
self.db_name = 'testing'
20+
self.db_name = 'test_db'
2121
self.app.config['MONGODB_DB'] = self.db_name
2222
self.app.config['TESTING'] = True
2323
# For Flask-WTF < 0.9

tests/test_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def assertDictContains(self, superset, subset):
2525

2626
def setUp(self):
2727
super(JSONAppTestCase, self).setUp()
28-
self.app.config['MONGODB_DB'] = 'testing'
28+
self.app.config['MONGODB_DB'] = 'test_db'
2929
self.app.config['TESTING'] = True
3030
self.app.json_encoder = DummyEncoder
3131
db = MongoEngine()

tests/test_json_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def assertDictContains(self, superset, subset):
1818

1919
def setUp(self):
2020
super(JSONAppTestCase, self).setUp()
21-
self.app.config['MONGODB_DB'] = 'testing'
21+
self.app.config['MONGODB_DB'] = 'test_db'
2222
self.app.config['TESTING'] = True
2323
self.app.config['TEMP_DB'] = True
2424
db = MongoEngine()

tests/test_pagination.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class PaginationTestCase(FlaskMongoEngineTestCase):
99

1010
def setUp(self):
1111
super(PaginationTestCase, self).setUp()
12-
self.db_name = 'testing'
12+
self.db_name = 'test_db'
1313
self.app.config['MONGODB_DB'] = self.db_name
1414
self.app.config['TESTING'] = True
1515
self.app.config['CSRF_ENABLED'] = False

tests/test_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SessionTestCase(FlaskMongoEngineTestCase):
99

1010
def setUp(self):
1111
super(SessionTestCase, self).setUp()
12-
self.db_name = 'testing'
12+
self.db_name = 'test_db'
1313
self.app.config['MONGODB_DB'] = self.db_name
1414
self.app.config['TESTING'] = True
1515
db = MongoEngine(self.app)

0 commit comments

Comments
 (0)