Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_db_params_from_environment_not_set(monkeypatch):
"""
Test missing db params from environment settings.
"""
# Arrange
monkeypatch.delenv('TEST_DBTYPE', raising=False)
# Act
with pytest.raises(ETLHelperDbParamsError,
match=r".*environment variable is not set.*"):
DbParams.from_environment(prefix='TEST_')
def test_db_params_validate_params():
with pytest.raises(ETLHelperDbParamsError, match=r'.* not in valid types .*'):
DbParams(dbtype='not valid')
def from_environment(cls, prefix='ETLHelper_'):
"""
Create DbParams object from parameters specified by environment
variables e.g. ETLHelper_dbtype, ETLHelper_host, ETLHelper_port, etc.
:param prefix: str, prefix to environment variable names
"""
dbparams_keys = [key for key in os.environ if key.startswith(prefix)]
dbparams_from_env = {key.replace(prefix, '').lower(): os.environ[key]
for key in dbparams_keys}
# Ensure dbtype has been set
dbtype_var = f'{prefix}dbtype'
if 'dbtype' not in dbparams_from_env:
msg = f"{dbtype_var} environment variable is not set"
raise ETLHelperDbParamsError(msg)
return cls(**dbparams_from_env)
:raises ETLHelperParamsError: Error if params are invalid
"""
# Get a set of the attributes to compare against required attributes.
given = set(self.keys())
try:
required_params = DB_HELPER_FACTORY.from_dbtype(self.dbtype).required_params
except ETLHelperHelperError:
msg = f'{self.dbtype} not in valid types ({DB_HELPER_FACTORY.helpers.keys()})'
raise ETLHelperDbParamsError(msg)
unset_params = (given ^ required_params) & required_params
if unset_params:
msg = f'{unset_params} not set. Required parameters are {required_params}'
raise ETLHelperDbParamsError(msg)