def test_split_options(self): # This should remind us to write tests for these if # they are ever supported. self.assertRaises(UnsupportedOption, split_options, 'minPoolSize=5') self.assertRaises(UnsupportedOption, split_options, 'waitQueueTimeoutMS=500') self.assertRaises(UnsupportedOption, split_options, 'waitQueueMultiple=5') self.assertRaises(UnsupportedOption, split_options, 'connectTimeoutMS=500') self.assertRaises(UnsupportedOption, split_options, 'socketTimeoutMS=500') self.assertRaises(ConfigurationError, split_options, 'foo') self.assertRaises(ConfigurationError, split_options, 'foo=bar') self.assertRaises(ConfigurationError, split_options, 'foo=bar;foo') self.assertRaises(ConfigurationError, split_options, 'w=foo') self.assertRaises(ConfigurationError, split_options, 'w=5.5') self.assert_(split_options, 'w=5') self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=foo') self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=5.5') self.assert_(split_options, 'wtimeoutms=500') self.assertRaises(ConfigurationError, split_options, 'fsync=foo') self.assertRaises(ConfigurationError, split_options, 'fsync=5.5') self.assertEqual({'fsync': True}, split_options('fsync=true')) self.assertEqual({'fsync': False}, split_options('fsync=false')) self.assertRaises(ConfigurationError, split_options, 'maxpoolsize=foo') self.assertRaises(ConfigurationError, split_options, 'maxpoolsize=5.5') self.assert_(split_options, 'maxpoolsize=50')
def test_split_options(self): self.assertRaises(ConfigurationError, split_options, 'foo') self.assertRaises(ConfigurationError, split_options, 'foo=bar') self.assertRaises(ConfigurationError, split_options, 'foo=bar;foo') self.assertRaises(ConfigurationError, split_options, 'socketTimeoutMS=foo') self.assertRaises(ConfigurationError, split_options, 'socketTimeoutMS=0.0') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=foo') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=0.0') self.assert_(split_options, 'socketTimeoutMS=300') self.assert_(split_options, 'connectTimeoutMS=300') self.assertEquals({'sockettimeoutms': 0.3}, split_options('socketTimeoutMS=300')) self.assertEquals({'sockettimeoutms': 0.0001}, split_options('socketTimeoutMS=0.1')) self.assertEquals({'connecttimeoutms': 0.3}, split_options('connectTimeoutMS=300')) self.assertEquals({'connecttimeoutms': 0.0001}, split_options('connectTimeoutMS=0.1')) self.assert_(split_options, 'connectTimeoutMS=300') self.assertTrue(isinstance(split_options('w=5')['w'], int)) self.assertTrue(isinstance(split_options('w=5.5')['w'], basestring)) self.assert_(split_options, 'w=foo') self.assert_(split_options, 'w=majority') self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=foo') self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=5.5') self.assert_(split_options, 'wtimeoutms=500') self.assertRaises(ConfigurationError, split_options, 'fsync=foo') self.assertRaises(ConfigurationError, split_options, 'fsync=5.5') self.assertEqual({'fsync': True}, split_options('fsync=true')) self.assertEqual({'fsync': False}, split_options('fsync=false')) self.assertRaises(ConfigurationError, split_options, 'maxpoolsize=foo') self.assertRaises(ConfigurationError, split_options, 'maxpoolsize=5.5') self.assert_(split_options, 'maxpoolsize=50')
def test_split_options(self): # This should remind us to write tests for these if # they are ever supported. self.assertRaises(UnsupportedOption, split_options, "connectTimeoutMS=500") self.assertRaises(UnsupportedOption, split_options, "socketTimeoutMS=500") self.assertRaises(ConfigurationError, split_options, "foo") self.assertRaises(ConfigurationError, split_options, "foo=bar") self.assertRaises(ConfigurationError, split_options, "foo=bar;foo") self.assertTrue(isinstance(split_options("w=5")["w"], int)) self.assertTrue(isinstance(split_options("w=5.5")["w"], basestring)) self.assert_(split_options, "w=foo") self.assert_(split_options, "w=majority") self.assertRaises(ConfigurationError, split_options, "wtimeoutms=foo") self.assertRaises(ConfigurationError, split_options, "wtimeoutms=5.5") self.assert_(split_options, "wtimeoutms=500") self.assertRaises(ConfigurationError, split_options, "fsync=foo") self.assertRaises(ConfigurationError, split_options, "fsync=5.5") self.assertEqual({"fsync": True}, split_options("fsync=true")) self.assertEqual({"fsync": False}, split_options("fsync=false")) self.assertRaises(ConfigurationError, split_options, "maxpoolsize=foo") self.assertRaises(ConfigurationError, split_options, "maxpoolsize=5.5") self.assert_(split_options, "maxpoolsize=50")
def test_split_options(self): self.assertRaises(ConfigurationError, split_options, 'foo') self.assertRaises(ConfigurationError, split_options, 'foo=bar') self.assertRaises(ConfigurationError, split_options, 'foo=bar;foo') self.assertRaises(ConfigurationError, split_options, 'socketTimeoutMS=foo') self.assertRaises(ConfigurationError, split_options, 'socketTimeoutMS=0.0') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=foo') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=0.0') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=1e100000') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=-1e100000') # On most platforms float('inf') and float('-inf') represent # +/- infinity, although on Python 2.4 and 2.5 on Windows those # expressions are invalid if not (sys.platform == "win32" and sys.version_info <= (2, 5)): self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=inf') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=-inf') self.assertTrue(split_options('socketTimeoutMS=300')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.3}, split_options('socketTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.0001}, split_options('socketTimeoutMS=0.1')) self.assertEqual({'connecttimeoutms': 0.3}, split_options('connectTimeoutMS=300')) self.assertEqual({'connecttimeoutms': 0.0001}, split_options('connectTimeoutMS=0.1')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertTrue(isinstance(split_options('w=5')['w'], int)) self.assertTrue(isinstance(split_options('w=5.5')['w'], basestring)) self.assertTrue(split_options('w=foo')) self.assertTrue(split_options('w=majority')) self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=foo') self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=5.5') self.assertTrue(split_options('wtimeoutms=500')) self.assertRaises(ConfigurationError, split_options, 'fsync=foo') self.assertRaises(ConfigurationError, split_options, 'fsync=5.5') self.assertEqual({'fsync': True}, split_options('fsync=true')) self.assertEqual({'fsync': False}, split_options('fsync=false')) self.assertEqual({'authmechanism': 'GSSAPI'}, split_options('authMechanism=GSSAPI')) self.assertEqual({'authmechanism': 'MONGODB-CR'}, split_options('authMechanism=MONGODB-CR')) self.assertEqual({'authmechanism': 'SCRAM-SHA-1'}, split_options('authMechanism=SCRAM-SHA-1')) self.assertRaises(ConfigurationError, split_options, 'authMechanism=foo') self.assertEqual({'authsource': 'foobar'}, split_options('authSource=foobar')) self.assertEqual({'maxpoolsize': 50}, split_options('maxpoolsize=50')) self.assertEqual({'localthresholdms': 300}, split_options('localThresholdMS=300'))
def test_split_options(self): self.assertRaises(ConfigurationError, split_options, 'foo') self.assertRaises(ConfigurationError, split_options, 'foo=bar;foo') self.assertTrue(split_options('ssl=true')) self.assertTrue(split_options('connect=true')) self.assertTrue(split_options('ssl_match_hostname=true')) # Test Invalid URI options that should throw warnings. with warnings.catch_warnings(): warnings.filterwarnings('error') self.assertRaises(Warning, split_options, 'foo=bar', warn=True) self.assertRaises(Warning, split_options, 'socketTimeoutMS=foo', warn=True) self.assertRaises(Warning, split_options, 'socketTimeoutMS=0.0', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=foo', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=0.0', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=1e100000', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=-1e100000', warn=True) self.assertRaises(Warning, split_options, 'ssl=foo', warn=True) self.assertRaises(Warning, split_options, 'connect=foo', warn=True) self.assertRaises(Warning, split_options, 'ssl_match_hostname=foo', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=inf', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=-inf', warn=True) self.assertRaises(Warning, split_options, 'wtimeoutms=foo', warn=True) self.assertRaises(Warning, split_options, 'wtimeoutms=5.5', warn=True) self.assertRaises(Warning, split_options, 'fsync=foo', warn=True) self.assertRaises(Warning, split_options, 'fsync=5.5', warn=True) self.assertRaises(Warning, split_options, 'authMechanism=foo', warn=True) # Test invalid options with warn=False. self.assertRaises(ConfigurationError, split_options, 'foo=bar') self.assertRaises(ValueError, split_options, 'socketTimeoutMS=foo') self.assertRaises(ValueError, split_options, 'socketTimeoutMS=0.0') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=foo') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=0.0') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=1e100000') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=-1e100000') self.assertRaises(ValueError, split_options, 'ssl=foo') self.assertRaises(ValueError, split_options, 'connect=foo') self.assertRaises(ValueError, split_options, 'ssl_match_hostname=foo') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=inf') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=-inf') self.assertRaises(ValueError, split_options, 'wtimeoutms=foo') self.assertRaises(ValueError, split_options, 'wtimeoutms=5.5') self.assertRaises(ValueError, split_options, 'fsync=foo') self.assertRaises(ValueError, split_options, 'fsync=5.5') self.assertRaises(ValueError, split_options, 'authMechanism=foo') # Test splitting options works when valid. self.assertTrue(split_options('socketTimeoutMS=300')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.3}, split_options('socketTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.0001}, split_options('socketTimeoutMS=0.1')) self.assertEqual({'connecttimeoutms': 0.3}, split_options('connectTimeoutMS=300')) self.assertEqual({'connecttimeoutms': 0.0001}, split_options('connectTimeoutMS=0.1')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertTrue(isinstance(split_options('w=5')['w'], int)) self.assertTrue(isinstance(split_options('w=5.5')['w'], string_type)) self.assertTrue(split_options('w=foo')) self.assertTrue(split_options('w=majority')) self.assertTrue(split_options('wtimeoutms=500')) self.assertEqual({'fsync': True}, split_options('fsync=true')) self.assertEqual({'fsync': False}, split_options('fsync=false')) self.assertEqual({'authmechanism': 'GSSAPI'}, split_options('authMechanism=GSSAPI')) self.assertEqual({'authmechanism': 'MONGODB-CR'}, split_options('authMechanism=MONGODB-CR')) self.assertEqual({'authmechanism': 'SCRAM-SHA-1'}, split_options('authMechanism=SCRAM-SHA-1')) self.assertEqual({'authsource': 'foobar'}, split_options('authSource=foobar')) self.assertEqual({'maxpoolsize': 50}, split_options('maxpoolsize=50'))
def test_split_options(self): self.assertRaises(ConfigurationError, split_options, 'foo') self.assertRaises(ConfigurationError, split_options, 'foo=bar') self.assertRaises(ConfigurationError, split_options, 'foo=bar;foo') self.assertRaises(ConfigurationError, split_options, 'socketTimeoutMS=foo') self.assertRaises(ConfigurationError, split_options, 'socketTimeoutMS=0.0') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=foo') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=0.0') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=1e100000') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=-1e100000') # On most platforms float('inf') and float('-inf') represent # +/- infinity, although on Python 2.4 and 2.5 on Windows those # expressions are invalid if not (sys.platform == "win32" and sys.version_info <= (2, 5)): self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=inf') self.assertRaises(ConfigurationError, split_options, 'connectTimeoutMS=-inf') self.assertTrue(split_options('socketTimeoutMS=300')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.3}, split_options('socketTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.0001}, split_options('socketTimeoutMS=0.1')) self.assertEqual({'connecttimeoutms': 0.3}, split_options('connectTimeoutMS=300')) self.assertEqual({'connecttimeoutms': 0.0001}, split_options('connectTimeoutMS=0.1')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertTrue(isinstance(split_options('w=5')['w'], int)) self.assertTrue(isinstance(split_options('w=5.5')['w'], basestring)) self.assertTrue(split_options('w=foo')) self.assertTrue(split_options('w=majority')) self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=foo') self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=5.5') self.assertTrue(split_options('wtimeoutms=500')) self.assertRaises(ConfigurationError, split_options, 'fsync=foo') self.assertRaises(ConfigurationError, split_options, 'fsync=5.5') self.assertEqual({'fsync': True}, split_options('fsync=true')) self.assertEqual({'fsync': False}, split_options('fsync=false')) # maxPoolSize isn't yet a documented URI option. self.assertRaises(ConfigurationError, split_options, 'maxpoolsize=50')
def test_split_options(self): self.assertRaises(ConfigurationError, split_options, 'foo') self.assertRaises(ConfigurationError, split_options, 'foo=bar;foo') self.assertTrue(split_options('ssl=true')) self.assertTrue(split_options('connect=true')) self.assertTrue(split_options('ssl_match_hostname=true')) # Test Invalid URI options that should throw warnings. with warnings.catch_warnings(): warnings.filterwarnings('error') self.assertRaises(Warning, split_options, 'foo=bar', warn=True) self.assertRaises(Warning, split_options, 'socketTimeoutMS=foo', warn=True) self.assertRaises(Warning, split_options, 'socketTimeoutMS=0.0', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=foo', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=0.0', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=1e100000', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=-1e100000', warn=True) self.assertRaises(Warning, split_options, 'ssl=foo', warn=True) self.assertRaises(Warning, split_options, 'connect=foo', warn=True) self.assertRaises(Warning, split_options, 'ssl_match_hostname=foo', warn=True) # On most platforms float('inf') and float('-inf') represent # +/- infinity, although on Python 2.4 and 2.5 on Windows those # expressions are invalid if not (sys.platform == "win32" and sys.version_info <= (2, 5)): self.assertRaises(Warning, split_options, 'connectTimeoutMS=inf', warn=True) self.assertRaises(Warning, split_options, 'connectTimeoutMS=-inf', warn=True) self.assertRaises(Warning, split_options, 'wtimeoutms=foo', warn=True) self.assertRaises(Warning, split_options, 'wtimeoutms=5.5', warn=True) self.assertRaises(Warning, split_options, 'fsync=foo', warn=True) self.assertRaises(Warning, split_options, 'fsync=5.5', warn=True) self.assertRaises(Warning, split_options, 'authMechanism=foo', warn=True) # Test invalid options with warn=False. self.assertRaises(ConfigurationError, split_options, 'foo=bar') self.assertRaises(ValueError, split_options, 'socketTimeoutMS=foo') self.assertRaises(ValueError, split_options, 'socketTimeoutMS=0.0') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=foo') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=0.0') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=1e100000') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=-1e100000') self.assertRaises(ValueError, split_options, 'ssl=foo') self.assertRaises(ValueError, split_options, 'connect=foo') self.assertRaises(ValueError, split_options, 'ssl_match_hostname=foo') if not (sys.platform == "win32" and sys.version_info <= (2, 5)): self.assertRaises(ValueError, split_options, 'connectTimeoutMS=inf') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=-inf') self.assertRaises(ValueError, split_options, 'wtimeoutms=foo') self.assertRaises(ValueError, split_options, 'wtimeoutms=5.5') self.assertRaises(ValueError, split_options, 'fsync=foo') self.assertRaises(ValueError, split_options, 'fsync=5.5') self.assertRaises(ValueError, split_options, 'authMechanism=foo') # Test splitting options works when valid. self.assertTrue(split_options('socketTimeoutMS=300')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.3}, split_options('socketTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.0001}, split_options('socketTimeoutMS=0.1')) self.assertEqual({'connecttimeoutms': 0.3}, split_options('connectTimeoutMS=300')) self.assertEqual({'connecttimeoutms': 0.0001}, split_options('connectTimeoutMS=0.1')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertTrue(isinstance(split_options('w=5')['w'], int)) self.assertTrue(isinstance(split_options('w=5.5')['w'], string_type)) self.assertTrue(split_options('w=foo')) self.assertTrue(split_options('w=majority')) self.assertTrue(split_options('wtimeoutms=500')) self.assertEqual({'fsync': True}, split_options('fsync=true')) self.assertEqual({'fsync': False}, split_options('fsync=false')) self.assertEqual({'authmechanism': 'GSSAPI'}, split_options('authMechanism=GSSAPI')) self.assertEqual({'authmechanism': 'MONGODB-CR'}, split_options('authMechanism=MONGODB-CR')) self.assertEqual({'authmechanism': 'SCRAM-SHA-1'}, split_options('authMechanism=SCRAM-SHA-1')) self.assertEqual({'authsource': 'foobar'}, split_options('authSource=foobar')) self.assertEqual({'maxpoolsize': 50}, split_options('maxpoolsize=50'))
def extract_db_config(uri: str): """ extract username, password and host with port from mongo uri :param uri: mongo uri :return: username, password, scheme, hosts """ user = None passwd = None dbase = None collection = None options = _CaseInsensitiveDictionary() is_mock = False if uri.startswith("mongomock://"): uri = uri.replace("mongomock://", "mongodb://", 1) is_mock = True if uri.startswith(SCHEME): scheme_free = uri[SCHEME_LEN:] scheme = uri[:SCHEME_LEN] elif uri.startswith(SRV_SCHEME): scheme_free = uri[SRV_SCHEME_LEN:] scheme = uri[:SRV_SCHEME_LEN] else: raise InvalidURI("Invalid URI scheme: URI must " "begin with '%s' or '%s'" % (SCHEME, SRV_SCHEME)) if not scheme_free: raise InvalidURI("Must provide at least one hostname or IP.") host_part, _, _ = scheme_free.partition("/") host_part, _, path_part = scheme_free.partition("/") if not host_part: host_part = path_part path_part = "" if not path_part and '?' in host_part: raise InvalidURI("A '/' is required between " "the host list and any options.") if path_part: dbase, _, opts = path_part.partition('?') if dbase: dbase = unquote_plus(dbase) if '.' in dbase: dbase, collection = dbase.split('.', 1) if _BAD_DB_CHARS.search(dbase): raise InvalidURI('Bad database name "%s"' % dbase) else: dbase = None if opts: options.update(split_options(opts, True, False, True)) if "@" in host_part: userinfo, _, hosts = host_part.rpartition("@") user, passwd = parse_userinfo(userinfo) hosts = scheme + hosts else: hosts = scheme + host_part settings = { "username": user, "password": passwd, "host": hosts, "db": dbase, "options": options, "collection": collection } if is_mock: settings['is_mock'] = is_mock return settings
def test_split_options(self): self.assertRaises(ConfigurationError, split_options, "foo") self.assertRaises(ConfigurationError, split_options, "foo=bar") self.assertRaises(ConfigurationError, split_options, "foo=bar;foo") self.assertRaises(ConfigurationError, split_options, "socketTimeoutMS=foo") self.assertRaises(ConfigurationError, split_options, "socketTimeoutMS=0.0") self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=foo") self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=0.0") self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=1e100000") self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=-1e100000") # On most platforms float('inf') and float('-inf') represent # +/- infinity, although on Python 2.4 and 2.5 on Windows those # expressions are invalid if not (sys.platform == "win32" and sys.version_info <= (2, 5)): self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=inf") self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=-inf") self.assertTrue(split_options("socketTimeoutMS=300")) self.assertTrue(split_options("connectTimeoutMS=300")) self.assertEqual({"sockettimeoutms": 0.3}, split_options("socketTimeoutMS=300")) self.assertEqual({"sockettimeoutms": 0.0001}, split_options("socketTimeoutMS=0.1")) self.assertEqual({"connecttimeoutms": 0.3}, split_options("connectTimeoutMS=300")) self.assertEqual({"connecttimeoutms": 0.0001}, split_options("connectTimeoutMS=0.1")) self.assertTrue(split_options("connectTimeoutMS=300")) self.assertTrue(isinstance(split_options("w=5")["w"], int)) self.assertTrue(isinstance(split_options("w=5.5")["w"], basestring)) self.assertTrue(split_options("w=foo")) self.assertTrue(split_options("w=majority")) self.assertRaises(ConfigurationError, split_options, "wtimeoutms=foo") self.assertRaises(ConfigurationError, split_options, "wtimeoutms=5.5") self.assertTrue(split_options("wtimeoutms=500")) self.assertRaises(ConfigurationError, split_options, "fsync=foo") self.assertRaises(ConfigurationError, split_options, "fsync=5.5") self.assertEqual({"fsync": True}, split_options("fsync=true")) self.assertEqual({"fsync": False}, split_options("fsync=false")) # maxPoolSize isn't yet a documented URI option. self.assertRaises(ConfigurationError, split_options, "maxpoolsize=50")
def test_split_options(self): self.assertRaises(ConfigurationError, split_options, 'foo') self.assertRaises(ConfigurationError, split_options, 'foo=bar') self.assertRaises(ConfigurationError, split_options, 'foo=bar;foo') self.assertRaises(ValueError, split_options, 'socketTimeoutMS=foo') self.assertRaises(ValueError, split_options, 'socketTimeoutMS=0.0') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=foo') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=0.0') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=1e100000') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=-1e100000') # On most platforms float('inf') and float('-inf') represent # +/- infinity, although on Python 2.4 and 2.5 on Windows those # expressions are invalid if not (sys.platform == "win32" and sys.version_info <= (2, 5)): self.assertRaises(ValueError, split_options, 'connectTimeoutMS=inf') self.assertRaises(ValueError, split_options, 'connectTimeoutMS=-inf') self.assertTrue(split_options('socketTimeoutMS=300')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.3}, split_options('socketTimeoutMS=300')) self.assertEqual({'sockettimeoutms': 0.0001}, split_options('socketTimeoutMS=0.1')) self.assertEqual({'connecttimeoutms': 0.3}, split_options('connectTimeoutMS=300')) self.assertEqual({'connecttimeoutms': 0.0001}, split_options('connectTimeoutMS=0.1')) self.assertTrue(split_options('connectTimeoutMS=300')) self.assertTrue(isinstance(split_options('w=5')['w'], int)) self.assertTrue(isinstance(split_options('w=5.5')['w'], string_type)) self.assertTrue(split_options('w=foo')) self.assertTrue(split_options('w=majority')) self.assertRaises(ValueError, split_options, 'wtimeoutms=foo') self.assertRaises(ValueError, split_options, 'wtimeoutms=5.5') self.assertTrue(split_options('wtimeoutms=500')) self.assertRaises(ValueError, split_options, 'fsync=foo') self.assertRaises(ValueError, split_options, 'fsync=5.5') self.assertEqual({'fsync': True}, split_options('fsync=true')) self.assertEqual({'fsync': False}, split_options('fsync=false')) self.assertEqual({'authmechanism': 'GSSAPI'}, split_options('authMechanism=GSSAPI')) self.assertEqual({'authmechanism': 'MONGODB-CR'}, split_options('authMechanism=MONGODB-CR')) self.assertEqual({'authmechanism': 'SCRAM-SHA-1'}, split_options('authMechanism=SCRAM-SHA-1')) self.assertRaises(ValueError, split_options, 'authMechanism=foo') self.assertEqual({'authsource': 'foobar'}, split_options('authSource=foobar')) self.assertEqual({'maxpoolsize': 50}, split_options('maxpoolsize=50'))
def test_split_options(self): self.assertRaises(ConfigurationError, split_options, "foo") self.assertRaises(ConfigurationError, split_options, "foo=bar") self.assertRaises(ConfigurationError, split_options, "foo=bar;foo") self.assertRaises(ConfigurationError, split_options, "socketTimeoutMS=foo") self.assertRaises(ConfigurationError, split_options, "socketTimeoutMS=0.0") self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=foo") self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=0.0") self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=inf") self.assertRaises(ConfigurationError, split_options, "connectTimeoutMS=-inf") self.assertTrue(split_options("socketTimeoutMS=300")) self.assertTrue(split_options("connectTimeoutMS=300")) self.assertEqual({"sockettimeoutms": 0.3}, split_options("socketTimeoutMS=300")) self.assertEqual({"sockettimeoutms": 0.0001}, split_options("socketTimeoutMS=0.1")) self.assertEqual({"connecttimeoutms": 0.3}, split_options("connectTimeoutMS=300")) self.assertEqual({"connecttimeoutms": 0.0001}, split_options("connectTimeoutMS=0.1")) self.assertTrue(split_options("connectTimeoutMS=300")) self.assertTrue(isinstance(split_options("w=5")["w"], int)) self.assertTrue(isinstance(split_options("w=5.5")["w"], basestring)) self.assertTrue(split_options("w=foo")) self.assertTrue(split_options("w=majority")) self.assertRaises(ConfigurationError, split_options, "wtimeoutms=foo") self.assertRaises(ConfigurationError, split_options, "wtimeoutms=5.5") self.assertTrue(split_options("wtimeoutms=500")) self.assertRaises(ConfigurationError, split_options, "fsync=foo") self.assertRaises(ConfigurationError, split_options, "fsync=5.5") self.assertEqual({"fsync": True}, split_options("fsync=true")) self.assertEqual({"fsync": False}, split_options("fsync=false")) # maxPoolSize isn't yet a documented URI option. self.assertRaises(ConfigurationError, split_options, "maxpoolsize=50")