uchill/backend/apps/schedule/tests/test_models.py

153 lines
5.3 KiB
Python

"""
Unit тесты для моделей расписания.
"""
import pytest
from django.utils import timezone
from datetime import timedelta
from apps.schedule.models import Subject, MentorSubject, Lesson, LessonTemplate, TimeSlot, Availability
from apps.users.models import User, Client, Parent
@pytest.mark.django_db
@pytest.mark.unit
class TestSubjectModel:
"""Тесты модели Subject."""
def test_create_subject(self):
"""Тест создания предмета."""
subject = Subject.objects.create(name='Математика')
assert subject.name == 'Математика'
assert subject.is_active is True
assert str(subject) == 'Математика'
def test_subject_unique_name(self):
"""Тест уникальности названия предмета."""
Subject.objects.create(name='Физика')
with pytest.raises(Exception): # IntegrityError или ValidationError
Subject.objects.create(name='Физика')
@pytest.mark.django_db
@pytest.mark.unit
class TestMentorSubjectModel:
"""Тесты модели MentorSubject."""
def test_create_mentor_subject(self, mentor_user):
"""Тест создания кастомного предмета ментора."""
mentor_subject = MentorSubject.objects.create(
mentor=mentor_user,
name='Программирование на Python'
)
assert mentor_subject.mentor == mentor_user
assert mentor_subject.name == 'Программирование на Python'
assert mentor_subject.usage_count == 0
@pytest.mark.django_db
@pytest.mark.unit
class TestLessonModel:
"""Тесты модели Lesson."""
def test_create_lesson(self, mentor_user, client_user):
"""Тест создания занятия."""
from apps.users.models import Client
# Создаем профиль клиента
client_profile = Client.objects.create(
user=client_user,
mentor=mentor_user
)
subject = Subject.objects.create(name='Математика')
lesson = Lesson.objects.create(
mentor=mentor_user,
client=client_profile,
subject=subject,
title='Урок алгебры',
start_time=timezone.now() + timedelta(hours=1),
duration_minutes=60,
status='scheduled'
)
assert lesson.mentor == mentor_user
assert lesson.client == client_profile
assert lesson.subject == subject
assert lesson.title == 'Урок алгебры'
assert lesson.status == 'scheduled'
assert lesson.duration_minutes == 60
def test_lesson_end_time(self, mentor_user, client_user):
"""Тест вычисления времени окончания занятия."""
from apps.users.models import Client
client_profile = Client.objects.create(
user=client_user,
mentor=mentor_user
)
start_time = timezone.now() + timedelta(hours=1)
lesson = Lesson.objects.create(
mentor=mentor_user,
client=client_profile,
title='Тест',
start_time=start_time,
duration_minutes=90,
status='scheduled'
)
expected_end_time = start_time + timedelta(minutes=90)
assert lesson.end_time == expected_end_time
def test_lesson_complete(self, mentor_user, client_user):
"""Тест завершения занятия."""
from apps.users.models import Client
client_profile = Client.objects.create(
user=client_user,
mentor=mentor_user
)
lesson = Lesson.objects.create(
mentor=mentor_user,
client=client_profile,
title='Тест',
start_time=timezone.now() - timedelta(hours=1),
duration_minutes=60,
status='scheduled'
)
lesson.complete(grade=5, notes='Отличная работа!')
assert lesson.status == 'completed'
assert lesson.mentor_grade == 5
assert lesson.notes == 'Отличная работа!'
assert lesson.completed_at is not None
@pytest.mark.django_db
@pytest.mark.unit
class TestLessonTemplateModel:
"""Тесты модели LessonTemplate."""
def test_create_template(self, mentor_user):
"""Тест создания шаблона занятия."""
subject = Subject.objects.create(name='Физика')
template = LessonTemplate.objects.create(
mentor=mentor_user,
subject=subject,
title='Еженедельный урок физики',
duration_minutes=60,
description='Регулярное занятие'
)
assert template.mentor == mentor_user
assert template.subject == subject
assert template.title == 'Еженедельный урок физики'
assert template.duration_minutes == 60