OpenAuth.Net/OpenAuth.Repository/Test/TestTransaction.cs

130 lines
3.7 KiB
C#
Raw Permalink Normal View History

2020-12-17 23:04:04 +08:00
using System;
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()
{
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()
{
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);
}
//如果没有插入成功,表示事务发生了回滚
Assert.IsFalse(unitWork.Any<SysUser>( u=>u.Id == account));
2020-12-17 23:04:04 +08:00
}
/// <summary>
/// 测试添加单个修改Z.EntityFramework.Plus条件修改
/// </summary>
private void AddAndUpdate(string account, IUnitWork<OpenAuthDBContext> unitWork)
2020-12-17 23:04:04 +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();
unitWork.Update<SysUser>(u => u.Id == account, u => new SysUser
2020-12-17 23:04:04 +08:00
{
Account = "Trans2_" + user.Account
});
}
[Test]
public void MultiUpdate()
{
var unitWork = _autofacServiceProvider.GetService<IUnitWork<OpenAuthDBContext>>();
var users = unitWork.Find<SysUser>(u => u.Account.Contains("test"));
foreach (var user in users)
{
user.Name = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss");
unitWork.Update(user);
}
unitWork.Save();
}
[Test]
public void MultiUpdate2()
{
var unitWork = _autofacServiceProvider.GetService<IUnitWork<OpenAuthDBContext>>();
var users = unitWork.Find<SysUser>(null).ToList();
unitWork.ExecuteWithTransaction(()=>
{
foreach (var req in users)
{
unitWork.Update<SysUser>(u =>u.Id == req.Id, user => new SysUser
{
Name = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss")
});
unitWork.Update<SysOrg>(u => u.Id == "08f41bf6-4388-4b1e-bd3e-2ff538b44b1b", u => new SysOrg
{
CreateTime = DateTime.Now
});
}
unitWork.Save();
});
}
2020-12-17 23:04:04 +08:00
}
}