1
+ import mongoengine
1
2
from mongoengine .context_managers import switch_db
3
+ from nose import SkipTest
4
+ from nose .tools import assert_raises
2
5
import pymongo
3
6
from pymongo .errors import InvalidURI
4
7
from pymongo .read_preferences import ReadPreference
10
13
11
14
class ConnectionTestCase (FlaskMongoEngineTestCase ):
12
15
13
- def _do_persist (self , db ):
16
+ def _do_persist (self , db , alias = None ):
14
17
"""Initialize a test Flask application and persist some data in
15
18
MongoDB, ultimately asserting that the connection works.
16
19
"""
17
- class Todo (db .Document ):
18
- title = db .StringField (max_length = 60 )
19
- text = db .StringField ()
20
- done = db .BooleanField (default = False )
20
+ if alias :
21
+ class Todo (db .Document ):
22
+ meta = {'db_alias' : alias }
23
+ title = db .StringField (max_length = 60 )
24
+ text = db .StringField ()
25
+ done = db .BooleanField (default = False )
26
+ else :
27
+ class Todo (db .Document ):
28
+ title = db .StringField (max_length = 60 )
29
+ text = db .StringField ()
30
+ done = db .BooleanField (default = False )
21
31
22
32
db .init_app (self .app )
23
33
Todo .drop_collection ()
@@ -41,7 +51,7 @@ def test_simple_connection(self):
41
51
'PORT' : 27017 ,
42
52
'DB' : 'flask_mongoengine_test_db'
43
53
}
44
- self ._do_persist (db )
54
+ self ._do_persist (db , alias = 'simple_conn' )
45
55
46
56
def test_host_as_uri_string (self ):
47
57
"""Make sure we can connect to a standalone MongoDB if we specify
@@ -51,14 +61,43 @@ def test_host_as_uri_string(self):
51
61
self .app .config ['MONGODB_HOST' ] = 'mongodb://localhost:27017/flask_mongoengine_test_db'
52
62
self ._do_persist (db )
53
63
64
+ def test_mongomock_host_as_uri_string (self ):
65
+ """Make sure we switch to mongomock if we specify the host as a mongomock URI.
66
+ """
67
+ if mongoengine .VERSION < (0 , 9 , 0 ):
68
+ raise SkipTest ('Mongomock not supported for mongoengine < 0.9.0' )
69
+ db = MongoEngine ()
70
+ self .app .config ['MONGODB_HOST' ] = 'mongomock://localhost:27017/flask_mongoengine_test_db'
71
+ with assert_raises (RuntimeError ) as exc :
72
+ self ._do_persist (db )
73
+ assert str (exc .exception ) == 'You need mongomock installed to mock MongoEngine.'
74
+
75
+ def test_mongomock_as_param (self ):
76
+ """Make sure we switch to mongomock when providing IS_MOCK option.
77
+ """
78
+ if mongoengine .VERSION < (0 , 9 , 0 ):
79
+ raise SkipTest ('Mongomock not supported for mongoengine < 0.9.0' )
80
+ db = MongoEngine ()
81
+ self .app .config ['MONGODB_SETTINGS' ] = {
82
+ 'ALIAS' : 'simple_conn' ,
83
+ 'HOST' : 'localhost' ,
84
+ 'PORT' : 27017 ,
85
+ 'DB' : 'flask_mongoengine_test_db' ,
86
+ 'IS_MOCK' : True
87
+ }
88
+ with assert_raises (RuntimeError ) as exc :
89
+ self ._do_persist (db , alias = 'simple_conn' )
90
+ assert str (exc .exception ) == 'You need mongomock installed to mock MongoEngine.'
91
+
54
92
def test_host_as_list (self ):
55
93
"""Make sure MONGODB_HOST can be a list hosts."""
56
94
db = MongoEngine ()
57
95
self .app .config ['MONGODB_SETTINGS' ] = {
58
96
'ALIAS' : 'host_list' ,
59
97
'HOST' : ['localhost:27017' ],
98
+ 'DB' : 'flask_mongoengine_test_db'
60
99
}
61
- self ._do_persist (db )
100
+ self ._do_persist (db , alias = 'host_list' )
62
101
63
102
def test_multiple_connections (self ):
64
103
"""Make sure establishing multiple connections to a standalone
@@ -117,6 +156,16 @@ def test_connection_with_invalid_uri(self):
117
156
self .app .config ['MONGODB_HOST' ] = 'mongo://localhost'
118
157
self .assertRaises (InvalidURI , MongoEngine , self .app )
119
158
159
+ def test_ingnored_mongodb_prefix_config (self ):
160
+ """Config starting by MONGODB_ but not used by flask-mongoengine
161
+ should be ignored.
162
+ """
163
+ db = MongoEngine ()
164
+ self .app .config ['MONGODB_HOST' ] = 'mongodb://localhost:27017/flask_mongoengine_test_db_prod'
165
+ # Invalid host, should trigger exception if used
166
+ self .app .config ['MONGODB_TEST_HOST' ] = 'dummy://localhost:27017/test'
167
+ self ._do_persist (db )
168
+
120
169
def test_connection_kwargs (self ):
121
170
"""Make sure additional connection kwargs work."""
122
171
0 commit comments