157 lines
4.6 KiB
Bash
157 lines
4.6 KiB
Bash
#!/bin/bash
|
|
|
|
# PostgreSQL 테스트 환경 설정 스크립트
|
|
|
|
echo "🐘 Setting up PostgreSQL test environment..."
|
|
echo "=========================================="
|
|
|
|
# 색상 정의
|
|
GREEN='\033[0;32m'
|
|
RED='\033[0;31m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# PostgreSQL 설치 확인
|
|
check_postgresql() {
|
|
if command -v psql &> /dev/null; then
|
|
echo -e "${GREEN}✓ PostgreSQL is installed${NC}"
|
|
return 0
|
|
else
|
|
echo -e "${RED}✗ PostgreSQL is not installed${NC}"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# PostgreSQL 서비스 상태 확인
|
|
check_postgresql_service() {
|
|
if systemctl is-active --quiet postgresql; then
|
|
echo -e "${GREEN}✓ PostgreSQL service is running${NC}"
|
|
return 0
|
|
else
|
|
echo -e "${YELLOW}⚠ PostgreSQL service is not running${NC}"
|
|
echo "Starting PostgreSQL service..."
|
|
sudo systemctl start postgresql
|
|
return $?
|
|
fi
|
|
}
|
|
|
|
# 테스트 데이터베이스 생성
|
|
create_test_database() {
|
|
local db_name="test_o2sound"
|
|
local db_user="postgres"
|
|
|
|
echo "Creating test database: $db_name"
|
|
|
|
# 데이터베이스가 이미 존재하는지 확인
|
|
if sudo -u postgres psql -lqt | cut -d \| -f 1 | grep -qw $db_name; then
|
|
echo -e "${YELLOW}⚠ Database '$db_name' already exists${NC}"
|
|
echo "Do you want to drop and recreate it? (y/N)"
|
|
read -r response
|
|
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
|
|
sudo -u postgres dropdb $db_name
|
|
sudo -u postgres createdb $db_name
|
|
echo -e "${GREEN}✓ Database recreated${NC}"
|
|
fi
|
|
else
|
|
sudo -u postgres createdb $db_name
|
|
echo -e "${GREEN}✓ Database created${NC}"
|
|
fi
|
|
}
|
|
|
|
# 환경 변수 설정
|
|
setup_environment() {
|
|
echo "Setting up environment variables..."
|
|
|
|
# .env.test 파일이 이미 있는지 확인
|
|
if [ -f .env.test ]; then
|
|
echo -e "${YELLOW}⚠ .env.test file already exists${NC}"
|
|
echo "Backing up existing file to .env.test.backup"
|
|
cp .env.test .env.test.backup
|
|
fi
|
|
|
|
# .env.local 파일이 있으면 복사하고 수정
|
|
if [ -f .env.local ]; then
|
|
echo "Using .env.local as template..."
|
|
cp .env.local .env.test
|
|
|
|
# 테스트용 데이터베이스 설정 추가/수정
|
|
echo "" >> .env.test
|
|
echo "# Test-specific overrides" >> .env.test
|
|
echo "TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/test_o2sound" >> .env.test
|
|
echo "DATABASE_NAME=test_o2sound" >> .env.test
|
|
echo "REDIS_DB=1" >> .env.test
|
|
|
|
echo -e "${GREEN}✓ Created .env.test from .env.local${NC}"
|
|
else
|
|
echo -e "${RED}✗ .env.local not found${NC}"
|
|
echo "Please create .env.local first, or create .env.test manually"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# python-dotenv 설치 확인
|
|
check_dotenv() {
|
|
echo "Checking python-dotenv installation..."
|
|
if poetry show python-dotenv &> /dev/null; then
|
|
echo -e "${GREEN}✓ python-dotenv is installed${NC}"
|
|
else
|
|
echo -e "${YELLOW}⚠ Installing python-dotenv...${NC}"
|
|
poetry add python-dotenv --group dev
|
|
fi
|
|
}
|
|
|
|
# 테스트 실행 함수
|
|
run_tests() {
|
|
echo -e "\n${YELLOW}Running tests with PostgreSQL...${NC}"
|
|
|
|
# 환경 변수 설정
|
|
export BE_ENV=local
|
|
export ENVIRONMENT=local
|
|
|
|
# .env.test 파일 로드
|
|
if [ -f .env.test ]; then
|
|
export $(cat .env.test | grep -v '^#' | xargs)
|
|
fi
|
|
|
|
# 테스트 실행
|
|
poetry run pytest tests/ "$@"
|
|
}
|
|
|
|
# 메인 실행
|
|
main() {
|
|
echo "1. Checking PostgreSQL installation..."
|
|
if ! check_postgresql; then
|
|
echo -e "${RED}Please install PostgreSQL first:${NC}"
|
|
echo " Ubuntu/Debian: sudo apt-get install postgresql postgresql-contrib"
|
|
echo " MacOS: brew install postgresql"
|
|
exit 1
|
|
fi
|
|
|
|
echo -e "\n2. Checking PostgreSQL service..."
|
|
check_postgresql_service
|
|
|
|
echo -e "\n3. Creating test database..."
|
|
create_test_database
|
|
|
|
echo -e "\n4. Checking python-dotenv..."
|
|
check_dotenv
|
|
|
|
echo -e "\n5. Setting up environment..."
|
|
setup_environment
|
|
|
|
echo -e "\n${GREEN}✅ PostgreSQL test environment is ready!${NC}"
|
|
echo -e "\nYou can now run tests with:"
|
|
echo " ./setup-postgres-test.sh test # Run all tests"
|
|
echo " ./setup-postgres-test.sh test -v # Run tests in verbose mode"
|
|
echo " ./setup-postgres-test.sh test tests/api/v1/test_auth.py # Run specific test"
|
|
|
|
# 인자가 있으면 테스트 실행
|
|
if [ "$1" == "test" ]; then
|
|
shift
|
|
run_tests "$@"
|
|
fi
|
|
}
|
|
|
|
# 스크립트 실행
|
|
main "$@"
|