From 3006e43861af79446d7cbebb7879af224820b18c Mon Sep 17 00:00:00 2001 From: warre Date: Sun, 25 Feb 2024 16:55:04 +0100 Subject: [PATCH] fixed linter --- backend/project/endpoints/users.py | 57 +++++++++++++++------------- backend/tests/__init__.py | 12 +++++- backend/tests/endpoints/conftest.py | 2 +- backend/tests/endpoints/user_test.py | 13 ++++++- backend/tests/models/conftest.py | 3 -- 5 files changed, 53 insertions(+), 34 deletions(-) diff --git a/backend/project/endpoints/users.py b/backend/project/endpoints/users.py index 33b70454..f7534e68 100644 --- a/backend/project/endpoints/users.py +++ b/backend/project/endpoints/users.py @@ -17,7 +17,8 @@ def get(self): It should return all users from the database. """ users = UserModel.query.all() - users_list = [{"uid": user.uid, "is_teacher": user.is_teacher, "is_admin": user.is_admin} for user in users] + users_list = [{"uid": user.uid, "is_teacher": user.is_teacher, "is_admin": user.is_admin} + for user in users] return users_list def post(self): @@ -47,31 +48,33 @@ def post(self): return {"Message": "User created successfully!"} def patch(self): - """ - Update the user's information. - - Returns: - dict: A dictionary containing the message indicating the success or failure of the update. - """ - uid = request.json.get('uid') - is_teacher = request.json.get('is_teacher') - is_admin = request.json.get('is_admin') - if uid is None: - return {"Message": "User ID is required!"}, 400 - - user = UserModel.query.get(uid) - if user is None: - return {"Message": "User not found!"}, 404 - - if is_teacher is not None: - user.is_teacher = is_teacher - if is_admin is not None: - user.is_admin = is_admin - - # Save the changes to the database - db.session.commit() - return {"Message": "User updated successfully!"} - + """ + Update the user's information. + + Returns: + dict: A dictionary containing the message indicating the success + or failure of the update. + """ + uid = request.json.get('uid') + is_teacher = request.json.get('is_teacher') + is_admin = request.json.get('is_admin') + if uid is None: + return {"Message": "User ID is required!"}, 400 + + + user = db.session.get(UserModel,uid) + if user is None: + return {"Message": "User not found!"}, 404 + + if is_teacher is not None: + user.is_teacher = is_teacher + if is_admin is not None: + user.is_admin = is_admin + + # Save the changes to the database + db.session.commit() + return {"Message": "User updated successfully!"} + def delete(self): """ This function will respond to DELETE requests made to /users. @@ -82,7 +85,7 @@ def delete(self): if uid is None: return {"Message": "User ID is required!"}, 400 - user = UserModel.query.get(uid) + user = db.session.get(UserModel, uid) if user is None: return {"Message": "User not found!"}, 404 diff --git a/backend/tests/__init__.py b/backend/tests/__init__.py index 3670f956..ec43b874 100644 --- a/backend/tests/__init__.py +++ b/backend/tests/__init__.py @@ -1,3 +1,13 @@ +""" +This module is used to create a SQLAlchemy URL object for a PostgreSQL database. + +It uses environment variables to get the necessary database configuration details: +- 'POSTGRES_DB': The name of the database. +- 'POSTGRES_USER': The username to connect to the database. +- 'POSTGRES_PASSWORD': The password to connect to the database. +- 'POSTGRES_HOST': The host where the database is located. + +""" import os from sqlalchemy.engine.url import URL from dotenv import load_dotenv @@ -13,4 +23,4 @@ host=DATABASE_HOST, database=DATABSE_NAME, password=DATABASE_PASSWORD -) \ No newline at end of file +) diff --git a/backend/tests/endpoints/conftest.py b/backend/tests/endpoints/conftest.py index a4bf9339..20c6961a 100644 --- a/backend/tests/endpoints/conftest.py +++ b/backend/tests/endpoints/conftest.py @@ -1,7 +1,7 @@ """ Configuration for pytest, Flask, and the test client.""" import pytest -from project import create_app_with_db from sqlalchemy import create_engine +from project import create_app_with_db from project import db from tests import db_url diff --git a/backend/tests/endpoints/user_test.py b/backend/tests/endpoints/user_test.py index e2c0f127..236d903d 100644 --- a/backend/tests/endpoints/user_test.py +++ b/backend/tests/endpoints/user_test.py @@ -1,3 +1,12 @@ +""" +This module tests user management endpoints. + +- test_post_delete_user: Tests user creation, deletion, and error handling for deletion + of non-existent user. +- test_get_users: Tests retrieval of all users, ensuring the response is a list. +- test_patch_user: Tests user update functionality and error handling for updating + non-existent user. +""" def test_post_delete_user(client): """Test whether the users page is accessible""" response = client.post("/users", json={ @@ -9,7 +18,7 @@ def test_post_delete_user(client): # Delete the user response = client.delete("/users", json={'uid': 'del'}) assert response.status_code == 200 - assert response.json == {"Message": f"User with id: del deleted successfully!"} + assert response.json == {"Message": "User with id: del deleted successfully!"} # Try to delete the user again response = client.delete("/users", json={'uid': 'del'}) @@ -53,4 +62,4 @@ def test_patch_user(client): 'is_teacher': False, 'is_admin': True }) - assert response.status_code == 404 \ No newline at end of file + assert response.status_code == 404 diff --git a/backend/tests/models/conftest.py b/backend/tests/models/conftest.py index d21d7b9a..a3d44c66 100644 --- a/backend/tests/models/conftest.py +++ b/backend/tests/models/conftest.py @@ -2,12 +2,9 @@ Configuration for the models tests. Contains all the fixtures needed for multiple models tests. """ -import os from datetime import datetime from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -from sqlalchemy.engine.url import URL -from dotenv import load_dotenv import pytest from project import db from project.models.courses import Courses