From 298e178cf385f6b099e8f6e00d51039fdad5fb6d Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Sun, 11 Dec 2022 14:59:56 +0800 Subject: [PATCH] Add unit test --- Src/Asp.NetCore2/MySqlTest/UnitTest/UTran.cs | 149 +++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 Src/Asp.NetCore2/MySqlTest/UnitTest/UTran.cs diff --git a/Src/Asp.NetCore2/MySqlTest/UnitTest/UTran.cs b/Src/Asp.NetCore2/MySqlTest/UnitTest/UTran.cs new file mode 100644 index 000000000..d792969e5 --- /dev/null +++ b/Src/Asp.NetCore2/MySqlTest/UnitTest/UTran.cs @@ -0,0 +1,149 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class UTran + { + public static void Init() + { + AdoTran(); + Tran(); + RollTest(); + Tran2(); + Console.ReadKey(); + } + + private static void AdoTran() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new UTran121()).ExecuteCommand(); + for (int i = 0; i < 1000; i++) + { + AdoTest(i).GetAwaiter().GetResult(); + } + var id = db.Queryable().ToList().First().ID; + if (id != 2000) + { + throw new Exception("unit error"); + } + + } + + public static async Task AdoTest(int i) + { + var db = NewUnitTest.Db; + await db.Ado.BeginTranAsync(); + + await db.Updateable() + .SetColumns(it => it.ID == it.ID+1) + .Where(it=>true) + .ExecuteCommandAsync(); + + await db.Updateable() + .SetColumns(it => it.ID == it.ID + 1) + .Where(it => true) + .ExecuteCommandAsync(); + + await db.Ado.CommitTranAsync(); + Console.WriteLine("ok"+i); + } + + + private static void Tran2() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new UTran121()).ExecuteCommand(); + for (int i = 0; i < 1000; i++) + { + Test(i); + } + + } + + + private static void Tran() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new UTran121()).ExecuteCommand(); + for (int i = 0; i < 1000; i++) + { + Test(i).GetAwaiter().GetResult(); + } + var id = db.Queryable().ToList().First().ID; + if (id != 2000) + { + throw new Exception("unit error"); + } + } + + public static async Task Test(int i) + { + var db = NewUnitTest.Db; + await db.BeginTranAsync(); + + await db.Updateable() + .SetColumns(it => it.ID == it.ID + 1) + .Where(it => true) + .ExecuteCommandAsync(); + + await db.Updateable() + .SetColumns(it => it.ID == it.ID + 1) + .Where(it => true) + .ExecuteCommandAsync(); + + await db.CommitTranAsync(); + Console.WriteLine("ok" + i); + } + + public static async Task RollTest() + { + int i = 0; + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(); + db.DbMaintenance.TruncateTable(); + db.Insertable(new UTran121() { ID=100}).ExecuteCommand(); + + try + { + Console.WriteLine( db.Queryable().First().ID); + + await db.BeginTranAsync(); + + await db.Updateable() + .SetColumns(it => it.ID == it.ID + 1) + .Where(it => true) + .ExecuteCommandAsync(); + + Console.WriteLine(db.Queryable().First().ID); + + throw new Exception(""); + + await db.CommitTranAsync(); + } + catch (Exception ex) + { + await db.RollbackTranAsync(); + if (db.Queryable().First().ID != 100) + { + throw new Exception("unit error"); + } + } + Console.WriteLine("ok" + i); + } + public class UTran121 + { + public int ID { get; set; } + } + } +}