Skip to content

Commit

Permalink
test: filmweb tests
Browse files Browse the repository at this point in the history
  • Loading branch information
suchencjusz committed Sep 12, 2024
1 parent 43b5784 commit 8ed33fc
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 8 deletions.
13 changes: 6 additions & 7 deletions src/filman_server/routes/filmweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def update_movie(
db_movie = crud.update_filmweb_movie(db, movie)
return db_movie
except IntegrityError:
raise HTTPException(status_code=400, detail="Movie already exists")
raise HTTPException(status_code=400, detail="Integrity error")


#
Expand Down Expand Up @@ -89,7 +89,7 @@ async def get_user_mapping(
return user_mapping


@filmweb_router.delete("/user/mapping/delete", response_model=schemas.FilmWebUserMapping)
@filmweb_router.delete("/user/mapping/delete", response_model=bool)
async def delete_user_mapping(
user_id: int | None = None,
filmweb_id: str | None = None,
Expand All @@ -102,6 +102,7 @@ async def delete_user_mapping(
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


Expand Down Expand Up @@ -132,18 +133,16 @@ async def add_watched_movie(

@filmweb_router.get("/user/watched/movies/get", response_model=List[schemas.FilmWebUserWatchedMovie])
async def get_watched_movies(
user_id: Optional[int] = None,
filmweb_id: Optional[str] = None,
discord_id: Optional[int] = None,
user_id: int | None = None,
filmweb_id: int | None = None,
discord_id: int | None = None,
db: Session = Depends(get_db),
):
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")

print(watched_movies)

return watched_movies


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,196 @@ def test_add_watched_movie(test_client):
assert response.json()[0]["comment"] == "good movie"
assert response.json()[0]["favorite"] == False

# l
# let user 2 watch 2 of the movies
response = test_client.post(
"/filmweb/user/watched/movies/add",
json={
"id_media": 1,
"filmweb_id": "steafan",
"date": "2024-09-11T20:21:58.072Z",
"rate": 5,
"comment": None,
"favorite": False,
},
)
assert response.status_code == 200
assert response.json()["id_media"] == 1
assert response.json()["filmweb_id"] == "steafan"
assert response.json()["rate"] == 5
assert response.json()["comment"] == None
assert response.json()["favorite"] == False

response = test_client.post(
"/filmweb/user/watched/movies/add",
json={
"id_media": 2,
"filmweb_id": "steafan",
"date": "2024-09-11T20:21:58.072Z",
"rate": 2,
"comment": "awesome",
"favorite": True,
},
)
assert response.status_code == 200
assert response.json()["id_media"] == 2
assert response.json()["filmweb_id"] == "steafan"
assert response.json()["rate"] == 2
assert response.json()["comment"] == "awesome"
assert response.json()["favorite"] == True

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

assert response.json()[1]["movie"]["id"] == 2
assert response.json()[1]["rate"] == 2
assert response.json()[1]["comment"] == "awesome"
assert response.json()[1]["favorite"] == True

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

response = test_client.post(
"/filmweb/user/watched/movies/add",
json={
"id_media": 1,
"filmweb_id": "albert",
"date": "2024-09-11T20:21:58.072Z",
"rate": 5,
"comment": None,
"favorite": False,
},
)
assert response.status_code == 200
assert response.json()["id_media"] == 1
assert response.json()["filmweb_id"] == "albert"
assert response.json()["rate"] == 5
assert response.json()["comment"] == None
assert response.json()["favorite"] == False

response = test_client.post(
"/filmweb/user/watched/movies/add",
json={
"id_media": 2,
"filmweb_id": "albert",
"date": "2024-09-11T20:21:58.072Z",
"rate": 2,
"comment": "awesome",
"favorite": True,
},
)
assert response.status_code == 200
assert response.json()["id_media"] == 2
assert response.json()["filmweb_id"] == "albert"
assert response.json()["rate"] == 2
assert response.json()["comment"] == "awesome"
assert response.json()["favorite"] == True

# check if the movies were added to the watched movies
response = test_client.get("/filmweb/user/watched/movies/get", params={"user_id": 3})
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

assert response.json()[1]["movie"]["id"] == 1
assert response.json()[1]["rate"] == 5
assert response.json()[1]["comment"] == None
assert response.json()[1]["favorite"] == False

assert response.json()[2]["movie"]["id"] == 2
assert response.json()[2]["rate"] == 2
assert response.json()[2]["comment"] == "awesome"
assert response.json()[2]["favorite"] == True

def test_user_mapping_delete(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

# remove user 1 mapping
response = test_client.delete("/filmweb/user/mapping/delete", params={"user_id": 1})
assert response.status_code == 200

# check user 1 watched movies
response = test_client.get("/filmweb/user/watched/movies/get", params={"user_id": 1})
assert response.status_code == 404

0 comments on commit 8ed33fc

Please sign in to comment.