Skip to content

Commit

Permalink
Resolvendo conflitos de merge
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielRoger07 committed Aug 10, 2024
2 parents b47d5ec + a4ec5f7 commit bb694ec
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 29 deletions.
13 changes: 12 additions & 1 deletion src/controller/savedVideosController.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ 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)
return {"status": status}


@WatchLater.get("/")
def get_watch_later_videos(user_id: str = Query(...), db: Session = Depends(get_db)):
videos = savedVideosRepository.get_watch_later_videos(db=db, user_id=user_id)
return {"videoList": videos}

# início das requisições do favorite

favorite = APIRouter(
Expand All @@ -47,4 +53,9 @@ def remove_from_favorites(video_id: str, user_id: str = Query(...), db: Session
user_id = user_id.strip()
video_id = video_id.strip()
savedVideosRepository.remove_favorite(db=db, video_id=video_id, user_id=user_id)
return {"message": "Removed from favorites"}
return {"message": "Removed from favorites"}

@favorite.get("/")
def get_favorite_videos(user_id: str = Query(...), db: Session = Depends(get_db)):
videos = savedVideosRepository.get_favorite_videos(db=db, user_id=user_id)
return {"videoList": videos}
1 change: 1 addition & 0 deletions src/junit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="0" time="0.074" timestamp="2024-08-09T00:45:13.498750" hostname="5a0db4a21511" /></testsuites>
17 changes: 17 additions & 0 deletions src/repository/savedVideosRepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ def check_watch_later_status(db: Session, video_id: str, user_id: str) -> bool:
print(f"Check Watch Later Status: video_id={video_id}, user_id={user_id}, found=False")
return False


def get_watch_later_videos(db: Session, user_id: str):
user_id = user_id.strip()
watch_later_entries = db.query(savedVideosModel.WatchLater).filter(
savedVideosModel.WatchLater.user_id == user_id,
savedVideosModel.WatchLater.status == True
).all()
return watch_later_entries


# início dos métodos do favorite

def create_favorite(db: Session, favorite: savedVideosSchema.FavoriteCreate):
Expand Down Expand Up @@ -99,3 +109,10 @@ def remove_favorite(db: Session, video_id: str, user_id: str):
else:
raise HTTPException(status_code=404, detail="Video not found in favorites")

def get_favorite_videos(db: Session, user_id: str):
user_id = user_id.strip()
favorite_entries = db.query(savedVideosModel.WatchLater).filter(
savedVideosModel.WatchLater.user_id == user_id,
savedVideosModel.WatchLater.statusfavorite == True
).all()
return favorite_entries
2 changes: 1 addition & 1 deletion tests/junit.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="12" time="4.369" timestamp="2024-08-09T21:13:03.726607" hostname="aaa8cc6c91a3"><testcase classname="test_favorite" name="test_add_to_favorite" time="0.163" /><testcase classname="test_favorite" name="test_check_favorite" time="0.013" /><testcase classname="test_favorite" name="test_remove_from_favorites" time="0.031" /><testcase classname="test_record" name="test_add_to_record" time="0.025" /><testcase classname="test_record" name="test_check_record" time="0.016" /><testcase classname="test_schedule.TestSchedule" name="test_schedule_get_schedule_day" time="0.688" /><testcase classname="test_schedule.TestSchedule" name="test_schedule_get_schedule_specific_day_invalid" time="0.004" /><testcase classname="test_schedule.TestSchedule" name="test_schedule_get_schedule_specific_day" time="0.606" /><testcase classname="test_schedule.TestSchedule" name="test_schedule_get_schedule_day_exception_handling" time="0.059" /><testcase classname="test_watch_later" name="test_add_to_watch_later" time="0.024" /><testcase classname="test_watch_later" name="test_check_watch_later_status" time="0.013" /><testcase classname="test_watch_later" name="test_remove_from_watch_later" time="0.022" /></testsuite></testsuites>
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="12" time="2.190" timestamp="2024-08-09T01:08:14.100951" hostname="eb7d7d6cc578"><testcase classname="test_favorite" name="test_add_to_favorite" time="0.044" /><testcase classname="test_favorite" name="test_check_favorite" time="0.013" /><testcase classname="test_favorite" name="test_remove_from_favorites" time="0.022" /><testcase classname="test_favorite" name="test_get_favorite_videos" time="0.023" /><testcase classname="test_schedule.TestSchedule" name="test_schedule_get_schedule_day" time="0.513" /><testcase classname="test_schedule.TestSchedule" name="test_schedule_get_schedule_specific_day_invalid" time="0.003" /><testcase classname="test_schedule.TestSchedule" name="test_schedule_get_schedule_specific_day" time="0.423" /><testcase classname="test_schedule.TestSchedule" name="test_schedule_get_schedule_day_exception_handling" time="0.005" /><testcase classname="test_watch_later" name="test_add_to_watch_later" time="0.008" /><testcase classname="test_watch_later" name="test_check_watch_later_status" time="0.014" /><testcase classname="test_watch_later" name="test_remove_from_watch_later" time="0.023" /><testcase classname="test_watch_later" name="test_get_watch_later_videos" time="0.023" /></testsuite></testsuites>
62 changes: 46 additions & 16 deletions tests/test_favorite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pytest, sys, os
import pytest, sys, os, uuid


sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src')))
Expand Down Expand Up @@ -39,28 +39,58 @@ def setup_database():
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"})
def test_add_to_favorite():
user_id = str(uuid.uuid4())
video_id = str(uuid.uuid4())
response = client.post("/api/favorite/", json={"user_id": user_id, "video_id": video_id})
assert response.status_code == 200
assert response.json()["user_id"] == "user123"
assert response.json()["video_id"] == "video123"
assert response.json()["user_id"] == user_id
assert response.json()["video_id"] == video_id
assert response.json()["statusfavorite"] is True

def test_check_favorite(setup_database):
response = client.get("/api/favorite/status/video123?user_id=user123")
print(response.json())
def test_check_favorite():
user_id = str(uuid.uuid4())
video_id = str(uuid.uuid4())

client.post("/api/favorite/", json={"user_id": user_id, "video_id": video_id})
response = client.get(f"/api/favorite/status/{video_id}?user_id={user_id}")
assert response.status_code == 200
assert response.json()["statusfavorite"] is True


def test_remove_from_favorites():
user_id = str(uuid.uuid4())
video_id = str(uuid.uuid4())

client.post("/api/favorite/", json={"user_id": user_id, "video_id": video_id})

response = client.get(f"/api/favorite/status/{video_id}?user_id={user_id}")
assert response.status_code == 200
assert response.json()["statusfavorite"] is True

def test_remove_from_favorites(setup_database):
response = client.delete("/api/favorite/video123?user_id=user123")
print("Response from DELETE:", response.json())

response = client.delete(f"/api/favorite/{video_id}?user_id={user_id}")
assert response.status_code == 200
assert response.json()["message"] == "Removed from favorites"


# Check status again to ensure it's removed
response = client.get("/api/favorite/status/video123?user_id=user123")
print("Response from GET status:", response.json())
response = client.get(f"/api/favorite/status/{video_id}?user_id={user_id}")
assert response.status_code == 200
assert response.json()["statusfavorite"] is False

def test_get_favorite_videos():
user_id = str(uuid.uuid4())

# Add multiple videos to the favorite list
video_ids = [str(uuid.uuid4()) for _ in range(3)]
for video_id in video_ids:
client.post("/api/favorite/", json={"user_id": user_id, "video_id": video_id})

# Retrieve the favorite list
response = client.get(f"/api/favorite/?user_id={user_id}")
assert response.status_code == 200
assert "videoList" in response.json()
video_list = response.json()["videoList"]

# Check if the specific video is in the favorite list
retrieved_video_ids = [item["video_id"] for item in video_list]
for video_id in video_ids:
assert video_id in retrieved_video_ids
75 changes: 64 additions & 11 deletions tests/test_watch_later.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import pytest, sys, os
import pytest, sys, os, uuid


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:
Expand All @@ -26,41 +32,88 @@ def override_get_db():
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_watch_later(setup_database):
response = client.post("/api/watch-later/", json={"user_id": "user123", "video_id": "video123"})


def test_add_to_watch_later():
user_id = str(uuid.uuid4())
video_id = str(uuid.uuid4())

response = client.post("/api/watch-later/", json={"user_id": user_id, "video_id": video_id})
assert response.status_code == 200
assert response.json()["user_id"] == "user123"
assert response.json()["video_id"] == "video123"
assert response.json()["user_id"] == user_id
assert response.json()["video_id"] == video_id
assert response.json()["status"] is True



def test_check_watch_later_status(setup_database):
response = client.get("/api/watch-later/status/video123?user_id=user123")
def test_check_watch_later_status():
user_id = str(uuid.uuid4())
video_id = str(uuid.uuid4())

client.post("/api/watch-later/", json={"user_id": user_id, "video_id": video_id})
response = client.get(f"/api/watch-later/status/{video_id}?user_id={user_id}")
assert response.status_code == 200
assert response.json()["status"] is True


def test_remove_from_watch_later(setup_database):
response = client.delete("/api/watch-later/video123?user_id=user123")
def test_remove_from_watch_later():
user_id = str(uuid.uuid4())
video_id = str(uuid.uuid4())

# Add the video to watch later list
client.post("/api/watch-later/", json={"user_id": user_id, "video_id": video_id})

# Ensure the video is added
response = client.get(f"/api/watch-later/status/{video_id}?user_id={user_id}")
assert response.status_code == 200
assert response.json()["status"] is True

# Remove the video from watch later list
response = client.delete(f"/api/watch-later/{video_id}?user_id={user_id}")
assert response.status_code == 200
assert response.json()["message"] == "Removed from watch later list"


# Check status again to ensure it's removed
response = client.get("/api/watch-later/status/video123?user_id=user123")
response = client.get(f"/api/watch-later/status/{video_id}?user_id={user_id}")
assert response.status_code == 200
assert response.json()["status"] is False


def test_get_watch_later_videos():
user_id = str(uuid.uuid4())

# Add multiple videos to the watch later list
video_ids = [str(uuid.uuid4()) for _ in range(3)]
for video_id in video_ids:
client.post("/api/watch-later/", json={"user_id": user_id, "video_id": video_id})


# Retrieve the watch later list
response = client.get(f"/api/watch-later/?user_id={user_id}")
assert response.status_code == 200
assert "videoList" in response.json()
video_list = response.json()["videoList"]

# Check if the specific video is in the watch later list
retrieved_video_ids = [item["video_id"] for item in video_list]
for video_id in video_ids:
assert video_id in retrieved_video_ids

0 comments on commit bb694ec

Please sign in to comment.