Skip to content

Commit

Permalink
tests: wrote some tests for filmweb endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
suchencjusz committed Sep 11, 2024
1 parent b47f655 commit 43b5784
Show file tree
Hide file tree
Showing 3 changed files with 265 additions and 72 deletions.
11 changes: 1 addition & 10 deletions src/filman_server/database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def delete_filmweb_user_mapping(
db: Session,
user_id: int | None,
discord_id: int | None,
filmweb_id: str,
filmweb_id: str | None,
) -> bool:
user = get_user(db, user_id, filmweb_id, discord_id)

Expand Down Expand Up @@ -442,15 +442,6 @@ def get_filmweb_user_watched_movies(
return watched_movies


def get_filmweb_user_watched_movies_ids(db: Session, filmweb_id: str):
result = (
db.query(models.FilmWebUserWatchedMovie.id_media) # select only the id field
.filter(models.FilmWebUserWatchedMovie.filmweb_id == filmweb_id)
.all()
)
return [id_media for (id_media,) in result] # extract ids from tuples


#
# TASKS
#
Expand Down
152 changes: 90 additions & 62 deletions src/filman_server/routes/filmweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
# raise HTTPException(status_code=400, detail="Movie already exists")


@filmweb_router.post("/update/movie", response_model=schemas.FilmWebMovie)
@filmweb_router.post(
"/movie/update",
response_model=schemas.FilmWebMovie,
summary="Update/insert movie to database",
description="Updates movie in database if it exists, otherwise inserts it",
)
async def update_movie(
movie: schemas.FilmWebMovie,
db: Session = Depends(get_db),
Expand All @@ -51,12 +56,61 @@ async def update_movie(
raise HTTPException(status_code=400, detail="Movie already exists")


#
# FILMWEB USER MAPPING
#


@filmweb_router.post("/user/mapping/set", response_model=schemas.FilmWebUserMapping)
async def set_user_mapping(
user_mapping: schemas.FilmWebUserMappingCreate,
db: Session = Depends(get_db),
):
try:
db_user_mapping = crud.set_filmweb_user_mapping(db, user_mapping)
return db_user_mapping
except IntegrityError:
raise HTTPException(status_code=400, detail="User mapping already exists")


@filmweb_router.get("/user/mapping/get", response_model=schemas.FilmWebUserMapping)
async def get_user_mapping(
user_id: int | None = None,
filmweb_id: str | None = None,
discord_id: int | None = None,
db: Session = Depends(get_db),
):
if user_id is None and filmweb_id is None and discord_id is None:
raise HTTPException(status_code=400, detail="At least one of user_id, filmweb_id or discord_id must be provided")

user_mapping = crud.get_filmweb_user_mapping(db, user_id, filmweb_id, discord_id)
if user_mapping is None:
raise HTTPException(status_code=404, detail="User mapping not found")
return user_mapping


@filmweb_router.delete("/user/mapping/delete", response_model=schemas.FilmWebUserMapping)
async def delete_user_mapping(
user_id: int | None = None,
filmweb_id: str | None = None,
discord_id: int | None = None,
db: Session = Depends(get_db),
):
if user_id is None and filmweb_id is None and discord_id is None:
raise HTTPException(status_code=400, detail="At least one of user_id, filmweb_id or discord_id must be provided")

user_mapping = crud.delete_filmweb_user_mapping(db, user_id, filmweb_id, discord_id)
if user_mapping is None:
raise HTTPException(status_code=404, detail="User mapping not found")
return user_mapping


#
# MOVIES WATCHED
#


@filmweb_router.post("/watched/movies/add", response_model=schemas.FilmWebUserWatchedMovieCreate)
@filmweb_router.post("/user/watched/movies/add", response_model=schemas.FilmWebUserWatchedMovieCreate)
async def add_watched_movie(
user_watched_movie: schemas.FilmWebUserWatchedMovieCreate,
db: Session = Depends(get_db),
Expand All @@ -66,50 +120,24 @@ async def add_watched_movie(

# tu sie powinien tworzyc task do crawlera
# ze sie musi pobrac film z filmweba i dodac do bazy
# ~2 nie koniecznie

if db_movie is None or db_movie is IntegrityError:
raise HTTPException(status_code=404, detail="Movie not found")

return db_movie
except IntegrityError:
raise HTTPException(status_code=400, detail="Movie already in user watched")


@filmweb_router.get("/watched/movies/get/ids", response_model=List[int])
async def get_watched_movies_ids(
id: Optional[int] = None,
filmweb_id: Optional[str] = None,
discord_id: Optional[int] = None,
db: Session = Depends(get_db),
):
user = crud.get_user(db, id, filmweb_id, discord_id)
if user is None:
raise HTTPException(status_code=404, detail="User not found")

filmweb_id = user.filmweb_id

watched_movies = crud.get_filmweb_user_watched_movies_ids(db, filmweb_id)
raise HTTPException(status_code=400, detail="Movie is already in user watched")

if watched_movies is None:
raise HTTPException(status_code=404, detail="User has no watched movies")

return watched_movies


@filmweb_router.get("/watched/movies/get", response_model=List[schemas.FilmWebUserWatchedMovie])
@filmweb_router.get("/user/watched/movies/get", response_model=List[schemas.FilmWebUserWatchedMovie])
async def get_watched_movies(
id: Optional[int] = None,
user_id: Optional[int] = None,
filmweb_id: Optional[str] = None,
discord_id: Optional[int] = None,
db: Session = Depends(get_db),
):
user = crud.get_user(db, id, filmweb_id, discord_id)
if user is None:
raise HTTPException(status_code=404, detail="User not found")

filmweb_id = user.filmweb_id

watched_movies = crud.get_filmweb_user_watched_movies(db, filmweb_id)
watched_movies = crud.get_filmweb_user_watched_movies(db, user_id, filmweb_id, discord_id)

if watched_movies is None:
raise HTTPException(status_code=404, detail="User has no watched movies")
Expand All @@ -124,36 +152,36 @@ async def get_watched_movies(
#


@filmweb_router.get("/get/series", response_model=schemas.FilmWebSeries)
async def get_series(
id: Optional[int] = None,
db: Session = Depends(get_db),
):
series = crud.get_series_filmweb_id(db, id)
if series is None:
raise HTTPException(status_code=404, detail="Series not found")
return series
# @filmweb_router.get("/get/series", response_model=schemas.FilmWebSeries)
# async def get_series(
# id: Optional[int] = None,
# db: Session = Depends(get_db),
# ):
# series = crud.get_series_filmweb_id(db, id)
# if series is None:
# raise HTTPException(status_code=404, detail="Series not found")
# return series


@filmweb_router.post("/add/series", response_model=schemas.FilmWebSeries)
async def add_series(
series: schemas.FilmWebSeriesCreate,
db: Session = Depends(get_db),
):
try:
db_series = crud.create_filmweb_series(db, series)
return db_series
except IntegrityError:
raise HTTPException(status_code=400, detail="Series already exists")
# @filmweb_router.post("/add/series", response_model=schemas.FilmWebSeries)
# async def add_series(
# series: schemas.FilmWebSeriesCreate,
# db: Session = Depends(get_db),
# ):
# try:
# db_series = crud.create_filmweb_series(db, series)
# return db_series
# except IntegrityError:
# raise HTTPException(status_code=400, detail="Series already exists")


@filmweb_router.post("/update/series", response_model=schemas.FilmWebSeries)
async def update_series(
series: schemas.FilmWebSeries,
db: Session = Depends(get_db),
):
try:
db_series = crud.update_filmweb_series(db, series)
return db_series
except IntegrityError:
raise HTTPException(status_code=400, detail="Series already exists")
# @filmweb_router.post("/series/update", response_model=schemas.FilmWebSeries)
# async def update_series(
# series: schemas.FilmWebSeries,
# db: Session = Depends(get_db),
# ):
# try:
# db_series = crud.update_filmweb_series(db, series)
# return db_series
# except IntegrityError:
# raise HTTPException(status_code=400, detail="Series already exists")
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import logging

import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from filman_server.database import models
from filman_server.database.db import Base, get_db
from filman_server.main import app

SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


# Override the get_db dependency to use the test database
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()


app.dependency_overrides[get_db] = override_get_db

models.Base.metadata.create_all(bind=engine)

client = TestClient(app)


@pytest.fixture(scope="module")
def test_client():
yield client


@pytest.fixture(autouse=True)
def clear_database():
Base.metadata.drop_all(bind=engine)
Base.metadata.create_all(bind=engine)


# create 3 movies
# post /filmweb/update/movie
def test_update_movie(test_client):
test_movies_data = [
{
"id": 628,
"title": "Matrix",
"year": 1999,
"poster_url": "https://fwcdn.pl/fpo/06/28/628/7685907_1.10.webp",
"community_rate": 7.6,
},
{
"id": 1,
"title": "Paragraf 187",
"year": 1997,
"poster_url": "https://fwcdn.pl/fpo/00/01/1/7418875_1.10.webp",
"community_rate": 7.3,
},
{
"id": 2,
"title": "Adwokat diabła",
"year": 1997,
"poster_url": "https://fwcdn.pl/fpo/00/02/2/6956729_1.10.webp",
"community_rate": 7.9,
},
]

for movie in test_movies_data:
response = test_client.post("/filmweb/movie/update", json=movie)
assert response.status_code == 200
assert response.json() == movie


# add watched movie for user
def test_add_watched_movie(test_client):
test_users_data = [
{"discord_id": 321309474667253282},
{"discord_id": 321309474167253283},
{"discord_id": 321309474267253284},
]

test_users_filmweb_ids = [
"maciek",
"steafan",
"albert",
]

test_movies_data = [
{
"id": 628,
"title": "Matrix",
"year": 1999,
"poster_url": "https://fwcdn.pl/fpo/06/28/628/7685907_1.10.webp",
"community_rate": 7.6,
},
{
"id": 1,
"title": "Paragraf 187",
"year": 1997,
"poster_url": "https://fwcdn.pl/fpo/00/01/1/7418875_1.10.webp",
"community_rate": 7.3,
},
{
"id": 2,
"title": "Adwokat diabła",
"year": 1997,
"poster_url": "https://fwcdn.pl/fpo/00/02/2/6956729_1.10.webp",
"community_rate": 7.9,
},
]

for movie in test_movies_data:
response = test_client.post("/filmweb/movie/update", json=movie)
assert response.status_code == 200
assert response.json() == movie

# create users
for user_data in test_users_data:
response = test_client.post("/users/create", json=user_data)
assert response.status_code == 200

user = response.json()

assert user["discord_id"] == user_data["discord_id"]

# map users to filmweb ids
for filweb_id, user_id in zip(test_users_filmweb_ids, [1, 2, 3]):
response = test_client.post("/filmweb/user/mapping/set", json={"user_id": user_id, "filmweb_id": filweb_id})
assert response.status_code == 200

# check user mappings via ids
for user_id, filmweb_id in zip([1, 2, 3], test_users_filmweb_ids):
response = test_client.get("/filmweb/user/mapping/get", params={"user_id": user_id})
assert response.status_code == 200
assert response.json()["filmweb_id"] == filmweb_id

# check user mappings via filmweb ids
for user_id, filmweb_id in zip([1, 2, 3], test_users_filmweb_ids):
response = test_client.get("/filmweb/user/mapping/get", params={"filmweb_id": filmweb_id})
assert response.status_code == 200
assert response.json()["user_id"] == user_id

# check user mappings via discord ids
for user_id, discord_id in zip([1, 2, 3], [321309474667253282, 321309474167253283, 321309474267253284]):
response = test_client.get("/filmweb/user/mapping/get", params={"discord_id": discord_id})
assert response.status_code == 200
assert response.json()["user_id"] == user_id

# let user 1 watch 1 of the movies
response = test_client.post(
"/filmweb/user/watched/movies/add",
json={
"id_media": 628,
"filmweb_id": "maciek",
"date": "2024-09-11T20:21:58.072Z",
"rate": 7,
"comment": "good movie",
"favorite": False,
},
)
assert response.status_code == 200

# check if the movie was added to the watched movies
response = test_client.get("/filmweb/user/watched/movies/get", params={"user_id": 1})
assert response.status_code == 200
assert response.json()[0]["movie"]["id"] == 628
assert response.json()[0]["rate"] == 7
assert response.json()[0]["comment"] == "good movie"
assert response.json()[0]["favorite"] == False

# l

0 comments on commit 43b5784

Please sign in to comment.