127 lines
3.0 KiB
Markdown
127 lines
3.0 KiB
Markdown
# SQLite ARRAY 타입 문제 해결 가이드
|
|
|
|
## 문제 원인
|
|
- PostgreSQL의 `ARRAY` 타입은 SQLite에서 지원되지 않습니다
|
|
- `Item` 모델의 `hashtags` 필드가 `ARRAY(String(30))` 타입으로 정의되어 있어 에러 발생
|
|
|
|
## 해결 방법
|
|
|
|
### 방법 1: Mock만 사용하는 간단한 테스트 (권장)
|
|
```bash
|
|
# simple 옵션으로 실행 (데이터베이스 없이 Mock만 사용)
|
|
./run-tests.sh simple
|
|
|
|
# 또는 특정 테스트만
|
|
./run-tests.sh auth
|
|
```
|
|
|
|
### 방법 2: 수동으로 conftest 변경
|
|
```bash
|
|
# 간단한 conftest 사용
|
|
cp tests/conftest_simple.py tests/conftest.py
|
|
|
|
# 테스트 실행
|
|
poetry run pytest tests/api/v1/test_auth.py -v
|
|
|
|
# 원래대로 복원
|
|
git checkout tests/conftest.py
|
|
```
|
|
|
|
### 방법 3: PostgreSQL 테스트 데이터베이스 사용
|
|
```bash
|
|
# PostgreSQL 설치 (Ubuntu)
|
|
sudo apt-get install postgresql postgresql-contrib
|
|
|
|
# 테스트 데이터베이스 생성
|
|
sudo -u postgres createdb test_o2sound
|
|
|
|
# 환경변수 설정
|
|
export TEST_DATABASE_URL="postgresql://postgres:password@localhost/test_o2sound"
|
|
|
|
# conftest.py 수정하여 PostgreSQL 사용
|
|
```
|
|
|
|
### 방법 4: 특정 모델 테스트 제외
|
|
```bash
|
|
# 데이터베이스 관련 테스트 제외하고 실행
|
|
poetry run pytest tests/ -v \
|
|
--ignore=tests/test_database_integration.py \
|
|
--ignore=tests/test_integration_scenarios.py
|
|
```
|
|
|
|
## 빠른 실행 명령어
|
|
|
|
### API 테스트만 실행 (Mock 사용)
|
|
```bash
|
|
# 인증 API 테스트
|
|
poetry run pytest tests/api/v1/test_auth.py -v -k "not database"
|
|
|
|
# 사용자 API 테스트
|
|
poetry run pytest tests/api/v1/test_user.py -v
|
|
|
|
# 모든 API 테스트 (데이터베이스 제외)
|
|
poetry run pytest tests/api/ -v
|
|
```
|
|
|
|
### 환경별 실행
|
|
```bash
|
|
# 개발 환경 (Mock만 사용)
|
|
TESTING=1 poetry run pytest tests/api/v1/test_auth.py -v
|
|
|
|
# CI/CD 환경
|
|
docker run --rm -v $(pwd):/app python:3.12 sh -c "cd /app && pip install pytest && pytest tests/"
|
|
```
|
|
|
|
## 영구적인 해결책
|
|
|
|
### 1. 테스트용 모델 생성
|
|
```python
|
|
# tests/models.py
|
|
from sqlalchemy import Column, String, Text
|
|
from app.domain.models.base import BaseModel
|
|
|
|
class TestItem(BaseModel):
|
|
__tablename__ = "test_items"
|
|
|
|
name = Column(String, nullable=False)
|
|
hashtags = Column(Text) # JSON으로 저장
|
|
```
|
|
|
|
### 2. 조건부 import
|
|
```python
|
|
# app/domain/models/item.py
|
|
import os
|
|
from sqlalchemy import Column, String
|
|
from sqlalchemy.dialects.postgresql import ARRAY
|
|
|
|
if os.getenv('TESTING'):
|
|
from sqlalchemy import Text as ARRAY_TYPE
|
|
else:
|
|
ARRAY_TYPE = ARRAY(String(30))
|
|
|
|
class Item(BaseModel):
|
|
hashtags = Column(ARRAY_TYPE, nullable=True)
|
|
```
|
|
|
|
## 추천 워크플로우
|
|
|
|
1. **로컬 개발**: Mock만 사용하는 간단한 테스트
|
|
```bash
|
|
./run-tests.sh auth
|
|
```
|
|
|
|
2. **CI/CD**: PostgreSQL 도커 컨테이너 사용
|
|
```yaml
|
|
services:
|
|
postgres:
|
|
image: postgres:15
|
|
env:
|
|
POSTGRES_PASSWORD: test
|
|
```
|
|
|
|
3. **통합 테스트**: 실제 PostgreSQL 사용
|
|
```bash
|
|
docker-compose up -d postgres
|
|
pytest tests/ --integration
|
|
```
|