Fix test failures and workload/websocket behavior

This commit is contained in:
beabigegg
2026-01-11 08:37:21 +08:00
parent 3bdc6ff1c9
commit f5f870da56
49 changed files with 3006 additions and 1132 deletions

View File

@@ -1,7 +1,7 @@
import pytest
import uuid
from datetime import datetime, timedelta
from app.models import User, Space, Project, Task, TaskStatus, ScheduledReport, ReportHistory, Blocker
from app.models import User, Space, Project, Task, TaskStatus, ScheduledReport, ReportHistory, Blocker, ProjectMember
from app.services.report_service import ReportService
@@ -76,6 +76,7 @@ def test_statuses(db, test_project):
name="To Do",
color="#808080",
position=0,
is_done=False,
)
in_progress = TaskStatus(
id=str(uuid.uuid4()),
@@ -83,6 +84,7 @@ def test_statuses(db, test_project):
name="In Progress",
color="#0000FF",
position=1,
is_done=False,
)
done = TaskStatus(
id=str(uuid.uuid4()),
@@ -90,6 +92,7 @@ def test_statuses(db, test_project):
name="Done",
color="#00FF00",
position=2,
is_done=True,
)
db.add_all([todo, in_progress, done])
db.commit()
@@ -165,12 +168,90 @@ class TestReportService:
stats = ReportService.get_weekly_stats(db, test_user.id)
assert stats["summary"]["completed_count"] == 1
assert stats["summary"]["in_progress_count"] == 1
assert stats["summary"]["in_progress_count"] == 2
assert stats["summary"]["overdue_count"] == 1
assert stats["summary"]["total_tasks"] == 3
assert len(stats["projects"]) == 1
assert stats["projects"][0]["project_title"] == "Report Test Project"
def test_weekly_stats_includes_project_members(self, db, test_user, test_space):
"""Project member should receive weekly stats for member projects."""
other_owner = User(
id=str(uuid.uuid4()),
email="owner2@example.com",
name="Other Owner",
role_id="00000000-0000-0000-0000-000000000003",
is_active=True,
is_system_admin=False,
)
db.add(other_owner)
db.commit()
member_project = Project(
id=str(uuid.uuid4()),
space_id=test_space.id,
title="Member Project",
description="Project for member stats",
owner_id=other_owner.id,
)
db.add(member_project)
db.commit()
db.add(ProjectMember(
id=str(uuid.uuid4()),
project_id=member_project.id,
user_id=test_user.id,
role="member",
added_by=other_owner.id,
))
db.commit()
member_status = TaskStatus(
id=str(uuid.uuid4()),
project_id=member_project.id,
name="In Progress",
color="#0000FF",
position=0,
is_done=False,
)
db.add(member_status)
db.commit()
task = Task(
id=str(uuid.uuid4()),
project_id=member_project.id,
title="Member Task",
status_id=member_status.id,
created_by=other_owner.id,
)
db.add(task)
db.commit()
stats = ReportService.get_weekly_stats(db, test_user.id)
project_titles = {project["project_title"] for project in stats["projects"]}
assert "Member Project" in project_titles
def test_completed_task_outside_week_not_counted(self, db, test_user, test_project, test_statuses):
"""Completed tasks outside the week window should not be counted."""
week_start = ReportService.get_week_start()
week_end = week_start + timedelta(days=7)
task = Task(
id=str(uuid.uuid4()),
project_id=test_project.id,
title="Completed Outside Week",
status_id=test_statuses["done"].id,
created_by=test_user.id,
)
task.updated_at = week_end + timedelta(days=1)
db.add(task)
db.commit()
stats = ReportService.get_weekly_stats(db, test_user.id, week_start)
assert stats["summary"]["completed_count"] == 0
def test_generate_weekly_report(self, db, test_user, test_project, test_tasks, test_statuses):
"""Test generating a weekly report."""
report = ReportService.generate_weekly_report(db, test_user.id)
@@ -216,6 +297,45 @@ class TestReportAPI:
assert "report_id" in data
assert "summary" in data
def test_weekly_report_subscription_toggle(self, client, test_user_token, db, test_user):
"""Test weekly report subscription toggle endpoints."""
response = client.get(
"/api/reports/weekly/subscription",
headers={"Authorization": f"Bearer {test_user_token}"},
)
assert response.status_code == 200
assert response.json()["is_active"] is False
response = client.put(
"/api/reports/weekly/subscription",
headers={"Authorization": f"Bearer {test_user_token}"},
json={"is_active": True},
)
assert response.status_code == 200
assert response.json()["is_active"] is True
response = client.get(
"/api/reports/weekly/subscription",
headers={"Authorization": f"Bearer {test_user_token}"},
)
assert response.status_code == 200
assert response.json()["is_active"] is True
response = client.put(
"/api/reports/weekly/subscription",
headers={"Authorization": f"Bearer {test_user_token}"},
json={"is_active": False},
)
assert response.status_code == 200
assert response.json()["is_active"] is False
scheduled = db.query(ScheduledReport).filter(
ScheduledReport.recipient_id == test_user.id,
ScheduledReport.report_type == "weekly",
).first()
assert scheduled is not None
assert scheduled.is_active is False
def test_list_report_history_empty(self, client, test_user_token):
"""Test listing report history when empty."""
response = client.get(