def test_get_user_pw(caplog): env = {"USR": "******", "PW": "my_pass"} conf = { "user_var": "USR", "password_var": "PW", } with mock.patch.dict(os.environ, env): user, pw = r.get_user_pw(conf["user_var"], conf["password_var"]) assert user == env["USR"] assert pw == env["PW"] with mock.patch.dict(os.environ, {}): with pytest.raises(ValueError): r.get_user_pw(conf["user_var"], conf["password_var"]) env2 = {"USR": "******"} with mock.patch.dict(os.environ, env2): user, pw = r.get_user_pw(conf["user_var"], conf["password_var"]) assert user == env["USR"] assert pw is None assert "not set" in caplog.text.lower()
def _get_oracle_connection(dbms_config: Dict): import cx_Oracle # Importing here avoids environment-specific dependencies user, pw = get_user_pw(dbms_config["user_var"], dbms_config["password_var"]) dsn_tns = cx_Oracle.makedsn( dbms_config["host"], dbms_config["port"], service_name=dbms_config["service_name"], ) logger.debug("Oracle connection string: %s", dsn_tns) kw_options = dbms_config.get("options", {}) connection = cx_Oracle.connect(user, pw, dsn_tns, **kw_options) return connection
def __init__(self, identifier, datasource_config, dbms_config): super().__init__(identifier, datasource_config) self.dbms_config = dbms_config logger.info( "Establishing Records database connection for datasource {}...".format(self.id) ) # if "connect" not in dbms_config: # raise ValueError(f'No connection string (property "connect") in datasource {self.id} config') dbtype = dbms_config['type'] user, pw = get_user_pw(dbms_config) host = dbms_config['host'] port = ":" + str(dbms_config['port']) if 'port' in dbms_config else '' service_name = "/?service_name=" + dbms_config['service_name'] if 'service_name' in dbms_config else '' connection_string = f"{dbtype}://{user}:{pw}@{host}{port}{service_name}" self.db = records.Database(connection_string)
def __init__(self, identifier: str, datasource_config: Dict, dbms_config: Optional[Dict]): super().__init__(identifier, datasource_config) self.dbms_config = dbms_config logger.info( "Establishing Records database connection for datasource {}...".format( self.id ) ) # if "connect" not in dbms_config: # raise ValueError(f'No connection string (property "connect") in datasource {self.id} config') dbtype = dbms_config["type"] user, pw = get_user_pw(dbms_config["user_var"], dbms_config["password_var"]) host = dbms_config["host"] port = ":" + str(dbms_config["port"]) if "port" in dbms_config else "" service_name = ( "/?service_name=" + dbms_config["service_name"] if "service_name" in dbms_config else "" ) kwargs = dbms_config.get("options", {}) connection_string = f"{dbtype}://{user}:{pw}@{host}{port}{service_name}" self.db = records.Database(connection_string, **kwargs)