You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.9 KiB
137 lines
4.9 KiB
from django.test import TestCase
|
|
from django.contrib.auth import get_user_model
|
|
from apps.account.models import StudentUser
|
|
from apps.course.models import Course, CourseCategory, Participant
|
|
from apps.transaction.models import TransactionParticipant
|
|
from apps.account.models import ProfessorUser
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class TransactionParticipantSignalTest(TestCase):
|
|
def setUp(self):
|
|
"""تنظیمات اولیه برای تست"""
|
|
# ایجاد کاربر
|
|
self.user = User.objects.create_user(
|
|
email='test@example.com',
|
|
password='testpass123'
|
|
)
|
|
|
|
# ایجاد استاد
|
|
self.professor = ProfessorUser.objects.create(
|
|
email='professor@example.com',
|
|
password='testpass123'
|
|
)
|
|
|
|
# ایجاد دستهبندی دوره
|
|
self.category = CourseCategory.objects.create(
|
|
name='Test Category',
|
|
slug='test-category'
|
|
)
|
|
|
|
# ایجاد دوره
|
|
self.course = Course.objects.create(
|
|
title='Test Course',
|
|
slug='test-course',
|
|
category=self.category,
|
|
professor=self.professor,
|
|
video_type='youtube_link',
|
|
level='beginner',
|
|
duration=10,
|
|
lessons_count=5,
|
|
description='Test course description',
|
|
is_free=False,
|
|
price=100.00,
|
|
final_price=100.00
|
|
)
|
|
|
|
def test_participant_created_on_success_status(self):
|
|
"""تست ایجاد participant هنگام تغییر وضعیت به SUCCESS"""
|
|
# ایجاد تراکنش با وضعیت PENDING
|
|
transaction = TransactionParticipant.objects.create(
|
|
user=self.user,
|
|
course=self.course,
|
|
price=100.00,
|
|
status=TransactionParticipant.TransactionStatus.PENDING
|
|
)
|
|
|
|
# بررسی که participant ایجاد نشده
|
|
self.assertFalse(
|
|
Participant.objects.filter(student=self.user, course=self.course).exists()
|
|
)
|
|
|
|
# تغییر وضعیت به SUCCESS
|
|
transaction.status = TransactionParticipant.TransactionStatus.SUCCESS
|
|
transaction.save()
|
|
|
|
# بررسی که participant ایجاد شده
|
|
self.assertTrue(
|
|
Participant.objects.filter(student=self.user, course=self.course).exists()
|
|
)
|
|
|
|
# بررسی که کاربر نقش student دارد
|
|
self.assertTrue(self.user.has_role('student'))
|
|
|
|
def test_participant_created_on_direct_success(self):
|
|
"""تست ایجاد participant هنگام ایجاد تراکنش با وضعیت SUCCESS"""
|
|
# ایجاد تراکنش مستقیماً با وضعیت SUCCESS
|
|
transaction = TransactionParticipant.objects.create(
|
|
user=self.user,
|
|
course=self.course,
|
|
price=100.00,
|
|
status=TransactionParticipant.TransactionStatus.SUCCESS
|
|
)
|
|
|
|
# بررسی که participant ایجاد شده
|
|
self.assertTrue(
|
|
Participant.objects.filter(student=self.user, course=self.course).exists()
|
|
)
|
|
|
|
# بررسی که کاربر نقش student دارد
|
|
self.assertTrue(self.user.has_role('student'))
|
|
|
|
def test_no_duplicate_participant(self):
|
|
"""تست عدم ایجاد participant تکراری"""
|
|
# ایجاد participant دستی
|
|
existing_participant = Participant.objects.create(
|
|
student=self.user,
|
|
course=self.course
|
|
)
|
|
|
|
# ایجاد تراکنش با وضعیت SUCCESS
|
|
transaction = TransactionParticipant.objects.create(
|
|
user=self.user,
|
|
course=self.course,
|
|
price=100.00,
|
|
status=TransactionParticipant.TransactionStatus.SUCCESS
|
|
)
|
|
|
|
# بررسی که فقط یک participant وجود دارد
|
|
self.assertEqual(
|
|
Participant.objects.filter(student=self.user, course=self.course).count(),
|
|
1
|
|
)
|
|
|
|
def test_model_helper_methods(self):
|
|
"""تست متدهای کمکی مدل"""
|
|
# ایجاد تراکنش
|
|
transaction = TransactionParticipant.objects.create(
|
|
user=self.user,
|
|
course=self.course,
|
|
price=100.00,
|
|
status=TransactionParticipant.TransactionStatus.PENDING
|
|
)
|
|
|
|
# بررسی که participant وجود ندارد
|
|
self.assertFalse(transaction.is_participant_enrolled())
|
|
self.assertIsNone(transaction.get_participant())
|
|
|
|
# ایجاد participant
|
|
participant = Participant.objects.create(
|
|
student=self.user,
|
|
course=self.course
|
|
)
|
|
|
|
# بررسی که participant وجود دارد
|
|
self.assertTrue(transaction.is_participant_enrolled())
|
|
self.assertEqual(transaction.get_participant(), participant)
|