mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-19 10:08:04 +08:00
同步OpenAuth.Core最新代码
This commit is contained in:
@@ -8,6 +8,10 @@ using OpenAuth.Repository.Interface;
|
||||
|
||||
namespace OpenAuth.Repository.Test
|
||||
{
|
||||
/// <summary>
|
||||
/// Repository测试基类
|
||||
/// 测试用于测试DbContext、UnitWork、Repository,如果需要测试业务逻辑,请使用OpenAuth.App里面的单元测试
|
||||
/// </summary>
|
||||
public class TestBase
|
||||
{
|
||||
protected AutofacServiceProvider _autofacServiceProvider;
|
||||
@@ -19,6 +23,7 @@ namespace OpenAuth.Repository.Test
|
||||
serviceCollection.AddMemoryCache();
|
||||
serviceCollection.AddOptions();
|
||||
serviceCollection.AddScoped(typeof(IRepository<>), typeof(BaseRepository<>));
|
||||
serviceCollection.AddScoped(typeof(IUnitWork), typeof(UnitWork));
|
||||
|
||||
serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
||||
options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));
|
||||
@@ -28,7 +33,6 @@ namespace OpenAuth.Repository.Test
|
||||
//注册repository层
|
||||
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
|
||||
|
||||
// builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>)).PropertiesAutowired();
|
||||
builder.Populate(serviceCollection);
|
||||
|
||||
var _container = builder.Build();
|
||||
|
@@ -94,24 +94,24 @@ namespace OpenAuth.Repository.Test
|
||||
Id = id
|
||||
});
|
||||
|
||||
var user = repository.FindSingle(u => u.Id == id);
|
||||
var user = repository.FirstOrDefault(u => u.Id == id);
|
||||
Assert.NotNull(user);
|
||||
|
||||
//修改一个实体
|
||||
account = "newuser_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss");
|
||||
user.Account = account;
|
||||
repository.Update(user);
|
||||
var newuser = repository.FindSingle(u => u.Account == account);
|
||||
var newuser = repository.FirstOrDefault(u => u.Account == account);
|
||||
Assert.NotNull(newuser);
|
||||
|
||||
//批量修改
|
||||
repository.Update(u => u.Id == id, u =>new User{ Name = account});
|
||||
newuser = repository.FindSingle(u => u.Name == account);
|
||||
newuser = repository.FirstOrDefault(u => u.Name == account);
|
||||
Assert.NotNull(newuser);
|
||||
|
||||
//删除
|
||||
repository.Delete(u =>u.Id == id);
|
||||
newuser = repository.FindSingle(u => u.Id == id);
|
||||
newuser = repository.FirstOrDefault(u => u.Id == id);
|
||||
Assert.IsNull(newuser);
|
||||
}
|
||||
}
|
||||
|
86
OpenAuth.Repository/Test/TestTransaction.cs
Normal file
86
OpenAuth.Repository/Test/TestTransaction.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
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>();
|
||||
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>();
|
||||
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<User>( u=>u.Id == account));
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试添加,单个修改,Z.EntityFramework.Plus条件修改
|
||||
/// </summary>
|
||||
private void AddAndUpdate(string account, IUnitWork unitWork)
|
||||
{
|
||||
var user = new User
|
||||
{
|
||||
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<User>(u => u.Id == account, u => new User
|
||||
{
|
||||
Account = "Trans2_" + user.Account
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user