From 83a63a32d2640aff676890d97571a9b732301350 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Mon, 14 Aug 2023 22:19:28 +0800 Subject: [PATCH] Add unit test --- .../SqlSeverTest/UnitTest/Main.cs | 1 + .../UnitTest/UnitOneToManyafdaa.cs | 331 ++++++++++++++++++ 2 files changed, 332 insertions(+) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitOneToManyafdaa.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs index f9200b4ca..66d21fbac 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + UnitOneToManyafdaa.Init(); Unitadfafa.Init(); AnimalTest.Init(); UnitOneToOneNAny.Init(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitOneToManyafdaa.cs b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitOneToManyafdaa.cs new file mode 100644 index 000000000..a5336d8a1 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UnitTest/UnitOneToManyafdaa.cs @@ -0,0 +1,331 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace OrmTest +{ + internal class UnitOneToManyafdaa + { + public static void Init() + { + Get().GetAwaiter().GetResult(); + } + + public static async Task Get() + { + var db = NewUnitTest.Db; + + + //建表 + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + db.CodeFirst.InitTables(); + + #region 数据库插入 + + var result = await db.InsertNav(new Test001() + { + Name = "A", + Other = 5, + TenantId = 1, + Sub001 = new List + { + new TestSubClass001 + { + Name = "张三", + TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 1, + TenantId = 1, + } + } + },new TestSubClass001 + { + Name = "sadsa",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 1, + TenantId = 1, + } + } + },new TestSubClass001 + { + Name = "123",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 1, + TenantId = 1, + } + } + },new TestSubClass001 + { + Name = "kukhg",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 1, + TenantId = 1, + } + } + } + }, + Sub002 = new List + { + new TestSubClass002 + { + Name = "张三",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 2, + TenantId = 1, + } + } + },new TestSubClass002 + { + Name = "sadsa",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 2, + TenantId = 1, + } + } + },new TestSubClass002 + { + Name = "123",TenantId = 1,Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 2, + TenantId = 1, + } + } + },new TestSubClass002 + { + Name = "kukhg",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 2, + TenantId = 1, + } + } + },new TestSubClass002 + { + Name = "fswrqqw",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 2, + TenantId = 1, + } + } + },new TestSubClass002 + { + Name = "kiukiyu",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 2, + TenantId = 1, + } + } + } + }, + Sub003 = new List + { + new TestSubClass003 + { + Name = "张三",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 3, + TenantId = 1, + } + } + },new TestSubClass003 + { + Name = "sadsa",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 3, + TenantId = 1, + } + } + },new TestSubClass003 + { + Name = "123",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 3, + TenantId = 1, + } + } + },new TestSubClass003 + { + Name = "kukhg",TenantId = 1, + Commons = new List + { + new Common + { + Desctiption = "描述", + Type = 3, + TenantId = 1, + } + } + } + } + }) + .Include(i => i.Sub001) + .Include(i => i.Sub002) + .Include(i => i.Sub003) + .ExecuteCommandAsync(); + + #endregion + + db.QueryFilter.AddTableFilter(x => x.TenantId == 1); + db.QueryFilter.AddTableFilter(x => x.TenantId == 1); + db.QueryFilter.AddTableFilter(x => x.TenantId == 1); + db.QueryFilter.AddTableFilter(x => x.TenantId == 1); + db.QueryFilter.AddTableFilter(x => x.TenantId == 1); + + //用例代码 + try + { + var riskList = db.Queryable() + .Select(t => new + { + Name = t.Name, + Other = t.Other, + Sub001Count = t.Sub001.Count(), + Sub002Count = t.Sub002.Count(), + Sub003Count = t.Sub003.Count(), + }) + .ToList(); + } + catch (Exception e) + { + + throw; + } + } + [SugarTable("unittestfadfa")] + public class Test001 : ITenantIdFilter + { + [SugarColumn(IsPrimaryKey = true)] + public long Id { get; set; } + + public string Name { get; set; } + + public int? Other { get; set; } + + [Navigate(NavigateType.OneToMany, nameof(TestSubClass001.PId))] + public List Sub001 { get; set; } + + [Navigate(NavigateType.OneToMany, nameof(TestSubClass002.PId))] + public List Sub002 { get; set; } + + [Navigate(NavigateType.OneToMany, nameof(TestSubClass003.PId))] + public List Sub003 { get; set; } + public long TenantId { get; set; } + } + + public class TestSubClass001 : ITenantIdFilter + { + [SugarColumn(IsPrimaryKey = true)] + public long Id { get; set; } + + public long PId { get; set; } + + public string Name { get; set; } + public long TenantId { get; set; } + + [Navigate(NavigateType.OneToMany, nameof(Common.SubId))] + public List Commons { get; set; } + } + + public class TestSubClass002 : ITenantIdFilter + { + [SugarColumn(IsPrimaryKey = true)] + public long Id { get; set; } + + public long PId { get; set; } + + public string Name { get; set; } + public long TenantId { get; set; } + + [Navigate(NavigateType.OneToMany, nameof(Common.SubId))] + public List Commons { get; set; } + } + + public class TestSubClass003 : ITenantIdFilter + { + [SugarColumn(IsPrimaryKey = true)] + public long Id { get; set; } + + public long PId { get; set; } + + public string Name { get; set; } + public long TenantId { get; set; } + + [Navigate(NavigateType.OneToMany, nameof(Common.SubId))] + public List Commons { get; set; } + } + + public class Common : ITenantIdFilter + { + public long Id { get; set; } + + public string Desctiption { get; set; } + + public long SubId { get; set; } + + public int Type { get; set; } + public long TenantId { get; set; } + } + + public interface ITenantIdFilter + { + public long TenantId { get; set; } + } + } +}