CounterDroneBackend/test/unit/CounterDrone.Core.Tests/DatabaseManagerTests.cs
tian ca0ce8aa52 重命名核心模型:TargetConfig→DroneProfile, SimTask→Scenario, TaskFullConfig→ScenarioConfig, TaskId→ScenarioId
- DroneType 枚举清理:移除 Electric/Piston,重编号 HighSpeed=2
- planner_config.json / defaults.json 同步迁移
- 238 单元测试全部通过
2026-06-18 15:02:58 +08:00

90 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using CounterDrone.Core;
using CounterDrone.Core.Models;
using CounterDrone.Core.Repository;
using Xunit;
namespace CounterDrone.Core.Tests
{
public class DatabaseManagerTests : IDisposable
{
private readonly string _testDir;
private readonly IPathProvider _paths;
private readonly DatabaseManager _dbManager;
public DatabaseManagerTests()
{
_testDir = Path.Combine(Path.GetTempPath(), $"cd_test_{Guid.NewGuid():N}");
_paths = new TestPathProvider(_testDir);
_dbManager = new DatabaseManager(_paths);
}
public void Dispose()
{
if (Directory.Exists(_testDir))
Directory.Delete(_testDir, true);
}
[Fact]
public void OpenMainDb_CreatesAllTables()
{
var db = _dbManager.OpenMainDb();
// 验证所有 12 张表存在(含 Meta
var tables = new[]
{
"ModelInfo", "AmmunitionSpec", "Scenario", "CombatScene",
"ControlZone", "DroneProfile", "EquipmentDeployment",
"CloudDispersal", "RoutePlan", "Waypoint",
"SimulationReport"
};
foreach (var table in tables)
{
var count = db.ExecuteScalar<int>(
$"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='{table}'");
Assert.Equal(1, count);
}
db.Close();
}
[Fact]
public void OpenFrameDb_CreatesFrameRecordTable()
{
var db = _dbManager.OpenFrameDb("task_001");
var count = db.ExecuteScalar<int>(
"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='SimFrameRecord'");
Assert.Equal(1, count);
db.Close();
}
[Fact]
public void DeleteFrameDb_RemovesFile()
{
var scenarioId = "task_del";
var db = _dbManager.OpenFrameDb(scenarioId);
db.Close();
_dbManager.DeleteFrameDb(scenarioId);
var dbPath = Path.Combine(_paths.GetFramesDir(), $"{scenarioId}.db");
Assert.False(File.Exists(dbPath));
}
[Fact]
public void OpenMainDb_IsIdempotent()
{
var db1 = _dbManager.OpenMainDb();
db1.Close();
// 第二次打开不抛异常
var db2 = _dbManager.OpenMainDb();
db2.Close();
}
}
}