159 lines
6.7 KiB
Python
159 lines
6.7 KiB
Python
import pytest
|
|
from datetime import date
|
|
from forms import AssessmentForm, ClassGroupForm, StudentForm
|
|
from models import db, ClassGroup
|
|
from repositories import ClassRepository
|
|
|
|
|
|
class TestAssessmentForm:
|
|
def test_assessment_form_valid_data(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
# Create form without CSRF token for testing
|
|
with app.test_request_context():
|
|
form = AssessmentForm()
|
|
form.title.data = 'Test Math'
|
|
form.description.data = 'Contrôle de mathématiques'
|
|
form.date.data = date(2023, 10, 15)
|
|
form.class_group_id.data = class_group.id
|
|
form.coefficient.data = 2.0
|
|
|
|
assert form.title.data == 'Test Math'
|
|
assert form.description.data == 'Contrôle de mathématiques'
|
|
assert form.date.data == date(2023, 10, 15)
|
|
assert form.class_group_id.data == class_group.id
|
|
assert form.coefficient.data == 2.0
|
|
|
|
def test_assessment_form_missing_title(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
with app.test_request_context():
|
|
# Test form data validation by checking field requirements
|
|
form = AssessmentForm()
|
|
# Title field is required, so it should be marked as required
|
|
assert form.title.flags.required
|
|
|
|
def test_assessment_form_coefficient_field(self, app):
|
|
with app.app_context():
|
|
with app.test_request_context():
|
|
form = AssessmentForm()
|
|
# Test that coefficient field exists and has validators
|
|
assert hasattr(form, 'coefficient')
|
|
assert form.coefficient.flags.required
|
|
# Check that NumberRange validator is present
|
|
has_number_range = any(v.__class__.__name__ == 'NumberRange' for v in form.coefficient.validators)
|
|
assert has_number_range
|
|
|
|
def test_assessment_form_default_values(self, app):
|
|
with app.app_context():
|
|
with app.test_request_context():
|
|
form = AssessmentForm()
|
|
# Test that defaults are callable functions, not values
|
|
assert callable(form.date.default)
|
|
assert form.coefficient.default == 1.0
|
|
|
|
def test_assessment_form_class_choices_populated(self, app):
|
|
with app.app_context():
|
|
class_group1 = ClassGroup(name="6A", year="2023-2024")
|
|
class_group2 = ClassGroup(name="6B", year="2023-2024")
|
|
db.session.add_all([class_group1, class_group2])
|
|
db.session.commit()
|
|
|
|
with app.test_request_context():
|
|
form = AssessmentForm()
|
|
class_repo = ClassRepository()
|
|
form.populate_class_choices(class_repo)
|
|
|
|
assert len(form.class_group_id.choices) >= 2
|
|
choice_names = [choice[1] for choice in form.class_group_id.choices]
|
|
assert "6A" in choice_names
|
|
assert "6B" in choice_names
|
|
|
|
|
|
class TestClassGroupForm:
|
|
def test_class_group_form_valid_data(self, app):
|
|
with app.app_context():
|
|
with app.test_request_context():
|
|
form = ClassGroupForm()
|
|
form.name.data = '6A'
|
|
form.description.data = 'Classe de 6ème A'
|
|
form.year.data = '2023-2024'
|
|
|
|
assert form.name.data == '6A'
|
|
assert form.description.data == 'Classe de 6ème A'
|
|
assert form.year.data == '2023-2024'
|
|
|
|
def test_class_group_form_name_required(self, app):
|
|
with app.app_context():
|
|
with app.test_request_context():
|
|
form = ClassGroupForm()
|
|
# Test that name field is required
|
|
assert form.name.flags.required
|
|
|
|
def test_class_group_form_default_year(self, app):
|
|
with app.app_context():
|
|
with app.test_request_context():
|
|
form = ClassGroupForm()
|
|
assert form.year.default == "2024-2025"
|
|
|
|
|
|
class TestStudentForm:
|
|
def test_student_form_valid_data(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
with app.test_request_context():
|
|
form = StudentForm()
|
|
form.first_name.data = 'Jean'
|
|
form.last_name.data = 'Dupont'
|
|
form.email.data = 'jean.dupont@example.com'
|
|
form.class_group_id.data = class_group.id
|
|
|
|
assert form.first_name.data == 'Jean'
|
|
assert form.last_name.data == 'Dupont'
|
|
assert form.email.data == 'jean.dupont@example.com'
|
|
assert form.class_group_id.data == class_group.id
|
|
|
|
def test_student_form_required_fields(self, app):
|
|
with app.app_context():
|
|
with app.test_request_context():
|
|
form = StudentForm()
|
|
# Test that required fields are marked as required
|
|
assert form.first_name.flags.required
|
|
assert form.last_name.flags.required
|
|
assert form.class_group_id.flags.required
|
|
|
|
def test_student_form_email_validator(self, app):
|
|
with app.app_context():
|
|
with app.test_request_context():
|
|
form = StudentForm()
|
|
# Test that email field has email validator
|
|
has_email_validator = any(v.__class__.__name__ == 'Email' for v in form.email.validators)
|
|
assert has_email_validator
|
|
# Email field should be optional
|
|
assert not form.email.flags.required
|
|
|
|
def test_student_form_optional_email(self, app):
|
|
with app.app_context():
|
|
class_group = ClassGroup(name="6A", year="2023-2024")
|
|
db.session.add(class_group)
|
|
db.session.commit()
|
|
|
|
with app.test_request_context():
|
|
form = StudentForm()
|
|
form.first_name.data = 'Jean'
|
|
form.last_name.data = 'Dupont'
|
|
form.class_group_id.data = class_group.id
|
|
# Don't set email - should be valid
|
|
|
|
assert form.first_name.data == 'Jean'
|
|
assert form.last_name.data == 'Dupont'
|
|
assert form.email.data is None |