finished controller unittests

This commit is contained in:
Lucas Jensen
2024-05-03 18:19:20 -07:00
parent 3812ca272f
commit e2b3978fec
6 changed files with 300 additions and 18 deletions

View File

@@ -9,11 +9,28 @@ from app.models.user import User
class UserController(BaseController):
def __init__(self) -> None:
"""
Handles all user-related operations and serves as an intermediate controller between
the main controller and the model layer.
Inherits from BaseController, which provides logging and other generic methods.
Testing: pass a mocked UserQueries object to the constructor.
"""
def __init__(self, user_queries=user_queries) -> None:
super().__init__()
self.db: UserQueries = user_queries
def get_users(self) -> list[User]:
"""Retrieves all users from the database and returns them as a list of User objects.
Raises:
HTTPException: If any error occurs during the retrieval process (status code 500)
Returns:
list[User]: A list of User objects which are suitable for a response body
"""
data = self.db.select_all_series()
try:
return [User(**e) for e in data]
@@ -23,8 +40,20 @@ class UserController(BaseController):
detail=f"Error creating user objects: {e}",
)
def get_user_by_id(self, id: int) -> User:
if (data := self.db.select_one_by_id(id)) is None:
def get_user_by_id(self, user_id: int) -> User:
"""Retrieves a single user from the database and returns it as a User object.
Args:
user_id (int): The ID of the user to retrieve
Raises:
HTTPException: If the user is not found (status code 404)
HTTPException: If any error occurs during the retrieval process (status code 500)
Returns:
User: A User object which is suitable for a response body
"""
if (data := self.db.select_one_by_id(user_id)) is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
)
@@ -37,7 +66,19 @@ class UserController(BaseController):
)
def get_user_by_email(self, email: str) -> User:
if (data := self.db.get_one_by_email(email)) is None:
"""Retrieves a single user from the database and returns it as a User object.
Args:
email (str): The email of the user to retrieve
Raises:
HTTPException: If the user is not found (status code 404)
HTTPException: If any error occurs during the retrieval process (status code 500)
Returns:
User: A User object which is suitable for a response body
"""
if (data := self.db.select_one_by_email(email)) is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="User does not exist"
)
@@ -50,7 +91,19 @@ class UserController(BaseController):
)
def get_user_by_sub(self, sub: str) -> User:
if (data := self.db.get_one_by_sub(sub)) is None:
"""Retrieves a single user from the database and returns it as a User object.
Args:
sub (str): The sub of the user to retrieve
Raises:
HTTPException: If the user is not found (status code 404)
HTTPException: If any error occurs during the retrieval process (status code 500)
Returns:
User: A User object which is suitable for a response body
"""
if (data := self.db.select_one_by_sub(sub)) is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
)