Skip to content

Commit c34498b

Browse files
committed
Merge pull request #209 from Nobatek/dev_JL_mongomock
ignoring parse_uri if testing config is true and uses mongomock uri schema
2 parents 8bcfd23 + ccafaaa commit c34498b

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ that much better:
2929
* Len Buckens - https://github.com/buckensl
3030
* Garito - https://github.com/garito
3131
* Jérôme Lafréchoux (Nobatek) - http://nobatek.com
32+
* Bruno Belarmino - https://github.com/brunobelarmino

flask_mongoengine/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from flask import abort, current_app
66

77
import mongoengine
8+
from distutils.version import StrictVersion
89

910
if mongoengine.__version__ == '0.7.10':
1011
from mongoengine.base import BaseField
@@ -86,8 +87,12 @@ def _create_connection(conn_settings):
8687
if 'replicaset' in conn:
8788
conn['replicaSet'] = conn.pop('replicaset')
8889

90+
if (StrictVersion(mongoengine.__version__) >= StrictVersion('0.10.6') and
91+
current_app.config['TESTING'] == True and
92+
conn.get('host', '').startswith('mongomock://')):
93+
pass
8994
# Handle uri style connections
90-
if "://" in conn.get('host', ''):
95+
elif "://" in conn.get('host', ''):
9196
uri_dict = uri_parser.parse_uri(conn['host'])
9297
conn['db'] = uri_dict['database']
9398

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def load_module(module_name, script_file):
3737
except:
3838
pass
3939

40-
test_requirements = ['nose', 'rednose', 'coverage']
40+
test_requirements = ['nose', 'rednose', 'coverage', 'mongomock']
4141

4242
setup(
4343
name='flask-mongoengine',

tests/test_connection.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import unittest
2+
import mongomock
3+
import mongoengine
4+
from pymongo.errors import InvalidURI
5+
from distutils.version import StrictVersion
6+
from flask.ext.mongoengine import MongoEngine
7+
from tests import FlaskMongoEngineTestCase
8+
9+
10+
class ConnectionTestCase(FlaskMongoEngineTestCase):
11+
12+
def test_ignore_parse_uri_if_testing_true_and_uses_mongomock_schema(self):
13+
self.app.config['TESTING'] = True
14+
self.app.config['MONGODB_ALIAS'] = 'unittest'
15+
self.app.config['MONGODB_HOST'] = 'mongomock://localhost'
16+
17+
if StrictVersion(mongoengine.__version__) >= StrictVersion('0.10.6'):
18+
db = MongoEngine(self.app)
19+
self.assertTrue(isinstance(db.connection, mongomock.MongoClient))
20+
21+
def test_parse_uri_if_testing_true_and_not_uses_mongomock_schema(self):
22+
self.app.config['TESTING'] = True
23+
self.app.config['MONGODB_ALIAS'] = 'unittest'
24+
self.app.config['MONGODB_HOST'] = 'mongo://localhost'
25+
26+
self.assertRaises(InvalidURI, MongoEngine, self.app)
27+
28+
def test_parse_uri_if_testing_not_true(self):
29+
self.app.config['TESTING'] = False
30+
self.app.config['MONGODB_ALIAS'] = 'unittest'
31+
self.app.config['MONGODB_HOST'] = 'mongomock://localhost'
32+
33+
self.assertRaises(InvalidURI, MongoEngine, self.app)
34+
35+
if __name__ == '__main__':
36+
unittest.main()

0 commit comments

Comments
 (0)