Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f95084d

Browse files
feat(config): add support for separate test database via .env.test
1 parent 6c9b1fa commit f95084d

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

‎.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ FRONTEND_HOST=http://localhost:5173
1010
# In staging and production, set this env var to the frontend host, e.g.
1111
# FRONTEND_HOST=https://dashboard.example.com
1212

13-
# Environment: local, staging, production
13+
# Environment: local, staging, production, test
1414
ENVIRONMENT=local
1515

1616
PROJECT_NAME="Full Stack FastAPI Project"

‎.env.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Environment used during automated testing
2+
ENVIRONMENT=test
3+
4+
# Postgres (isolated database for testing)
5+
POSTGRES_SERVER=localhost
6+
POSTGRES_PORT=5432
7+
POSTGRES_DB=app_test
8+
POSTGRES_USER=postgres
9+
POSTGRES_PASSWORD=changethis

‎README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,25 @@ You can (and should) pass these as environment variables from secrets.
140140

141141
Read the [deployment.md](./deployment.md) docs for more details.
142142

143+
### Testing Environment
144+
145+
This project supports running in a test environment using a separate `.env.test` file.
146+
It allows isolating the testing database from the development one to prevent data collisions.
147+
148+
The test configuration sets:
149+
150+
- `ENVIRONMENT=test`
151+
- `POSTGRES_DB=app_test`
152+
153+
The application automatically detects this environment and uses a separate database configuration.
154+
You can run tests with:
155+
156+
```bash
157+
ENVIRONMENT=test pytest
158+
```
159+
You can also create a dedicated PostgreSQL service or volume for testing if needed.
160+
See config.py for the environment-specific logic.
161+
143162
### Generate Secret Keys
144163

145164
Some environment variables in the `.env` file have a default value of `changethis`.

‎backend/app/core/config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ class Settings(BaseSettings):
3333
)
3434
API_V1_STR: str = "/api/v1"
3535
SECRET_KEY: str = secrets.token_urlsafe(32)
36-
# 60 minutes * 24 hours * 8 days = 8 days
3736
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
3837
FRONTEND_HOST: str = "http://localhost:5173"
39-
ENVIRONMENT: Literal["local", "staging", "production"] = "local"
38+
ENVIRONMENT: Literal["local", "staging", "production", "test"] = "local"
4039

4140
BACKEND_CORS_ORIGINS: Annotated[
4241
list[AnyUrl] | str, BeforeValidator(parse_cors)
@@ -60,13 +59,17 @@ def all_cors_origins(self) -> list[str]:
6059
@computed_field # type: ignore[prop-decorator]
6160
@property
6261
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
62+
db_name = self.POSTGRES_DB
63+
# Si el entorno es de test, usamos una base separada
64+
if self.ENVIRONMENT == "test":
65+
db_name = f"{self.POSTGRES_DB}_test"
6366
return MultiHostUrl.build(
6467
scheme="postgresql+psycopg",
6568
username=self.POSTGRES_USER,
6669
password=self.POSTGRES_PASSWORD,
6770
host=self.POSTGRES_SERVER,
6871
port=self.POSTGRES_PORT,
69-
path=self.POSTGRES_DB,
72+
path=db_name,
7073
)
7174

7275
SMTP_TLS: bool = True
@@ -113,7 +116,6 @@ def _enforce_non_default_secrets(self) -> Self:
113116
self._check_default_secret(
114117
"FIRST_SUPERUSER_PASSWORD", self.FIRST_SUPERUSER_PASSWORD
115118
)
116-
117119
return self
118120

119121

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /