diff --git a/backend/project/models/course_relations.py b/backend/project/models/course_relations.py index 7b30ad48..0677ef54 100644 --- a/backend/project/models/course_relations.py +++ b/backend/project/models/course_relations.py @@ -3,6 +3,8 @@ from sqlalchemy import Integer, Column, ForeignKey, PrimaryKeyConstraint, String from project import db +from project.models.users import Users +from project.models.courses import Courses class BaseCourseRelation(db.Model): """Base class for course relation models, @@ -12,9 +14,11 @@ class BaseCourseRelation(db.Model): __abstract__ = True - course_id = Column(Integer, ForeignKey('Courses.course_id'), nullable=False) - uid = Column(String(255), ForeignKey("Users.uid"), nullable=False) - __table_args__ = PrimaryKeyConstraint("course_id", "uid") + course_id = Column(Integer, ForeignKey('courses.course_id'), nullable=False) + uid = Column(String(255), ForeignKey("users.uid"), nullable=False) + __table_args__ = ( + PrimaryKeyConstraint("course_id", "uid"), + ) class CourseAdmins(BaseCourseRelation): """Admin to course relation model""" diff --git a/backend/project/models/courses.py b/backend/project/models/courses.py index 7a105881..0881ca56 100644 --- a/backend/project/models/courses.py +++ b/backend/project/models/courses.py @@ -2,7 +2,7 @@ # pylint: disable=too-few-public-methods from sqlalchemy import Integer, Column, ForeignKey, String from project import db - +from project.models.users import Users class Courses(db.Model): """This class described the courses table, @@ -12,4 +12,4 @@ class Courses(db.Model): course_id = Column(Integer, primary_key=True) name = Column(String(50), nullable=False) ufora_id = Column(String(50), nullable=True) - teacher = Column(String(255), ForeignKey("Users.uid"), nullable=False) + teacher = Column(String(255), ForeignKey("users.uid"), nullable=False) diff --git a/backend/project/models/model_test.py b/backend/project/models/model_test.py new file mode 100644 index 00000000..0226f58e --- /dev/null +++ b/backend/project/models/model_test.py @@ -0,0 +1,85 @@ +""" +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/project/models/projects.py b/backend/project/models/projects.py index 68f68ae9..a64f6082 100644 --- a/backend/project/models/projects.py +++ b/backend/project/models/projects.py @@ -2,6 +2,7 @@ # pylint: disable=too-few-public-methods from sqlalchemy import ARRAY, Boolean, Column, DateTime, ForeignKey, Integer, String, Text from project import db +from project.models.courses import Courses class Projects(db.Model): """This class describes the projects table, @@ -19,7 +20,7 @@ class Projects(db.Model): descriptions = Column(Text, nullable=False) assignment_file = Column(String(50)) deadline = Column(DateTime(timezone=True)) - course_id = Column(Integer, ForeignKey("Courses.course_id"), nullable=False) + course_id = Column(Integer, ForeignKey("courses.course_id"), nullable=False) visible_for_students = Column(Boolean, nullable=False) archieved = Column(Boolean, nullable=False) test_path = Column(String(50)) diff --git a/backend/project/models/submissions.py b/backend/project/models/submissions.py index 5b20cee4..f9de28b4 100644 --- a/backend/project/models/submissions.py +++ b/backend/project/models/submissions.py @@ -2,6 +2,7 @@ # pylint: disable=too-few-public-methods from sqlalchemy import Column,String,ForeignKey,Integer,CheckConstraint,DateTime,Boolean from project import db +from project.models.users import Users class Submissions(db.Model): """This class describes the submissions table, @@ -16,8 +17,8 @@ class Submissions(db.Model): __tablename__ = "submissions" submission_id = Column(Integer, nullable=False, primary_key=True) - uid = Column(String(255), ForeignKey("Users.uid"), nullable=False) - project_id = Column(Integer, ForeignKey("Projects.project_id"), nullable=False) + uid = Column(String(255), ForeignKey("users.uid"), nullable=False) + project_id = Column(Integer, ForeignKey("projects.project_id"), nullable=False) grading = Column(Integer, CheckConstraint("grading >= 0 AND grading <= 20")) submission_time = Column(DateTime(timezone=True), nullable=False) submission_path = Column(String(50), nullable=False)