From 61ac8721d29e18ae371cef1aad0c32944c05f6e5 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 15 Jan 2023 14:24:07 +0800 Subject: [PATCH] Add unit test --- Src/Asp.Net/PgSqlTest/PgSqlTest.csproj | 1 + Src/Asp.Net/PgSqlTest/UnitTest/Main.cs | 1 + Src/Asp.Net/PgSqlTest/UnitTest/ULock.cs | 67 +++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 Src/Asp.Net/PgSqlTest/UnitTest/ULock.cs diff --git a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj index 303b3bf3a..3d4e04b5a 100644 --- a/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj +++ b/Src/Asp.Net/PgSqlTest/PgSqlTest.csproj @@ -104,6 +104,7 @@ + diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs b/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs index 840d3cc37..b7fc3bb8a 100644 --- a/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs +++ b/Src/Asp.Net/PgSqlTest/UnitTest/Main.cs @@ -31,6 +31,7 @@ namespace OrmTest } public static void Init() { + ULock.Init(); UInsert3.Init(); UnitSubToList.Init(); UnitByteArray.Init(); diff --git a/Src/Asp.Net/PgSqlTest/UnitTest/ULock.cs b/Src/Asp.Net/PgSqlTest/UnitTest/ULock.cs new file mode 100644 index 000000000..c7bd2cf09 --- /dev/null +++ b/Src/Asp.Net/PgSqlTest/UnitTest/ULock.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class ULock + { + public static void Init() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + var dt= DateTime.Now; + var id=db.Insertable(new ULockEntity() + { + Name = "oldName", + Ver= dt + }).ExecuteReturnIdentity(); + + var data = db.Updateable(new ULockEntity() + { + Id = id, + Ver= DateTime.Now, + Name = "newname", + + }).ExecuteCommandWithOptLock(); + if (data != 1) { throw new Exception("unit error"); }; + var data2 = db.Updateable(new ULockEntity() + { + Id = id, + Name = "newname2", + + }).ExecuteCommandWithOptLock(); + if (data2 != 0) { throw new Exception("unit error"); }; + + var data3 = db.Updateable(new ULockEntity() + { + Id = id, + Name = "newname2", + + }) + .UpdateColumns(z=>z.Name).ExecuteCommandWithOptLock(); + if (data3 != 0) { throw new Exception("unit error"); } + var ver = db.Queryable().InSingle(id); + var data4 = db.Updateable(new ULockEntity() + { + Id = id, + Name = "newname2", + Ver = ver.Ver + }) + .UpdateColumns(z => z.Name).ExecuteCommandWithOptLock(); + if (data4 != 1) { throw new Exception("unit error"); } + } + + public class ULockEntity + { + [SqlSugar.SugarColumn(IsPrimaryKey =true,IsIdentity =true)] + public int Id { get; set; } + public string Name { get; set; } + [SqlSugar.SugarColumn(IsEnableUpdateVersionValidation = true, ColumnDataType ="timestamp" )] + public DateTime Ver { get; set; } + } + } +}