mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-07-17 01:46:30 +08:00
⚠️feat: 完善测试访问多数据库
This commit is contained in:
parent
695f5af89f
commit
d83a40e07e
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Autofac.Extensions.DependencyInjection;
|
using Autofac.Extensions.DependencyInjection;
|
||||||
@ -60,29 +61,52 @@ namespace OpenAuth.App.Test
|
|||||||
|
|
||||||
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
||||||
|
|
||||||
|
|
||||||
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
||||||
.ToDictionary(x => x.Key, x => x.Value);
|
.ToDictionary(x => x.Key, x => x.Value);;
|
||||||
|
|
||||||
var connectionString = config.GetSection("ConnectionStrings")["OpenAuthDBContext"];
|
|
||||||
|
|
||||||
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{connectionString}");
|
|
||||||
|
|
||||||
var sqlsugarTypes = UtilMethods.EnumToDictionary<SqlSugar.DbType>();
|
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 =>
|
serviceCollection.AddScoped<ISqlSugarClient>(s =>
|
||||||
{
|
{
|
||||||
var sqlSugar = new SqlSugarClient(new ConnectionConfig()
|
// 获取所有连接字符串配置
|
||||||
{
|
var connectionStrings = config.GetSection("ConnectionStrings").GetChildren()
|
||||||
DbType = dbType.Value,
|
.ToDictionary(x => x.Key, x => x.Value);
|
||||||
ConnectionString = connectionString,
|
|
||||||
IsAutoCloseConnection = true
|
|
||||||
});
|
|
||||||
|
|
||||||
if(dbType.Value != SqlSugar.DbType.PostgreSQL){
|
// 准备ConnectionConfig列表
|
||||||
return sqlSugar;
|
var connectionConfigs = new List<ConnectionConfig>();
|
||||||
|
|
||||||
|
// 遍历所有连接字符串
|
||||||
|
foreach (var conn in connectionStrings)
|
||||||
|
{
|
||||||
|
// 获取对应的数据库类型
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var sqlSugar = new SqlSugarClient(connectionConfigs);
|
||||||
|
|
||||||
|
// 配置PostgreSQL数据库处理
|
||||||
|
foreach (var connConfig in connectionConfigs)
|
||||||
|
{
|
||||||
|
if(connConfig.DbType == SqlSugar.DbType.PostgreSQL)
|
||||||
|
{
|
||||||
// 配置bool类型转换为smallint
|
// 配置bool类型转换为smallint
|
||||||
sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) =>
|
sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) =>
|
||||||
{
|
{
|
||||||
@ -98,6 +122,10 @@ namespace OpenAuth.App.Test
|
|||||||
// 返回修改后的 SQL 和参数
|
// 返回修改后的 SQL 和参数
|
||||||
return new System.Collections.Generic.KeyValuePair<string, SugarParameter[]>(sql, parameters);
|
return new System.Collections.Generic.KeyValuePair<string, SugarParameter[]>(sql, parameters);
|
||||||
};
|
};
|
||||||
|
break; // 找到一个PostgreSQL连接后就设置一次即可
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return sqlSugar;
|
return sqlSugar;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
@ -68,28 +69,50 @@ namespace OpenAuth.Repository.Test
|
|||||||
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
serviceCollection.AddDbContext<OpenAuthDBContext>();
|
||||||
|
|
||||||
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
var dbtypes = config.GetSection("AppSetting:DbTypes").GetChildren()
|
||||||
.ToDictionary(x => x.Key, x => x.Value);
|
.ToDictionary(x => x.Key, x => x.Value);;
|
||||||
|
|
||||||
var connectionString = config.GetSection("ConnectionStrings")["OpenAuthDBContext"];
|
|
||||||
Console.WriteLine($"单元测试数据库信息:{dbtypes[httpContextAccessorMock.Object.GetTenantId()]}/{connectionString}");
|
|
||||||
|
|
||||||
var sqlsugarTypes = UtilMethods.EnumToDictionary<SqlSugar.DbType>();
|
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 =>
|
serviceCollection.AddScoped<ISqlSugarClient>(s =>
|
||||||
{
|
{
|
||||||
var sqlSugar = new SqlSugarClient(new ConnectionConfig()
|
// 获取所有连接字符串配置
|
||||||
{
|
var connectionStrings = config.GetSection("ConnectionStrings").GetChildren()
|
||||||
DbType = dbType.Value,
|
.ToDictionary(x => x.Key, x => x.Value);
|
||||||
ConnectionString = connectionString,
|
|
||||||
IsAutoCloseConnection = true
|
|
||||||
});
|
|
||||||
|
|
||||||
if(dbType.Value != SqlSugar.DbType.PostgreSQL){
|
// 准备ConnectionConfig列表
|
||||||
return sqlSugar;
|
var connectionConfigs = new List<ConnectionConfig>();
|
||||||
|
|
||||||
|
// 遍历所有连接字符串
|
||||||
|
foreach (var conn in connectionStrings)
|
||||||
|
{
|
||||||
|
// 获取对应的数据库类型
|
||||||
|
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}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var sqlSugar = new SqlSugarClient(connectionConfigs);
|
||||||
|
|
||||||
|
// 配置PostgreSQL数据库处理
|
||||||
|
foreach (var connConfig in connectionConfigs)
|
||||||
|
{
|
||||||
|
if(connConfig.DbType == SqlSugar.DbType.PostgreSQL)
|
||||||
|
{
|
||||||
// 配置bool类型转换为smallint
|
// 配置bool类型转换为smallint
|
||||||
sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) =>
|
sqlSugar.Aop.OnExecutingChangeSql = (sql, parameters) =>
|
||||||
{
|
{
|
||||||
@ -105,6 +128,10 @@ namespace OpenAuth.Repository.Test
|
|||||||
// 返回修改后的 SQL 和参数
|
// 返回修改后的 SQL 和参数
|
||||||
return new System.Collections.Generic.KeyValuePair<string, SugarParameter[]>(sql, parameters);
|
return new System.Collections.Generic.KeyValuePair<string, SugarParameter[]>(sql, parameters);
|
||||||
};
|
};
|
||||||
|
break; // 找到一个PostgreSQL连接后就设置一次即可
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return sqlSugar;
|
return sqlSugar;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -578,8 +578,8 @@ COMMENT ON TABLE "EXTERNALDATASOURCE" IS '外部数据源管理';
|
|||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Records of 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', 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-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-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
|
-- Table structure for FLOWAPPROVER
|
||||||
|
@ -480,8 +480,8 @@ COMMENT ON TABLE "public"."externaldatasource" IS '外部数据源管理';
|
|||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Records of externaldatasource
|
-- 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-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-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
|
-- Table structure for flowapprover
|
||||||
|
@ -1522,10 +1522,10 @@ GO
|
|||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Records of ExternalDataSource
|
-- 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
|
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
|
GO
|
||||||
|
|
||||||
|
|
||||||
|
@ -373,8 +373,8 @@ CREATE TABLE `externaldatasource` (
|
|||||||
-- ----------------------------
|
-- ----------------------------
|
||||||
-- Records of 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-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-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
|
-- Table structure for flowapprover
|
||||||
|
Loading…
Reference in New Issue
Block a user