78
79
split_hosts('/tmp/mongodb-27017.sock'))
79
80
self.assertEqual([('/tmp/mongodb-27017.sock', None),
80
81
('example.com', 27017)],
81
split_hosts('/tmp/mongodb-27017.sock,'
82
split_hosts('/tmp/mongodb-27017.sock,'
83
84
self.assertEqual([('example.com', 27017),
84
85
('/tmp/mongodb-27017.sock', None)],
85
split_hosts('example.com:27017,'
86
'/tmp/mongodb-27017.sock'))
87
self.assertRaises(ConfigurationError, split_hosts, '::1', 27017)
88
self.assertRaises(ConfigurationError, split_hosts, '[::1:27017')
89
self.assertRaises(ConfigurationError, split_hosts, '::1')
90
self.assertRaises(ConfigurationError, split_hosts, '::1]:27017')
86
split_hosts('example.com:27017,'
87
'/tmp/mongodb-27017.sock'))
88
self.assertRaises(ValueError, split_hosts, '::1', 27017)
89
self.assertRaises(ValueError, split_hosts, '[::1:27017')
90
self.assertRaises(ValueError, split_hosts, '::1')
91
self.assertRaises(ValueError, split_hosts, '::1]:27017')
91
92
self.assertEqual([('::1', 27017)], split_hosts('[::1]:27017'))
92
93
self.assertEqual([('::1', 27017)], split_hosts('[::1]'))
95
96
self.assertRaises(ConfigurationError, split_options, 'foo')
96
97
self.assertRaises(ConfigurationError, split_options, 'foo=bar')
97
98
self.assertRaises(ConfigurationError, split_options, 'foo=bar;foo')
98
self.assertRaises(ConfigurationError, split_options, 'socketTimeoutMS=foo')
99
self.assertRaises(ConfigurationError, split_options, 'socketTimeoutMS=0.0')
100
self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=foo')
101
self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=0.0')
102
self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=1e100000')
103
self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=-1e100000')
99
self.assertRaises(ValueError, split_options, 'socketTimeoutMS=foo')
100
self.assertRaises(ValueError, split_options, 'socketTimeoutMS=0.0')
101
self.assertRaises(ValueError, split_options, 'connectTimeoutMS=foo')
102
self.assertRaises(ValueError, split_options, 'connectTimeoutMS=0.0')
103
self.assertRaises(ValueError, split_options, 'connectTimeoutMS=1e100000')
104
self.assertRaises(ValueError, split_options, 'connectTimeoutMS=-1e100000')
105
106
# On most platforms float('inf') and float('-inf') represent
106
107
# +/- infinity, although on Python 2.4 and 2.5 on Windows those
107
108
# expressions are invalid
108
109
if not (sys.platform == "win32" and sys.version_info <= (2, 5)):
109
self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=inf')
110
self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=-inf')
110
self.assertRaises(ValueError, split_options, 'connectTimeoutMS=inf')
111
self.assertRaises(ValueError, split_options, 'connectTimeoutMS=-inf')
112
113
self.assertTrue(split_options('socketTimeoutMS=300'))
113
114
self.assertTrue(split_options('connectTimeoutMS=300'))
117
118
self.assertEqual({'connecttimeoutms': 0.0001}, split_options('connectTimeoutMS=0.1'))
118
119
self.assertTrue(split_options('connectTimeoutMS=300'))
119
120
self.assertTrue(isinstance(split_options('w=5')['w'], int))
120
self.assertTrue(isinstance(split_options('w=5.5')['w'], basestring))
121
self.assertTrue(isinstance(split_options('w=5.5')['w'], string_type))
121
122
self.assertTrue(split_options('w=foo'))
122
123
self.assertTrue(split_options('w=majority'))
123
self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=foo')
124
self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=5.5')
124
self.assertRaises(ValueError, split_options, 'wtimeoutms=foo')
125
self.assertRaises(ValueError, split_options, 'wtimeoutms=5.5')
125
126
self.assertTrue(split_options('wtimeoutms=500'))
126
self.assertRaises(ConfigurationError, split_options, 'fsync=foo')
127
self.assertRaises(ConfigurationError, split_options, 'fsync=5.5')
127
self.assertRaises(ValueError, split_options, 'fsync=foo')
128
self.assertRaises(ValueError, split_options, 'fsync=5.5')
128
129
self.assertEqual({'fsync': True}, split_options('fsync=true'))
129
130
self.assertEqual({'fsync': False}, split_options('fsync=false'))
130
131
self.assertEqual({'authmechanism': 'GSSAPI'},
131
132
split_options('authMechanism=GSSAPI'))
132
133
self.assertEqual({'authmechanism': 'MONGODB-CR'},
133
134
split_options('authMechanism=MONGODB-CR'))
135
self.assertEqual({'authmechanism': 'SCRAM-SHA-1'},
136
split_options('authMechanism=SCRAM-SHA-1'))
137
self.assertRaises(ValueError,
138
split_options, 'authMechanism=foo')
134
139
self.assertEqual({'authsource': 'foobar'}, split_options('authSource=foobar'))
135
# maxPoolSize isn't yet a documented URI option.
136
self.assertRaises(ConfigurationError, split_options, 'maxpoolsize=50')
140
self.assertEqual({'maxpoolsize': 50}, split_options('maxpoolsize=50'))
138
142
def test_parse_uri(self):
139
143
self.assertRaises(InvalidURI, parse_uri, "http://foobar.com")
140
144
self.assertRaises(InvalidURI, parse_uri, "http://foo@foobar.com")
141
self.assertRaises(ConfigurationError,
145
self.assertRaises(ValueError,
142
146
parse_uri, "mongodb://::1", 27017)
197
201
":27017/test.yield_historical.in"))
199
203
res = copy.deepcopy(orig)
200
res['nodelist'] = [("::1", 27017)]
201
res['options'] = {'slaveok': True}
202
self.assertEqual(res, parse_uri("mongodb://[::1]:27017/?slaveOk=true"))
204
res = copy.deepcopy(orig)
205
res['nodelist'] = [("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 27017)]
206
res['options'] = {'slaveok': True}
207
self.assertEqual(res, parse_uri(
208
"mongodb://[2001:0db8:85a3:0000:0000"
209
":8a2e:0370:7334]:27017/?slaveOk=true"))
211
res = copy.deepcopy(orig)
212
204
res['nodelist'] = [("/tmp/mongodb-27017.sock", None)]
213
205
self.assertEqual(res, parse_uri("mongodb:///tmp/mongodb-27017.sock"))
274
266
res = copy.deepcopy(orig)
275
267
res.update({'username': 'fred', 'password': 'foobar'})
276
268
res.update({'database': 'test', 'collection': 'yield_historical.in'})
277
res['options'] = {'slaveok': True}
278
269
self.assertEqual(res,
279
270
parse_uri("mongodb://fred:foobar@localhost/"
280
"test.yield_historical.in?slaveok=true"))
271
"test.yield_historical.in"))
282
273
res = copy.deepcopy(orig)
283
res['options'] = {'readpreference': ReadPreference.SECONDARY}
274
res['options'] = {'readpreference': ReadPreference.SECONDARY.mode}
284
275
self.assertEqual(res,
285
276
parse_uri("mongodb://localhost/?readPreference=secondary"))
337
328
"@localhost/foo?authMechanism=GSSAPI"))
339
330
res = copy.deepcopy(orig)
340
res['options'] = {'readpreference': ReadPreference.SECONDARY,
331
res['options'] = {'readpreference': ReadPreference.SECONDARY.mode,
341
332
'readpreferencetags': [
342
333
{'dc': 'west', 'use': 'website'},
343
334
{'dc': 'east', 'use': 'website'}]}
351
342
"readpreferencetags=dc:east,use:website"))
353
344
res = copy.deepcopy(orig)
354
res['options'] = {'readpreference': ReadPreference.SECONDARY,
345
res['options'] = {'readpreference': ReadPreference.SECONDARY.mode,
355
346
'readpreferencetags': [
356
347
{'dc': 'west', 'use': 'website'},
357
348
{'dc': 'east', 'use': 'website'},
384
375
# Ensure parsing a unicode returns option names that can be passed
385
376
# as kwargs. In Python 2.4, keyword argument names must be ASCII.
386
377
# In all Pythons, str is the type of valid keyword arg names.
387
res = parse_uri(unicode("mongodb://localhost/?fsync=true"))
378
res = parse_uri(_unicode("mongodb://localhost/?fsync=true"))
388
379
for key in res['options']:
389
380
self.assertTrue(isinstance(key, str))