Skip to content

Commit

Permalink
A first succesfull test for user model
Browse files Browse the repository at this point in the history
  • Loading branch information
JibrilExe committed Feb 22, 2024
1 parent ba36b8a commit a54e712
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
10 changes: 7 additions & 3 deletions backend/project/models/course_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"""
Expand Down
4 changes: 2 additions & 2 deletions backend/project/models/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
85 changes: 85 additions & 0 deletions backend/project/models/model_test.py
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion backend/project/models/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
Expand Down
5 changes: 3 additions & 2 deletions backend/project/models/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit a54e712

Please sign in to comment.