Skip to content

Commit 6b86fc4

Browse files
committed
- 增加DynamicEntity - Attribute单元测试
1 parent 52ce785 commit 6b86fc4

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

FreeSql.Tests/FreeSql.Tests/DynamicEntity/DynamicEntityTest.cs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,62 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using FreeSql.DataAnnotations;
7+
using FreeSql.Extensions.DynamicEntity;
8+
using Xunit;
69

710
namespace FreeSql.Tests.DynamicEntity
811
{
9-
internal class DynamicEntityTest
12+
public class DynamicEntityTest
1013
{
14+
private static IFreeSql fsql = new FreeSqlBuilder().UseConnectionString(DataType.PostgreSQL,
15+
"Host=192.168.0.36;Port=5432;Username=postgres;Password=123; Database=test;ArrayNullabilityMode=Always;Pooling=true;Minimum Pool Size=1")
16+
.UseMonitorCommand(d => Console.WriteLine(d.CommandText)).Build();
17+
18+
19+
[Fact]
20+
public void NormalTest()
21+
{
22+
Type type = DynamicCompileHelper.DynamicBuilder()
23+
.Class("NormalUsers")
24+
.Property("Id", typeof(int))
25+
.Property("Name", typeof(string))
26+
.Property("Address", typeof(string))
27+
.Build();
28+
var dict = new Dictionary<string, object>
29+
{
30+
["Name"] = "张三",
31+
["Id"] = 1,
32+
["Address"] = "北京市"
33+
};
34+
var instance = DynamicCompileHelper.CreateObjectByType(type, dict);
35+
//根据Type生成表
36+
fsql.CodeFirst.SyncStructure(type);
37+
fsql.Insert<object>().AsType(type).AppendData(instance).ExecuteAffrows();
38+
}
39+
40+
[Fact]
41+
public void AttributeTest()
42+
{
43+
Type type = DynamicCompileHelper.DynamicBuilder()
44+
.Class("AttributeUsers", new TableAttribute() { Name = "T_Attribute_User" },
45+
new IndexAttribute("Name_Index", "Name", false))
46+
.Property("Id", typeof(int),
47+
new ColumnAttribute() { IsPrimary = true, IsIdentity = true, Position = 1 })
48+
.Property("Name", typeof(string),
49+
new ColumnAttribute() { StringLength = 20, Position = 2 })
50+
.Property("Address", typeof(string),
51+
new ColumnAttribute() { StringLength = 150, Position = 3 })
52+
.Build();
53+
var dict = new Dictionary<string, object>
54+
{
55+
["Name"] = "张三",
56+
["Address"] = "北京市"
57+
};
58+
var instance = DynamicCompileHelper.CreateObjectByType(type, dict);
59+
//根据Type生成表
60+
fsql.CodeFirst.SyncStructure(type);
61+
fsql.Insert<object>().AsType(type).AppendData(instance).ExecuteAffrows();
62+
}
1163
}
12-
}
64+
}

0 commit comments

Comments
 (0)