Skip to content

Commit 552bcc4

Browse files
committed
fixup! fix: Add DB env to integration
Signed-off-by: Alan Peixinho <alan.peixinho@profusion.mobi>
1 parent 3e6bc74 commit 552bcc4

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ jobs:
164164
165165
- name: Run integration tests with coverage
166166
run: |
167-
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 poetry run pytest -m "integration" --use-local-db --run-all --cov=kernelCI_app --cov=kernelCI_cache --cov-report=term-missing
167+
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 DJANGO_SETTINGS_MODULE=kernelCI.test_settings poetry run pytest -m "integration" --use-local-db --run-all --cov=kernelCI_app --cov=kernelCI_cache --cov-report=term-missing
168168
cp .coverage coverage-integration.sqlite
169169
working-directory: ./backend
170170

docs/IntegrationTests.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ docker compose -f docker-compose.test.yml up --build -d
258258
**Step 5: Run integration tests**
259259
```bash
260260
cd backend
261-
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 poetry run pytest -m integration --use-local-db --run-all
261+
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 DJANGO_SETTINGS_MODULE=kernelCI.test_settings poetry run pytest -m integration --use-local-db --run-all
262262
```
263263

264264
**Step 6: Clean up**
@@ -278,30 +278,42 @@ The test environment uses `backend/kernelCI/test_settings.py` which:
278278
Most integration tests are HTTP black-box tests against the running `test_backend`
279279
container, but some (e.g. management-command tests) are DB-backed and use
280280
`@pytest.mark.django_db` with factories. Those connect to PostgreSQL directly from
281-
the host, so the run commands set `DB_HOST=127.0.0.1 DB_PORT=5435` to reach the
282-
`test_db` container (published on port `5435`); `pytest-django` then creates its own
283-
isolated `test_*` database, separate from the seeded one used by the HTTP tests.
281+
the host, so the run commands set `DB_HOST=127.0.0.1` and `DB_PORT=5435` to reach the
282+
`test_db` container (published on port `5435`) instead of the in-network `test_db:5432`
283+
default. The name/user/password come from `test_settings` defaults (`kcidb_test`/
284+
`test_user`/`test_password`), so they don't need to be passed. `pytest-django` then
285+
creates its own isolated `test_*` database, separate from the seeded one used by the
286+
HTTP tests.
287+
288+
The commands also set `DJANGO_SETTINGS_MODULE=kernelCI.test_settings` explicitly. The
289+
`--use-local-db` flag alone is not enough: `pytest-django` reads the `DJANGO_SETTINGS_MODULE`
290+
ini option from `pyproject.toml` (`kernelCI.settings`) and configures Django before
291+
`conftest.py` can switch it. Under `kernelCI.settings` the `cache`/`notifications` DBs are
292+
file-based sqlite under `/volume_data`, which does not exist on the CI runner; the parallel
293+
(`-n 4`) test-DB setup then fails with "unable to open database file". `test_settings` uses
294+
in-memory sqlite instead, and the env var wins over the ini, so it forces the correct
295+
settings.
284296

285297
### Running Specific Tests
286298

287299
**Run all integration tests:**
288300
```bash
289-
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 poetry run pytest -m integration --use-local-db --run-all
301+
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 DJANGO_SETTINGS_MODULE=kernelCI.test_settings poetry run pytest -m integration --use-local-db --run-all
290302
```
291303

292304
**Run only a subset of tests (faster):**
293305
```bash
294-
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 poetry run pytest -m integration --use-local-db
306+
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 DJANGO_SETTINGS_MODULE=kernelCI.test_settings poetry run pytest -m integration --use-local-db
295307
```
296308

297309
**Run specific test file:**
298310
```bash
299-
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 poetry run pytest -m integration --use-local-db backend/kernelCI_app/tests/integrationTests/<your_test>.py
311+
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 DJANGO_SETTINGS_MODULE=kernelCI.test_settings poetry run pytest -m integration --use-local-db backend/kernelCI_app/tests/integrationTests/<your_test>.py
300312
```
301313

302314
**Run specific method:**
303315
```bash
304-
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 poetry run pytest -m integration --use-local-db backend/kernelCI_app/tests/integrationTests/<your_test>.py::<your_function>
316+
DB_HOST=127.0.0.1 DB_PORT=5435 TEST_BASE_URL=http://localhost:8001 DJANGO_SETTINGS_MODULE=kernelCI.test_settings poetry run pytest -m integration --use-local-db backend/kernelCI_app/tests/integrationTests/<your_test>.py::<your_function>
305317
```
306318

307319
### Troubleshooting

0 commit comments

Comments
 (0)