~ubuntu-branches/ubuntu/wily/pymongo/wily-proposed

« back to all changes in this revision

Viewing changes to test/test_uri_parser.py

  • Committer: Package Import Robot
  • Author(s): Federico Ceratto
  • Date: 2015-04-26 22:43:13 UTC
  • mfrom: (24.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20150426224313-0hga2jphvf0rrmfe
Tags: 3.0.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2011-2014 MongoDB, Inc.
 
1
# Copyright 2011-2015 MongoDB, Inc.
2
2
#
3
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
4
# you may not use this file except in compliance with the License.
15
15
"""Test the pymongo uri_parser module."""
16
16
 
17
17
import copy
18
 
import unittest
19
18
import sys
20
19
 
21
20
sys.path[0:0] = [""]
29
28
from pymongo.errors import ConfigurationError, InvalidURI
30
29
from pymongo import ReadPreference
31
30
from bson.binary import JAVA_LEGACY
 
31
from bson.py3compat import string_type, _unicode
 
32
from test import unittest
32
33
 
33
34
 
34
35
class TestURI(unittest.TestCase):
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
 
                                    'example.com:27017'))
 
82
                         split_hosts('/tmp/mongodb-27017.sock,'
 
83
                                     'example.com:27017'))
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]'))
93
94
 
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')
104
105
 
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')
111
112
 
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'))
137
141
 
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)
143
147
 
144
148
        orig = {
197
201
                                   ":27017/test.yield_historical.in"))
198
202
 
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"))
203
 
 
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"))
210
 
 
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"))
214
206
 
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"))
281
272
 
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"))
286
277
 
337
328
                                   "@localhost/foo?authMechanism=GSSAPI"))
338
329
 
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"))
352
343
 
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'},
376
367
                                   "@localhost/foo?uuidrepresentation="
377
368
                                   "javaLegacy"))
378
369
 
379
 
        self.assertRaises(ConfigurationError, parse_uri,
 
370
        self.assertRaises(ValueError, parse_uri,
380
371
                          "mongodb://user%40domain.com:password"
381
372
                          "@localhost/foo?uuidrepresentation=notAnOption")
382
373
 
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))
390
381