Files
WCTDataMiner/tests/Gpulse.WCT.DataAnalyzer.Tests/Commands/AggregateCommandTests.cs
Scottxjw 7a8a1e9a87 test: 更新分析功能和迁移测试用例
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-25 11:30:16 +08:00

86 lines
3.4 KiB
C#

using Microsoft.EntityFrameworkCore;
namespace Gpulse.WCT.DataAnalyzer.Tests.Commands;
[TestFixture]
public class AggregateCommandTests : CommandTestBase
{
[Test]
public async Task Aggregate_EmptyDatabase_ReportsZero()
{
var (code, stdout, _) = await Aggregate().RunCaptureAsync();
Assert.Multiple(() =>
{
Assert.That(code, Is.EqualTo(0));
Assert.That(stdout, Does.Contain("Aggregated 0 local Ploss records into 0 release records."));
});
}
[Test]
public async Task Aggregate_AfterParse_GroupsByThresholdIntoReleaseDb()
{
var dir = NewDir();
var file = TestFixtures.WritePlossLog(dir, TestFixtures.PlossFileName,
new TestFixtures.PlossTwoLineRow(Field7: 100, Field9: 350, Field10: 350),
new TestFixtures.PlossTwoLineRow(Field7: 110, Field9: 350, Field10: 350),
new TestFixtures.PlossTwoLineRow(Field7: 200, Field9: 500, Field10: 500));
await Parse().RunCaptureAsync("--file", file);
var (code, stdout, _) = await Aggregate().RunCaptureAsync();
Assert.Multiple(() =>
{
Assert.That(code, Is.EqualTo(0));
Assert.That(stdout, Does.Contain("Aggregated 3 local Ploss records into 1 release records."));
});
var record = await Host.ReleaseDb.ChargingParameters.SingleAsync();
Assert.Multiple(() =>
{
// 维度来自文件名:奇瑞(车厂) / E5(车型) / iPhone15(RxType)
Assert.That(record.CarFactory, Is.EqualTo("奇瑞"));
Assert.That(record.CarModel, Is.EqualTo("E5"));
Assert.That(record.PhoneModel, Is.EqualTo("iPhone15"));
// Field9=350 的两行 Field7 取平均 = (100+110)/2
Assert.That(record.Power350mW, Is.EqualTo(105));
Assert.That(record.Power500mW, Is.EqualTo(200));
});
}
[Test]
public async Task Aggregate_DefaultRebuild_DoesNotDuplicateOnSecondRun()
{
var dir = NewDir();
var file = TestFixtures.WritePlossLog(dir, TestFixtures.PlossFileName,
new TestFixtures.PlossTwoLineRow(Field7: 100, Field9: 350, Field10: 350));
await Parse().RunCaptureAsync("--file", file);
await Aggregate().RunCaptureAsync();
await Aggregate().RunCaptureAsync();
Assert.That(await Host.ReleaseDb.ChargingParameters.CountAsync(), Is.EqualTo(1));
}
[Test]
public async Task Aggregate_RebuildFalse_InsertsIntoEmptyTable()
{
// 注:发布库对业务键 (车厂/车型/手机厂商/型号) 有唯一索引;
// append 模式会对全部本地记录重新入库,因此只适合表为空时使用,
// 对已聚合过的同一数据再 append 会因唯一索引冲突而失败(聚合命令捕获并输出 "Aggregation failed")。
var dir = NewDir();
var file = TestFixtures.WritePlossLog(dir, TestFixtures.PlossFileName,
new TestFixtures.PlossTwoLineRow(Field7: 100, Field9: 350, Field10: 350));
await Parse().RunCaptureAsync("--file", file);
var (code, stdout, _) = await Aggregate().RunCaptureAsync("--rebuild", "false");
Assert.Multiple(() =>
{
Assert.That(code, Is.EqualTo(0));
Assert.That(stdout, Does.Contain("Aggregated 1 local Ploss records into 1 release records."));
});
Assert.That(await Host.ReleaseDb.ChargingParameters.CountAsync(), Is.EqualTo(1));
}
}