mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-11-08 02:14:44 +08:00
增加SqlSugar启动
This commit is contained in:
@@ -15,6 +15,7 @@ using Microsoft.Extensions.Options;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using OpenAuth.Repository.Interface;
|
||||
using SqlSugar;
|
||||
|
||||
namespace OpenAuth.Repository.Test
|
||||
{
|
||||
@@ -65,6 +66,58 @@ namespace OpenAuth.Repository.Test
|
||||
serviceCollection.AddScoped(x => httpContextAccessorMock.Object);
|
||||
|
||||
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
||||
|
||||
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
|
||||
var connectionString = config.GetSection("ConnectionStrings")["OpenAuthDBContext"];
|
||||
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{connectionString}");
|
||||
|
||||
serviceCollection.AddScoped<ISqlSugarClient>(s =>
|
||||
{
|
||||
|
||||
SqlSugarClient sqlSugar;
|
||||
if(dbtypes.ContainsValue(Define.DBTYPE_SQLSERVER))
|
||||
{
|
||||
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||
{
|
||||
DbType = SqlSugar.DbType.SqlServer,
|
||||
ConnectionString = connectionString,
|
||||
IsAutoCloseConnection = true,
|
||||
});
|
||||
}
|
||||
else if(dbtypes.ContainsValue(Define.DBTYPE_MYSQL)) //mysql
|
||||
{
|
||||
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||
{
|
||||
DbType = SqlSugar.DbType.MySql,
|
||||
ConnectionString = connectionString,
|
||||
IsAutoCloseConnection = true,
|
||||
});
|
||||
}
|
||||
else if(dbtypes.ContainsValue(Define.DBTYPE_PostgreSQL)) //PostgreSQL
|
||||
{
|
||||
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||
{
|
||||
DbType = SqlSugar.DbType.PostgreSQL,
|
||||
ConnectionString = connectionString,
|
||||
IsAutoCloseConnection = true,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
sqlSugar = new SqlSugarClient (new ConnectionConfig()
|
||||
{
|
||||
DbType = SqlSugar.DbType.Oracle,
|
||||
ConnectionString = connectionString,
|
||||
IsAutoCloseConnection = true,
|
||||
});
|
||||
}
|
||||
|
||||
return sqlSugar;
|
||||
});
|
||||
|
||||
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
@@ -75,12 +128,6 @@ namespace OpenAuth.Repository.Test
|
||||
|
||||
var _container = builder.Build();
|
||||
_autofacServiceProvider = new AutofacServiceProvider(_container);
|
||||
|
||||
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
|
||||
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{config.GetSection("ConnectionStrings")["OpenAuthDBContext"]}");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
93
OpenAuth.Repository/Test/TestSugarDynamicLinq.cs
Normal file
93
OpenAuth.Repository/Test/TestSugarDynamicLinq.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 TestSugarDynamicLinq : 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