CounterDroneBackend/test/unit/CounterDrone.Core.Tests/DatabaseManagerTests.cs
tian 8eeb43f0e8 feat: 统一默认数据架构 (defaults.json + DefaultData)
单一数据源 data/defaults.json,含弹药/编队/火力单元/无人机/探测/天气六类预设

版本追踪: Meta 表 + version 字段,更新时 InsertOrReplace,不删用户数据

名称统一 [Demo] 前缀,UI 中可识别为模拟数据

删除: DefaultAmmunition.cs, DefaultFireUnits.cs, default_ammo.json, default_formations.json, TestAmmo.cs

TestPathProvider 自动复制数据文件到测试目录

集成测试精简: 4个简单变体跳过,保留6个核心场景,39s
2026-06-15 15:46:20 +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", "SimTask", "CombatScene",
"ControlZone", "TargetConfig", "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 taskId = "task_del";
var db = _dbManager.OpenFrameDb(taskId);
db.Close();
_dbManager.DeleteFrameDb(taskId);
var dbPath = Path.Combine(_paths.GetFramesDir(), $"{taskId}.db");
Assert.False(File.Exists(dbPath));
}
[Fact]
public void OpenMainDb_IsIdempotent()
{
var db1 = _dbManager.OpenMainDb();
db1.Close();
// 第二次打开不抛异常
var db2 = _dbManager.OpenMainDb();
db2.Close();
}
}
}