mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-19 18:22:11 +08:00
转移.net core 3.1,为.NET 5做准备
This commit is contained in:
48
OpenAuth.Repository/Test/TestBase.cs
Normal file
48
OpenAuth.Repository/Test/TestBase.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System.Reflection;
|
||||
using Autofac;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NUnit.Framework;
|
||||
using OpenAuth.Repository.Interface;
|
||||
|
||||
namespace OpenAuth.Repository.Test
|
||||
{
|
||||
public class TestBase
|
||||
{
|
||||
protected AutofacServiceProvider _autofacServiceProvider;
|
||||
|
||||
[SetUp]
|
||||
public void Init()
|
||||
{
|
||||
var serviceCollection = GetService();
|
||||
serviceCollection.AddMemoryCache();
|
||||
serviceCollection.AddOptions();
|
||||
serviceCollection.AddScoped(typeof(IRepository<>), typeof(BaseRepository<>));
|
||||
|
||||
serviceCollection.AddDbContext<OpenAuthDBContext>(options =>
|
||||
options.UseSqlServer("Data Source=.;Initial Catalog=OpenAuthDB;User=sa;Password=000000;Integrated Security=True"));
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
//注册repository层
|
||||
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
|
||||
|
||||
// builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>)).PropertiesAutowired();
|
||||
builder.Populate(serviceCollection);
|
||||
|
||||
var _container = builder.Build();
|
||||
_autofacServiceProvider = new AutofacServiceProvider(_container);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试框架默认只注入了缓存Cache,配置Option;
|
||||
/// 如果在测试的过程中需要模拟登录用户,cookie等信息,需要重写该方法,可以参考TestFlow的写法
|
||||
/// </summary>
|
||||
public virtual ServiceCollection GetService()
|
||||
{
|
||||
return new ServiceCollection();
|
||||
}
|
||||
}
|
||||
}
|
118
OpenAuth.Repository/Test/TestDbContext.cs
Normal file
118
OpenAuth.Repository/Test/TestDbContext.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NUnit.Framework;
|
||||
using OpenAuth.Repository.Domain;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using OpenAuth.Repository.Interface;
|
||||
|
||||
namespace OpenAuth.Repository.Test
|
||||
{
|
||||
class TestDbContext :TestBase
|
||||
{
|
||||
[Test]
|
||||
public void TestAddUser()
|
||||
{
|
||||
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
|
||||
int count = dbcontext.Users.Count();
|
||||
Console.WriteLine($"添加前用户数:{count}");
|
||||
var account = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss");
|
||||
Console.WriteLine(account);
|
||||
|
||||
dbcontext.Users.Add(new User
|
||||
{
|
||||
Account = account,
|
||||
Name = account,
|
||||
Password = "000000"
|
||||
,Id=Guid.NewGuid().ToString()
|
||||
});
|
||||
dbcontext.SaveChanges();
|
||||
Assert.True(dbcontext.Users.Any(u => u.Account == account));
|
||||
|
||||
int aftercount = dbcontext.Users.Count();
|
||||
Assert.AreEqual(aftercount, count + 1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestLoadMenu()
|
||||
{
|
||||
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
|
||||
Stopwatch watch = Stopwatch.StartNew();
|
||||
var modules = from module in dbcontext.Modules
|
||||
select new
|
||||
{
|
||||
Name = module.Name,
|
||||
Elements = dbcontext.ModuleElements.Where(u => u.ModuleId == module.Id)
|
||||
};
|
||||
|
||||
Console.WriteLine(JsonHelper.Instance.Serialize(modules));
|
||||
watch.Stop();
|
||||
Console.WriteLine($"总耗时:{watch.ElapsedMilliseconds}");
|
||||
}
|
||||
[Test]
|
||||
public void TestLoadMenu2()
|
||||
{
|
||||
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
|
||||
Stopwatch watch = Stopwatch.StartNew();
|
||||
var modules = from module in dbcontext.Set<Module>().AsNoTracking().AsQueryable()
|
||||
select new
|
||||
{
|
||||
Name = module.Name,
|
||||
Id = module.Id,
|
||||
IconName = module.IconName,
|
||||
Url = module.Url,
|
||||
ParentId = module.ParentId,
|
||||
ParentName = module.ParentName,
|
||||
Elements = dbcontext.Set<ModuleElement>().AsNoTracking().AsQueryable().Where(u => u.ModuleId == module.Id)
|
||||
};
|
||||
|
||||
Console.WriteLine(JsonHelper.Instance.Serialize(modules));
|
||||
watch.Stop();
|
||||
Console.WriteLine($"总耗时:{watch.ElapsedMilliseconds}");
|
||||
}
|
||||
|
||||
|
||||
//测试独立的repository
|
||||
[Test]
|
||||
public void TestBaseRepository()
|
||||
{
|
||||
var account = "user_" + DateTime.Now.ToString("yyyy_MM_dd HH:mm:ss");
|
||||
var id = account;
|
||||
|
||||
Console.WriteLine(account);
|
||||
|
||||
var repository = _autofacServiceProvider.GetService<IRepository<User>>();
|
||||
|
||||
//新增
|
||||
repository.Add(new User
|
||||
{
|
||||
Account = account,
|
||||
Name = account,
|
||||
Password = "000000",
|
||||
Id = id
|
||||
});
|
||||
|
||||
var user = repository.FindSingle(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);
|
||||
Assert.NotNull(newuser);
|
||||
|
||||
//批量修改
|
||||
repository.Update(u => u.Id == id, u =>new User{ Name = account});
|
||||
newuser = repository.FindSingle(u => u.Name == account);
|
||||
Assert.NotNull(newuser);
|
||||
|
||||
//删除
|
||||
repository.Delete(u =>u.Id == id);
|
||||
newuser = repository.FindSingle(u => u.Id == id);
|
||||
Assert.IsNull(newuser);
|
||||
}
|
||||
}
|
||||
}
|
93
OpenAuth.Repository/Test/TestDynamicLinq.cs
Normal file
93
OpenAuth.Repository/Test/TestDynamicLinq.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Infrastructure;
|
||||
using NUnit.Framework;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace OpenAuth.Repository.Test
|
||||
{
|
||||
class TestDynamicLinq : TestBase
|
||||
{
|
||||
[Test]
|
||||
public void GenerateFilter()
|
||||
{
|
||||
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
|
||||
var json = @"
|
||||
{
|
||||
""Operation"": ""and"",
|
||||
""Filters"": [{
|
||||
""Key"": ""Name"",
|
||||
""Contrast"": ""=="",
|
||||
""Value"": ""admin""
|
||||
|
||||
},
|
||||
{
|
||||
""Key"": ""Name"",
|
||||
""Contrast"": ""=="",
|
||||
""Value"": ""admin""
|
||||
}
|
||||
],
|
||||
""Children"": [{
|
||||
""Operation"": ""or"",
|
||||
""Filters"": [{
|
||||
""Key"": ""Name"",
|
||||
""Contrast"": ""=="",
|
||||
""Value"": ""admin""
|
||||
},
|
||||
{
|
||||
""Key"": ""Name"",
|
||||
""Contrast"": ""=="",
|
||||
""Value"": ""admin""
|
||||
}
|
||||
],
|
||||
""Children"": [
|
||||
|
||||
|
||||
]
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
";
|
||||
|
||||
var query = dbcontext.Users.GenerateFilter("c",json);
|
||||
Console.WriteLine(query.Expression.ToString());
|
||||
|
||||
Console.WriteLine(JsonHelper.Instance.Serialize(query.ToList()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDynamic()
|
||||
{
|
||||
FilterGroup sub = new FilterGroup
|
||||
{
|
||||
Operation = "or"
|
||||
};
|
||||
sub.Filters = new[]
|
||||
{
|
||||
new Filter {Key = "Name", Value = "name", Contrast = "=="},
|
||||
new Filter {Key = "Sex", Value = "10", Contrast = "=="}
|
||||
};
|
||||
|
||||
FilterGroup filterGroup = new FilterGroup
|
||||
{
|
||||
Operation = "and"
|
||||
};
|
||||
filterGroup.Filters = new[]
|
||||
{
|
||||
new Filter {Key = "Account", Value = "name", Contrast = "=="},
|
||||
new Filter {Key = "Password", Value = "10", Contrast = "=="}
|
||||
};
|
||||
|
||||
filterGroup.Children = new[]
|
||||
{
|
||||
sub
|
||||
};
|
||||
|
||||
var dbcontext = _autofacServiceProvider.GetService<OpenAuthDBContext>();
|
||||
|
||||
var query = dbcontext.Users.GenerateFilter("c",JsonHelper.Instance.Serialize(filterGroup));
|
||||
Console.WriteLine(query.Expression.ToString());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user