Skip to content

Commit 4bcd64f

Browse files
committed
Final migration of test_connection tests
1 parent a3a6dab commit 4bcd64f

File tree

1 file changed

+132
-1
lines changed

1 file changed

+132
-1
lines changed

tests/test_connection.py

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import mongoengine
2+
import pymongo
23
import pytest
34
from mongoengine.connection import ConnectionFailure
5+
from mongoengine.context_managers import switch_db
46
from pymongo.database import Database
5-
from pymongo.mongo_client import MongoClient
67
from pymongo.errors import InvalidURI
8+
from pymongo.mongo_client import MongoClient
9+
from pymongo.read_preferences import ReadPreference
10+
711
from flask_mongoengine import MongoEngine, current_mongoengine_instance
812

913

@@ -195,3 +199,130 @@ def test_connection__should_raise__if_uri_not_properly_formatted(app, config_ext
195199
str(error.value)
196200
== "Invalid URI scheme: URI must begin with 'mongodb://' or 'mongodb+srv://'"
197201
)
202+
203+
204+
def test_connection__should_accept_host_as_list(app):
205+
"""Make sure MONGODB_HOST can be a list hosts."""
206+
db = MongoEngine()
207+
app.config["MONGODB_SETTINGS"] = {
208+
"ALIAS": "host_list",
209+
"HOST": ["localhost:27017"],
210+
"DB": "flask_mongoengine_list_test_db",
211+
}
212+
db.init_app(app)
213+
214+
connection = mongoengine.get_connection("host_list")
215+
mongo_engine_db = mongoengine.get_db("host_list")
216+
assert isinstance(mongo_engine_db, Database)
217+
assert isinstance(connection, MongoClient)
218+
assert mongo_engine_db.name == "flask_mongoengine_list_test_db"
219+
assert connection.HOST == "localhost"
220+
assert connection.PORT == 27017
221+
222+
223+
def test_multiple_connections(app):
224+
"""Make sure establishing multiple connections to a standalone
225+
MongoDB and switching between them works.
226+
"""
227+
db = MongoEngine()
228+
app.config["MONGODB_SETTINGS"] = [
229+
{
230+
"ALIAS": "default",
231+
"DB": "flask_mongoengine_test_db_1",
232+
"HOST": "localhost",
233+
"PORT": 27017,
234+
},
235+
{
236+
"ALIAS": "alternative",
237+
"DB": "flask_mongoengine_test_db_2",
238+
"HOST": "localhost",
239+
"PORT": 27017,
240+
},
241+
]
242+
243+
class Todo(db.Document):
244+
title = db.StringField(max_length=60)
245+
246+
db.init_app(app)
247+
# Drop default collection from init
248+
Todo.drop_collection()
249+
Todo.meta = {"db_alias": "alternative"}
250+
# Drop 'alternative' collection initiated early.
251+
Todo.drop_collection()
252+
253+
# Make sure init correct and both databases are clean
254+
with switch_db(Todo, "default") as Todo:
255+
doc = Todo.objects().first()
256+
assert doc is None
257+
258+
with switch_db(Todo, "alternative") as Todo:
259+
doc = Todo.objects().first()
260+
assert doc is None
261+
262+
# Test saving a doc via the default connection
263+
with switch_db(Todo, "default") as Todo:
264+
todo = Todo()
265+
todo.text = "Sample"
266+
todo.title = "Testing"
267+
todo.done = True
268+
s_todo = todo.save()
269+
270+
f_to = Todo.objects().first()
271+
assert s_todo.title == f_to.title
272+
273+
# Make sure the doc still doesn't exist in the alternative db
274+
with switch_db(Todo, "alternative") as Todo:
275+
doc = Todo.objects().first()
276+
assert doc is None
277+
278+
# Make sure switching back to the default connection shows the doc
279+
with switch_db(Todo, "default") as Todo:
280+
doc = Todo.objects().first()
281+
assert doc is not None
282+
283+
284+
def test_ingnored_mongodb_prefix_config(app):
285+
"""Config starting by MONGODB_ but not used by flask-mongoengine
286+
should be ignored.
287+
"""
288+
db = MongoEngine()
289+
app.config[
290+
"MONGODB_HOST"
291+
] = "mongodb://localhost:27017/flask_mongoengine_test_db_prod"
292+
# Invalid host, should trigger exception if used
293+
app.config["MONGODB_TEST_HOST"] = "dummy://localhost:27017/test"
294+
db.init_app(app)
295+
296+
connection = mongoengine.get_connection()
297+
mongo_engine_db = mongoengine.get_db()
298+
assert isinstance(mongo_engine_db, Database)
299+
assert isinstance(connection, MongoClient)
300+
assert mongo_engine_db.name == "flask_mongoengine_test_db_prod"
301+
assert connection.HOST == "localhost"
302+
assert connection.PORT == 27017
303+
304+
305+
def test_connection_kwargs(app):
306+
"""Make sure additional connection kwargs work."""
307+
308+
# Figure out whether to use "MAX_POOL_SIZE" or "MAXPOOLSIZE" based
309+
# on PyMongo version (former was changed to the latter as described
310+
# in https://jira.mongodb.org/browse/PYTHON-854)
311+
# TODO remove once PyMongo < 3.0 support is dropped
312+
if pymongo.version_tuple[0] >= 3:
313+
MAX_POOL_SIZE_KEY = "MAXPOOLSIZE"
314+
else:
315+
MAX_POOL_SIZE_KEY = "MAX_POOL_SIZE"
316+
317+
app.config["MONGODB_SETTINGS"] = {
318+
"ALIAS": "tz_aware_true",
319+
"DB": "flask_mongoengine_testing_tz_aware",
320+
"TZ_AWARE": True,
321+
"READ_PREFERENCE": ReadPreference.SECONDARY,
322+
MAX_POOL_SIZE_KEY: 10,
323+
}
324+
db = MongoEngine(app)
325+
326+
assert db.connection.codec_options.tz_aware
327+
assert db.connection.max_pool_size == 10
328+
assert db.connection.read_preference == ReadPreference.SECONDARY

0 commit comments

Comments
 (0)