using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using NewLife;
using XCode.Code;
using XCode.DataAccessLayer;
using Xunit;
namespace XCoder.App.Tests;
/// <summary>数据建模:从 SQLite 表结构生成实体代码(DEV-1 的核心链路)</summary>
/// <remarks>
/// 用临时 SQLite 库作为数据源,生成实体到临时目录,验证生成结果文件真实存在且包含实体类型与字段。
/// 全程不触碰仓库目录,测试后可安全清理。
/// </remarks>
public class EntityBuilderIntegrationTests : IDisposable
{
private readonly String _dir;
private readonly String _connName;
private readonly String _output;
public EntityBuilderIntegrationTests()
{
_dir = Path.Combine(Path.GetTempPath(), "xcoder-entity-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_dir);
_connName = "xcoder_entity_" + Guid.NewGuid().ToString("N")[..8];
_output = Path.Combine(_dir, "output");
DAL.AddConnStr(_connName, $"Data Source={Path.Combine(_dir, "model.db")}", null, "SQLite");
}
public void Dispose()
{
try
{
DAL.ConnStrs.Remove(_connName);
}
catch (Exception)
{
// 连接串可能已被移除
}
try
{
if (Directory.Exists(_dir)) Directory.Delete(_dir, true);
}
catch (Exception)
{
// 文件被占用时忽略
}
}
[Fact]
[DisplayName("生成实体时输出目录会创建并写出 .cs 文件")]
public void BuildTables_WritesEntityFile()
{
var dal = DAL.Create(_connName);
dal.Execute("create table devices(id integer primary key autoincrement, name varchar(50) not null, port int)");
var table = dal.Tables!.First(e => e.TableName.EqualIgnoreCase("devices"));
table.ConnName = _connName;
var option = new EntityBuilderOption
{
BaseClass = "Entity",
ConnName = _connName,
Namespace = "XCoder.Temp",
Output = _output,
};
var rs = EntityBuilder.BuildTables([table], option);
// 返回值不代表文件数量,以实际生成的文件为准
Assert.True(rs >= 0);
var files = Directory.GetFiles(_output, "*.cs", SearchOption.AllDirectories);
Assert.NotEmpty(files);
var text = File.ReadAllText(files[0]);
Assert.Contains("class", text);
Assert.Contains("Devices", text);
}
[Fact]
[DisplayName("字段被写进实体,生成文件含 Name 与 Port 属性")]
public void BuildTables_ContainsColumns()
{
var dal = DAL.Create(_connName);
dal.Execute("create table sensors(id integer primary key autoincrement, name varchar(50), port int, shown bit)");
var table = dal.Tables!.First(e => e.TableName.EqualIgnoreCase("sensors"));
table.ConnName = _connName;
var option = new EntityBuilderOption
{
BaseClass = "Entity",
ConnName = _connName,
Namespace = "XCoder.Temp",
Output = _output,
};
EntityBuilder.BuildTables([table], option);
var files = Directory.GetFiles(_output, "*.cs", SearchOption.AllDirectories);
Assert.NotEmpty(files);
var text = String.Join("\n", files.Select(File.ReadAllText));
Assert.Contains("Port", text);
Assert.Contains("Name", text);
}
[Fact]
[DisplayName("指定输出目录时不会抛异常并会落地文件")]
public void BuildTables_WithOutput_NoThrow()
{
var dal = DAL.Create(_connName);
dal.Execute("create table t1(id integer primary key autoincrement, code varchar(10))");
var table = dal.Tables!.First(e => e.TableName.EqualIgnoreCase("t1"));
table.ConnName = _connName;
var option = new EntityBuilderOption
{
BaseClass = "Entity",
ConnName = _connName,
Namespace = "XCoder.Temp",
Output = _output,
ChineseFileName = false,
};
var rs = EntityBuilder.BuildTables([table], option);
Assert.True(rs >= 0);
Assert.NotEmpty(Directory.GetFiles(_output, "*.cs", SearchOption.AllDirectories));
}
}
|