From f95084d5c930cfeaa79012f39785cacfc88bafae Mon Sep 17 00:00:00 2001 From: EmiFunes91 Date: Sun, 18 May 2025 01:24:47 -0300 Subject: [PATCH] feat(config): add support for separate test database via .env.test --- .env | 2 +- .env.test | 9 +++++++++ README.md | 19 +++++++++++++++++++ backend/app/core/config.py | 10 ++++++---- 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 .env.test diff --git a/.env b/.env index 1d44286e25..e9e95b6382 100644 --- a/.env +++ b/.env @@ -10,7 +10,7 @@ FRONTEND_HOST=http://localhost:5173 # In staging and production, set this env var to the frontend host, e.g. # FRONTEND_HOST=https://dashboard.example.com -# Environment: local, staging, production +# Environment: local, staging, production, test ENVIRONMENT=local PROJECT_NAME="Full Stack FastAPI Project" diff --git a/.env.test b/.env.test new file mode 100644 index 0000000000..450e781a45 --- /dev/null +++ b/.env.test @@ -0,0 +1,9 @@ +# Environment used during automated testing +ENVIRONMENT=test + +# Postgres (isolated database for testing) +POSTGRES_SERVER=localhost +POSTGRES_PORT=5432 +POSTGRES_DB=app_test +POSTGRES_USER=postgres +POSTGRES_PASSWORD=changethis diff --git a/README.md b/README.md index afe124f3fb..acabf60406 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,25 @@ You can (and should) pass these as environment variables from secrets. Read the [deployment.md](./deployment.md) docs for more details. +### Testing Environment + +This project supports running in a test environment using a separate `.env.test` file. +It allows isolating the testing database from the development one to prevent data collisions. + +The test configuration sets: + +- `ENVIRONMENT=test` +- `POSTGRES_DB=app_test` + +The application automatically detects this environment and uses a separate database configuration. +You can run tests with: + +```bash +ENVIRONMENT=test pytest +``` +You can also create a dedicated PostgreSQL service or volume for testing if needed. +See config.py for the environment-specific logic. + ### Generate Secret Keys Some environment variables in the `.env` file have a default value of `changethis`. diff --git a/backend/app/core/config.py b/backend/app/core/config.py index d58e03c87d..bd5eca4d35 100644 --- a/backend/app/core/config.py +++ b/backend/app/core/config.py @@ -33,10 +33,9 @@ class Settings(BaseSettings): ) API_V1_STR: str = "/api/v1" SECRET_KEY: str = secrets.token_urlsafe(32) - # 60 minutes * 24 hours * 8 days = 8 days ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 FRONTEND_HOST: str = "http://localhost:5173" - ENVIRONMENT: Literal["local", "staging", "production"] = "local" + ENVIRONMENT: Literal["local", "staging", "production", "test"] = "local" BACKEND_CORS_ORIGINS: Annotated[ list[AnyUrl] | str, BeforeValidator(parse_cors) @@ -60,13 +59,17 @@ def all_cors_origins(self) -> list[str]: @computed_field # type: ignore[prop-decorator] @property def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn: + db_name = self.POSTGRES_DB + # Si el entorno es de test, usamos una base separada + if self.ENVIRONMENT == "test": + db_name = f"{self.POSTGRES_DB}_test" return MultiHostUrl.build( scheme="postgresql+psycopg", username=self.POSTGRES_USER, password=self.POSTGRES_PASSWORD, host=self.POSTGRES_SERVER, port=self.POSTGRES_PORT, - path=self.POSTGRES_DB, + path=db_name, ) SMTP_TLS: bool = True @@ -113,7 +116,6 @@ def _enforce_non_default_secrets(self) -> Self: self._check_default_secret( "FIRST_SUPERUSER_PASSWORD", self.FIRST_SUPERUSER_PASSWORD ) - return self