From c7260cd39dbf01d5be8bb23289144a3d256d360b Mon Sep 17 00:00:00 2001
From: sunkaixuan <610262374@qq.com>
Date: Sat, 22 Oct 2022 13:29:18 +0800
Subject: [PATCH] Add unit test
---
Src/Asp.Net/SqliteTest/SqliteTest.csproj | 1 +
Src/Asp.Net/SqliteTest/UnitTest/Main.cs | 1 +
.../SqliteTest/UnitTest/UnitInsertNavN.cs | 160 ++++++++++++++++++
3 files changed, 162 insertions(+)
create mode 100644 Src/Asp.Net/SqliteTest/UnitTest/UnitInsertNavN.cs
diff --git a/Src/Asp.Net/SqliteTest/SqliteTest.csproj b/Src/Asp.Net/SqliteTest/SqliteTest.csproj
index 15814d5ac..ade4e030c 100644
--- a/Src/Asp.Net/SqliteTest/SqliteTest.csproj
+++ b/Src/Asp.Net/SqliteTest/SqliteTest.csproj
@@ -77,6 +77,7 @@
+
diff --git a/Src/Asp.Net/SqliteTest/UnitTest/Main.cs b/Src/Asp.Net/SqliteTest/UnitTest/Main.cs
index 16d96fa09..f1ced768e 100644
--- a/Src/Asp.Net/SqliteTest/UnitTest/Main.cs
+++ b/Src/Asp.Net/SqliteTest/UnitTest/Main.cs
@@ -31,6 +31,7 @@ namespace OrmTest
}
public static void Init()
{
+ UnitInsertNavN.Init();
UNavTest.Init();
UnitTestReturnPkList.Init();
UCustom01.Init();
diff --git a/Src/Asp.Net/SqliteTest/UnitTest/UnitInsertNavN.cs b/Src/Asp.Net/SqliteTest/UnitTest/UnitInsertNavN.cs
new file mode 100644
index 000000000..af669058c
--- /dev/null
+++ b/Src/Asp.Net/SqliteTest/UnitTest/UnitInsertNavN.cs
@@ -0,0 +1,160 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using SqlSugar;
+namespace OrmTest
+{
+ public class UnitInsertNavN
+ {
+ public static void Init()
+ {
+ //导航更新问题
+ //关联层级大于或等于四层时 存在脏数据
+
+ Console.WriteLine("----------程序开始----------");
+
+ //创建数据库对象
+ SqlSugarClient db = NewUnitTest.Db;
+
+ db.Aop.OnLogExecuting = (sql, pars) =>
+ {
+ Console.WriteLine(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType,sql,pars));//输出sql,查看执行sql 性能无影响
+ };
+
+ //如果不存在创建数据库存在不会重复创建
+ db.DbMaintenance.CreateDatabase();
+ //根据实体名创建对应表
+ db.CodeFirst.InitTables(typeof(ClassA), typeof(ClassB), typeof(ClassC), typeof(ClassD));
+ db.DbMaintenance.TruncateTable(typeof(ClassA), typeof(ClassB), typeof(ClassC), typeof(ClassD));
+ //清空表中数据
+ db.Deleteable().ExecuteCommand();
+ db.Deleteable().ExecuteCommand();
+ db.Deleteable().ExecuteCommand();
+ db.Deleteable().ExecuteCommand();
+
+ //初始化数据 共四层 都是一对多的关系表
+ ClassA a = new ClassA();
+ a.AId = "a";
+
+ ClassB b = new ClassB();
+ b.BId = "b";
+ b.AId = a.AId;
+ a.B = new List();
+ a.B.Add(b);
+
+ ClassC c = new ClassC();
+ c.CId = "c";
+ c.BId = b.BId;
+ b.C = new List();
+ b.C.Add(c);
+
+ ClassD d = new ClassD();
+ d.DId = "d";
+ d.CId = c.CId;
+ c.D = new List();
+ c.D.Add(d);
+
+ //导航插入四表数据
+ db.InsertNav(a)
+ .Include(t => t.B)
+ .ThenInclude(t => t.C)
+ .ThenInclude(t => t.D)
+ .ExecuteCommand();
+
+ //修改数据 修改b表id 修改c表对应b表id 修改d表对应c表id
+ b.BId = "bb";
+ c.BId = "bb";
+ c.CId = "cc";
+ d.CId = "cc";
+ d.DId = "dd";
+
+ //导航更新四表数据
+ db.UpdateNav(a)
+ .Include(t => t.B, new UpdateNavOptions { OneToManyDeleteAll = true }) //这里声明了删除脏数据 但只清理到了下一层 第四层还是存在脏数据
+ .ThenInclude(t => t.C)
+ .ThenInclude(t => t.D)
+ .ExecuteCommand();
+
+ //正常情况D表数据库中应该只有1条 如果是多条则说明四级表数据存在垃圾数据
+ var c_count = db.Queryable().Count();
+ var d_count = db.Queryable().Count();
+
+ Console.WriteLine($"第三级表中总数据条数:{c_count}");
+ Console.WriteLine($"第四级表中总数据条数:{d_count}");
+
+ if (c_count > 1)
+ {
+ throw new Exception("unit error");
+ }
+ else
+ {
+ Console.WriteLine($"三级表中不存在脏数据!");
+ }
+
+ if (d_count > 1)
+ {
+ throw new Exception("unit error");
+ }
+ else
+ {
+ Console.WriteLine($"四级表中不存在脏数据!");
+ }
+
+ Console.WriteLine("----------程序结束----------");
+
+ }
+
+ ///
+ /// 一级类
+ ///
+ [SugarTable("tb_a")]
+ public class ClassA
+ {
+ [SugarColumn(IsPrimaryKey = true)]
+ public string AId { get; set; }
+ [Navigate(NavigateType.OneToMany, nameof(ClassB.AId))]
+ public List B { get; set; }
+ [SugarColumn(IsNullable = true)]
+ public DateTime? UpdateTime { get; set; }
+ }
+ ///
+ /// 二级类
+ ///
+ [SugarTable("tb_b")]
+ public class ClassB
+ {
+ [SugarColumn(IsPrimaryKey = true)]
+ public string BId { get; set; }
+ public string AId { get; set; }
+ [Navigate(NavigateType.OneToMany, nameof(ClassC.BId))]
+ public List C { get; set; }
+ }
+ ///
+ /// 三级类
+ ///
+ [SugarTable("tb_c")]
+ public class ClassC
+ {
+ [SugarColumn(IsPrimaryKey = true)]
+ public string CId { get; set; }
+
+ public string BId { get; set; }
+
+ [Navigate(NavigateType.OneToMany, nameof(ClassD.DId))]
+ public List D { get; set; }
+ }
+ ///
+ /// 四级类
+ ///
+ [SugarTable("tb_d")]
+ public class ClassD
+ {
+ [SugarColumn(IsPrimaryKey = true)]
+ public string DId { get; set; }
+
+ public string CId { get; set; }
+ }
+ }
+}
\ No newline at end of file