Skip to content

Commit

Permalink
Adicao dos metodos create/post do favorite e teste
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeJRdev committed Aug 3, 2024
1 parent b28857b commit 4a2011e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/controller/savedVideosController.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

load_dotenv()

from controller import commentController, scheduleController
from controller import commentController, scheduleController, savedVideosController
from controller.savedVideosController import WatchLater


Expand Down Expand Up @@ -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():
Expand Down
14 changes: 14 additions & 0 deletions src/repository/savedVideosRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

48 changes: 48 additions & 0 deletions tests/test_favorite.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4a2011e

Please sign in to comment.