def test_using_options(self): """Test parsing of connection strings using options. """ # Parse connection using an invalid options object. # Only a warning is logged and no options are used. cnx_str = 'myuser@localhost:3333' expected = {'user': '******', 'host': 'localhost', 'port': 3333} result = parse_connection(cnx_str, options=[]) self.assertEqual( result, expected, "Connection string '{0}' parsed to {1}, " "expected {2}".format(cnx_str, result, expected)) # Parse connection using options (valid dictionary). cnx_str = 'myuser@localhost:3306' test_opts = { 'charset': 'test-charset', 'ssl_cert': 'test-cert', 'ssl_ca': 'test-ca', 'ssl_key': 'test-key', 'ssl': 'test-ssl', } expected = {'user': '******', 'host': 'localhost', 'port': 3306} expected.update(test_opts) result = parse_connection(cnx_str, options=test_opts) self.assertEqual( result, expected, "Connection string '{0}' parsed to {1}, " "expected {2}".format(cnx_str, result, expected))
def test_using_options(self): """Test parsing of connection strings using options. """ # Parse connection using an invalid options object. # Only a warning is logged and no options are used. cnx_str = 'myuser@localhost:3333' expected = {'user': '******', 'host': 'localhost', 'port': 3333} result = parse_connection(cnx_str, options=[]) self.assertEqual(result, expected, "Connection string '{0}' parsed to {1}, " "expected {2}".format(cnx_str, result, expected)) # Parse connection using options (valid dictionary). cnx_str = 'myuser@localhost:3306' test_opts = { 'charset': 'test-charset', 'ssl_cert': 'test-cert', 'ssl_ca': 'test-ca', 'ssl_key': 'test-key', 'ssl': 'test-ssl', } expected = {'user': '******', 'host': 'localhost', 'port': 3306} expected.update(test_opts) result = parse_connection(cnx_str, options=test_opts) self.assertEqual(result, expected, "Connection string '{0}' parsed to {1}, " "expected {2}".format(cnx_str, result, expected))
def test_invalid(self): """Test parsing of invalid connection strings. A GadgetCnxFormatError should be raised if the connection string is invalid. """ for cnx_str in self.invalid_specificers: # Expect GadgetCnxFormatError: Error parsing connection string. with self.assertRaises(GadgetCnxFormatError): parse_connection(cnx_str)
def test_valid(self): """Test parsing of valid connection strings. """ for source, expected in self.valid_specifiers: result = _cnx_dict_to_str(parse_connection(source)) self.assertEqual( expected, result, u"Connection string '{0}' parsed to {1}, " u"expected {2}".format(source, result, expected)) parse_connection('myuser@localhost:3306', options=[])
def test_valid(self): """Test parsing of valid connection strings. """ for source, expected in self.valid_specifiers: result = _cnx_dict_to_str(parse_connection(source)) self.assertEqual(expected, result, u"Connection string '{0}' parsed to {1}, " u"expected {2}".format(source, result, expected)) parse_connection('myuser@localhost:3306', options=[])
def __call__(self, parser_, namespace, values, option_string=None): try: param_dict = parse_connection(values) # Retrieve the set of already parsed connection strings and # check throw an error if the connection value is repeated. cache_name = "_{0}_cache".format(self.dest) # param_dict is a dictionary so it is not hashable. # we need a hashable representation of the connection parameters # to check if we've passed the same connection string more than # once. hashable_params = tuple(sorted(param_dict.items())) conn_list_cache = getattr(namespace, cache_name, set()) if hashable_params in conn_list_cache: parser_.error("Cannot pass the same connection value more " "than once: {0}".format(values)) conn_list_cache.add(hashable_params) setattr(namespace, cache_name, conn_list_cache) # Not a repeated value, so add it to the list of connections conn_list = getattr(namespace, self.dest, None) conn_list = [] if conn_list is None else conn_list conn_list.append(param_dict) setattr(namespace, self.dest, conn_list) if self.ask_pass: # get the existing list of passwords to ask password_list = getattr(namespace, _APPEND_PASSWORD_LIST, []) # add (argument_name, values ) tuple to the list password_list.append((self.dest, values)) setattr(namespace, _APPEND_PASSWORD_LIST, password_list) except GadgetCnxFormatError as e: parser_.error(e.errmsg)
def __call__(self, parser_, namespace, values, option_string=None): try: param_dict = parse_connection(values) setattr(namespace, self.dest, param_dict) if self.ask_pass: # get the existing list of passwords to ask password_list = getattr(namespace, _STORE_PASSWORD_LIST, []) # add (argument_name, values ) tuple to the list password_list.append((self.dest, values)) setattr(namespace, _STORE_PASSWORD_LIST, password_list) except GadgetCnxFormatError as e: parser_.error(e.errmsg)