2020-12-17 23:04:04 +08:00
|
|
|
|
using System;
|
2021-04-17 17:06:48 +08:00
|
|
|
|
using System.Linq;
|
2020-12-17 23:04:04 +08:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using OpenAuth.Repository.Domain;
|
|
|
|
|
using OpenAuth.Repository.Interface;
|
|
|
|
|
|
|
|
|
|
namespace OpenAuth.Repository.Test
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试事务
|
|
|
|
|
/// </summary>
|
|
|
|
|
class TestTransaction : TestBase
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试事务正常提交
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void NormalSubmit()
|
|
|
|
|
{
|
2020-12-29 23:52:06 +08:00
|
|
|
|
var unitWork = _autofacServiceProvider.GetService<IUnitWork<OpenAuthDBContext>>();
|
2020-12-17 23:04:04 +08:00
|
|
|
|
unitWork.ExecuteWithTransaction(() =>
|
|
|
|
|
{
|
|
|
|
|
var account = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
AddAndUpdate(account, unitWork);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试事务回滚
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void SubmitWithRollback()
|
|
|
|
|
{
|
2020-12-29 23:52:06 +08:00
|
|
|
|
var unitWork = _autofacServiceProvider.GetService<IUnitWork<OpenAuthDBContext>>();
|
2020-12-17 23:04:04 +08:00
|
|
|
|
var account = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
unitWork.ExecuteWithTransaction(() =>
|
|
|
|
|
{
|
|
|
|
|
AddAndUpdate(account, unitWork);
|
|
|
|
|
|
|
|
|
|
throw new Exception("模拟异常");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//如果没有插入成功,表示事务发生了回滚
|
2025-02-19 12:44:42 +08:00
|
|
|
|
Assert.IsFalse(unitWork.Any<SysUser>( u=>u.Id == account));
|
2020-12-17 23:04:04 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 测试添加,单个修改,Z.EntityFramework.Plus条件修改
|
|
|
|
|
/// </summary>
|
2020-12-29 23:52:06 +08:00
|
|
|
|
private void AddAndUpdate(string account, IUnitWork<OpenAuthDBContext> unitWork)
|
2020-12-17 23:04:04 +08:00
|
|
|
|
{
|
2025-02-19 12:44:42 +08:00
|
|
|
|
var user = new SysUser
|
2020-12-17 23:04:04 +08:00
|
|
|
|
{
|
|
|
|
|
Id = account,
|
|
|
|
|
Account = account,
|
|
|
|
|
Name = account,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
unitWork.Add(user);
|
|
|
|
|
|
|
|
|
|
unitWork.Save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user.Account = "Trans_" + user.Account;
|
|
|
|
|
user.Name = "Trans_" + user.Name;
|
|
|
|
|
unitWork.Update(user);
|
|
|
|
|
|
|
|
|
|
unitWork.Save();
|
|
|
|
|
|
2025-02-19 12:44:42 +08:00
|
|
|
|
unitWork.Update<SysUser>(u => u.Id == account, u => new SysUser
|
2020-12-17 23:04:04 +08:00
|
|
|
|
{
|
|
|
|
|
Account = "Trans2_" + user.Account
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-02-20 22:06:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void MultiUpdate()
|
|
|
|
|
{
|
|
|
|
|
var unitWork = _autofacServiceProvider.GetService<IUnitWork<OpenAuthDBContext>>();
|
|
|
|
|
|
2025-02-19 12:44:42 +08:00
|
|
|
|
var users = unitWork.Find<SysUser>(u => u.Account.Contains("test"));
|
2021-02-20 22:06:02 +08:00
|
|
|
|
foreach (var user in users)
|
|
|
|
|
{
|
|
|
|
|
user.Name = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss");
|
|
|
|
|
unitWork.Update(user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unitWork.Save();
|
|
|
|
|
|
|
|
|
|
}
|
2021-04-17 17:06:48 +08:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void MultiUpdate2()
|
|
|
|
|
{
|
|
|
|
|
var unitWork = _autofacServiceProvider.GetService<IUnitWork<OpenAuthDBContext>>();
|
2025-02-19 12:44:42 +08:00
|
|
|
|
var users = unitWork.Find<SysUser>(null).ToList();
|
2021-04-17 17:06:48 +08:00
|
|
|
|
unitWork.ExecuteWithTransaction(()=>
|
|
|
|
|
{
|
|
|
|
|
foreach (var req in users)
|
|
|
|
|
{
|
2025-02-19 12:44:42 +08:00
|
|
|
|
unitWork.Update<SysUser>(u =>u.Id == req.Id, user => new SysUser
|
2021-04-17 17:06:48 +08:00
|
|
|
|
{
|
|
|
|
|
Name = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss")
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2022-01-21 17:31:34 +08:00
|
|
|
|
unitWork.Update<SysOrg>(u => u.Id == "08f41bf6-4388-4b1e-bd3e-2ff538b44b1b", u => new SysOrg
|
2021-04-17 17:06:48 +08:00
|
|
|
|
{
|
|
|
|
|
CreateTime = DateTime.Now
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unitWork.Save();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
2020-12-17 23:04:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|