From 40a81b7b50e0e5b7b5a5ad0ac9d4923e5753ba87 Mon Sep 17 00:00:00 2001 From: Argo-Surface Date: Sat, 19 Jan 2019 15:58:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=EF=BC=9A?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=E5=B0=86=E5=A4=9A=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=94=AF=E6=8C=81=E6=95=B4=E5=90=88=E5=88=B0?= =?UTF-8?q?TestHelper=E6=96=87=E4=BB=B6=E4=B8=AD=EF=BC=8C=E6=9B=B4?= =?UTF-8?q?=E6=94=B9=E6=AD=A4=E6=96=87=E4=BB=B6=E7=9A=84=E5=AE=8F=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=8D=B3=E5=8F=AF=E6=9B=B4=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=20#SQLite/MySQL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- UnitTest/Bootstrap.Admin/BAWebHost.cs | 5 +- .../BootstrapAdminStartup.cs | 42 +--------- UnitTest/TestHelper.cs | 77 ++++++++++++++++++- 3 files changed, 80 insertions(+), 44 deletions(-) diff --git a/UnitTest/Bootstrap.Admin/BAWebHost.cs b/UnitTest/Bootstrap.Admin/BAWebHost.cs index 6341e634..4c17fcf5 100644 --- a/UnitTest/Bootstrap.Admin/BAWebHost.cs +++ b/UnitTest/Bootstrap.Admin/BAWebHost.cs @@ -44,10 +44,7 @@ namespace Bootstrap.Admin { base.ConfigureWebHost(builder); - var sqlConnectionStrings = "Data Source=.;Initial Catalog=UnitTest;User ID=sa;Password=sa"; - builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair[] { - new KeyValuePair("ConnectionStrings:ba", sqlConnectionStrings) - })); + TestHelper.ConfigureWebHost(builder); } public string Login(string userName = "Admin", string password = "123789") diff --git a/UnitTest/Bootstrap.DataAccess/BootstrapAdminStartup.cs b/UnitTest/Bootstrap.DataAccess/BootstrapAdminStartup.cs index 37f2f676..d37e351a 100644 --- a/UnitTest/Bootstrap.DataAccess/BootstrapAdminStartup.cs +++ b/UnitTest/Bootstrap.DataAccess/BootstrapAdminStartup.cs @@ -1,15 +1,5 @@ -//#define SQLite -//#define MySQL - -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using System.Collections.Generic; - -#if SQLite -using System; -using System.IO; +using Microsoft.Extensions.DependencyInjection; using UnitTest; -#endif namespace Bootstrap.DataAccess { @@ -17,39 +7,13 @@ namespace Bootstrap.DataAccess { static BootstrapAdminStartup() { - var sqlConnectionStrings = "Data Source=.;Initial Catalog=UnitTest;User ID=sa;Password=sa"; - var mysqlConnectionStrings = "Server=.;Database=UnitTest;Uid=argozhang;Pwd=argo@163.com;SslMode=none;"; - var config = new ConfigurationBuilder().AddInMemoryCollection(new KeyValuePair[] { - new KeyValuePair("ConnectionStrings:ba", sqlConnectionStrings), - new KeyValuePair("DB:0:Enabled", "false"), - - new KeyValuePair("DB:1:Enabled", "false"), - new KeyValuePair("DB:1:ProviderName", "SQLite"), - new KeyValuePair("DB:1:ConnectionStrings:ba", "Data Source=UnitTest.db"), - - new KeyValuePair("DB:2:Enabled", "false"), - new KeyValuePair("DB:2:ProviderName", "MySql"), - new KeyValuePair("DB:2:ConnectionStrings:ba", mysqlConnectionStrings), - new KeyValuePair("LongbowCache:Enabled", "false") - }).Build(); + var config = TestHelper.CreateConfiguraton(); var sc = new ServiceCollection(); - sc.AddSingleton(config); + sc.AddSingleton(config); sc.AddConfigurationManager(config); sc.AddCacheManager(config); sc.AddDbAdapter(); - -#if SQLite - config["DB:1:Enabled"] = "true"; - - // Copy File - var dbPath = TestHelper.RetrievePath($"UnitTest{Path.DirectorySeparatorChar}DB{Path.DirectorySeparatorChar}UnitTest.db"); - var dbFile = Path.Combine(AppContext.BaseDirectory, "UnitTest.db"); - if (!File.Exists(dbFile)) File.Copy(dbPath, dbFile); - -#elif MySQL - config["DB:2:Enabled"]= "true"; -#endif } } } diff --git a/UnitTest/TestHelper.cs b/UnitTest/TestHelper.cs index 18edf5fe..867f9b38 100644 --- a/UnitTest/TestHelper.cs +++ b/UnitTest/TestHelper.cs @@ -1,4 +1,9 @@ -using System; +//#define SQLite +//#define MySQL +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; using System.IO; namespace UnitTest @@ -36,6 +41,76 @@ namespace UnitTest var targetFile = Path.Combine(AppContext.BaseDirectory, "Longbow.lic"); if (!File.Exists(targetFile)) File.Copy(licFile, targetFile, true); + +#if SQLite + CopySQLiteDBFile(); +#endif + } + + /// + /// + /// + public static void CopySQLiteDBFile() + { + var dbPath = RetrievePath($"UnitTest{Path.DirectorySeparatorChar}DB{Path.DirectorySeparatorChar}UnitTest.db"); + var dbFile = Path.Combine(AppContext.BaseDirectory, "UnitTest.db"); + if (!File.Exists(dbFile)) File.Copy(dbPath, dbFile); + } + + private const string SqlConnectionStrings = "Data Source=.;Initial Catalog=UnitTest;User ID=sa;Password=sa"; + private const string SQLiteConnectionStrings = "Data Source=UnitTest.db;"; + private const string MySqlConnectionStrings = "Server=.;Database=UnitTest;Uid=argozhang;Pwd=argo@163.com;SslMode=none;"; + + public static void ConfigureWebHost(IWebHostBuilder builder) + { + builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair[] { + new KeyValuePair("ConnectionStrings:ba", SqlConnectionStrings) + })); +#if SQLite + builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair[] { + new KeyValuePair("DB:0:Enabled", "false"), + new KeyValuePair("DB:1:Enabled", "false"), + new KeyValuePair("DB:2:ConnectionStrings:ba", SQLiteConnectionStrings) + })); +#endif + +#if MySQL + builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair[] { + new KeyValuePair("DB:0:Enabled", "false"), + new KeyValuePair("DB:1:ConnectionStrings:ba", MySqlConnectionStrings) + })); +#endif + } + + public static IConfiguration CreateConfiguraton() + { + var config = new ConfigurationBuilder().AddInMemoryCollection(new KeyValuePair[] { + new KeyValuePair("ConnectionStrings:ba", SqlConnectionStrings), + new KeyValuePair("DB:0:Enabled", "false"), + + new KeyValuePair("DB:1:Enabled", "false"), + new KeyValuePair("DB:1:ProviderName", "SQLite"), + new KeyValuePair("DB:1:ConnectionStrings:ba", SQLiteConnectionStrings), + + new KeyValuePair("DB:2:Enabled", "false"), + new KeyValuePair("DB:2:ProviderName", "MySql"), + new KeyValuePair("DB:2:ConnectionStrings:ba", MySqlConnectionStrings), + new KeyValuePair("LongbowCache:Enabled", "false") + }); + +#if SQLite + config.AddInMemoryCollection(new KeyValuePair[] { + new KeyValuePair("DB:1:Enabled", "true") + }); + CopySQLiteDBFile(); +#endif + +#if MySQL + config.AddInMemoryCollection(new KeyValuePair[] { + new KeyValuePair("DB:2:Enabled", "true") + }); +#endif + return config.Build(); } } }