()
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.Options;
return new TestAssessmentDbContext(options);
}
///
/// 创建测试测评类型
///
private AssessmentType CreateAssessmentType(long id)
{
return new AssessmentType
{
Id = id,
Name = $"Test Assessment Type {id}",
Code = $"TEST_{id}",
ImageUrl = $"https://example.com/type_{id}.jpg",
IntroContent = $"Introduction for type {id}
",
Price = 99.00m,
QuestionCount = 80,
Sort = (int)(id % 100),
Status = 1,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
IsDeleted = false
};
}
///
/// 创建测试题目
///
private Question CreateQuestion(long id, long typeId, int questionNo, int status, bool isDeleted)
{
return new Question
{
Id = id,
AssessmentTypeId = typeId,
QuestionNo = questionNo,
Content = $"Question {questionNo} content for type {typeId}",
Sort = questionNo,
Status = status,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
IsDeleted = isDeleted
};
}
///
/// 创建测试订单
///
private Order CreateOrder(long id, long userId)
{
return new Order
{
Id = id,
OrderNo = $"ORD{id:D10}",
UserId = userId,
OrderType = 1,
ProductId = 1,
ProductName = "Test Assessment",
Amount = 99.00m,
PayAmount = 99.00m,
Status = 2, // 已支付
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
IsDeleted = false
};
}
///
/// 创建测试测评记录
///
private AssessmentRecord CreateAssessmentRecord(long id, long userId, long orderId, long typeId)
{
return new AssessmentRecord
{
Id = id,
UserId = userId,
OrderId = orderId,
AssessmentTypeId = typeId,
Name = $"TestUser{userId}",
Phone = "13800138000",
Gender = 1,
Age = 18,
EducationStage = 3,
Province = "北京市",
City = "北京市",
District = "朝阳区",
Status = 4, // 已完成
CreateTime = DateTime.Now.AddMinutes(-id), // 不同的创建时间以测试排序
UpdateTime = DateTime.Now,
IsDeleted = false
};
}
///
/// 创建测试邀请码
///
/// 种子值,用于生成唯一ID和Code
/// 状态:1未分配 2已分配 3已使用
private InviteCode CreateInviteCode(long seed, int status)
{
return new InviteCode
{
Id = seed,
Code = GenerateRandomInviteCode(seed),
BatchNo = $"BATCH{seed / 100:D6}",
AssignUserId = status >= 2 ? seed + 500 : null,
AssignTime = status >= 2 ? DateTime.Now.AddDays(-7) : null,
UseUserId = status == 3 ? seed + 600 : null,
UseOrderId = status == 3 ? seed + 700 : null,
UseTime = status == 3 ? DateTime.Now.AddDays(-1) : null,
Status = status,
CreateTime = DateTime.Now.AddDays(-10),
UpdateTime = DateTime.Now,
IsDeleted = false
};
}
///
/// 创建测试报告分类
///
/// 分类ID
/// 测评类型ID
/// 分类类型:1八大智能 2个人特质 3细分能力 4先天学习 5学习能力 6大脑类型 7性格类型 8未来能力
private ReportCategory CreateReportCategory(long id, long typeId, int categoryType)
{
var categoryNames = new Dictionary
{
{ 1, "语言智能" },
{ 2, "领导力" },
{ 3, "逻辑推理" },
{ 4, "视觉学习" },
{ 5, "专注力" },
{ 6, "左脑型" },
{ 7, "外向型" },
{ 8, "创新能力" }
};
var categoryCodes = new Dictionary
{
{ 1, "LINGUISTIC" },
{ 2, "LEADERSHIP" },
{ 3, "LOGICAL" },
{ 4, "VISUAL" },
{ 5, "FOCUS" },
{ 6, "LEFT_BRAIN" },
{ 7, "EXTROVERT" },
{ 8, "INNOVATION" }
};
return new ReportCategory
{
Id = id,
AssessmentTypeId = typeId,
ParentId = 0,
Name = categoryNames.GetValueOrDefault(categoryType, $"Category_{categoryType}"),
Code = categoryCodes.GetValueOrDefault(categoryType, $"CAT_{categoryType}"),
CategoryType = categoryType,
ScoreRule = 1,
Sort = categoryType,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
IsDeleted = false
};
}
///
/// 创建测试测评结果
///
/// 结果ID
/// 测评记录ID
/// 分类ID
private AssessmentResult CreateAssessmentResult(long id, long recordId, long categoryId)
{
var random = new Random((int)(id % int.MaxValue));
var score = (decimal)(random.Next(60, 100));
var maxScore = 100m;
var percentage = score / maxScore * 100;
var starLevel = score >= 90 ? 5 : score >= 80 ? 4 : score >= 70 ? 3 : score >= 60 ? 2 : 1;
return new AssessmentResult
{
Id = id,
RecordId = recordId,
CategoryId = categoryId,
Score = score,
MaxScore = maxScore,
Percentage = percentage,
Rank = 1,
StarLevel = starLevel,
CreateTime = DateTime.Now
};
}
///
/// 生成随机邀请码(5位大写字母)
///
/// 种子值
private static string GenerateRandomInviteCode(long seed)
{
const string charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var random = new Random((int)(seed % int.MaxValue));
var chars = new char[5];
for (int i = 0; i < 5; i++)
{
chars[i] = charset[random.Next(charset.Length)];
}
return new string(chars);
}
#endregion
}
///
/// 测试用DbContext,继承自MiAssessmentDbContext但忽略外键关系验证
///
public class TestAssessmentDbContext : MiAssessmentDbContext
{
public TestAssessmentDbContext(DbContextOptions options)
: base(CreateBaseOptions(options))
{
}
private static DbContextOptions CreateBaseOptions(DbContextOptions options)
{
var builder = new DbContextOptionsBuilder();
builder.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
.ConfigureWarnings(w => w.Ignore(Microsoft.EntityFrameworkCore.Diagnostics.InMemoryEventId.TransactionIgnoredWarning));
return builder.Options;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// 不调用基类的OnModelCreating,避免外键关系验证
// 只配置必要的表映射
modelBuilder.Entity().ToTable("assessment_types");
modelBuilder.Entity().ToTable("questions");
modelBuilder.Entity().ToTable("assessment_records");
modelBuilder.Entity().ToTable("assessment_answers");
modelBuilder.Entity().ToTable("orders");
modelBuilder.Entity().ToTable("invite_codes");
modelBuilder.Entity().ToTable("report_categories");
modelBuilder.Entity().ToTable("assessment_results");
// 忽略导航属性
modelBuilder.Entity().Ignore(e => e.AssessmentType);
modelBuilder.Entity().Ignore(e => e.AssessmentType);
modelBuilder.Entity().Ignore(e => e.Record);
modelBuilder.Entity().Ignore(e => e.Question);
modelBuilder.Entity().Ignore(e => e.AssessmentType);
modelBuilder.Entity().Ignore(e => e.Record);
modelBuilder.Entity().Ignore(e => e.Category);
}
}