1
1
from util import FlaskRequestTest , FlaskPyMongoTest
2
2
3
3
import time
4
+ import os
5
+ import unittest
4
6
5
7
import pymongo
6
8
import flask
@@ -13,55 +15,65 @@ class CustomDict(dict):
13
15
14
16
15
17
class FlaskPyMongoConfigTest (FlaskRequestTest ):
18
+
16
19
def setUp (self ):
17
- self .app = flask .Flask ('test' )
18
- self .context = self .app .test_request_context ('/' )
19
- self .context .push ()
20
+ super (FlaskPyMongoConfigTest , self ).setUp ()
21
+
22
+ conn = pymongo .MongoClient (port = self .port )
23
+ conn .test_db .command (
24
+ "createUser" ,
25
+ "flask" ,
26
+ pwd = "pymongo" ,
27
+ roles = ["readWrite" ],
28
+ )
20
29
21
30
def tearDown (self ):
22
- self .context .pop ()
31
+ super (FlaskPyMongoConfigTest , self ).tearDown ()
32
+
33
+ conn = pymongo .MongoClient (port = self .port )
34
+ conn .test_db .command ("dropUser" , "flask" )
23
35
24
36
def test_default_config_prefix (self ):
25
37
self .app .config ['MONGO_DBNAME' ] = 'flask_pymongo_test_db'
26
38
self .app .config ['MONGO_HOST' ] = 'localhost'
27
- self .app .config ['MONGO_PORT' ] = 27017
39
+ self .app .config ['MONGO_PORT' ] = self . port
28
40
29
41
mongo = flask_pymongo .PyMongo (self .app )
30
42
assert mongo .db .name == 'flask_pymongo_test_db' , 'wrong dbname: %s' % mongo .db .name
31
43
if pymongo .version_tuple [0 ] > 2 :
32
44
time .sleep (0.2 )
33
- assert ('localhost' , 27017 ) == mongo .cx .address
45
+ assert ('localhost' , self . port ) == mongo .cx .address
34
46
else :
35
47
assert mongo .cx .host == 'localhost'
36
- assert mongo .cx .port == 27017
48
+ assert mongo .cx .port == self . port
37
49
38
50
def test_custom_config_prefix (self ):
39
51
self .app .config ['CUSTOM_DBNAME' ] = 'flask_pymongo_test_db'
40
52
self .app .config ['CUSTOM_HOST' ] = 'localhost'
41
- self .app .config ['CUSTOM_PORT' ] = 27017
53
+ self .app .config ['CUSTOM_PORT' ] = self . port
42
54
43
55
mongo = flask_pymongo .PyMongo (self .app , 'CUSTOM' )
44
56
assert mongo .db .name == 'flask_pymongo_test_db' , 'wrong dbname: %s' % mongo .db .name
45
57
if pymongo .version_tuple [0 ] > 2 :
46
58
time .sleep (0.2 )
47
- assert ('localhost' , 27017 ) == mongo .cx .address
59
+ assert ('localhost' , self . port ) == mongo .cx .address
48
60
else :
49
61
assert mongo .cx .host == 'localhost'
50
- assert mongo .cx .port == 27017
62
+ assert mongo .cx .port == self . port
51
63
52
64
def test_converts_str_to_int (self ):
53
65
self .app .config ['MONGO_DBNAME' ] = 'flask_pymongo_test_db'
54
66
self .app .config ['MONGO_HOST' ] = 'localhost'
55
- self .app .config ['MONGO_PORT' ] = '27017'
67
+ self .app .config ['MONGO_PORT' ] = str ( self . port )
56
68
57
69
mongo = flask_pymongo .PyMongo (self .app )
58
70
assert mongo .db .name == 'flask_pymongo_test_db' , 'wrong dbname: %s' % mongo .db .name
59
71
if pymongo .version_tuple [0 ] > 2 :
60
72
time .sleep (0.2 )
61
- assert ('localhost' , 27017 ) == mongo .cx .address
73
+ assert ('localhost' , self . port ) == mongo .cx .address
62
74
else :
63
75
assert mongo .cx .host == 'localhost'
64
- assert mongo .cx .port == 27017
76
+ assert mongo .cx .port == self . port
65
77
66
78
def test_rejects_invalid_string (self ):
67
79
self .app .config ['MONGO_PORT' ] = '27017x'
@@ -70,6 +82,7 @@ def test_rejects_invalid_string(self):
70
82
71
83
def test_multiple_pymongos (self ):
72
84
for prefix in ('ONE' , 'TWO' ):
85
+ self .app .config ['%s_PORT' % prefix ] = self .port
73
86
self .app .config ['%s_DBNAME' % prefix ] = prefix
74
87
75
88
for prefix in ('ONE' , 'TWO' ):
@@ -78,7 +91,7 @@ def test_multiple_pymongos(self):
78
91
# this test passes if it raises no exceptions
79
92
80
93
def test_config_with_uri (self ):
81
- self .app .config ['MONGO_URI' ] = 'mongodb://localhost:27017 /flask_pymongo_test_db'
94
+ self .app .config ['MONGO_URI' ] = 'mongodb://localhost:{} /flask_pymongo_test_db' . format ( self . port )
82
95
83
96
with warnings .catch_warnings ():
84
97
# URI connections without a username and password
@@ -88,11 +101,12 @@ def test_config_with_uri(self):
88
101
assert mongo .db .name == 'flask_pymongo_test_db' , 'wrong dbname: %s' % mongo .db .name
89
102
if pymongo .version_tuple [0 ] > 2 :
90
103
time .sleep (0.2 )
91
- assert ('localhost' , 27017 ) == mongo .cx .address
104
+ assert ('localhost' , self . port ) == mongo .cx .address
92
105
else :
93
106
assert mongo .cx .host == 'localhost'
94
- assert mongo .cx .port == 27017
107
+ assert mongo .cx .port == self . port
95
108
109
+ @unittest .skip ("URI without port won't work with tox-docker's non-default port" )
96
110
def test_config_with_uri_no_port (self ):
97
111
self .app .config ['MONGO_URI' ] = 'mongodb://localhost/flask_pymongo_test_db'
98
112
@@ -104,12 +118,13 @@ def test_config_with_uri_no_port(self):
104
118
assert mongo .db .name == 'flask_pymongo_test_db' , 'wrong dbname: %s' % mongo .db .name
105
119
if pymongo .version_tuple [0 ] > 2 :
106
120
time .sleep (0.2 )
107
- assert ('localhost' , 27017 ) == mongo .cx .address
121
+ assert ('localhost' , self . port ) == mongo .cx .address
108
122
else :
109
123
assert mongo .cx .host == 'localhost'
110
- assert mongo .cx .port == 27017
124
+ assert mongo .cx .port == self . port
111
125
112
126
def test_config_with_document_class (self ):
127
+ self .app .config ['MONGO_PORT' ] = self .port
113
128
self .app .config ['MONGO_DOCUMENT_CLASS' ] = CustomDict
114
129
mongo = flask_pymongo .PyMongo (self .app )
115
130
if pymongo .version_tuple [0 ] > 2 :
@@ -118,14 +133,15 @@ def test_config_with_document_class(self):
118
133
assert mongo .cx .document_class == CustomDict
119
134
120
135
def test_config_without_document_class (self ):
136
+ self .app .config ['MONGO_PORT' ] = self .port
121
137
mongo = flask_pymongo .PyMongo (self .app )
122
138
if pymongo .version_tuple [0 ] > 2 :
123
139
assert mongo .cx .codec_options .document_class == dict
124
140
else :
125
141
assert mongo .cx .document_class == dict
126
142
127
143
def test_host_with_port_does_not_get_overridden_by_separate_port_config_value (self ):
128
- self .app .config ['MONGO_HOST' ] = 'localhost:27017'
144
+ self .app .config ['MONGO_HOST' ] = 'localhost:{}' . format ( self . port )
129
145
self .app .config ['MONGO_PORT' ] = 27018
130
146
131
147
with warnings .catch_warnings ():
@@ -135,13 +151,13 @@ def test_host_with_port_does_not_get_overridden_by_separate_port_config_value(se
135
151
mongo = flask_pymongo .PyMongo (self .app )
136
152
if pymongo .version_tuple [0 ] > 2 :
137
153
time .sleep (0.2 )
138
- assert ('localhost' , 27017 ) == mongo .cx .address
154
+ assert ('localhost' , self . port ) == mongo .cx .address
139
155
else :
140
156
assert mongo .cx .host == 'localhost'
141
- assert mongo .cx .port == 27017
157
+ assert mongo .cx .port == self . port
142
158
143
159
def test_uri_prioritised_over_host_and_port (self ):
144
- self .app .config ['MONGO_URI' ] = 'mongodb://localhost:27017 /database_name'
160
+ self .app .config ['MONGO_URI' ] = 'mongodb://localhost:{} /database_name' . format ( self . port )
145
161
self .app .config ['MONGO_HOST' ] = 'some_other_host'
146
162
self .app .config ['MONGO_PORT' ] = 27018
147
163
self .app .config ['MONGO_DBNAME' ] = 'not_the_correct_db_name'
@@ -153,17 +169,16 @@ def test_uri_prioritised_over_host_and_port(self):
153
169
mongo = flask_pymongo .PyMongo (self .app )
154
170
if pymongo .version_tuple [0 ] > 2 :
155
171
time .sleep (0.2 )
156
- assert ('localhost' , 27017 ) == mongo .cx .address
172
+ assert ('localhost' , self . port ) == mongo .cx .address
157
173
else :
158
174
assert mongo .cx .host == 'localhost'
159
- assert mongo .cx .port == 27017
175
+ assert mongo .cx .port == self . port
160
176
assert mongo .db .name == 'database_name'
161
177
162
178
163
179
def test_missing_auth_mechanism_in_nonprefixed_config (self ):
164
-
165
180
self .app .config ["MONGO_HOST" ] = 'localhost'
166
- self .app .config ["MONGO_PORT" ] = 27017
181
+ self .app .config ["MONGO_PORT" ] = self . port
167
182
self .app .config ["MONGO_USERNAME" ] = 'flask'
168
183
self .app .config ["MONGO_PASSWORD" ] = 'pymongo'
169
184
self .app .config ['MONGO_DBNAME' ] = 'test_db'
@@ -175,16 +190,15 @@ def test_missing_auth_mechanism_in_nonprefixed_config(self):
175
190
if pymongo .version_tuple [0 ] > 2 :
176
191
time .sleep (0.2 )
177
192
178
- assert ('localhost' , 27017 ) == mongo .cx .address
193
+ assert ('localhost' , self . port ) == mongo .cx .address
179
194
else :
180
195
assert mongo .cx .host == 'localhost'
181
- assert mongo .cx .port == 27017
196
+ assert mongo .cx .port == self . port
182
197
183
198
184
199
def test_missing_auth_mechanism_in_prefixed_config (self ):
185
-
186
200
self .app .config ["CUSTOM_MONGO_HOST" ] = 'localhost'
187
- self .app .config ["CUSTOM_MONGO_PORT" ] = 27017
201
+ self .app .config ["CUSTOM_MONGO_PORT" ] = self . port
188
202
self .app .config ["CUSTOM_MONGO_USERNAME" ] = 'flask'
189
203
self .app .config ["CUSTOM_MONGO_PASSWORD" ] = 'pymongo'
190
204
self .app .config ['CUSTOM_MONGO_DBNAME' ] = 'test_db'
@@ -196,10 +210,10 @@ def test_missing_auth_mechanism_in_prefixed_config(self):
196
210
if pymongo .version_tuple [0 ] > 2 :
197
211
time .sleep (0.2 )
198
212
199
- assert ('localhost' , 27017 ) == mongo .cx .address
213
+ assert ('localhost' , self . port ) == mongo .cx .address
200
214
else :
201
215
assert mongo .cx .host == 'localhost'
202
- assert mongo .cx .port == 27017
216
+ assert mongo .cx .port == self . port
203
217
204
218
205
219
@@ -218,6 +232,7 @@ def test_create_with_document_class(self):
218
232
# copying standard DBNAME, so this DB gets also deleted by tearDown
219
233
self .app .config ['CUSTOM_DBNAME' ] = self .app .config ['MONGO_DBNAME' ]
220
234
self .app .config ['CUSTOM_DOCUMENT_CLASS' ] = CustomDict
235
+ self .app .config ['CUSTOM_PORT' ] = self .port
221
236
# not using self.mongo, because we want to use updated config
222
237
# also using CUSTOM, to avoid duplicate config_prefix exception
223
238
mongo = flask_pymongo .PyMongo (self .app , 'CUSTOM' )
0 commit comments