Add unit test

This commit is contained in:
sunkaixuan 2022-12-11 16:17:17 +08:00
parent b1420cf503
commit 3e59500c8f
3 changed files with 150 additions and 0 deletions

View File

@ -203,6 +203,7 @@
<Compile Include="UnitTest\UQueryable2.cs" />
<Compile Include="UnitTest\UQueue.cs" />
<Compile Include="UnitTest\UTran.cs" />
<Compile Include="UnitTest\UTran3.cs" />
<Compile Include="Models\TestTree.cs" />
<Compile Include="Config.cs" />
<Compile Include="Program.cs" />

View File

@ -119,6 +119,7 @@ namespace OrmTest
Queryable2();
QueryableAsync();
AopTest();
UTran3.Init();
//Thread();
//Thread2();
//Thread3();

View File

@ -0,0 +1,148 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public class UTran3
{
public static void Init()
{
AdoTran();
Tran();
RollTest().GetAwaiter().GetResult();
Tran2();
}
private static void AdoTran()
{
var db = NewUnitTest.Db;
db.CodeFirst.InitTables<UTran121>();
db.DbMaintenance.TruncateTable<UTran121>();
db.Insertable(new UTran121()).ExecuteCommand();
for (int i = 0; i < 1000; i++)
{
AdoTest(i).GetAwaiter().GetResult();
}
var id = db.Queryable<UTran121>().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<UTran121>()
.SetColumns(it => it.ID == it.ID+1)
.Where(it=>true)
.ExecuteCommandAsync();
await db.Updateable<UTran121>()
.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<UTran121>();
db.DbMaintenance.TruncateTable<UTran121>();
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<UTran121>();
db.DbMaintenance.TruncateTable<UTran121>();
db.Insertable(new UTran121()).ExecuteCommand();
for (int i = 0; i < 1000; i++)
{
Test(i).GetAwaiter().GetResult();
}
var id = db.Queryable<UTran121>().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<UTran121>()
.SetColumns(it => it.ID == it.ID + 1)
.Where(it => true)
.ExecuteCommandAsync();
await db.Updateable<UTran121>()
.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<UTran121>();
db.DbMaintenance.TruncateTable<UTran121>();
db.Insertable(new UTran121() { ID=100}).ExecuteCommand();
try
{
Console.WriteLine( db.Queryable<UTran121>().First().ID);
await db.BeginTranAsync();
await db.Updateable<UTran121>()
.SetColumns(it => it.ID == it.ID + 1)
.Where(it => true)
.ExecuteCommandAsync();
Console.WriteLine(db.Queryable<UTran121>().First().ID);
throw new Exception("");
await db.CommitTranAsync();
}
catch (Exception ex)
{
await db.RollbackTranAsync();
if (db.Queryable<UTran121>().First().ID != 100)
{
throw new Exception("unit error");
}
}
Console.WriteLine("ok" + i);
}
public class UTran121
{
public int ID { get; set; }
}
}
}