From 8c3a09c4c7a534b215ef11d7ffb162b4920f219e Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 2 Aug 2022 22:10:00 +0800 Subject: [PATCH] Add unit test --- .../SqlServerTest/SqlServerTest.csproj | 1 + Src/Asp.Net/SqlServerTest/UnitTest/Main.cs | 2 + .../SqlServerTest/UnitTest/UnitInsertNav.cs | 123 ++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 Src/Asp.Net/SqlServerTest/UnitTest/UnitInsertNav.cs diff --git a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj index bf5cda9dc..e7d96f390 100644 --- a/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj +++ b/Src/Asp.Net/SqlServerTest/SqlServerTest.csproj @@ -97,6 +97,7 @@ + diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs index 04c4c0cae..26b731f79 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/Main.cs @@ -31,6 +31,8 @@ namespace OrmTest } public static void Init() { + UnitInsertNav.Init(); + UnitInsertNav.Init(); UnitSelectN.Init(); UnitOneToOneN2.Init(); UnitManyToManyUpdate.Init(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/UnitInsertNav.cs b/Src/Asp.Net/SqlServerTest/UnitTest/UnitInsertNav.cs new file mode 100644 index 000000000..1b20c725a --- /dev/null +++ b/Src/Asp.Net/SqlServerTest/UnitTest/UnitInsertNav.cs @@ -0,0 +1,123 @@ +using SqlSugar; +using System.Linq.Expressions; +using System.Text; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace OrmTest +{ + public class UnitInsertNav + { + + public static void Init() + { + //生成 DI 容器 + + var context = NewUnitTest.Db; ; + context.Aop.DataExecuting = (oldValue, entityInfo) => + { + if (entityInfo.PropertyName == "Iden" && entityInfo.OperationType == DataFilterType.InsertByObject) + { + entityInfo.SetValue(Guid.NewGuid().ToString("N").ToUpper()); + } + }; + + //建表 + if (!context.DbMaintenance.IsAnyTable("MENU_INFO_1", false)) + { + context.CodeFirst.InitTables(); + } + //建表 + if (!context.DbMaintenance.IsAnyTable("ACTIVITY_RULE_1", false)) + { + context.CodeFirst.InitTables(); + } + context.DbMaintenance.TruncateTable(); + var result = context.Insertable(new ActivityRule() + { + MenuIden = "1", + RuleContent = "1" + }).ExecuteReturnEntity (); + + MenuInfo menuInfo = new MenuInfo + { + MenuCode = "2", + ActivityRules = new List { new ActivityRule() { Iden = result.Iden, RuleContent = "2" } } + }; + Console.WriteLine("begin insert nav"); + context.InsertNav(menuInfo) + .Include(s => s.ActivityRules) + .ExecuteCommand (); + + var list = context.Queryable() + .Includes(x => x.ActivityRules) + .ToList(); + + if (list.First().ActivityRules.Count() == 0) + { + throw new Exception("unit error"); + } + + } + + + /// + /// + /// + [SugarTable("MENU_INFO_1")] + public partial class MenuInfo + { + public MenuInfo() + { + + } + /// + /// Desc:唯一编号 + /// Default: + /// Nullable:False + /// + [SugarColumn(IsPrimaryKey = true, ColumnName = "IDEN")] + public string Iden { get; set; } + + /// + /// Desc:菜单编号 + /// Default: + /// Nullable:True + /// + [SugarColumn(ColumnName = "MENU_CODE")] + public string MenuCode { get; set; } + + [SugarColumn(IsIgnore = true)] + [Navigate(NavigateType.OneToMany, nameof(ActivityRule.MenuIden))] + public List ActivityRules { get; set; } + + } + /// + ///组织信息 + /// + [SugarTable("ACTIVITY_RULE_1")] + public partial class ActivityRule + { + public ActivityRule() + { + } + /// + /// uuid + /// + [SugarColumn(IsPrimaryKey = true, ColumnName = "IDEN")] + public string Iden { get; set; } + + /// + /// 菜单IDEN + /// + [SugarColumn(ColumnName = "MENU_IDEN")] + public string MenuIden { get; set; } + /// + /// uuid + /// + [SugarColumn(ColumnName = "RUlE_CONTENT")] + public string RuleContent { get; set; } + } + } +}