Skip to content

Commit 4547b79

Browse files
committed
better connection tests + docstrings + remove (unnecessary?) replicaset rename
1 parent 58f5e07 commit 4547b79

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

flask_mongoengine/connection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ def _sanitize_settings(settings):
4242
resolved_settings['read_preference'] = resolved_settings.get('read_preference', ReadPreference.PRIMARY)
4343

4444
# Rename "replicaset" to "replicaSet" if it exists in the dict
45-
if 'replicaset' in resolved_settings:
46-
resolved_settings['replicaSet'] = resolved_settings.pop('replicaset')
45+
# TODO is this necessary? PyMongo normalizes the options and makes them
46+
# all lowercase via pymongo.common.validate (which is called in
47+
# MongoClient.__init__), so both "replicaset and "replicaSet" should be
48+
# valid
49+
# if 'replicaset' in resolved_settings:
50+
# resolved_settings['replicaSet'] = resolved_settings.pop('replicaset')
4751

4852
# Clean up empty values
4953
for k, v in resolved_settings.items():

tests/test_connection.py

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1+
from mongoengine.context_managers import switch_db
2+
import pymongo
13
from pymongo.errors import InvalidURI
4+
from pymongo.read_preferences import ReadPreference
25

36
from flask_mongoengine import MongoEngine
4-
from tests import FlaskMongoEngineTestCase
57

8+
from .tests import FlaskMongoEngineTestCase
69

7-
class ConnectionTestCase(FlaskMongoEngineTestCase):
810

9-
def test_live_connection(self):
10-
db = MongoEngine()
11-
self.app.config['MONGODB_SETTINGS'] = {
12-
'HOST': 'localhost',
13-
'PORT': 27017,
14-
'DB': 'flask_mongoengine_test_db'
15-
}
16-
self._do_persist(db)
17-
18-
def test_live_connection_with_uri_string(self):
19-
db = MongoEngine()
20-
self.app.config['MONGO_URI'] = 'mongodb://localhost:27017/flask_mongoengine_test_db'
21-
self._do_persist(db)
11+
class ConnectionTestCase(FlaskMongoEngineTestCase):
2212

2313
def _do_persist(self, db):
24-
"""Initialize test Flask application and persist some data in
14+
"""Initialize a test Flask application and persist some data in
2515
MongoDB, ultimately asserting that the connection works.
2616
"""
2717
class Todo(db.Document):
@@ -42,9 +32,28 @@ class Todo(db.Document):
4232
f_to = Todo.objects().first()
4333
self.assertEqual(s_todo.title, f_to.title)
4434

45-
def test_multiple_connections(self):
46-
from mongoengine.context_managers import switch_db
35+
def test_simple_connection(self):
36+
"""Make sure a simple connection to a standalone MongoDB works."""
37+
db = MongoEngine()
38+
self.app.config['MONGODB_SETTINGS'] = {
39+
'HOST': 'localhost',
40+
'PORT': 27017,
41+
'DB': 'flask_mongoengine_test_db'
42+
}
43+
self._do_persist(db)
44+
45+
def test_connection_with_uri_string(self):
46+
"""Make sure we can connect to a standalone MongoDB if we specify
47+
the host as a MongoDB URI.
48+
"""
49+
db = MongoEngine()
50+
self.app.config['MONGODB_HOST'] = 'mongodb://localhost:27017/flask_mongoengine_test_db'
51+
self._do_persist(db)
4752

53+
def test_multiple_connections(self):
54+
"""Make sure establishing multiple connections to a standalone
55+
MongoDB and switching between them works.
56+
"""
4857
db = MongoEngine()
4958
self.app.config['MONGODB_SETTINGS'] = [
5059
{
@@ -92,16 +101,35 @@ class Todo(db.Document):
92101
self.assertNotEqual(doc, None)
93102

94103
def test_connection_with_invalid_uri(self):
95-
self.app.config['MONGODB_ALIAS'] = 'unittest_2'
104+
"""Make sure connecting via an invalid URI raises an InvalidURI
105+
exception.
106+
"""
96107
self.app.config['MONGODB_HOST'] = 'mongo://localhost'
97108
self.assertRaises(InvalidURI, MongoEngine, self.app)
98109

99110
def test_connection_kwargs(self):
111+
"""Make sure additional connection kwargs work."""
112+
113+
# Figure out whether to use "MAX_POOL_SIZE" or "MAXPOOLSIZE" based
114+
# on PyMongo version (former was changed to the latter as described
115+
# in https://jira.mongodb.org/browse/PYTHON-854)
116+
# TODO remove once PyMongo < 3.0 support is dropped
117+
if pymongo.version_tuple[0] >= 3:
118+
MAX_POOL_SIZE_KEY = 'MAXPOOLSIZE'
119+
else:
120+
MAX_POOL_SIZE_KEY = 'MAX_POOL_SIZE'
121+
100122
self.app.config['MONGODB_SETTINGS'] = {
101123
'DB': 'flask_mongoengine_testing_tz_aware',
102-
'alias': 'tz_aware_true',
103-
'TZ_AWARE': True
124+
'TZ_AWARE': True,
125+
'READ_PREFERENCE': ReadPreference.SECONDARY,
126+
MAX_POOL_SIZE_KEY: 10,
104127
}
105128
db = MongoEngine()
106129
db.init_app(self.app)
107130
self.assertTrue(db.connection.client.codec_options.tz_aware)
131+
self.assertEqual(db.connection.client.max_pool_size, 10)
132+
self.assertEqual(
133+
db.connection.client.read_preference,
134+
ReadPreference.SECONDARY
135+
)

0 commit comments

Comments
 (0)