File tree Expand file tree Collapse file tree 3 files changed +13
-2
lines changed Expand file tree Collapse file tree 3 files changed +13
-2
lines changed Original file line number Diff line number Diff line change 11
22import pytest
3+ import os
34from sqlalchemy import create_engine , inspect
45from sqlalchemy .orm import sessionmaker
56from fastapi .testclient import TestClient
67from .main import create_app
78from .dependencies import get_db , database_url
89from .models import Base
910
11+ TEST_DATABASE_URL = os .getenv ("TEST_DATABASE_URL" , "postgresql://app_user:secret@db/app_db_test" )
1012
1113@pytest .fixture (scope = "session" )
1214def test_engine ():
1315 """Create test database engine"""
14- test_engine = create_engine (database_url )
16+ test_engine = create_engine (TEST_DATABASE_URL )
1517 Base .metadata .create_all (bind = test_engine )
1618 yield test_engine
1719 Base .metadata .drop_all (bind = test_engine )
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ services:
77 - " 8000:8000"
88 environment :
99 - DATABASE_URL=postgresql://app_user:secret@db/app_db
10+ - TEST_DATABASE_URL=postgresql://app_user:secret@db/app_db_test
1011 depends_on :
1112 db :
1213 condition : service_healthy
@@ -18,13 +19,13 @@ services:
1819 container_name : fastapi_db
1920 restart : unless-stopped
2021 environment :
21- POSTGRES_DB : app_db
2222 POSTGRES_USER : app_user
2323 POSTGRES_PASSWORD : secret
2424 ports :
2525 - " 5432:5432"
2626 volumes :
2727 - postgres_data:/var/lib/postgresql/data
28+ - ./init-dbs.sh:/docker-entrypoint-initdb.d/init-dbs.sh:ro
2829 healthcheck :
2930 test : ["CMD-SHELL", "pg_isready -U app_user -d app_db"]
3031 interval : 10s
Original file line number Diff line number Diff line change 1+ # init-db.sh
2+ #! /bin/bash
3+ set -e
4+
5+ psql -v ON_ERROR_STOP=1 --username " $POSTGRES_USER " << -EOSQL
6+ CREATE DATABASE app_db;
7+ CREATE DATABASE app_db_test;
8+ EOSQL
You can’t perform that action at this time.
0 commit comments