From 746a24b88e707e192e75b51aaea7793b131b1c36 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 28 Sep 2023 14:14:16 +0800 Subject: [PATCH] Add unit test --- .../UnitTest/Models/Unit02/IDeleted.cs | 33 ++++++++++++ .../UnitTest/Models/Unit02/Special.cs | 51 +++++++++++++++++++ .../SqliteTest/UnitTest/UnitBizDelete.cs | 37 ++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 Src/Asp.NetCore2/SqliteTest/UnitTest/Models/Unit02/IDeleted.cs create mode 100644 Src/Asp.NetCore2/SqliteTest/UnitTest/Models/Unit02/Special.cs create mode 100644 Src/Asp.NetCore2/SqliteTest/UnitTest/UnitBizDelete.cs diff --git a/Src/Asp.NetCore2/SqliteTest/UnitTest/Models/Unit02/IDeleted.cs b/Src/Asp.NetCore2/SqliteTest/UnitTest/Models/Unit02/IDeleted.cs new file mode 100644 index 000000000..776d6828b --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/UnitTest/Models/Unit02/IDeleted.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugarTest +{ + + public interface IDeleted + { + /// + /// 默认假删除 + /// + //[FakeDelete(true)] // 设置假删除的值 + bool IsDeleted { get; set; } + + /// + /// 删除用户ID + /// + long? DeletedUserId { get; set; } + + /// + /// 删除用户名称 + /// + string DeletedUserName { get; set; } + + /// + /// 删除时间 + /// + DateTimeOffset? DeletedTime { get; set; } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/UnitTest/Models/Unit02/Special.cs b/Src/Asp.NetCore2/SqliteTest/UnitTest/Models/Unit02/Special.cs new file mode 100644 index 000000000..2e8d1d940 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/UnitTest/Models/Unit02/Special.cs @@ -0,0 +1,51 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugarTest +{ + + [SugarTable("Special", TableDescription = "专题", IsDisabledDelete = true)] + public class Special : IDeleted + { + /// + /// 非自增ID + /// + [SugarColumn(ColumnDescription = "ID", IsPrimaryKey = true)] + public long Id { get; set; } + /// + /// 名称 + /// + [SugarColumn(ColumnDescription = "名称")] + public string Name { get; set; } + + /// + /// 默认假删除 + /// + [System.Text.Json.Serialization.JsonIgnore] + [Newtonsoft.Json.JsonIgnore] + [SugarColumn(ColumnDescription = "默认假删除", DefaultValue = "false")] + public bool IsDeleted { get; set; } + + /// + /// 删除用户ID + /// + [SugarColumn(ColumnDescription = "删除用户ID", IsOnlyIgnoreInsert = true, IsNullable = true)] + public long? DeletedUserId { get; set; } + + /// + /// 删除用户 + /// + [SugarColumn(ColumnDescription = "删除用户", IsNullable = true, IsOnlyIgnoreInsert = true)] + public string? DeletedUserName { get; set; } + + /// + /// 删除时间 + /// + [SugarColumn(ColumnDescription = "删除时间", IsOnlyIgnoreInsert = true, IsNullable = true)] + public DateTimeOffset? DeletedTime { get; set; } + } +} \ No newline at end of file diff --git a/Src/Asp.NetCore2/SqliteTest/UnitTest/UnitBizDelete.cs b/Src/Asp.NetCore2/SqliteTest/UnitTest/UnitBizDelete.cs new file mode 100644 index 000000000..4bd969367 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/UnitTest/UnitBizDelete.cs @@ -0,0 +1,37 @@ +using SqlSugar; +using SqlSugarTest; +using System; +using System.Collections.Generic; +using System.Text; + +namespace OrmTest +{ + internal class UnitBizDelete + { + public static void Init() + { + var _db = NewUnitTest.Db; + _db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings + { + + IsAutoDeleteQueryFilter = true,//启用删除查询过滤器 + IsAutoUpdateQueryFilter = true//启用更新查询过滤器 (表达式更新,如果是实体方式更新建议先查询在更新) + }; + _db.CodeFirst.InitTables(typeof(Special));//创建表 + var entityType = typeof(Special); + + _db.QueryFilter.AddTableFilter(it => it.IsDeleted == false); + //新增数据 + var special = new Special(); + special.Id = SnowFlakeSingle.Instance.NextId(); + special.Name = "测试的哦"; + _db.Insertable(special).ExecuteCommand(); + + //假删除数据 + _db.Deleteable().In(special.Id) + .IsLogic() + .ExecuteCommand("IsDeleted", true, "DeletedTime", "DeletedUserName", "admin"); + + } + } +}