mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-15 23:13:40 +08:00
⚠️feat: 完善测试访问多数据库
This commit is contained in:
parent
695f5af89f
commit
d83a40e07e
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
@ -60,44 +61,71 @@ namespace OpenAuth.App.Test
|
||||
|
||||
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}");
|
||||
|
||||
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);;
|
||||
var sqlsugarTypes = UtilMethods.EnumToDictionary<SqlSugar.DbType>();
|
||||
var dbType = sqlsugarTypes.FirstOrDefault(it =>
|
||||
dbtypes.ToDictionary(u => u.Key, v => v.Value.ToLower()).ContainsValue(it.Key));
|
||||
|
||||
serviceCollection.AddScoped<ISqlSugarClient>(s =>
|
||||
{
|
||||
var sqlSugar = new SqlSugarClient(new ConnectionConfig()
|
||||
// 获取所有连接字符串配置
|
||||
var connectionStrings = config.GetSection("ConnectionStrings").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
|
||||
// 准备ConnectionConfig列表
|
||||
var connectionConfigs = new List<ConnectionConfig>();
|
||||
|
||||
// 遍历所有连接字符串
|
||||
foreach (var conn in connectionStrings)
|
||||
{
|
||||
DbType = dbType.Value,
|
||||
ConnectionString = connectionString,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
|
||||
if(dbType.Value != SqlSugar.DbType.PostgreSQL){
|
||||
return sqlSugar;
|
||||
}
|
||||
// 配置bool类型转换为smallint
|
||||
sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) =>
|
||||
{
|
||||
foreach (var param in parameters)
|
||||
// 获取对应的数据库类型
|
||||
var connDbType = dbtypes.ContainsKey(conn.Key) ?
|
||||
sqlsugarTypes.FirstOrDefault(it => dbtypes[conn.Key].ToLower().Contains(it.Key)).Value :
|
||||
DbType.SqlServer; // 如果没有定义DbType,使用默认类型
|
||||
|
||||
// 创建连接配置
|
||||
var config = new ConnectionConfig
|
||||
{
|
||||
if (param.Value is bool boolValue)
|
||||
{
|
||||
param.DbType = System.Data.DbType.Int16;
|
||||
// 将 bool 转换为 smallint
|
||||
param.Value = boolValue ? (short)1 : (short)0;
|
||||
}
|
||||
DbType = connDbType,
|
||||
ConnectionString = conn.Value,
|
||||
IsAutoCloseConnection = true,
|
||||
};
|
||||
|
||||
// 如果不是默认连接,设置ConfigId
|
||||
if (conn.Key != Define.DEFAULT_TENANT_ID)
|
||||
{
|
||||
config.ConfigId = conn.Key;
|
||||
}
|
||||
// 返回修改后的 SQL 和参数
|
||||
return new System.Collections.Generic.KeyValuePair<string, SugarParameter[]>(sql, parameters);
|
||||
};
|
||||
|
||||
connectionConfigs.Add(config);
|
||||
Console.WriteLine($"添加数据库连接: {conn.Key} / {(dbtypes.ContainsKey(conn.Key) ? dbtypes[conn.Key] : "未指定类型")},连接字符串:{conn.Value}");
|
||||
}
|
||||
|
||||
var sqlSugar = new SqlSugarClient(connectionConfigs);
|
||||
|
||||
// 配置PostgreSQL数据库处理
|
||||
foreach (var connConfig in connectionConfigs)
|
||||
{
|
||||
if(connConfig.DbType == SqlSugar.DbType.PostgreSQL)
|
||||
{
|
||||
// 配置bool类型转换为smallint
|
||||
sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) =>
|
||||
{
|
||||
foreach (var param in parameters)
|
||||
{
|
||||
if (param.Value is bool boolValue)
|
||||
{
|
||||
param.DbType = System.Data.DbType.Int16;
|
||||
// 将 bool 转换为 smallint
|
||||
param.Value = boolValue ? (short)1 : (short)0;
|
||||
}
|
||||
}
|
||||
// 返回修改后的 SQL 和参数
|
||||
return new System.Collections.Generic.KeyValuePair<string, SugarParameter[]>(sql, parameters);
|
||||
};
|
||||
break; // 找到一个PostgreSQL连接后就设置一次即可
|
||||
}
|
||||
}
|
||||
|
||||
return sqlSugar;
|
||||
});
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@ -67,44 +68,70 @@ namespace OpenAuth.Repository.Test
|
||||
|
||||
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}");
|
||||
|
||||
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);;
|
||||
var sqlsugarTypes = UtilMethods.EnumToDictionary<SqlSugar.DbType>();
|
||||
var dbType = sqlsugarTypes.FirstOrDefault(it =>
|
||||
dbtypes.ToDictionary(u => u.Key, v => v.Value.ToLower()).ContainsValue(it.Key));
|
||||
|
||||
serviceCollection.AddScoped<ISqlSugarClient>(s =>
|
||||
{
|
||||
var sqlSugar = new SqlSugarClient(new ConnectionConfig()
|
||||
// 获取所有连接字符串配置
|
||||
var connectionStrings = config.GetSection("ConnectionStrings").GetChildren()
|
||||
.ToDictionary(x => x.Key, x => x.Value);
|
||||
|
||||
// 准备ConnectionConfig列表
|
||||
var connectionConfigs = new List<ConnectionConfig>();
|
||||
|
||||
// 遍历所有连接字符串
|
||||
foreach (var conn in connectionStrings)
|
||||
{
|
||||
DbType = dbType.Value,
|
||||
ConnectionString = connectionString,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
|
||||
if(dbType.Value != SqlSugar.DbType.PostgreSQL){
|
||||
return sqlSugar;
|
||||
// 获取对应的数据库类型
|
||||
var connDbType = dbtypes.ContainsKey(conn.Key) ?
|
||||
sqlsugarTypes.FirstOrDefault(it => dbtypes[conn.Key].ToLower().Contains(it.Key)).Value :
|
||||
DbType.SqlServer; // 如果没有定义DbType,使用默认类型
|
||||
|
||||
// 创建连接配置
|
||||
var config = new ConnectionConfig
|
||||
{
|
||||
DbType = connDbType,
|
||||
ConnectionString = conn.Value,
|
||||
IsAutoCloseConnection = true,
|
||||
};
|
||||
|
||||
// 如果不是默认连接,设置ConfigId
|
||||
if (conn.Key != Define.DEFAULT_TENANT_ID)
|
||||
{
|
||||
config.ConfigId = conn.Key;
|
||||
}
|
||||
|
||||
connectionConfigs.Add(config);
|
||||
Console.WriteLine($"添加数据库连接: {conn.Key} / {(dbtypes.ContainsKey(conn.Key) ? dbtypes[conn.Key] : "未指定类型")},连接字符串:{conn.Value}");
|
||||
}
|
||||
|
||||
// 配置bool类型转换为smallint
|
||||
sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) =>
|
||||
var sqlSugar = new SqlSugarClient(connectionConfigs);
|
||||
|
||||
// 配置PostgreSQL数据库处理
|
||||
foreach (var connConfig in connectionConfigs)
|
||||
{
|
||||
foreach (var param in parameters)
|
||||
if(connConfig.DbType == SqlSugar.DbType.PostgreSQL)
|
||||
{
|
||||
if (param.Value is bool boolValue)
|
||||
// 配置bool类型转换为smallint
|
||||
sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) =>
|
||||
{
|
||||
param.DbType = System.Data.DbType.Int16;
|
||||
// 将 bool 转换为 smallint
|
||||
param.Value = boolValue ? (short)1 : (short)0;
|
||||
}
|
||||
foreach (var param in parameters)
|
||||
{
|
||||
if (param.Value is bool boolValue)
|
||||
{
|
||||
param.DbType = System.Data.DbType.Int16;
|
||||
// 将 bool 转换为 smallint
|
||||
param.Value = boolValue ? (short)1 : (short)0;
|
||||
}
|
||||
}
|
||||
// 返回修改后的 SQL 和参数
|
||||
return new System.Collections.Generic.KeyValuePair<string, SugarParameter[]>(sql, parameters);
|
||||
};
|
||||
break; // 找到一个PostgreSQL连接后就设置一次即可
|
||||
}
|
||||
// 返回修改后的 SQL 和参数
|
||||
return new System.Collections.Generic.KeyValuePair<string, SugarParameter[]>(sql, parameters);
|
||||
};
|
||||
}
|
||||
|
||||
return sqlSugar;
|
||||
});
|
||||
|
||||
|
@ -578,8 +578,8 @@ COMMENT ON TABLE "EXTERNALDATASOURCE" IS '外部数据源管理';
|
||||
-- ----------------------------
|
||||
-- Records of EXTERNALDATASOURCE
|
||||
-- ----------------------------
|
||||
INSERT INTO "EXTERNALDATASOURCE" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '本地SqlServer', '1', 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', '1', TO_DATE('2025-03-15 20:09:35', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2025-03-18 11:42:08', 'SYYYY-MM-DD HH24:MI:SS'), '0', NULL, '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
INSERT INTO "EXTERNALDATASOURCE" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '本地mysql', '0', 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', '3306', 'openauthpro', 'root', '000000', '1', TO_DATE('2025-03-15 00:02:11', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2025-03-15 20:12:30', 'SYYYY-MM-DD HH24:MI:SS'), '0', NULL, '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
INSERT INTO "EXTERNALDATASOURCE" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '非默认的SqlServer', '1', 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', '1', TO_DATE('2025-03-15 20:09:35', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2025-03-18 11:42:08', 'SYYYY-MM-DD HH24:MI:SS'), '0', NULL, '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
INSERT INTO "EXTERNALDATASOURCE" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '非默认的mysql', '0', 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', '3306', 'openauthpro', 'root', '000000', '1', TO_DATE('2025-03-15 00:02:11', 'SYYYY-MM-DD HH24:MI:SS'), TO_DATE('2025-03-15 20:12:30', 'SYYYY-MM-DD HH24:MI:SS'), '0', NULL, '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for FLOWAPPROVER
|
||||
|
@ -480,8 +480,8 @@ COMMENT ON TABLE "public"."externaldatasource" IS '外部数据源管理';
|
||||
-- ----------------------------
|
||||
-- Records of externaldatasource
|
||||
-- ----------------------------
|
||||
INSERT INTO "public"."externaldatasource" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '本地SqlServer', 1, 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', 1, '2025-03-15 20:09:35', '2025-03-18 11:42:08', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
INSERT INTO "public"."externaldatasource" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '本地mysql', 0, 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', 3306, 'openauthpro', 'root', '000000', 1, '2025-03-15 00:02:11', '2025-03-15 20:12:30', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
INSERT INTO "public"."externaldatasource" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '非默认的SqlServer', 1, 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', 1, '2025-03-15 20:09:35', '2025-03-18 11:42:08', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
INSERT INTO "public"."externaldatasource" VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '非默认的mysql', 0, 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', 3306, 'openauthpro', 'root', '000000', 1, '2025-03-15 00:02:11', '2025-03-15 20:12:30', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for flowapprover
|
||||
|
@ -1522,10 +1522,10 @@ GO
|
||||
-- ----------------------------
|
||||
-- Records of ExternalDataSource
|
||||
-- ----------------------------
|
||||
INSERT INTO [dbo].[ExternalDataSource] ([Id], [Name], [DbType], [ConnectionString], [Server], [Port], [DatabaseName], [UserName], [Password], [Enabled], [CreateTime], [UpdateTime], [TestSuccess], [Description], [CreateUserId], [CreateUserName], [UpdateUserId], [UpdateUserName]) VALUES (N'94279d93-cef3-46e0-83af-3b7dc48764fe', N'本地SqlServer', N'1', N'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', N'localhost', NULL, N'openauthpro', N'sa', N'000000', N'1', N'2025-03-15', N'2025-03-18', N'0', N'', N'00000000-0000-0000-0000-000000000000', N'超级管理员', N'00000000-0000-0000-0000-000000000000', N'超级管理员')
|
||||
INSERT INTO [dbo].[ExternalDataSource] ([Id], [Name], [DbType], [ConnectionString], [Server], [Port], [DatabaseName], [UserName], [Password], [Enabled], [CreateTime], [UpdateTime], [TestSuccess], [Description], [CreateUserId], [CreateUserName], [UpdateUserId], [UpdateUserName]) VALUES (N'94279d93-cef3-46e0-83af-3b7dc48764fe', N'非默认的SqlServer', N'1', N'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', N'localhost', NULL, N'openauthpro', N'sa', N'000000', N'1', N'2025-03-15', N'2025-03-18', N'0', N'', N'00000000-0000-0000-0000-000000000000', N'超级管理员', N'00000000-0000-0000-0000-000000000000', N'超级管理员')
|
||||
GO
|
||||
|
||||
INSERT INTO [dbo].[ExternalDataSource] ([Id], [Name], [DbType], [ConnectionString], [Server], [Port], [DatabaseName], [UserName], [Password], [Enabled], [CreateTime], [UpdateTime], [TestSuccess], [Description], [CreateUserId], [CreateUserName], [UpdateUserId], [UpdateUserName]) VALUES (N'94279d93-cef3-46e0-83af-3b7dc48764ff', N'本地mysql', N'0', N'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', N'127.0.0.1', N'3306', N'openauthpro', N'root', N'000000', N'1', N'2025-03-15', N'2025-03-15', N'0', N'', N'00000000-0000-0000-0000-000000000000', N'超级管理员', N'00000000-0000-0000-0000-000000000000', N'超级管理员')
|
||||
INSERT INTO [dbo].[ExternalDataSource] ([Id], [Name], [DbType], [ConnectionString], [Server], [Port], [DatabaseName], [UserName], [Password], [Enabled], [CreateTime], [UpdateTime], [TestSuccess], [Description], [CreateUserId], [CreateUserName], [UpdateUserId], [UpdateUserName]) VALUES (N'94279d93-cef3-46e0-83af-3b7dc48764ff', N'非默认的mysql', N'0', N'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', N'127.0.0.1', N'3306', N'openauthpro', N'root', N'000000', N'1', N'2025-03-15', N'2025-03-15', N'0', N'', N'00000000-0000-0000-0000-000000000000', N'超级管理员', N'00000000-0000-0000-0000-000000000000', N'超级管理员')
|
||||
GO
|
||||
|
||||
|
||||
|
@ -373,8 +373,8 @@ CREATE TABLE `externaldatasource` (
|
||||
-- ----------------------------
|
||||
-- Records of externaldatasource
|
||||
-- ----------------------------
|
||||
INSERT INTO `externaldatasource` VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '本地SqlServer', 1, 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', 1, '2025-03-15 20:09:35', '2025-03-18 11:42:08', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
INSERT INTO `externaldatasource` VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '本地mysql', 0, 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', 3306, 'openauthpro', 'root', '000000', 1, '2025-03-15 00:02:11', '2025-03-15 20:12:30', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
INSERT INTO `externaldatasource` VALUES ('94279d93-cef3-46e0-83af-3b7dc48764fe', '非默认的SqlServer', 1, 'Data Source=localhost;Encrypt=false;Initial Catalog=openauthpro;User=sa;Password=000000;', 'localhost', NULL, 'openauthpro', 'sa', '000000', 1, '2025-03-15 20:09:35', '2025-03-18 11:42:08', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
INSERT INTO `externaldatasource` VALUES ('94279d93-cef3-46e0-83af-3b7dc48764ff', '非默认的mysql', 0, 'Server=127.0.0.1;Database=openauthpro;User=root;Password=000000;', '127.0.0.1', 3306, 'openauthpro', 'root', '000000', 1, '2025-03-15 00:02:11', '2025-03-15 20:12:30', 0, '', '00000000-0000-0000-0000-000000000000', '超级管理员', '00000000-0000-0000-0000-000000000000', '超级管理员');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for flowapprover
|
||||
|
Loading…
Reference in New Issue
Block a user