194 lines
6.9 KiB
C#
194 lines
6.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
||
using HardwarePerformance.Core.Entities;
|
||
using HardwarePerformance.Infrastructure.Data;
|
||
|
||
namespace HardwarePerformance.Infrastructure.Tests
|
||
{
|
||
/// <summary>
|
||
/// 测试基类,提供InMemory数据库设置
|
||
/// </summary>
|
||
public abstract class TestBase : IDisposable
|
||
{
|
||
protected AppDbContext _context;
|
||
protected DbContextOptions<AppDbContext> _options;
|
||
|
||
public TestBase()
|
||
{
|
||
// 创建InMemory数据库选项
|
||
_options = new DbContextOptionsBuilder<AppDbContext>()
|
||
.UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString())
|
||
.Options;
|
||
|
||
// 创建数据库上下文
|
||
_context = new AppDbContext(_options);
|
||
|
||
// 初始化数据库
|
||
SeedDatabase();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化测试数据
|
||
/// </summary>
|
||
protected virtual void SeedDatabase()
|
||
{
|
||
// 创建测试类别
|
||
var categories = new List<Category>
|
||
{
|
||
new Category { Id = 1, Name = "手机CPU", Description = "移动设备处理器", IconUrl = "cpu" },
|
||
new Category { Id = 2, Name = "电脑CPU", Description = "桌面处理器", IconUrl = "desktop" },
|
||
new Category { Id = 3, Name = "手机GPU", Description = "移动设备图形处理器", IconUrl = "gpu" },
|
||
new Category { Id = 4, Name = "电脑GPU", Description = "桌面图形处理器", IconUrl = "graphics" }
|
||
};
|
||
|
||
_context.Categories.AddRange(categories);
|
||
|
||
// 创建测试数据源
|
||
var dataSources = new List<DataSource>
|
||
{
|
||
new DataSource { Id = 1, Name = "GeekBench", Description = "GeekBench基准测试", Url = "https://www.geekbench.com" },
|
||
new DataSource { Id = 2, Name = "3DMark", Description = "3DMark图形性能测试", Url = "https://www.3dmark.com" },
|
||
new DataSource { Id = 3, Name = "AnTuTu", Description = "AnTuTu综合性能测试", Url = "https://www.antutu.com" }
|
||
};
|
||
|
||
_context.DataSources.AddRange(dataSources);
|
||
|
||
// 创建测试产品
|
||
var products = new List<Product>
|
||
{
|
||
new Product
|
||
{
|
||
Id = 1,
|
||
Name = "Snapdragon 8 Gen 3",
|
||
Model = "SM8650-AB",
|
||
Manufacturer = "Qualcomm",
|
||
CategoryId = 1,
|
||
ReleaseDate = new DateTime(2023, 10, 24),
|
||
CurrentRank = 1,
|
||
ImageUrl = "https://example.com/snapdragon8gen3.jpg"
|
||
},
|
||
new Product
|
||
{
|
||
Id = 2,
|
||
Name = "Apple A17 Pro",
|
||
Model = "A17 Pro",
|
||
Manufacturer = "Apple",
|
||
CategoryId = 1,
|
||
ReleaseDate = new DateTime(2023, 9, 12),
|
||
CurrentRank = 2,
|
||
ImageUrl = "https://example.com/a17pro.jpg"
|
||
},
|
||
new Product
|
||
{
|
||
Id = 3,
|
||
Name = "Intel Core i9-13900K",
|
||
Model = "i9-13900K",
|
||
Manufacturer = "Intel",
|
||
CategoryId = 2,
|
||
ReleaseDate = new DateTime(2022, 10, 20),
|
||
CurrentRank = 1,
|
||
ImageUrl = "https://example.com/i913900k.jpg"
|
||
},
|
||
new Product
|
||
{
|
||
Id = 4,
|
||
Name = "AMD Ryzen 9 7950X",
|
||
Model = "Ryzen 9 7950X",
|
||
Manufacturer = "AMD",
|
||
CategoryId = 2,
|
||
ReleaseDate = new DateTime(2022, 9, 27),
|
||
CurrentRank = 2,
|
||
ImageUrl = "https://example.com/ryzen97950x.jpg"
|
||
}
|
||
};
|
||
|
||
_context.Products.AddRange(products);
|
||
|
||
// 创建测试性能分数
|
||
var performanceScores = new List<PerformanceScore>
|
||
{
|
||
new PerformanceScore
|
||
{
|
||
Id = 1,
|
||
ProductId = 1,
|
||
DataSourceId = 1,
|
||
Score = 7300,
|
||
TestType = "Single-Core",
|
||
TestDate = new DateTime(2023, 11, 1)
|
||
},
|
||
new PerformanceScore
|
||
{
|
||
Id = 2,
|
||
ProductId = 1,
|
||
DataSourceId = 1,
|
||
Score = 22000,
|
||
TestType = "Multi-Core",
|
||
TestDate = new DateTime(2023, 11, 1)
|
||
},
|
||
new PerformanceScore
|
||
{
|
||
Id = 3,
|
||
ProductId = 2,
|
||
DataSourceId = 1,
|
||
Score = 7100,
|
||
TestType = "Single-Core",
|
||
TestDate = new DateTime(2023, 10, 1)
|
||
},
|
||
new PerformanceScore
|
||
{
|
||
Id = 4,
|
||
ProductId = 2,
|
||
DataSourceId = 1,
|
||
Score = 21000,
|
||
TestType = "Multi-Core",
|
||
TestDate = new DateTime(2023, 10, 1)
|
||
},
|
||
new PerformanceScore
|
||
{
|
||
Id = 5,
|
||
ProductId = 3,
|
||
DataSourceId = 1,
|
||
Score = 3200,
|
||
TestType = "Single-Core",
|
||
TestDate = new DateTime(2022, 11, 1)
|
||
},
|
||
new PerformanceScore
|
||
{
|
||
Id = 6,
|
||
ProductId = 3,
|
||
DataSourceId = 1,
|
||
Score = 22000,
|
||
TestType = "Multi-Core",
|
||
TestDate = new DateTime(2022, 11, 1)
|
||
},
|
||
new PerformanceScore
|
||
{
|
||
Id = 7,
|
||
ProductId = 4,
|
||
DataSourceId = 1,
|
||
Score = 3100,
|
||
TestType = "Single-Core",
|
||
TestDate = new DateTime(2022, 10, 1)
|
||
},
|
||
new PerformanceScore
|
||
{
|
||
Id = 8,
|
||
ProductId = 4,
|
||
DataSourceId = 1,
|
||
Score = 21000,
|
||
TestType = "Multi-Core",
|
||
TestDate = new DateTime(2022, 10, 1)
|
||
}
|
||
};
|
||
|
||
_context.PerformanceScores.AddRange(performanceScores);
|
||
|
||
// 保存更改
|
||
_context.SaveChanges();
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
_context.Dispose();
|
||
}
|
||
}
|
||
} |