Skip to content

Commit

Permalink
constructing pytests for models
Browse files Browse the repository at this point in the history
  • Loading branch information
JibrilExe committed Feb 23, 2024
1 parent 88757ae commit 2312254
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 233 deletions.
230 changes: 0 additions & 230 deletions backend/project/models/model_test.py

This file was deleted.

53 changes: 52 additions & 1 deletion backend/tests/models/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import os
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from datetime import datetime
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 sqlalchemy.engine.url import URL
from dotenv import load_dotenv
Expand Down Expand Up @@ -39,4 +44,50 @@ def db_session():
@pytest.fixture
def valid_user():
user = Users(uid="student", is_teacher=False, is_admin=False)
return user
return user

@pytest.fixture
def teachers():
users = [Users(uid=str(i), is_teacher=True, is_admin=False) for i in range(10)]
return users

@pytest.fixture
def course_teacher():
sel2_teacher = Users(uid="Bart", is_teacher=True, is_admin=False)
return sel2_teacher

@pytest.fixture
def course(course_teacher):
sel2 = Courses(name="Sel2", teacher=course_teacher.uid)
return sel2

@pytest.fixture
def course_students():
students = [
Users(uid="student_sel2_" + str(i), is_teacher=False, is_admin=False)
for i in range(5)
]
return students

@pytest.fixture
def course_students_relation(course,course_students):
course_relations = [
CourseStudents(course_id=course.course_id, uid=course_students[i].uid)
for i in range(5)
]
return course_relations

@pytest.fixture
def assistent():
assist = Users(uid="assistent_sel2")
return assist

@pytest.fixture()
def course_admin(course,assistent):
admin_relation = CourseAdmins(uid=assistent.uid, course_id=course.course_id)
return admin_relation

@pytest.fixture()
def teacher():
user = Users(uid="teacher", is_teacher=True)
return user
93 changes: 93 additions & 0 deletions backend/tests/models/course_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import pytest
from sqlalchemy.exc import IntegrityError
from project.models.courses import Courses
from project.models.users import Users
from project.models.course_relations import CourseAdmins, CourseStudents


class TestUserModel:
"""Test class for the database models"""

def test_foreignkey_courses_teacher(self, db_session, course: Courses):
"""Tests the foreign key relation between courses and the teacher uid"""
with pytest.raises(
IntegrityError,
match="Courses should throw a foreign key error on the teacher uid",
):
db_session.add(course)
db_session.commit()

def test_correct_course(self, db_session, course: Courses, course_teacher: Users):
"""Tests wether added course and a teacher are correctly connected"""
db_session.add(course_teacher)
db_session.commit()

db_session.add(course)
db_session.commit()
assert (
db_session.query(Courses).filter_by(name=course.name).first().teacher
== course_teacher.uid
)

def test_foreignkey_coursestudents_uid(
self, db_session, course, course_teacher, course_students_relation
):
"""Test the foreign key of the CourseStudent related to the student uid"""
db_session.add(course_teacher)
db_session.commit()

db_session.add(course)
db_session.commit()

with pytest.raises(
IntegrityError,
match="Course_relations should throw a foreign key error on the student uid",
):
db_session.add_all(course_students_relation)
db_session.commit()

def test_correct_courserelations(
self,
db_session,
course,
course_teacher,
course_students,
course_students_relation,
assistent,
course_admin,
):
"""Tests if we get the expected results for correct usage of CourseStudents and CourseAdmins"""
db_session.add(course_teacher)
db_session.commit()

db_session.add(course)
db_session.commit()

db_session.add_all(course_students)
db_session.commit()

db_session.add_all(course_students_relation)
db_session.commit()

student_check = [
s.uid
for s in db_session.query(CourseStudents)
.filter_by(course_id=course.course_id)
.all()
]
student_uids = [s.uid for s in course_students]
assert student_check == student_uids

db_session.add(assistent)
db_session.commit()

db_session.add(course_admin)
db_session.commit()

assert (
db_session.query(CourseAdmins)
.filter_by(course_id=course.course_id)
.first()
.uid
== assistent.uid
)
Loading

0 comments on commit 2312254

Please sign in to comment.