Skip to content

feat(config): add support for separate test database via .env.test #1629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
10 changes: 6 additions & 4 deletions backend/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -113,7 +116,6 @@ def _enforce_non_default_secrets(self) -> Self:
self._check_default_secret(
"FIRST_SUPERUSER_PASSWORD", self.FIRST_SUPERUSER_PASSWORD
)

return self


Expand Down
Loading