I use an external Database for some cases, e.g. collect values for Named Vocabularies. The connection values (host, user, etc... lives outside the addon in a secret.cfg as a part of buildout and differs between the enviroments (Test, Dev, Stage, Prod)
[instance]
environment-vars +=
DB_HOST 127.0.0.1
DB_NAME my_db_name
DB_USER john_doe
DB_PW 123xxx
# config.py
DB_SETTINGS = {
"DB_HOST": os.environ.get("DB_HOST", None),
"DB_USER": os.environ.get("DB_USER", None),
"DB_PASSWORD": os.environ.get("DB_PW", None),
"DB_NAME": os.environ.get("DB_NAME", None)
}
But in a Test the Enviroment Variable is not available:
bin/test -s my.addon -t DatabaseFunctionalTest
V1 = os.environ.get("DB_HOST", None)
print(V1) -> None
only this helps:
DB_HOST=127.0.0.1 bin/test -s my.addon -t DatabaseFunctionalTest
V1 = os.environ.get("DB_HOST", None)
print(V1) -> 127.0.0.1
How can i provide different configurations for different enviroments, e.g. Test, Dev, Stage, Prod ... without to write this pseudocode:
def get_config():
if is_in_test:
return config1
if is_in_dev:
return config2
if is_in_stage:
return config3
if is_in_prod:
return config4