Skip to content

Commit

Permalink
Allow for mongomock:// URI
Browse files Browse the repository at this point in the history
  • Loading branch information
alysivji committed Mar 4, 2017
1 parent 7d71fef commit d4318e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ that much better:
* Denny Huang - https://github.com/denny0223
* Stefan Wojcik - https://github.com/wojcikstefan
* John Cass - https://github.com/jcass77
* Aly Sivji - https://github.com/alysivji
10 changes: 9 additions & 1 deletion flask_mongoengine/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ def _sanitize_settings(settings):

# Handle uri style connections
if "://" in resolved_settings.get('host', ''):
uri_dict = uri_parser.parse_uri(resolved_settings['host'])
# this section pulls the database name from the URI
# PyMongo requires URI to start with mongodb:// to parse
# this workaround allows mongomock to work
uri_to_check = resolved_settings['host']

if uri_to_check.startswith('mongomock://'):
uri_to_check = uri_to_check.replace('mongomock://', 'mongodb://')

uri_dict = uri_parser.parse_uri(uri_to_check)
resolved_settings['db'] = uri_dict['database']

# Add a default name param or use the "db" key if exists
Expand Down
8 changes: 8 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ def test_host_as_uri_string(self):
self.app.config['MONGODB_HOST'] = 'mongodb://localhost:27017/flask_mongoengine_test_db'
self._do_persist(db)

def test_mongomock_host_as_uri_string(self):
"""Make sure we can connect to the mongomock object if we specify
the host as a mongomock URI.
"""
db = MongoEngine()
self.app.config['MONGODB_HOST'] = 'mongomock://localhost:27017/flask_mongoengine_test_db'
self._do_persist(db)

def test_host_as_list(self):
"""Make sure MONGODB_HOST can be a list hosts."""
db = MongoEngine()
Expand Down

0 comments on commit d4318e9

Please sign in to comment.