Fix tests for users and highscores
This commit is contained in:
@@ -3,7 +3,15 @@ from passlib.context import CryptContext
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.models import User
|
||||
from src.crud.highscores import (get_highest_high_scores,
|
||||
get_most_recent_high_scores)
|
||||
from src.schemas.users import UserCreate
|
||||
from src.enums import CourseEnum, MinigameEnum
|
||||
from src.models import CourseProgress, LearnableProgress
|
||||
from src.schemas.highscores import SavedMinigameProgress
|
||||
from src.schemas.courseprogress import SavedCourseProgress
|
||||
from src.schemas.learnableprogress import SavedLearnableProgress
|
||||
from src.schemas.users import SavedUser
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
@@ -20,15 +28,26 @@ def check_empty_fields(username: str, password: str, avatar_index: int):
|
||||
|
||||
def patch_user(db: Session, username: str, user: UserCreate):
|
||||
"""Changes the username and/or the password of a User"""
|
||||
check_empty_fields(user.username, user.password, user.avatar_index)
|
||||
#check_empty_fields(user.username, user.password, user.avatar_index)
|
||||
db_user = get_user_by_username(db, username)
|
||||
potential_duplicate = get_user_by_username(db, user.username)
|
||||
if potential_duplicate:
|
||||
if potential_duplicate.user_id != db_user.user_id:
|
||||
raise HTTPException(status_code=400, detail="Username already registered")
|
||||
db_user.username = user.username
|
||||
db_user.hashed_password = pwd_context.hash(user.password)
|
||||
db_user.avatar_index = user.avatar_index
|
||||
|
||||
if user.playtime < 0:
|
||||
raise HTTPException(status_code=400, detail="Negative playtime is invalid")
|
||||
|
||||
if len(user.username) > 0:
|
||||
db_user.username = user.username
|
||||
|
||||
if len(user.password) > 0:
|
||||
db_user.hashed_password = pwd_context.hash(user.password)
|
||||
|
||||
if user.avatar_index > -1:
|
||||
db_user.avatar_index = user.avatar_index
|
||||
|
||||
db_user.playtime += user.playtime
|
||||
db.commit()
|
||||
|
||||
|
||||
@@ -40,3 +59,76 @@ def get_user_by_username(db: Session, username: str):
|
||||
def get_users(db: Session):
|
||||
"""Fetch a list of all users"""
|
||||
return db.query(User).all()
|
||||
|
||||
|
||||
def get_saved_data(db: Session, username: str):
|
||||
"""Fetches all saved progress for the current user from the database"""
|
||||
user = get_user_by_username(db, username)
|
||||
minigames = []
|
||||
courses = []
|
||||
|
||||
for minigame in MinigameEnum:
|
||||
minigames.append(
|
||||
SavedMinigameProgress(
|
||||
minigame_index=minigame,
|
||||
latest_scores=[
|
||||
score for score in get_most_recent_high_scores(db, minigame, 10)
|
||||
],
|
||||
highest_scores=[
|
||||
score
|
||||
for score in get_highest_high_scores(db, minigame, user, 10, False)
|
||||
],
|
||||
)
|
||||
)
|
||||
|
||||
course_progress_query = (
|
||||
db.query(CourseProgress).filter(CourseProgress.owner_id == user.user_id).all()
|
||||
)
|
||||
|
||||
for course_progress in course_progress_query:
|
||||
learnable_progress_query = (
|
||||
db.query(LearnableProgress)
|
||||
.filter(
|
||||
LearnableProgress.course_progress_id
|
||||
== course_progress.course_progress_id
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
learnables = [
|
||||
SavedLearnableProgress(
|
||||
index=lp.index, in_use=lp.in_use, name=lp.name, progress=lp.progress
|
||||
)
|
||||
for lp in learnable_progress_query
|
||||
]
|
||||
|
||||
completed_learnables = sum(
|
||||
[1 if learnable.progress == 5.0 else 0 for learnable in learnables]
|
||||
)
|
||||
|
||||
in_use_learnables = sum(
|
||||
[1 if learnable.in_use else 0 for learnable in learnables]
|
||||
)
|
||||
|
||||
total_learnables = len(learnables)
|
||||
|
||||
courses.append(
|
||||
SavedCourseProgress(
|
||||
course_index=course_progress.course,
|
||||
progress=course_progress.progress,
|
||||
completed_learnables=completed_learnables,
|
||||
in_use_learnables=in_use_learnables,
|
||||
total_learnables=total_learnables,
|
||||
learnables=learnables,
|
||||
)
|
||||
)
|
||||
|
||||
user_progress = SavedUser(
|
||||
username=user.username,
|
||||
avatar_index=user.avatar_index,
|
||||
playtime=user.playtime,
|
||||
minigames=minigames,
|
||||
courses=courses,
|
||||
)
|
||||
|
||||
return user_progress
|
||||
|
||||
Reference in New Issue
Block a user