Skip to content

Commit

Permalink
增加模型管理单元测试,表里具有相近名字的字段
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Mar 14, 2024
1 parent b791ae4 commit 58357cc
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 15 deletions.
27 changes: 16 additions & 11 deletions XCode/Code/EntityBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ protected virtual void AddNameSpace()
// baseClass += "IModel";
//}

var bs = baseClass?.Split(',').Select(e => e.Trim()).ToList() ?? new List<String>();
var bs = baseClass?.Split(',').Select(e => e.Trim()).ToList() ?? [];

// 数据类的基类只有接口,业务类基类则比较复杂
var name = "";
Expand Down Expand Up @@ -680,7 +680,7 @@ protected override void BuildIndexItems()

if (!type.IsNullOrEmpty())
{
if (!type.Contains(".") && conv.GetMethod("To" + type, new Type[] { typeof(Object) }) != null)
if (!type.Contains(".") && conv.GetMethod("To" + type, [typeof(Object)]) != null)
{
switch (type)
{
Expand Down Expand Up @@ -988,7 +988,7 @@ protected virtual void BuildCctor()
WriteLine("}");
}

static String[] _validExcludes = new[] { "CreateUser", "CreateUserIP", "UpdateUser", "UpdateUserIP", "TraceId" };
static String[] _validExcludes = ["CreateUser", "CreateUserIP", "UpdateUser", "UpdateUserIP", "TraceId"];
/// <summary>数据验证</summary>
protected virtual void BuildValid()
{
Expand Down Expand Up @@ -1325,7 +1325,7 @@ protected virtual void BuildExtendSearch()
var names = new List<String>();

// 主键
IDataColumn pk = null;
IDataColumn? pk = null;
if (Table.PrimaryKeys.Length == 1)
{
pk = Table.PrimaryKeys[0];
Expand All @@ -1343,7 +1343,7 @@ protected virtual void BuildExtendSearch()
WriteLine("public static {3} {0}({1} {2})", methodName, type, name, ClassName);
WriteLine("{");
{
if (pk.DataType.IsInt())
if (pk.DataType != null && pk.DataType.IsInt())
WriteLine("if ({0} <= 0) return null;", name);
else if (pk.DataType == typeof(String))
WriteLine("if ({0}.IsNullOrEmpty()) return null;", name);
Expand Down Expand Up @@ -1378,7 +1378,7 @@ protected virtual void BuildExtendSearch()
if (cs == null || cs.Length != di.Columns.Length) continue;

// 索引最后一个字段如果是主键Id,则该不参与生成查询方法
if (pk != null && cs[cs.Length - 1].ColumnName.EqualIgnoreCase(pk.ColumnName))
if (pk != null && cs[^1].ColumnName.EqualIgnoreCase(pk.ColumnName))
{
cs = cs.Take(cs.Length - 1).ToArray();
}
Expand Down Expand Up @@ -1420,7 +1420,7 @@ protected virtual void BuildExtendSearch()
var type = dc.Properties["Type"];
if (type.IsNullOrEmpty()) type = dc.DataType?.Name;

ps[dc.CamelName()] = type;
ps[dc.CamelName()] = type!;
}
var args = ps.Join(", ", e => $"{e.Value} {e.Key}");

Expand Down Expand Up @@ -1524,7 +1524,7 @@ protected virtual void BuildExtendSearch()
protected virtual void BuildSearch()
{
// 收集索引信息,索引中的所有字段都参与,构造一个高级查询模板
var idx = Table.Indexes ?? new List<IDataIndex>();
var idx = Table.Indexes ?? [];
var cs = new List<IDataColumn>();
if (idx != null && idx.Count > 0)
{
Expand All @@ -1546,10 +1546,10 @@ protected virtual void BuildSearch()
{
// 时间字段。无差别支持UpdateTime/CreateTime
var dcTime = cs.FirstOrDefault(e => e.DataType == typeof(DateTime));
dcTime ??= Table.GetColumns(new[] { "UpdateTime", "CreateTime" })?.FirstOrDefault();
dcTime ??= Table.GetColumns(["UpdateTime", "CreateTime"])?.FirstOrDefault();
var dcSnow = cs.FirstOrDefault(e => e.PrimaryKey && !e.Identity && e.DataType == typeof(Int64));

cs.Remove(dcTime);
if (dcTime != null) cs.Remove(dcTime);
cs.RemoveAll(e => e.Name.EqualIgnoreCase("key", "page"));
if (dcSnow != null || dcTime != null)
cs.RemoveAll(e => e.Name.EqualIgnoreCase("start", "end"));
Expand All @@ -1563,11 +1563,16 @@ protected virtual void BuildSearch()
{
WriteLine("/// <param name=\"{0}\">{1}</param>", dc.CamelName(), dc.Description);
}
if (dcSnow != null || dcTime != null)
if (dcTime != null)
{
WriteLine("/// <param name=\"start\">{0}开始</param>", dcTime.DisplayName);
WriteLine("/// <param name=\"end\">{0}结束</param>", dcTime.DisplayName);
}
else if (dcSnow != null)
{
WriteLine("/// <param name=\"start\">{0}开始</param>", dcSnow.DisplayName);
WriteLine("/// <param name=\"end\">{0}结束</param>", dcSnow.DisplayName);
}
WriteLine("/// <param name=\"key\">关键字</param>");
WriteLine("/// <param name=\"page\">分页参数信息。可携带统计和数据权限扩展查询等信息</param>");
WriteLine("/// <returns>实体列表</returns>");
Expand Down
4 changes: 2 additions & 2 deletions XCode/DataAccessLayer/Database/SqlServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,8 +724,8 @@ private Int32 BatchExecute(String sql, List<IDataParameter[]> psList)
}
finally
{
//if (conn != null) Database.Pool.Put(conn);
EndTrace(OnCreateCommand(sql, CommandType.Text));
var cmd = OnCreateCommand(sql, CommandType.Text);
EndTrace(cmd);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions XCode/DataAccessLayer/Model/ModelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ public static List<IDataColumn> GetAllColumns(this IDataTable table, IEnumerable
/// <param name="table"></param>
/// <param name="columnNames"></param>
/// <returns></returns>
public static IDataIndex GetIndex(this IDataTable table, params String[] columnNames)
public static IDataIndex? GetIndex(this IDataTable table, params String[] columnNames)
{
var dis = table?.Indexes;
var dis = table.Indexes;
if (dis == null || dis.Count <= 0 || columnNames == null || columnNames.Length <= 0) return null;

//var di = dis.FirstOrDefault(e => e != null && e.Columns.EqualIgnoreCase(columnNames));
Expand Down
32 changes: 32 additions & 0 deletions XUnitTest.XCode/Model/City.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<EntityModel>
<Tables>
<Table Name="CorePerson" TableName="core_person" Description="居民信息" DbType="MySql">
<Columns>
<Column Name="PersonID" DataType="Int32" RawType="int(11)" Identity="True" PrimaryKey="True" Description="编号" />
<Column Name="Pname" DataType="String" RawType="varchar(50)" Description="姓名" />
<Column Name="Psex" DataType="Int32" RawType="int(11)" Nullable="True" Description="性别" />
<Column Name="CreditNo" DataType="String" RawType="char(50)" Description="身份证号" />
<Column Name="Mobile" DataType="String" RawType="char(50)" Description="联系电话" />
<Column Name="BuildID" DataType="Int32" RawType="int(11)" Nullable="True" Description="楼宇ID" />
<Column ColumnName="Build_ID" DataType="Int32" RawType="int(11)" Nullable="True" Description="平台楼号" />
<Column Name="UnitNum" DataType="String" RawType="varchar(20)" Length="20" Description="单元号" />
<Column Name="HouseNum" DataType="String" RawType="varchar(20)" Length="20" Description="房屋号" />
<Column Name="CreateUser" DataType="String" RawType="varchar(100)" Length="100" Description="创建者" />
<Column Name="CreateUserId" DataType="Int32" RawType="int(11)" Description="创建者ID" />
<Column Name="CreateTime" DataType="DateTime" Description="创建时间" />
<Column Name="CreateIP" DataType="String" RawType="varchar(50)" Description="创建IP" />
<Column Name="UpdateUser" DataType="String" RawType="varchar(100)" Length="100" Description="修改用户" />
<Column Name="UpdateUserId" DataType="Int32" RawType="int(11)" Description="修改用户ID" />
<Column Name="UpdateTime" DataType="DateTime" Description="修改时间" />
<Column Name="UpdateIP" DataType="String" RawType="varchar(50)" Description="修改IP" />
<Column Name="Remark" DataType="String" RawType="varchar(200)" Length="200" Description="备注" />
</Columns>
<Indexes>
<Index Name="CreditNoPName" Columns="Pname,CreditNo" Unique="True" />
<Index Name="Build_IDIndex" Columns="Build_ID" />
<Index Name="BuildIDIndex" Columns="BuildID" />
</Indexes>
</Table>
</Tables>
</EntityModel>
23 changes: 23 additions & 0 deletions XUnitTest.XCode/Model/ModelHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,27 @@ public void Import2023()
Assert.Equal("部门", dep.DisplayName);
Assert.Equal("部门。组织机构,多级树状结构", dep.Description);
}

[Fact]
public void ImportCity()
{
var file = "Model/City.xml";
var tables = DAL.ImportFrom(file);

Assert.NotNull(tables);
Assert.NotEmpty(tables);
Assert.Equal(1, tables.Count);

var option = new EntityBuilderOption();
var atts = new NullableDictionary<String, String>(StringComparer.OrdinalIgnoreCase);
var xml = File.ReadAllText(file.GetFullPath());

tables = ModelHelper.FromXml(xml, DAL.CreateTable, option, atts);
Assert.NotNull(tables);
Assert.NotEmpty(tables);
Assert.Equal(1, tables.Count);

var xml2 = DAL.Export(tables);
Assert.Equal(xml, xml2);
}
}
3 changes: 3 additions & 0 deletions XUnitTest.XCode/XUnitTest.XCode.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@
<None Update="Code\Member.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Model\City.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Model\Member2023.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

0 comments on commit 58357cc

Please sign in to comment.