From 4a2011ef5410ee74355ba0de113e4a424e65217c Mon Sep 17 00:00:00 2001 From: felipeJRdev Date: Sat, 3 Aug 2024 11:42:29 -0300 Subject: [PATCH] Adicao dos metodos create/post do favorite e teste --- src/controller/savedVideosController.py | 10 ++++++ src/main.py | 3 +- src/repository/savedVideosRepository.py | 14 ++++++++ tests/test_favorite.py | 48 +++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/test_favorite.py diff --git a/src/controller/savedVideosController.py b/src/controller/savedVideosController.py index e926e09..074c6bf 100644 --- a/src/controller/savedVideosController.py +++ b/src/controller/savedVideosController.py @@ -29,3 +29,13 @@ def check_watch_later(video_id: str, user_id: str = Query(...), db: Session = De status = savedVideosRepository.check_watch_later_status(db=db, video_id=video_id, user_id=user_id) print(f"Status for video_id={video_id}, user_id={user_id} is {status}") return {"status": status} + +# início das requisições do favorite + +favorite = APIRouter( + prefix="/favorite" +) + +@favorite.post("/") +def add_to_favorite(favorite: savedVideosSchema.FavoriteCreate, db: Session = Depends(get_db)): + return savedVideosRepository.create_favorite(db=db, favorite=favorite) diff --git a/src/main.py b/src/main.py index acfdffd..457634d 100755 --- a/src/main.py +++ b/src/main.py @@ -8,7 +8,7 @@ load_dotenv() -from controller import commentController, scheduleController +from controller import commentController, scheduleController, savedVideosController from controller.savedVideosController import WatchLater @@ -37,6 +37,7 @@ app.include_router(WatchLater, prefix="/api") #app.include_router(prefix="/api", router=commentController.comment) app.include_router(prefix="/api", router=scheduleController.schedule) +app.include_router(prefix="/api", router=savedVideosController.favorite) @app.get("/") async def root(): diff --git a/src/repository/savedVideosRepository.py b/src/repository/savedVideosRepository.py index 0a57b9e..a4562f4 100644 --- a/src/repository/savedVideosRepository.py +++ b/src/repository/savedVideosRepository.py @@ -51,3 +51,17 @@ def check_watch_later_status(db: Session, video_id: str, user_id: str) -> bool: return watch_later_entry.status print(f"Check Watch Later Status: video_id={video_id}, user_id={user_id}, found=False") return False + +# início dos métodos do favorite + +def create_favorite(db: Session, favorite: savedVideosSchema.FavoriteCreate): + db_favorite = savedVideosModel.WatchLater( + user_id = favorite.user_id.strip(), + video_id = favorite.video_id.strip(), + statusfavorite = True + ) + db.add(db_favorite) + db.commit() + db.refresh(db_favorite) + return db_favorite + \ No newline at end of file diff --git a/tests/test_favorite.py b/tests/test_favorite.py new file mode 100644 index 0000000..90b10fd --- /dev/null +++ b/tests/test_favorite.py @@ -0,0 +1,48 @@ +import pytest, sys, os + + +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src'))) + + +from fastapi.testclient import TestClient +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from src.database import Base, get_db +from src.main import app + + +# Crie um banco de dados de teste em memória +SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db" +engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}) +TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + + +# Dependência para usar o banco de dados de teste +def override_get_db(): + try: + db = TestingSessionLocal() + yield db + finally: + db.close() + + +app.dependency_overrides[get_db] = override_get_db + + +client = TestClient(app) + + +@pytest.fixture(scope="module") +def setup_database(): + Base.metadata.create_all(bind=engine) + yield + Base.metadata.drop_all(bind=engine) + + +def test_add_to_favorite(setup_database): + response = client.post("/api/favorite/", json={"user_id": "user123", "video_id": "video123"}) + assert response.status_code == 200 + assert response.json()["user_id"] == "user123" + assert response.json()["video_id"] == "video123" + assert response.json()["statusfavorite"] is True +