1
1
import time
2
2
3
3
import pymongo
4
+ import pytest
4
5
5
6
from flask_pymongo .tests .util import FlaskRequestTest
6
7
import flask_pymongo
7
8
8
9
10
+ class CouldNotConnect (Exception ):
11
+ pass
12
+
13
+
9
14
class FlaskPyMongoConfigTest (FlaskRequestTest ):
10
15
11
16
def setUp (self ):
@@ -32,7 +37,7 @@ def test_config_with_uri_in_flask_conf_var(self):
32
37
uri = "mongodb://localhost:{}/{}" .format (self .port , self .dbname )
33
38
self .app .config ["MONGO_URI" ] = uri
34
39
35
- mongo = flask_pymongo .PyMongo (self .app )
40
+ mongo = flask_pymongo .PyMongo (self .app , connect = True )
36
41
37
42
_wait_until_connected (mongo )
38
43
assert mongo .db .name == self .dbname
@@ -41,7 +46,7 @@ def test_config_with_uri_in_flask_conf_var(self):
41
46
def test_config_with_uri_passed_directly (self ):
42
47
uri = "mongodb://localhost:{}/{}" .format (self .port , self .dbname )
43
48
44
- mongo = flask_pymongo .PyMongo (self .app , uri )
49
+ mongo = flask_pymongo .PyMongo (self .app , uri , connect = True )
45
50
46
51
_wait_until_connected (mongo )
47
52
assert mongo .db .name == self .dbname
@@ -68,11 +73,19 @@ class CustomDict(dict):
68
73
69
74
assert type (mongo .db .things .find_one ()) == CustomDict
70
75
76
+ def test_it_doesnt_connect_by_default (self ):
77
+ uri = "mongodb://localhost:{}/{}" .format (self .port , self .dbname )
78
+
79
+ mongo = flask_pymongo .PyMongo (self .app , uri )
80
+
81
+ with pytest .raises (CouldNotConnect ):
82
+ _wait_until_connected (mongo , timeout = 0.2 )
83
+
71
84
72
85
def _wait_until_connected (mongo , timeout = 1.0 ):
73
86
start = time .time ()
74
87
while time .time () < (start + timeout ):
75
88
if mongo .cx .nodes :
76
89
return
77
90
time .sleep (0.05 )
78
- raise RuntimeError ("could not prove mongodb connected in %r seconds" % timeout )
91
+ raise CouldNotConnect ("could not prove mongodb connected in %r seconds" % timeout )
0 commit comments