82 lines
3.0 KiB
C#
82 lines
3.0 KiB
C#
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace Gpulse.WCT.DataAnalyzer.Tests.Commands;
|
||
|
|
|
||
|
|
[TestFixture]
|
||
|
|
public class CleanCommandTests : CommandTestBase
|
||
|
|
{
|
||
|
|
[Test]
|
||
|
|
public async Task Clean_NoConfirm_PrintsPrompt()
|
||
|
|
{
|
||
|
|
var (code, stdout, _) = await Clean().RunCaptureAsync("--all");
|
||
|
|
|
||
|
|
Assert.Multiple(() =>
|
||
|
|
{
|
||
|
|
Assert.That(code, Is.EqualTo(0));
|
||
|
|
Assert.That(stdout, Does.Contain("Please add --confirm to confirm the clean operation."));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task Clean_InvalidScenarioGuid_PrintsMessage()
|
||
|
|
{
|
||
|
|
var (_, stdout, _) = await Clean().RunCaptureAsync("--scenario", "not-a-guid", "--confirm");
|
||
|
|
|
||
|
|
Assert.That(stdout, Does.Contain("Invalid scenario ID: not-a-guid"));
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task Clean_Scenario_NotExist_PrintsZeroCount()
|
||
|
|
{
|
||
|
|
var scenarioId = Guid.NewGuid();
|
||
|
|
var (_, stdout, _) = await Clean().RunCaptureAsync("--scenario", scenarioId.ToString(), "--confirm");
|
||
|
|
|
||
|
|
Assert.That(stdout, Does.Contain($"Cleaned 0 records for scenario {scenarioId}"));
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task Clean_Scenario_WithConfirm_SoftDeletesRelatedRecords()
|
||
|
|
{
|
||
|
|
var dir = NewDir();
|
||
|
|
var file = TestFixtures.WritePlossLog(dir, TestFixtures.PlossFileName,
|
||
|
|
new TestFixtures.PlossTwoLineRow(),
|
||
|
|
new TestFixtures.PlossTwoLineRow(Field9: 2500, Field12: 1));
|
||
|
|
await Parse().RunCaptureAsync("--file", file);
|
||
|
|
|
||
|
|
var scenarioId = await Host.LocalDb.TestScenarios.Select(s => s.Id).SingleAsync();
|
||
|
|
|
||
|
|
var (code, stdout, _) = await Clean().RunCaptureAsync("--scenario", scenarioId.ToString(), "--confirm");
|
||
|
|
|
||
|
|
Assert.Multiple(() =>
|
||
|
|
{
|
||
|
|
Assert.That(code, Is.EqualTo(0));
|
||
|
|
Assert.That(stdout, Does.Contain($"Cleaned 3 records for scenario {scenarioId}"));
|
||
|
|
});
|
||
|
|
|
||
|
|
// 2 条 Ploss + 1 个场景全部软删除
|
||
|
|
Assert.That(await Host.LocalDb.PlossRecords.IgnoreQueryFilters().CountAsync(r => r.IsDeleted), Is.EqualTo(2));
|
||
|
|
Assert.That(await Host.LocalDb.TestScenarios.IgnoreQueryFilters().CountAsync(s => s.IsDeleted), Is.EqualTo(1));
|
||
|
|
}
|
||
|
|
|
||
|
|
[Test]
|
||
|
|
public async Task Clean_All_WithConfirm_SoftDeletesEverything()
|
||
|
|
{
|
||
|
|
var dir = NewDir();
|
||
|
|
var file = TestFixtures.WritePlossLog(dir, TestFixtures.PlossFileName,
|
||
|
|
new TestFixtures.PlossTwoLineRow(),
|
||
|
|
new TestFixtures.PlossTwoLineRow());
|
||
|
|
await Parse().RunCaptureAsync("--file", file);
|
||
|
|
|
||
|
|
var (code, stdout, _) = await Clean().RunCaptureAsync("--all", "--confirm");
|
||
|
|
|
||
|
|
Assert.Multiple(() =>
|
||
|
|
{
|
||
|
|
Assert.That(code, Is.EqualTo(0));
|
||
|
|
Assert.That(stdout, Does.Contain("Cleaned 3 records in total"));
|
||
|
|
});
|
||
|
|
|
||
|
|
Assert.That(await Host.LocalDb.PlossRecords.IgnoreQueryFilters().CountAsync(r => r.IsDeleted), Is.EqualTo(2));
|
||
|
|
Assert.That(await Host.LocalDb.TestScenarios.IgnoreQueryFilters().CountAsync(s => s.IsDeleted), Is.EqualTo(1));
|
||
|
|
}
|
||
|
|
}
|