410 lines
22 KiB
C#
410 lines
22 KiB
C#
using System.Text.Json;
|
|
using ClosedXML.Excel;
|
|
using Jiaowu.Api.Controllers;
|
|
using Jiaowu.Api.Domain.Academic;
|
|
using Jiaowu.Api.Domain.Identity;
|
|
using Jiaowu.Api.Domain.System;
|
|
using Jiaowu.Api.Infrastructure.Auth;
|
|
using Jiaowu.Api.Infrastructure.Dormitories;
|
|
using Jiaowu.Api.Infrastructure.Persistence;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Jiaowu.Api.Tests;
|
|
|
|
public sealed class DormitoryHygieneControllerTests
|
|
{
|
|
[Fact]
|
|
public async Task UpsertScores_CalculatesGradeAndKeepsPublishedState()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync();
|
|
fixture.Batch.Status = DormitoryHygieneBatchStatus.Published;
|
|
await fixture.Db.SaveChangesAsync();
|
|
var controller = new DormitoryHygieneController(fixture.Db, fixture.AllScope);
|
|
|
|
var result = await controller.UpsertScores(fixture.Batch.Id,
|
|
new UpsertDormitoryHygieneScoresRequest([
|
|
new DormitoryHygieneScoreRequest(fixture.Room.Id, 18, 17, 16, 19, 20, "物品摆放需改进", "已复核", true)
|
|
]), CancellationToken.None);
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
fixture.Db.ChangeTracker.Clear();
|
|
var score = await fixture.Db.DormitoryHygieneScores.SingleAsync();
|
|
Assert.Equal(90, score.TotalScore);
|
|
Assert.Equal(DormitoryHygieneGrade.Excellent, score.Grade);
|
|
Assert.True(score.IsViolation);
|
|
Assert.Equal("物品摆放需改进", score.Issues);
|
|
Assert.Equal(DormitoryHygieneBatchStatus.Published,
|
|
(await fixture.Db.DormitoryHygieneBatches.SingleAsync()).Status);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CollegeViewer_OnlySeesRoomsOccupiedByAccessibleStudents()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync(includeSecondCollege: true);
|
|
var controller = new DormitoryHygieneController(fixture.Db, fixture.CollegeScope);
|
|
|
|
var result = Assert.IsType<OkObjectResult>(await controller.GetRooms(pageSize: 30, ct: CancellationToken.None));
|
|
using var json = JsonDocument.Parse(JsonSerializer.Serialize(result.Value));
|
|
var roomNumbers = json.RootElement.GetProperty("Items")
|
|
.EnumerateArray()
|
|
.Select(room => room.GetProperty("RoomNumber").GetString())
|
|
.ToArray();
|
|
|
|
Assert.Contains(fixture.Room.RoomNumber, roomNumbers);
|
|
Assert.DoesNotContain(fixture.OtherRoom!.RoomNumber, roomNumbers);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DormitoryManager_WithNonGlobalDefaultScope_SeesAllRooms()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync(includeSecondCollege: true);
|
|
var controller = new DormitoryHygieneController(fixture.Db, fixture.AllScope);
|
|
var result = Assert.IsType<OkObjectResult>(await controller.GetRooms(pageSize: 30, ct: CancellationToken.None));
|
|
var json = JsonSerializer.Serialize(result.Value);
|
|
Assert.Contains(fixture.Room.RoomNumber, json);
|
|
Assert.Contains(fixture.OtherRoom!.RoomNumber, json);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Mine_ReturnsOnlyPublishedScoresForCurrentDormitory()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync();
|
|
var published = fixture.Batch;
|
|
published.Status = DormitoryHygieneBatchStatus.Published;
|
|
var draft = new DormitoryHygieneBatch { Name = "未发布检查", InspectionDate = new DateOnly(2026, 10, 19) };
|
|
fixture.Db.DormitoryHygieneScores.AddRange(
|
|
ScoreFor(published.Id, fixture.Room.Id, 95, true),
|
|
ScoreFor(draft.Id, fixture.Room.Id, 60));
|
|
fixture.Db.Add(draft);
|
|
await fixture.Db.SaveChangesAsync();
|
|
var controller = new DormitoryHygieneController(fixture.Db, fixture.StudentScope);
|
|
|
|
var result = Assert.IsType<OkObjectResult>(await controller.Mine(CancellationToken.None));
|
|
var json = JsonSerializer.Serialize(result.Value);
|
|
|
|
Assert.Contains(published.Id.ToString(), json);
|
|
Assert.Contains("\"IsViolation\":true", json);
|
|
Assert.DoesNotContain(draft.Id.ToString(), json);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Mine_MatchesScoresToActualStayWindowsAcrossTransfer()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync();
|
|
var targetRoom = new DormitoryRoom { DormitoryBuildingId = fixture.Room.DormitoryBuildingId, RoomNumber = "102", Floor = 1 };
|
|
var targetBed = new DormitoryBed { DormitoryRoomId = targetRoom.Id, BedNumber = "1" };
|
|
var occupancy = await fixture.Db.DormitoryOccupancies.SingleAsync(x => x.StudentId == fixture.Student.Id);
|
|
var originalBedId = occupancy.DormitoryBedId;
|
|
occupancy.DormitoryBedId = targetBed.Id;
|
|
occupancy.CheckInDate = new DateOnly(2026, 10, 10);
|
|
fixture.Db.AddRange(targetRoom, targetBed,
|
|
new DormitoryStayHistory { StudentId = fixture.Student.Id, ToBedId = originalBedId, Action = DormitoryStayAction.CheckIn,
|
|
EffectiveDate = new DateOnly(2026, 9, 1), OperatedByUserId = fixture.AllScope.Current.UserId },
|
|
new DormitoryStayHistory { StudentId = fixture.Student.Id, FromBedId = originalBedId, ToBedId = targetBed.Id,
|
|
Action = DormitoryStayAction.Transfer, EffectiveDate = new DateOnly(2026, 10, 10), OperatedByUserId = fixture.AllScope.Current.UserId });
|
|
var batches = new[]
|
|
{
|
|
new DormitoryHygieneBatch { Name = "原宿舍入住期间", InspectionDate = new DateOnly(2026, 9, 15), Status = DormitoryHygieneBatchStatus.Published },
|
|
new DormitoryHygieneBatch { Name = "原宿舍迁出之后", InspectionDate = new DateOnly(2026, 10, 15), Status = DormitoryHygieneBatchStatus.Published },
|
|
new DormitoryHygieneBatch { Name = "新宿舍入住之前", InspectionDate = new DateOnly(2026, 9, 20), Status = DormitoryHygieneBatchStatus.Published },
|
|
new DormitoryHygieneBatch { Name = "新宿舍入住之后", InspectionDate = new DateOnly(2026, 10, 16), Status = DormitoryHygieneBatchStatus.Published }
|
|
};
|
|
fixture.Db.AddRange(batches);
|
|
fixture.Db.AddRange(ScoreFor(batches[0].Id, fixture.Room.Id, 90), ScoreFor(batches[1].Id, fixture.Room.Id, 80),
|
|
ScoreFor(batches[2].Id, targetRoom.Id, 70), ScoreFor(batches[3].Id, targetRoom.Id, 95));
|
|
await fixture.Db.SaveChangesAsync();
|
|
|
|
var result = Assert.IsType<OkObjectResult>(await new DormitoryHygieneController(fixture.Db, fixture.StudentScope).Mine(CancellationToken.None));
|
|
var json = JsonSerializer.Serialize(result.Value);
|
|
|
|
Assert.Contains(batches[0].Id.ToString(), json);
|
|
Assert.Contains(batches[3].Id.ToString(), json);
|
|
Assert.DoesNotContain(batches[1].Id.ToString(), json);
|
|
Assert.DoesNotContain(batches[2].Id.ToString(), json);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Publish_RejectsBatchWithoutScores()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync();
|
|
var controller = new DormitoryHygieneController(fixture.Db, fixture.AllScope);
|
|
|
|
var result = await controller.Publish(fixture.Batch.Id, CancellationToken.None);
|
|
|
|
var conflict = Assert.IsType<ConflictObjectResult>(result);
|
|
Assert.Contains("没有宿舍成绩", Assert.IsType<ProblemDetails>(conflict.Value).Detail);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateBatch_PreservesPublishedState()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync();
|
|
fixture.Batch.Status = DormitoryHygieneBatchStatus.Published;
|
|
await fixture.Db.SaveChangesAsync();
|
|
var controller = new DormitoryHygieneController(fixture.Db, fixture.AllScope);
|
|
|
|
var result = await controller.UpdateBatch(fixture.Batch.Id,
|
|
new CreateDormitoryHygieneBatchRequest("第七周复查", new DateOnly(2026, 10, 18), "宿管科"),
|
|
CancellationToken.None);
|
|
|
|
Assert.IsType<OkObjectResult>(result);
|
|
fixture.Db.ChangeTracker.Clear();
|
|
var batch = await fixture.Db.DormitoryHygieneBatches.SingleAsync();
|
|
Assert.Equal("第七周复查", batch.Name);
|
|
Assert.Equal(new DateOnly(2026, 10, 18), batch.InspectionDate);
|
|
Assert.Equal("宿管科", batch.Inspector);
|
|
Assert.Equal(DormitoryHygieneBatchStatus.Published, batch.Status);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteBatch_DeletesDraftScoresButRejectsPublishedBatch()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync();
|
|
fixture.Db.Add(ScoreFor(fixture.Batch.Id, fixture.Room.Id, 70));
|
|
await fixture.Db.SaveChangesAsync();
|
|
var controller = new DormitoryHygieneController(fixture.Db, fixture.AllScope);
|
|
|
|
Assert.IsType<OkObjectResult>(await controller.DeleteBatch(
|
|
fixture.Batch.Id, CancellationToken.None));
|
|
Assert.Empty(await fixture.Db.DormitoryHygieneBatches.ToListAsync());
|
|
Assert.Empty(await fixture.Db.DormitoryHygieneScores.ToListAsync());
|
|
|
|
var published = new DormitoryHygieneBatch
|
|
{
|
|
Name = "已发布检查", InspectionDate = new DateOnly(2026, 10, 20),
|
|
Status = DormitoryHygieneBatchStatus.Published
|
|
};
|
|
fixture.Db.Add(published);
|
|
await fixture.Db.SaveChangesAsync();
|
|
var conflict = Assert.IsType<ConflictObjectResult>(await controller.DeleteBatch(
|
|
published.Id, CancellationToken.None));
|
|
Assert.Contains("不能删除", Assert.IsType<ProblemDetails>(conflict.Value).Detail);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Export_ReturnsCurrentBatchScoresWorkbook()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync();
|
|
fixture.Db.Add(ScoreFor(fixture.Batch.Id, fixture.Room.Id, 90, true));
|
|
await fixture.Db.SaveChangesAsync();
|
|
var controller = new DormitoryHygieneController(fixture.Db, fixture.AllScope);
|
|
|
|
var result = Assert.IsType<FileContentResult>(await controller.Export(fixture.Batch.Id, CancellationToken.None));
|
|
|
|
using var workbook = new XLWorkbook(new MemoryStream(result.FileContents));
|
|
var sheet = workbook.Worksheet("宿舍卫生成绩");
|
|
Assert.Equal("宿舍楼", sheet.Cell("A1").GetString());
|
|
Assert.Equal("楼层", sheet.Cell("C1").GetString());
|
|
Assert.Equal("是否违纪", sheet.Cell("I1").GetString());
|
|
Assert.Equal("是", sheet.Cell("I2").GetString());
|
|
Assert.Equal("总分", sheet.Cell("L1").GetString());
|
|
Assert.Equal(90, sheet.Cell("L2").GetDouble());
|
|
Assert.Equal("优秀", sheet.Cell("M2").GetString());
|
|
var headers = Assert.IsType<string[]>(Assert.IsType<OkObjectResult>(controller.ExportColumns()).Value);
|
|
Assert.Equal("总分", headers[11]);
|
|
var custom = Assert.IsType<FileContentResult>(await controller.Export(
|
|
fixture.Batch.Id, CancellationToken.None, columns: "11,0", format: "xlsx"));
|
|
using var selected = new XLWorkbook(new MemoryStream(custom.FileContents));
|
|
Assert.Equal("总分", selected.Worksheet(1).Cell(1, 1).GetString());
|
|
Assert.Equal(90, selected.Worksheet(1).Cell(2, 1).GetDouble());
|
|
Assert.Equal("宿舍楼", selected.Worksheet(1).Cell(1, 2).GetString());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CreateCalculation_QueuesSelectedPublishedBatches()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync();
|
|
fixture.Batch.Status = DormitoryHygieneBatchStatus.Published;
|
|
await fixture.Db.SaveChangesAsync();
|
|
var controller = new DormitoryHygieneController(fixture.Db, fixture.AllScope);
|
|
|
|
var result = await controller.CreateCalculation(
|
|
new CreateDormitoryHygieneCalculationRequest(fixture.Term.Id, [fixture.Batch.Id]),
|
|
CancellationToken.None);
|
|
|
|
Assert.IsType<AcceptedResult>(result);
|
|
var calculation = await fixture.Db.DormitoryHygieneCalculations.SingleAsync();
|
|
Assert.Equal(DormitoryHygieneCalculationStatus.Queued, calculation.Status);
|
|
Assert.Equal(1, calculation.SelectedBatchCount);
|
|
Assert.Equal(fixture.Batch.Id,
|
|
(await fixture.Db.DormitoryHygieneCalculationBatches.SingleAsync()).DormitoryHygieneBatchId);
|
|
var outbox = await fixture.Db.BackgroundJobOutboxMessages.SingleAsync();
|
|
Assert.Equal(BackgroundJobKind.DormitoryHygieneCalculation, outbox.JobKind);
|
|
Assert.Equal(calculation.Id, outbox.JobId);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CalculationProcessor_UsesStayWindowsAndTracksMissingScores()
|
|
{
|
|
await using var fixture = await Fixture.CreateAsync();
|
|
var originalOccupancy = await fixture.Db.DormitoryOccupancies.SingleAsync(x => x.StudentId == fixture.Student.Id);
|
|
var originalBedId = originalOccupancy.DormitoryBedId;
|
|
fixture.Db.DormitoryOccupancies.Remove(originalOccupancy);
|
|
var targetRoom = new DormitoryRoom
|
|
{
|
|
DormitoryBuildingId = fixture.Room.DormitoryBuildingId,
|
|
RoomNumber = "102",
|
|
Floor = 1
|
|
};
|
|
var targetBed = new DormitoryBed { DormitoryRoomId = targetRoom.Id, BedNumber = "1" };
|
|
var batches = new[]
|
|
{
|
|
new DormitoryHygieneBatch { Name = "调宿前", InspectionDate = new DateOnly(2026, 9, 15), Status = DormitoryHygieneBatchStatus.Published },
|
|
new DormitoryHygieneBatch { Name = "调宿后", InspectionDate = new DateOnly(2026, 10, 15), Status = DormitoryHygieneBatchStatus.Published },
|
|
new DormitoryHygieneBatch { Name = "缺少宿舍成绩", InspectionDate = new DateOnly(2026, 10, 20), Status = DormitoryHygieneBatchStatus.Published },
|
|
new DormitoryHygieneBatch { Name = "退宿后", InspectionDate = new DateOnly(2026, 11, 15), Status = DormitoryHygieneBatchStatus.Published }
|
|
};
|
|
var calculation = new DormitoryHygieneCalculation
|
|
{
|
|
AcademicTermId = fixture.Term.Id,
|
|
CreatedByUserId = fixture.AllScope.Current.UserId,
|
|
SelectedBatchCount = batches.Length
|
|
};
|
|
fixture.Db.AddRange(targetRoom, targetBed);
|
|
fixture.Db.AddRange(batches);
|
|
fixture.Db.AddRange(
|
|
new DormitoryStayHistory { StudentId = fixture.Student.Id, ToBedId = originalBedId,
|
|
Action = DormitoryStayAction.CheckIn, EffectiveDate = new DateOnly(2026, 9, 1),
|
|
OperatedByUserId = fixture.AllScope.Current.UserId },
|
|
new DormitoryStayHistory { StudentId = fixture.Student.Id, FromBedId = originalBedId, ToBedId = targetBed.Id,
|
|
Action = DormitoryStayAction.Transfer, EffectiveDate = new DateOnly(2026, 10, 10),
|
|
OperatedByUserId = fixture.AllScope.Current.UserId },
|
|
new DormitoryStayHistory { StudentId = fixture.Student.Id, FromBedId = targetBed.Id,
|
|
Action = DormitoryStayAction.Checkout, EffectiveDate = new DateOnly(2026, 11, 1),
|
|
OperatedByUserId = fixture.AllScope.Current.UserId },
|
|
ScoreFor(batches[0].Id, fixture.Room.Id, 90),
|
|
ScoreFor(batches[1].Id, targetRoom.Id, 70, true),
|
|
ScoreFor(batches[3].Id, targetRoom.Id, 100),
|
|
calculation);
|
|
fixture.Db.DormitoryHygieneCalculationBatches.AddRange(batches.Select(batch =>
|
|
new DormitoryHygieneCalculationBatch
|
|
{
|
|
DormitoryHygieneCalculationId = calculation.Id,
|
|
DormitoryHygieneBatchId = batch.Id
|
|
}));
|
|
await fixture.Db.SaveChangesAsync();
|
|
|
|
await new DormitoryHygieneCalculationProcessor(fixture.Db)
|
|
.ProcessAsync(calculation.Id, CancellationToken.None);
|
|
|
|
fixture.Db.ChangeTracker.Clear();
|
|
var summary = await fixture.Db.DormitoryHygieneStudentSummaries.SingleAsync();
|
|
Assert.Equal(80m, summary.AverageScore);
|
|
Assert.Equal(3, summary.ExpectedBatchCount);
|
|
Assert.Equal(2, summary.IncludedScoreCount);
|
|
Assert.Equal(1, summary.MissingScoreCount);
|
|
Assert.Equal(1, summary.ViolationCount);
|
|
var completed = await fixture.Db.DormitoryHygieneCalculations.SingleAsync();
|
|
Assert.Equal(DormitoryHygieneCalculationStatus.Succeeded, completed.Status);
|
|
Assert.Equal(1, completed.StudentCount);
|
|
Assert.Equal(0, completed.StudentWithoutScoreCount);
|
|
}
|
|
|
|
private static DormitoryHygieneScore ScoreFor(Guid batchId, Guid roomId, decimal total, bool isViolation = false) => new()
|
|
{
|
|
DormitoryHygieneBatchId = batchId,
|
|
DormitoryRoomId = roomId,
|
|
FloorScore = total / 5,
|
|
BeddingScore = total / 5,
|
|
BelongingsScore = total / 5,
|
|
FacilitiesScore = total / 5,
|
|
SafetyScore = total / 5,
|
|
TotalScore = total,
|
|
IsViolation = isViolation,
|
|
Grade = total >= 90 ? DormitoryHygieneGrade.Excellent : DormitoryHygieneGrade.Qualified
|
|
};
|
|
|
|
private sealed class Fixture : IAsyncDisposable
|
|
{
|
|
private readonly SqliteConnection connection;
|
|
public AppDbContext Db { get; }
|
|
public DormitoryRoom Room { get; }
|
|
public Student Student { get; }
|
|
public DormitoryRoom? OtherRoom { get; }
|
|
public DormitoryHygieneBatch Batch { get; }
|
|
public AcademicTerm Term { get; }
|
|
public ICurrentUserDataScope AllScope { get; }
|
|
public ICurrentUserDataScope CollegeScope { get; }
|
|
public ICurrentUserDataScope StudentScope { get; }
|
|
|
|
private Fixture(SqliteConnection connection, AppDbContext db, DormitoryRoom room, Student student,
|
|
DormitoryRoom? otherRoom, DormitoryHygieneBatch batch, AcademicTerm term, Guid collegeId, Guid userId)
|
|
{
|
|
this.connection = connection;
|
|
Db = db;
|
|
Room = room;
|
|
Student = student;
|
|
OtherRoom = otherRoom;
|
|
Batch = batch;
|
|
Term = term;
|
|
AllScope = new TestScope(userId, null, EffectiveDataScopeResolver.Resolve([SystemRoles.DormitoryAdmin]), SystemRoles.DormitoryAdmin);
|
|
CollegeScope = new TestScope(Guid.NewGuid(), collegeId, DataScope.College, SystemRoles.CollegeAdmin);
|
|
StudentScope = new TestScope(userId, collegeId, DataScope.Self, SystemRoles.Student);
|
|
}
|
|
|
|
public static async Task<Fixture> CreateAsync(bool includeSecondCollege = false)
|
|
{
|
|
var connection = new SqliteConnection("Data Source=:memory:");
|
|
await connection.OpenAsync();
|
|
var db = new AppDbContext(new DbContextOptionsBuilder<AppDbContext>().UseSqlite(connection).Options);
|
|
await db.Database.EnsureCreatedAsync();
|
|
var userId = Guid.NewGuid();
|
|
var user = new ApplicationUser { Id = userId, UserName = "student", DisplayName = "学生甲" };
|
|
var term = new AcademicTerm
|
|
{
|
|
Code = "2026-AUTUMN", Name = "2026-2027 学年第一学期", AcademicYear = "2026-2027",
|
|
Season = TermSeason.Autumn, StartDate = new DateOnly(2026, 9, 1), EndDate = new DateOnly(2027, 1, 15),
|
|
IsCurrent = true
|
|
};
|
|
var college = new College { Code = "CS", Name = "计算机学院" };
|
|
var major = new Major { Code = "SE", Name = "软件工程", CollegeId = college.Id, DegreeType = "工学" };
|
|
var classRoom = new AdministrativeClass { Code = "SE2026", Name = "软件 2026 级", MajorId = major.Id, Grade = 2026 };
|
|
var student = new Student { StudentNumber = "202600001", Name = "学生甲", UserId = userId, AdministrativeClassId = classRoom.Id, EnrollmentYear = 2026, EnrollmentDate = new DateOnly(2026, 9, 1) };
|
|
var building = new DormitoryBuilding { Code = "MY1", Name = "梅园 1 栋", FloorCount = 9 };
|
|
var room = new DormitoryRoom { DormitoryBuildingId = building.Id, RoomNumber = "101", Floor = 1 };
|
|
var beds = Enumerable.Range(1, 4).Select(number => new DormitoryBed
|
|
{
|
|
DormitoryRoomId = room.Id,
|
|
BedNumber = number.ToString()
|
|
}).ToArray();
|
|
var occupancy = new DormitoryOccupancy { DormitoryBedId = beds[0].Id, StudentId = student.Id, CheckInDate = new DateOnly(2026, 9, 1) };
|
|
var batch = new DormitoryHygieneBatch { Name = "第七周检查", InspectionDate = new DateOnly(2026, 10, 16) };
|
|
db.AddRange(user, term, college, major, classRoom, student, building, room, occupancy, batch);
|
|
db.AddRange(beds);
|
|
DormitoryRoom? otherRoom = null;
|
|
if (includeSecondCollege)
|
|
{
|
|
var otherCollege = new College { Code = "ART", Name = "艺术学院" };
|
|
var otherMajor = new Major { Code = "ART", Name = "艺术学", CollegeId = otherCollege.Id, DegreeType = "艺术学" };
|
|
var otherClass = new AdministrativeClass { Code = "ART2026", Name = "艺术 2026 级", MajorId = otherMajor.Id, Grade = 2026 };
|
|
var otherStudent = new Student { StudentNumber = "202600002", Name = "学生乙", AdministrativeClassId = otherClass.Id, EnrollmentYear = 2026, EnrollmentDate = new DateOnly(2026, 9, 1) };
|
|
var otherBuilding = new DormitoryBuilding { Code = "LY2", Name = "兰园 2 栋", FloorCount = 9 };
|
|
otherRoom = new DormitoryRoom { DormitoryBuildingId = otherBuilding.Id, RoomNumber = "902", Floor = 9 };
|
|
var otherBeds = Enumerable.Range(1, 4).Select(number => new DormitoryBed
|
|
{
|
|
DormitoryRoomId = otherRoom.Id,
|
|
BedNumber = number.ToString()
|
|
}).ToArray();
|
|
db.AddRange(otherCollege, otherMajor, otherClass, otherStudent, otherBuilding, otherRoom,
|
|
new DormitoryOccupancy { DormitoryBedId = otherBeds[0].Id, StudentId = otherStudent.Id, CheckInDate = new DateOnly(2026, 9, 1) });
|
|
db.AddRange(otherBeds);
|
|
}
|
|
await db.SaveChangesAsync();
|
|
return new Fixture(connection, db, room, student, otherRoom, batch, term, college.Id, userId);
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await Db.DisposeAsync();
|
|
await connection.DisposeAsync();
|
|
}
|
|
}
|
|
|
|
private sealed class TestScope(Guid userId, Guid? collegeId, DataScope dataScope, string role) : ICurrentUserDataScope
|
|
{
|
|
public CurrentUserScope Current { get; } = new(userId, "测试用户", collegeId, dataScope, new HashSet<string>([role]));
|
|
}
|
|
}
|