332 lines
12 KiB
Python
332 lines
12 KiB
Python
import pytest
|
|
from datetime import datetime, date
|
|
from models import db, ClassGroup, Student, Assessment, Exercise, GradingElement, Grade
|
|
from .conftest import create_student_with_enrollment
|
|
|
|
|
|
class TestClassGroup:
|
|
def test_create_class_group(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", description="Classe de 6ème A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
assert class_group.id is not None
|
|
assert class_group.name == "6A"
|
|
assert class_group.description == "Classe de 6ème A"
|
|
assert class_group.year == "2023-2024"
|
|
|
|
def test_class_group_repr(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
assert repr(class_group) == "<ClassGroup 6A>"
|
|
|
|
def test_class_group_unique_name(self, app):
|
|
with app.app_context():
|
|
class_group1 = ClassGroup(name="6A", year="2023-2024")
|
|
class_group2 = ClassGroup(name="6A", year="2023-2024")
|
|
|
|
db.session.add(class_group1)
|
|
db.session.commit()
|
|
|
|
db.session.add(class_group2)
|
|
with pytest.raises(Exception):
|
|
db.session.commit()
|
|
|
|
|
|
class TestStudent:
|
|
def test_create_student(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
# Utiliser le helper pour créer un étudiant avec inscription temporelle
|
|
student = create_student_with_enrollment(
|
|
"Jean",
|
|
"Dupont",
|
|
class_group.id,
|
|
email="jean.dupont@example.com"
|
|
)
|
|
db.session.commit()
|
|
|
|
assert student.id is not None
|
|
assert student.first_name == "Jean"
|
|
assert student.last_name == "Dupont"
|
|
assert student.email == "jean.dupont@example.com"
|
|
|
|
# Vérifier l'inscription temporelle au lieu de class_group_id
|
|
assert len(student.enrollments) == 1
|
|
assert student.enrollments[0].class_group_id == class_group.id
|
|
|
|
def test_student_full_name_property(self, app):
|
|
with app.app_context():
|
|
# Pas besoin de class_group pour tester full_name
|
|
student = Student(
|
|
first_name="Jean",
|
|
last_name="Dupont"
|
|
)
|
|
assert student.full_name == "Jean Dupont"
|
|
|
|
def test_student_repr(self, app):
|
|
with app.app_context():
|
|
# Pas besoin de class_group pour tester __repr__
|
|
student = Student(
|
|
first_name="Jean",
|
|
last_name="Dupont"
|
|
)
|
|
assert repr(student) == "<Student Jean Dupont>"
|
|
|
|
def test_student_unique_email(self, app):
|
|
with app.app_context():
|
|
# Pas besoin de class_group pour tester contrainte unique email
|
|
student1 = Student(
|
|
first_name="Jean",
|
|
last_name="Dupont",
|
|
email="jean.dupont@example.com"
|
|
)
|
|
student2 = Student(
|
|
first_name="Marie",
|
|
last_name="Martin",
|
|
email="jean.dupont@example.com"
|
|
)
|
|
|
|
db.session.add(student1)
|
|
db.session.commit()
|
|
|
|
db.session.add(student2)
|
|
with pytest.raises(Exception):
|
|
db.session.commit()
|
|
|
|
|
|
class TestAssessment:
|
|
def test_create_assessment(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
assessment = Assessment(
|
|
title="Contrôle de mathématiques",
|
|
description="Contrôle sur les fractions",
|
|
date=date(2023, 10, 15),
|
|
trimester=1,
|
|
class_group_id=class_group.id,
|
|
coefficient=2.0
|
|
)
|
|
db.session.add(assessment)
|
|
db.session.commit()
|
|
|
|
assert assessment.id is not None
|
|
assert assessment.title == "Contrôle de mathématiques"
|
|
assert assessment.description == "Contrôle sur les fractions"
|
|
assert assessment.date == date(2023, 10, 15)
|
|
assert assessment.coefficient == 2.0
|
|
|
|
def test_assessment_default_coefficient(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
assessment = Assessment(
|
|
title="Contrôle",
|
|
trimester=1,
|
|
class_group_id=class_group.id
|
|
)
|
|
# Default value is set in the column definition, check after saving
|
|
db.session.add(assessment)
|
|
db.session.commit()
|
|
assert assessment.coefficient == 1.0
|
|
|
|
def test_assessment_repr(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
assessment = Assessment(
|
|
title="Contrôle de mathématiques",
|
|
class_group_id=class_group.id
|
|
)
|
|
assert repr(assessment) == "<Assessment Contrôle de mathématiques>"
|
|
|
|
|
|
class TestExercise:
|
|
def test_create_exercise(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
assessment = Assessment(title="Contrôle", trimester=1, class_group_id=1)
|
|
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
assessment.class_group_id = class_group.id
|
|
db.session.add(assessment)
|
|
db.session.commit()
|
|
|
|
exercise = Exercise(
|
|
assessment_id=assessment.id,
|
|
title="Exercice 1",
|
|
description="Calculs avec fractions",
|
|
order=1
|
|
)
|
|
db.session.add(exercise)
|
|
db.session.commit()
|
|
|
|
assert exercise.id is not None
|
|
assert exercise.title == "Exercice 1"
|
|
assert exercise.description == "Calculs avec fractions"
|
|
assert exercise.order == 1
|
|
|
|
def test_exercise_default_order(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
assessment = Assessment(title="Contrôle", trimester=1, class_group_id=1)
|
|
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
assessment.class_group_id = class_group.id
|
|
db.session.add(assessment)
|
|
db.session.commit()
|
|
|
|
exercise = Exercise(
|
|
assessment_id=assessment.id,
|
|
title="Exercice 1"
|
|
)
|
|
# Default value is set in the column definition, check after saving
|
|
db.session.add(exercise)
|
|
db.session.commit()
|
|
assert exercise.order == 1
|
|
|
|
def test_exercise_repr(self, app):
|
|
with app.app_context():
|
|
exercise = Exercise(title="Exercice 1", assessment_id=1)
|
|
assert repr(exercise) == "<Exercise Exercice 1>"
|
|
|
|
|
|
class TestGradingElement:
|
|
def test_create_grading_element(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
assessment = Assessment(title="Contrôle", trimester=1, class_group_id=1)
|
|
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
assessment.class_group_id = class_group.id
|
|
db.session.add(assessment)
|
|
db.session.commit()
|
|
|
|
exercise = Exercise(assessment_id=assessment.id, title="Exercice 1")
|
|
db.session.add(exercise)
|
|
db.session.commit()
|
|
|
|
grading_element = GradingElement(
|
|
exercise_id=exercise.id,
|
|
label="Question 1",
|
|
description="Calculer 1/2 + 1/3",
|
|
skill="Additionner des fractions",
|
|
max_points=4.0,
|
|
grading_type="notes"
|
|
)
|
|
db.session.add(grading_element)
|
|
db.session.commit()
|
|
|
|
assert grading_element.id is not None
|
|
assert grading_element.label == "Question 1"
|
|
assert grading_element.max_points == 4.0
|
|
assert grading_element.grading_type == "notes"
|
|
|
|
def test_grading_element_default_type(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
assessment = Assessment(title="Contrôle", trimester=1, class_group_id=1)
|
|
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
assessment.class_group_id = class_group.id
|
|
db.session.add(assessment)
|
|
db.session.commit()
|
|
|
|
exercise = Exercise(assessment_id=assessment.id, title="Exercice 1")
|
|
db.session.add(exercise)
|
|
db.session.commit()
|
|
|
|
grading_element = GradingElement(
|
|
exercise_id=exercise.id,
|
|
label="Question 1",
|
|
max_points=4.0
|
|
)
|
|
# Default value is set in the column definition, check after saving
|
|
db.session.add(grading_element)
|
|
db.session.commit()
|
|
assert grading_element.grading_type == "notes"
|
|
|
|
def test_grading_element_repr(self, app):
|
|
with app.app_context():
|
|
grading_element = GradingElement(
|
|
exercise_id=1,
|
|
label="Question 1",
|
|
max_points=4.0
|
|
)
|
|
assert repr(grading_element) == "<GradingElement Question 1>"
|
|
|
|
|
|
class TestGrade:
|
|
def test_create_grade(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
# Utiliser le helper pour créer un étudiant avec inscription temporelle
|
|
student = create_student_with_enrollment("Jean", "Dupont", class_group.id)
|
|
db.session.commit()
|
|
|
|
assessment = Assessment(title="Contrôle", trimester=1, class_group_id=class_group.id)
|
|
db.session.add(assessment)
|
|
db.session.commit()
|
|
|
|
exercise = Exercise(assessment_id=assessment.id, title="Exercice 1")
|
|
db.session.add(exercise)
|
|
db.session.commit()
|
|
|
|
grading_element = GradingElement(
|
|
exercise_id=exercise.id,
|
|
label="Question 1",
|
|
max_points=4.0
|
|
)
|
|
db.session.add(grading_element)
|
|
db.session.commit()
|
|
|
|
grade = Grade(
|
|
student_id=student.id,
|
|
grading_element_id=grading_element.id,
|
|
value="3.5",
|
|
comment="Très bien"
|
|
)
|
|
db.session.add(grade)
|
|
db.session.commit()
|
|
|
|
assert grade.id is not None
|
|
assert grade.value == "3.5"
|
|
assert grade.comment == "Très bien"
|
|
|
|
def test_grade_repr_with_student(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
# Utiliser le helper pour créer un étudiant avec inscription temporelle
|
|
student = create_student_with_enrollment("Jean", "Dupont", class_group.id)
|
|
db.session.commit()
|
|
|
|
grade = Grade(
|
|
student_id=student.id,
|
|
grading_element_id=1,
|
|
value="3.5"
|
|
)
|
|
db.session.add(grade)
|
|
db.session.commit()
|
|
|
|
assert repr(grade) == "<Grade 3.5 for Jean>" |