From 019705f5c4c35aa84f4dd6597a683ca8a05329ad Mon Sep 17 00:00:00 2001 From: wktls63 Date: Tue, 1 Oct 2024 15:42:37 +0900 Subject: [PATCH] test(content): update and pass test cases for content functionality --- services/content-service/app/content.py | 44 +++++++------- services/content-service/app/main.py | 10 ++-- services/content-service/app/models.py | 4 +- services/content-service/app/schemas.py | 15 +++++ services/content-service/requirements.txt | 25 +++++++- services/content-service/tests/conftest.py | 5 ++ .../content-service/tests/test_content.py | 59 +++++++++++++++++++ 7 files changed, 128 insertions(+), 34 deletions(-) create mode 100644 services/content-service/app/schemas.py create mode 100644 services/content-service/tests/conftest.py create mode 100644 services/content-service/tests/test_content.py diff --git a/services/content-service/app/content.py b/services/content-service/app/content.py index fd9f52e..5659c01 100644 --- a/services/content-service/app/content.py +++ b/services/content-service/app/content.py @@ -1,40 +1,38 @@ -from fastapi import APIRouter, Depends, HTTPException, status +# content.py + +from fastapi import APIRouter, HTTPException, Depends from sqlalchemy.orm import Session -from app.models import Content from app.schemas import ContentCreate, Content as ContentSchema +from app.models import Content from app.db import get_db -# 콘텐츠 관련 API 엔드포인트들을 관리할 라우터 router = APIRouter() # 콘텐츠 생성 엔드포인트 -@router.post("/", response_model=ContentSchema, status_code=status.HTTP_201_CREATED) +@router.post("/", response_model=ContentSchema, status_code=201) def create_content(content: ContentCreate, db: Session = Depends(get_db)): - db_content = Content(**content.dict()) - db.add(db_content) + new_content = Content( + title=content.title, + description=content.description, + category=content.category, + creator=content.creator + ) + db.add(new_content) db.commit() - db.refresh(db_content) - return db_content + db.refresh(new_content) + return new_content -# 전체 콘텐츠 조회 엔드포인트 +# 콘텐츠 목록 조회 엔드포인트 @router.get("/", response_model=list[ContentSchema]) def get_contents(db: Session = Depends(get_db)): - return db.query(Content).all() - -# 특정 콘텐츠 조회 엔드포인트 -@router.get("/{content_id}", response_model=ContentSchema) -def get_content(content_id: int, db: Session = Depends(get_db)): - db_content = db.query(Content).filter(Content.id == content_id).first() - if db_content is None: - raise HTTPException(status_code=404, detail="Content not found") - return db_content + contents = db.query(Content).all() + return contents # 콘텐츠 삭제 엔드포인트 -@router.delete("/{content_id}", status_code=status.HTTP_204_NO_CONTENT) +@router.delete("/{content_id}", status_code=204) def delete_content(content_id: int, db: Session = Depends(get_db)): - db_content = db.query(Content).filter(Content.id == content_id).first() - if db_content is None: + content = db.query(Content).filter(Content.id == content_id).first() + if content is None: raise HTTPException(status_code=404, detail="Content not found") - db.delete(db_content) + db.delete(content) db.commit() - return None diff --git a/services/content-service/app/main.py b/services/content-service/app/main.py index ac3a23b..e5572f3 100644 --- a/services/content-service/app/main.py +++ b/services/content-service/app/main.py @@ -1,12 +1,10 @@ +# main.py from fastapi import FastAPI -from app.db import Base, engine from app.content import router as content_router -# FastAPI 애플리케이션 생성 app = FastAPI() -# 데이터베이스 테이블 자동 생성 -Base.metadata.create_all(bind=engine) - -# 콘텐츠 API 라우터 등록 +# 콘텐츠 서비스 라우터 추가 app.include_router(content_router, prefix="/contents", tags=["contents"]) + +# 추가적인 라우터나 미들웨어 설정이 여기에 들어갈 수 있습니다. diff --git a/services/content-service/app/models.py b/services/content-service/app/models.py index a911dc0..31bd9d4 100644 --- a/services/content-service/app/models.py +++ b/services/content-service/app/models.py @@ -8,5 +8,5 @@ class Content(Base): id = Column(Integer, primary_key=True, index=True) title = Column(String(255), nullable=False) # 콘텐츠 제목 description = Column(Text, nullable=True) # 콘텐츠 설명 - category = Column(String(50), nullable=False) # 콘텐츠 카테고리 (예: 영화, 공연, 뮤지컬 등) - creator = Column(String(100), nullable=False) # 제작자 정보 + category = Column(String, nullable=True) # 선택적 필드로 변경 + creator = Column(String, nullable=True) # 선택적 필드로 변경 \ No newline at end of file diff --git a/services/content-service/app/schemas.py b/services/content-service/app/schemas.py new file mode 100644 index 0000000..8ac739c --- /dev/null +++ b/services/content-service/app/schemas.py @@ -0,0 +1,15 @@ +# schemas.py + +from pydantic import BaseModel + +class ContentCreate(BaseModel): + title: str + description: str + category: str # 필수 필드로 설정 + creator: str # 필수 필드로 설정 + +class Content(ContentCreate): + id: int + + class Config: + from_attributes = True diff --git a/services/content-service/requirements.txt b/services/content-service/requirements.txt index 4e7b387..d9fb4c8 100644 --- a/services/content-service/requirements.txt +++ b/services/content-service/requirements.txt @@ -1,5 +1,24 @@ -uvicorn==0.30.6 -psycopg2-binary==2.9.9 +annotated-types==0.7.0 +anyio==4.6.0 +certifi==2024.8.30 +click==8.1.7 +exceptiongroup==1.2.2 fastapi==0.112.2 +h11==0.14.0 +httpcore==1.0.5 +httpx==0.27.2 +idna==3.10 +iniconfig==2.0.0 +packaging==24.1 +pluggy==1.5.0 +psycopg2-binary==2.9.9 +pydantic==2.9.2 +pydantic_core==2.23.4 +pytest==8.3.3 +python-dotenv==1.0.1 +sniffio==1.3.1 SQLAlchemy==2.0.32 -python-dotenv==1.0.1 \ No newline at end of file +starlette==0.38.5 +tomli==2.0.1 +typing_extensions==4.12.2 +uvicorn==0.30.6 diff --git a/services/content-service/tests/conftest.py b/services/content-service/tests/conftest.py new file mode 100644 index 0000000..56fb40a --- /dev/null +++ b/services/content-service/tests/conftest.py @@ -0,0 +1,5 @@ +import sys +import os + +# 프로젝트의 루트 경로를 PYTHONPATH에 추가 +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))) \ No newline at end of file diff --git a/services/content-service/tests/test_content.py b/services/content-service/tests/test_content.py new file mode 100644 index 0000000..52b0150 --- /dev/null +++ b/services/content-service/tests/test_content.py @@ -0,0 +1,59 @@ +# tests/test_content.py + +from fastapi.testclient import TestClient +from app.main import app # FastAPI 앱을 임포트 + +# TestClient 생성 +client = TestClient(app) + +def test_create_content(): + response = client.post( + "/contents/", + json={ + "title": "Test Movie", + "description": "A great movie", + "category": "Movie", # 필수 필드 추가 + "creator": "John Doe" # 필수 필드 추가 + } + ) + assert response.status_code == 201 + assert response.json()["title"] == "Test Movie" + + +def test_get_content(): + # 콘텐츠 생성 + response = client.post( + "/contents/", + json={ + "title": "Test Performance", + "description": "A live performance", + "category": "Performance", # 필수 필드 추가 + "creator": "Jane Smith" # 필수 필드 추가 + } + ) + content_id = response.json()["id"] + + # 콘텐츠 목록 조회 + response = client.get("/contents/") + assert response.status_code == 200 + contents = response.json() + assert len(contents) > 0 + assert any(content["title"] == "Test Performance" for content in contents) + + +def test_delete_content(): + # 콘텐츠 생성 + response = client.post( + "/contents/", + json={ + "title": "Test Art", + "description": "An amazing art", + "category": "Art", # 필수 필드 추가 + "creator": "John Artist" # 필수 필드 추가 + } + ) + content_id = response.json()["id"] + + # 콘텐츠 삭제 + response = client.delete(f"/contents/{content_id}") + assert response.status_code == 204