Skip to content

Commit d3b7373

Browse files
committed
Ensure that **kwargs are passed to mongoengine.
1 parent 38b1606 commit d3b7373

File tree

8 files changed

+56
-32
lines changed

8 files changed

+56
-32
lines changed

flask_mongoengine/connection.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -254,28 +254,37 @@ 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, remove_pass=True):
258+
259+
if settings and isinstance(settings, dict):
260+
resolved_setting = dict()
261+
for k, v in settings.items():
262+
if k.startswith("MONGODB_"):
263+
resolved_setting[k[8:].lower()] = v
264+
else:
265+
resolved_setting[k.lower()] = v
266+
267+
resolved_setting['alias'] = resolved_setting.get('alias', DEFAULT_CONNECTION_NAME)
268+
if (resolved_setting.has_key('db')):
269+
resolved_setting['name'] = resolved_setting.pop('db')
270+
else:
271+
resolved_setting['name'] = 'test'
272+
resolved_setting['host'] = resolved_setting.get('host', 'localhost')
273+
resolved_setting['port'] = resolved_setting.get('port', 27017)
274+
resolved_setting['username'] = resolved_setting.get('username', None)
275+
# default to ReadPreference.PRIMARY if no read_preference is supplied
276+
resolved_setting['read_preference'] = resolved_setting.get('read_preference', ReadPreference.PRIMARY)
277+
if 'replicaset' in resolved_setting:
278+
resolved_setting['replicaSet'] = resolved_setting.pop('replicaset')
279+
if remove_pass:
280+
try:
281+
del resolved_setting['password']
282+
except KeyError:
283+
# Password not specified, ignore.
284+
pass
285+
286+
return resolved_setting
287+
return settings
279288

280289

281290
def fetch_connection_settings(config, remove_pass=True):

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 == True)

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)