finished controller unittests
This commit is contained in:
@@ -7,12 +7,31 @@ from app.models.group import Group
|
||||
|
||||
|
||||
class GroupController(BaseController):
|
||||
def __init__(self) -> None:
|
||||
"""
|
||||
Handles all group-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.
|
||||
|
||||
The corresponding table contains only one row.
|
||||
|
||||
Testing: pass a mocked GroupQueries object to the constructor.
|
||||
"""
|
||||
|
||||
def __init__(self, group_queries=group_queries) -> None:
|
||||
super().__init__()
|
||||
self.db: GroupQueries = group_queries
|
||||
self.group_queries: GroupQueries = group_queries
|
||||
|
||||
def get_group(self) -> Group:
|
||||
if (data := self.db.select_one_by_id()) is None:
|
||||
"""Retrieves the group from the database and returns it as a Group object.
|
||||
|
||||
Raises:
|
||||
HTTPException: If the group is not found (status code 404)
|
||||
HTTPException: If any error occurs during the retrieval process (status code 500)
|
||||
|
||||
Returns:
|
||||
Group: A Group object which is suitable for a response body
|
||||
"""
|
||||
if (data := self.group_queries.select_one_by_id()) is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="Group not found"
|
||||
)
|
||||
@@ -25,8 +44,19 @@ class GroupController(BaseController):
|
||||
)
|
||||
|
||||
def update_group_bio(self, bio: str) -> Group:
|
||||
"""Updates the group's bio in the database and returns the updated Group object.
|
||||
|
||||
Args:
|
||||
bio (str): The new bio for the group
|
||||
|
||||
Raises:
|
||||
HTTPException: If any error occurs during the update process (status code 500)
|
||||
|
||||
Returns:
|
||||
Group: The updated Group object which is suitable for a response body
|
||||
"""
|
||||
try:
|
||||
self.db.update_group_bio(bio)
|
||||
self.group_queries.update_group_bio(bio)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
@@ -7,7 +7,7 @@ class UserQueries(BaseQueries):
|
||||
super().__init__()
|
||||
self.table = USER_TABLE
|
||||
|
||||
def get_one_by_email(self, email: str) -> dict | None:
|
||||
def select_one_by_email(self, email: str) -> dict | None:
|
||||
query = f"SELECT * FROM {self.table} WHERE email = %s"
|
||||
db = self.connect_db()
|
||||
cursor = db.cursor(dictionary=True)
|
||||
@@ -18,7 +18,7 @@ class UserQueries(BaseQueries):
|
||||
|
||||
return data
|
||||
|
||||
def get_one_by_sub(self, sub: str) -> dict | None:
|
||||
def select_one_by_sub(self, sub: str) -> dict | None:
|
||||
query = f"SELECT * FROM {self.table} WHERE sub = %s"
|
||||
db = self.connect_db()
|
||||
cursor = db.cursor(dictionary=True)
|
||||
|
||||
Reference in New Issue
Block a user