185 lines
3.6 KiB
Markdown
185 lines
3.6 KiB
Markdown
# PostgreSQL 테스트 환경 설정 가이드
|
|
|
|
## 🐘 PostgreSQL 설치 및 설정
|
|
|
|
### 1. PostgreSQL 설치
|
|
|
|
#### Ubuntu/Debian
|
|
```bash
|
|
sudo apt update
|
|
sudo apt install postgresql postgresql-contrib
|
|
```
|
|
|
|
#### MacOS
|
|
```bash
|
|
brew install postgresql
|
|
brew services start postgresql
|
|
```
|
|
|
|
### 2. 테스트 데이터베이스 설정
|
|
|
|
#### 자동 설정 (권장)
|
|
```bash
|
|
# 실행 권한 부여
|
|
chmod +x setup-postgres-test.sh
|
|
|
|
# PostgreSQL 테스트 환경 자동 설정
|
|
./setup-postgres-test.sh
|
|
|
|
# 설정 후 바로 테스트 실행
|
|
./setup-postgres-test.sh test
|
|
```
|
|
|
|
#### 수동 설정
|
|
```bash
|
|
# PostgreSQL에 접속
|
|
sudo -u postgres psql
|
|
|
|
# 테스트 데이터베이스 생성
|
|
CREATE DATABASE test_o2sound;
|
|
|
|
# 사용자 비밀번호 설정 (옵션)
|
|
ALTER USER postgres PASSWORD 'postgres';
|
|
|
|
# 종료
|
|
\q
|
|
```
|
|
|
|
### 3. 환경 변수 설정
|
|
|
|
`.env.test` 파일이 자동으로 생성됩니다:
|
|
```env
|
|
TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/test_o2sound
|
|
```
|
|
|
|
## 🚀 테스트 실행
|
|
|
|
### 기본 실행
|
|
```bash
|
|
# 모든 테스트 실행
|
|
./run-tests.sh
|
|
|
|
# 특정 테스트만 실행
|
|
./run-tests.sh auth
|
|
./run-tests.sh user
|
|
./run-tests.sh moviemaker
|
|
```
|
|
|
|
### 고급 옵션
|
|
```bash
|
|
# Verbose 모드
|
|
./run-tests.sh verbose
|
|
|
|
# 커버리지 포함
|
|
./run-tests.sh coverage
|
|
|
|
# 통합 테스트만
|
|
./run-tests.sh integration
|
|
|
|
# SQLite 폴백 (제한적)
|
|
./run-tests.sh sqlite
|
|
```
|
|
|
|
## 🔧 문제 해결
|
|
|
|
### PostgreSQL 연결 실패
|
|
```bash
|
|
# PostgreSQL 서비스 상태 확인
|
|
sudo systemctl status postgresql
|
|
|
|
# 서비스 시작
|
|
sudo systemctl start postgresql
|
|
|
|
# 인증 방법 확인 (pg_hba.conf)
|
|
sudo nano /etc/postgresql/*/main/pg_hba.conf
|
|
# local all postgres trust 로 변경
|
|
sudo systemctl restart postgresql
|
|
```
|
|
|
|
### 권한 문제
|
|
```bash
|
|
# 현재 사용자로 데이터베이스 생성
|
|
createdb -U $USER test_o2sound
|
|
|
|
# 또는 postgres 사용자로 실행
|
|
sudo -u postgres createdb test_o2sound
|
|
```
|
|
|
|
### 포트 충돌
|
|
```bash
|
|
# PostgreSQL 포트 확인
|
|
sudo netstat -plnt | grep 5432
|
|
|
|
# 다른 포트 사용 시
|
|
TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5433/test_o2sound
|
|
```
|
|
|
|
## 📋 테스트 데이터베이스 관리
|
|
|
|
### 데이터베이스 초기화
|
|
```bash
|
|
# 데이터베이스 삭제 및 재생성
|
|
sudo -u postgres dropdb test_o2sound
|
|
sudo -u postgres createdb test_o2sound
|
|
```
|
|
|
|
### 테이블 확인
|
|
```bash
|
|
sudo -u postgres psql -d test_o2sound -c "\dt"
|
|
```
|
|
|
|
### 테스트 후 정리
|
|
```bash
|
|
# 모든 테이블 삭제
|
|
sudo -u postgres psql -d test_o2sound -c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"
|
|
```
|
|
|
|
## 🎯 CI/CD 설정
|
|
|
|
### GitHub Actions
|
|
```yaml
|
|
services:
|
|
postgres:
|
|
image: postgres:15
|
|
env:
|
|
POSTGRES_PASSWORD: postgres
|
|
POSTGRES_DB: test_o2sound
|
|
options: >-
|
|
--health-cmd pg_isready
|
|
--health-interval 10s
|
|
--health-timeout 5s
|
|
--health-retries 5
|
|
ports:
|
|
- 5432:5432
|
|
|
|
steps:
|
|
- name: Run tests
|
|
env:
|
|
TEST_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/test_o2sound
|
|
run: |
|
|
poetry install
|
|
poetry run pytest tests/
|
|
```
|
|
|
|
### Docker Compose
|
|
```yaml
|
|
version: '3.8'
|
|
services:
|
|
test-db:
|
|
image: postgres:15
|
|
environment:
|
|
POSTGRES_DB: test_o2sound
|
|
POSTGRES_PASSWORD: postgres
|
|
ports:
|
|
- "5432:5432"
|
|
```
|
|
|
|
## ✅ 장점
|
|
|
|
1. **완전한 호환성**: PostgreSQL 특수 타입 (ARRAY, UUID, JSONB) 모두 지원
|
|
2. **실제 환경과 동일**: 프로덕션과 동일한 데이터베이스 사용
|
|
3. **트랜잭션 격리**: 각 테스트가 독립적으로 실행
|
|
4. **빠른 실행**: 메모리 기반 테스트보다 약간 느리지만 더 정확
|
|
|
|
이제 PostgreSQL을 사용하여 모든 테스트를 문제없이 실행할 수 있습니다!
|