Files
Academic-Affairs-System/tests/Jiaowu.Api.Tests/CourseSelectionRulesTests.cs
T
biss 56b13b3adb 已完成“选课候补与自动递补”完整流程。
学生端:满员后可加入/取消候补,展示候补顺位和当前候补人数。
自动递补:退课释放名额后,按候补时间顺序重新校验学籍、范围、学分及时间冲突;不合格者自动失效并继续下一位。
管理端:教学班显示候补人数,名单抽屉提供候补队列及移出操作。
状态通知:递补成功、资格失效、批次关闭、管理员移出均会通知学生。
并发安全:退课和递补放在 Serializable 事务中执行。
数据库:已补充 SQLite 开发迁移和 MySQL 正式迁移。
2026-07-26 15:39:50 +08:00

123 lines
4.1 KiB
C#

using Jiaowu.Api.Domain.Academic;
using Jiaowu.Api.Infrastructure.CourseSelection;
namespace Jiaowu.Api.Tests;
public sealed class CourseSelectionRulesTests
{
[Fact]
public void Selection_requires_open_status_and_active_window()
{
var now = new DateTime(2026, 7, 24, 8, 0, 0, DateTimeKind.Utc);
var round = CreateRound(now.AddHours(-1), now.AddHours(1));
Assert.True(CourseSelectionRules.IsSelectionOpen(round, now));
round.Status = CourseSelectionRoundStatus.Closed;
Assert.False(CourseSelectionRules.IsSelectionOpen(round, now));
round.Status = CourseSelectionRoundStatus.Open;
Assert.False(CourseSelectionRules.IsSelectionOpen(round, now.AddHours(2)));
}
[Fact]
public void Withdrawal_respects_deadline()
{
var now = new DateTime(2026, 7, 24, 8, 0, 0, DateTimeKind.Utc);
var round = CreateRound(now.AddDays(-1), now.AddDays(1));
round.WithdrawalEndsAt = now.AddMinutes(30);
Assert.True(CourseSelectionRules.CanWithdraw(round, now));
Assert.False(CourseSelectionRules.CanWithdraw(round, now.AddHours(1)));
}
[Fact]
public void Waitlist_promotion_uses_withdrawal_window_and_retake_capacity()
{
var now = new DateTime(2026, 7, 24, 8, 0, 0, DateTimeKind.Utc);
var round = CreateRound(now.AddHours(-1), now.AddHours(1));
round.WithdrawalEndsAt = now.AddHours(2);
Assert.True(CourseSelectionRules.CanPromoteWaitlist(round, now));
Assert.Equal(60, CourseSelectionRules.EffectiveCapacity(60, false));
Assert.Equal(69, CourseSelectionRules.EffectiveCapacity(60, true));
round.Status = CourseSelectionRoundStatus.Closed;
Assert.False(CourseSelectionRules.CanPromoteWaitlist(round, now));
}
[Fact]
public void Schedule_conflict_detects_overlapping_weeks_and_periods()
{
var selected = CreateEntry(1, 1, 2, 1, 16, WeekPattern.All);
var overlapping = CreateEntry(1, 2, 2, 1, 16, WeekPattern.All);
Assert.True(CourseSelectionRules.HasScheduleConflict(
[overlapping],
[selected]));
}
[Fact]
public void Odd_and_even_week_entries_do_not_conflict()
{
var selected = CreateEntry(3, 5, 2, 1, 16, WeekPattern.Odd);
var candidate = CreateEntry(3, 5, 2, 1, 16, WeekPattern.Even);
Assert.False(CourseSelectionRules.HasScheduleConflict(
[candidate],
[selected]));
}
[Theory]
[InlineData(CourseNature.GeneralRequired, true)]
[InlineData(CourseNature.GeneralElective, false)]
[InlineData(CourseNature.MajorRequired, false)]
[InlineData(CourseNature.MajorElective, false)]
[InlineData(CourseNature.Practice, false)]
public void Proxy_enrollment_is_limited_to_general_required_courses(
CourseNature nature,
bool expected)
{
Assert.Equal(expected, CourseSelectionRules.SupportsProxyEnrollment(nature));
}
[Theory]
[InlineData(TeachingTaskSchedulingMode.Standard, true)]
[InlineData(TeachingTaskSchedulingMode.Flexible, false)]
public void Only_standard_courses_require_a_published_schedule(
TeachingTaskSchedulingMode schedulingMode,
bool expected)
{
Assert.Equal(
expected,
CourseSelectionRules.RequiresPublishedSchedule(schedulingMode));
}
private static CourseSelectionRound CreateRound(DateTime startsAt, DateTime endsAt) =>
new()
{
Name = "第一轮选课",
StartsAt = startsAt,
EndsAt = endsAt,
WithdrawalEndsAt = endsAt.AddDays(1),
Status = CourseSelectionRoundStatus.Open
};
private static ScheduleEntry CreateEntry(
int dayOfWeek,
int startPeriod,
int periodCount,
int startWeek,
int endWeek,
WeekPattern weekPattern) =>
new()
{
DayOfWeek = dayOfWeek,
StartPeriod = startPeriod,
PeriodCount = periodCount,
StartWeek = startWeek,
EndWeek = endWeek,
WeekPattern = weekPattern
};
}