65 lines
2.0 KiB
Python
65 lines
2.0 KiB
Python
import pytest
|
|
from rest_framework.exceptions import ValidationError
|
|
|
|
from accounts.models import User
|
|
from content.models import ContentInteraction, ContentItem, VideoProgress
|
|
from content.services import complete_video
|
|
from progression.models import RewardTransaction, UserAbility
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_complete_video_重复提交只发放一次能力碎片():
|
|
user = User.objects.create_user(
|
|
username="video_user",
|
|
password="StrongPass_2026",
|
|
nickname="视频用户",
|
|
)
|
|
video = ContentItem.objects.create(
|
|
slug="video-modeling",
|
|
title="建模视频",
|
|
kind=ContentItem.Kind.VIDEO,
|
|
ability_dimension=ContentItem.Ability.MODELING,
|
|
duration_seconds=360,
|
|
is_published=True,
|
|
)
|
|
|
|
first, first_reward = complete_video(user, video)
|
|
second, second_reward = complete_video(user, video)
|
|
|
|
ability = UserAbility.objects.get(
|
|
user=user,
|
|
dimension=UserAbility.Dimension.MODELING,
|
|
)
|
|
assert first.completed is True
|
|
assert second.reward_granted is True
|
|
assert first_reward == {"ability": "modeling", "fragments": 1}
|
|
assert second_reward is None
|
|
assert ability.fragments == 1
|
|
assert RewardTransaction.objects.filter(user=user).count() == 1
|
|
assert VideoProgress.objects.filter(user=user, content=video).count() == 1
|
|
assert ContentInteraction.objects.filter(
|
|
user=user,
|
|
content=video,
|
|
action=ContentInteraction.Action.COMPLETE,
|
|
).count() == 1
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_complete_video_非视频内容拒绝完成():
|
|
user = User.objects.create_user(
|
|
username="knowledge_user",
|
|
password="StrongPass_2026",
|
|
nickname="知识用户",
|
|
)
|
|
content = ContentItem.objects.create(
|
|
slug="knowledge-item",
|
|
title="知识卡片",
|
|
kind=ContentItem.Kind.KNOWLEDGE,
|
|
is_published=True,
|
|
)
|
|
|
|
with pytest.raises(ValidationError, match="只有视频"):
|
|
complete_video(user, content)
|
|
|
|
assert RewardTransaction.objects.count() == 0
|