Skip to content

Commit d4318e9

Browse files
committed
Allow for mongomock:// URI
1 parent 7d71fef commit d4318e9

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ that much better:
3535
* Denny Huang - https://github.com/denny0223
3636
* Stefan Wojcik - https://github.com/wojcikstefan
3737
* John Cass - https://github.com/jcass77
38+
* Aly Sivji - https://github.com/alysivji

flask_mongoengine/connection.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ def _sanitize_settings(settings):
2424

2525
# Handle uri style connections
2626
if "://" in resolved_settings.get('host', ''):
27-
uri_dict = uri_parser.parse_uri(resolved_settings['host'])
27+
# this section pulls the database name from the URI
28+
# PyMongo requires URI to start with mongodb:// to parse
29+
# this workaround allows mongomock to work
30+
uri_to_check = resolved_settings['host']
31+
32+
if uri_to_check.startswith('mongomock://'):
33+
uri_to_check = uri_to_check.replace('mongomock://', 'mongodb://')
34+
35+
uri_dict = uri_parser.parse_uri(uri_to_check)
2836
resolved_settings['db'] = uri_dict['database']
2937

3038
# Add a default name param or use the "db" key if exists

tests/test_connection.py

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ def test_host_as_uri_string(self):
5151
self.app.config['MONGODB_HOST'] = 'mongodb://localhost:27017/flask_mongoengine_test_db'
5252
self._do_persist(db)
5353

54+
def test_mongomock_host_as_uri_string(self):
55+
"""Make sure we can connect to the mongomock object if we specify
56+
the host as a mongomock URI.
57+
"""
58+
db = MongoEngine()
59+
self.app.config['MONGODB_HOST'] = 'mongomock://localhost:27017/flask_mongoengine_test_db'
60+
self._do_persist(db)
61+
5462
def test_host_as_list(self):
5563
"""Make sure MONGODB_HOST can be a list hosts."""
5664
db = MongoEngine()

0 commit comments

Comments
 (0)