uchill/backend/apps/subscriptions/tests/test_services.py

84 lines
2.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Unit тесты для сервисов подписок.
"""
import pytest
from decimal import Decimal
from django.utils import timezone
from datetime import timedelta
from apps.subscriptions.services import SubscriptionService, PaymentService, PromoCodeService
from apps.subscriptions.models import SubscriptionPlan, Subscription, PromoCode
@pytest.mark.django_db
@pytest.mark.unit
class TestSubscriptionService:
"""Тесты сервиса SubscriptionService."""
def test_calculate_price_with_discount(self, mentor_user):
"""Тест расчета цены со скидкой."""
plan = SubscriptionPlan.objects.create(
name='Базовый',
slug='basic',
price=Decimal('1000.00'),
duration_days=30
)
service = SubscriptionService()
price = service.calculate_price(
plan=plan,
duration_days=90,
student_count=5
)
assert price > 0
def test_create_subscription(self, mentor_user):
"""Тест создания подписки."""
plan = SubscriptionPlan.objects.create(
name='Базовый',
slug='basic',
price=Decimal('1000.00'),
duration_days=30
)
service = SubscriptionService()
subscription = service.create_subscription(
user=mentor_user,
plan=plan,
duration_days=30,
student_count=5
)
assert subscription.user == mentor_user
assert subscription.plan == plan
assert subscription.status == 'pending'
@pytest.mark.django_db
@pytest.mark.unit
class TestPromoCodeService:
"""Тесты сервиса PromoCodeService."""
def test_validate_promo_code(self):
"""Тест валидации промокода."""
promo = PromoCode.objects.create(
code='TEST2024',
discount_percent=20,
is_active=True
)
service = PromoCodeService()
is_valid, discount = service.validate_promo_code('TEST2024')
assert is_valid is True
assert discount == 20
def test_invalid_promo_code(self):
"""Тест невалидного промокода."""
service = PromoCodeService()
is_valid, discount = service.validate_promo_code('INVALID')
assert is_valid is False
assert discount == 0