From 0802c3ddf04cf1e1102154f31a28b763659685fd Mon Sep 17 00:00:00 2001 From: Aron Buzogany Date: Fri, 23 Feb 2024 10:50:25 +0100 Subject: [PATCH] created test directory to test models --- backend/project/models/model_test.py | 85 ---------------------------- backend/tests/models/conftest.py | 42 ++++++++++++++ backend/tests/models/users_test.py | 10 ++++ 3 files changed, 52 insertions(+), 85 deletions(-) delete mode 100644 backend/project/models/model_test.py create mode 100644 backend/tests/models/conftest.py create mode 100644 backend/tests/models/users_test.py diff --git a/backend/project/models/model_test.py b/backend/project/models/model_test.py deleted file mode 100644 index 0226f58e..00000000 --- a/backend/project/models/model_test.py +++ /dev/null @@ -1,85 +0,0 @@ -""" -To run this test make sure you are in the backend folder and run: -python -m unittest .\project\models\model_test.py -""" - -import unittest -import os -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker -from project import db -from project.models.courses import Courses -from project.models.course_relations import CourseAdmins, CourseStudents -from project.models.projects import Projects -from project.models.submissions import Submissions -from project.models.users import Users -from dotenv import load_dotenv - - -class TestModels(unittest.TestCase): - """Test class for the database models""" - - def setUp(self): - # Create an in-memory SQLite database engine - # Load environment variables from .env file - load_dotenv() - - # Access the environment variables - db_host = os.getenv('DB_HOST') - engine = create_engine(db_host) - - # Create tables defined by your models - db.metadata.create_all(engine) - - # Create a session for interacting with the database - session = sessionmaker(bind=engine) - self.session = session() - - - def tearDown(self): - # Flush and close the session - self.session.expire_all() - self.session.rollback() - self.session.close() - - # Delete all data from each table - for table in reversed(db.metadata.sorted_tables): - self.session.execute(table.delete()) - - # Commit the transaction - self.session.commit() - - def test_users(self): - """Test function for user model, - first i simply test storing and retrieving a student, - then i test if a query for only the teachers/admin works""" - user = Users(uid="student", is_teacher=False, is_admin=False) - - # Add the user to the session - self.session.add(user) - - # Commit the session to persist the user in the database - self.session.commit() - - # Query the database for the user based on their uid - retrieved_user = self.session.query(Users).filter_by(uid="student").first() - - # Assert that the retrieved user matches the one we created - self.assertIsNotNone(retrieved_user) - self.assertEqual(retrieved_user.uid, "student") - self.assertEqual(retrieved_user.is_teacher, False) - self.assertEqual(retrieved_user.is_admin, False) - - for i in range(10): - user = Users(uid=str(i),is_teacher=True,is_admin=False) - self.session.add(user) - self.session.commit() - teacher_count = 0 - for usr in self.session.query(Users).filter_by(is_teacher=True): - teacher_count+=1 - self.assertTrue(usr.is_teacher) - self.assertEqual(teacher_count,10) - - -if __name__ == "__main__": - unittest.main() diff --git a/backend/tests/models/conftest.py b/backend/tests/models/conftest.py new file mode 100644 index 00000000..5642bd9d --- /dev/null +++ b/backend/tests/models/conftest.py @@ -0,0 +1,42 @@ +""" + +""" +import os +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from project import db +from project.models.users import Users +from sqlalchemy.engine.url import URL +from dotenv import load_dotenv +import pytest + +load_dotenv() + +DATABSE_NAME = os.getenv('POSTGRES_DB') +DATABASE_USER = os.getenv('POSTGRES_USER') +DATABASE_PASSWORD = os.getenv('POSTGRES_PASSWORD') +DATABASE_HOST = os.getenv('POSTGRES_HOST') + +url = URL.create( + drivername="postgresql", + username=DATABASE_USER, + host=DATABASE_HOST, + database=DATABSE_NAME, + password=DATABASE_PASSWORD +) + +engine = create_engine(url) +Session = sessionmaker(bind=engine) + +@pytest.fixture +def db_session(): + db.metadata.create_all(engine) + session = Session() + yield session + session.rollback() + session.close() + +@pytest.fixture +def valid_user(): + user = Users(uid="student", is_teacher=False, is_admin=False) + return user diff --git a/backend/tests/models/users_test.py b/backend/tests/models/users_test.py new file mode 100644 index 00000000..e2117fef --- /dev/null +++ b/backend/tests/models/users_test.py @@ -0,0 +1,10 @@ +from project.models.users import Users + + +class TestModels: + """Test class for the database models""" + + def test_valid_user(self, db_session, valid_user): + db_session.add(valid_user) + db_session.commit() + assert valid_user in db_session.query(Users).all() \ No newline at end of file