单元测试:重构代码将多数据库支持整合到TestHelper文件中,更改此文件的宏定义即可更改数据库 #SQLite/MySQL

This commit is contained in:
Argo-Surface 2019-01-19 15:58:24 +08:00
parent 44dd905141
commit 40a81b7b50
3 changed files with 80 additions and 44 deletions

View File

@ -44,10 +44,7 @@ namespace Bootstrap.Admin
{ {
base.ConfigureWebHost(builder); base.ConfigureWebHost(builder);
var sqlConnectionStrings = "Data Source=.;Initial Catalog=UnitTest;User ID=sa;Password=sa"; TestHelper.ConfigureWebHost(builder);
builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("ConnectionStrings:ba", sqlConnectionStrings)
}));
} }
public string Login(string userName = "Admin", string password = "123789") public string Login(string userName = "Admin", string password = "123789")

View File

@ -1,15 +1,5 @@
//#define SQLite using Microsoft.Extensions.DependencyInjection;
//#define MySQL
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
#if SQLite
using System;
using System.IO;
using UnitTest; using UnitTest;
#endif
namespace Bootstrap.DataAccess namespace Bootstrap.DataAccess
{ {
@ -17,39 +7,13 @@ namespace Bootstrap.DataAccess
{ {
static BootstrapAdminStartup() static BootstrapAdminStartup()
{ {
var sqlConnectionStrings = "Data Source=.;Initial Catalog=UnitTest;User ID=sa;Password=sa"; var config = TestHelper.CreateConfiguraton();
var mysqlConnectionStrings = "Server=.;Database=UnitTest;Uid=argozhang;Pwd=argo@163.com;SslMode=none;";
var config = new ConfigurationBuilder().AddInMemoryCollection(new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("ConnectionStrings:ba", sqlConnectionStrings),
new KeyValuePair<string, string>("DB:0:Enabled", "false"),
new KeyValuePair<string, string>("DB:1:Enabled", "false"),
new KeyValuePair<string, string>("DB:1:ProviderName", "SQLite"),
new KeyValuePair<string, string>("DB:1:ConnectionStrings:ba", "Data Source=UnitTest.db"),
new KeyValuePair<string, string>("DB:2:Enabled", "false"),
new KeyValuePair<string, string>("DB:2:ProviderName", "MySql"),
new KeyValuePair<string, string>("DB:2:ConnectionStrings:ba", mysqlConnectionStrings),
new KeyValuePair<string, string>("LongbowCache:Enabled", "false")
}).Build();
var sc = new ServiceCollection(); var sc = new ServiceCollection();
sc.AddSingleton<IConfiguration>(config); sc.AddSingleton(config);
sc.AddConfigurationManager(config); sc.AddConfigurationManager(config);
sc.AddCacheManager(config); sc.AddCacheManager(config);
sc.AddDbAdapter(); 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
} }
} }
} }

View File

@ -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; using System.IO;
namespace UnitTest namespace UnitTest
@ -36,6 +41,76 @@ namespace UnitTest
var targetFile = Path.Combine(AppContext.BaseDirectory, "Longbow.lic"); var targetFile = Path.Combine(AppContext.BaseDirectory, "Longbow.lic");
if (!File.Exists(targetFile)) File.Copy(licFile, targetFile, true); if (!File.Exists(targetFile)) File.Copy(licFile, targetFile, true);
#if SQLite
CopySQLiteDBFile();
#endif
}
/// <summary>
///
/// </summary>
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<string, string>[] {
new KeyValuePair<string, string>("ConnectionStrings:ba", SqlConnectionStrings)
}));
#if SQLite
builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("DB:0:Enabled", "false"),
new KeyValuePair<string, string>("DB:1:Enabled", "false"),
new KeyValuePair<string, string>("DB:2:ConnectionStrings:ba", SQLiteConnectionStrings)
}));
#endif
#if MySQL
builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("DB:0:Enabled", "false"),
new KeyValuePair<string, string>("DB:1:ConnectionStrings:ba", MySqlConnectionStrings)
}));
#endif
}
public static IConfiguration CreateConfiguraton()
{
var config = new ConfigurationBuilder().AddInMemoryCollection(new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("ConnectionStrings:ba", SqlConnectionStrings),
new KeyValuePair<string, string>("DB:0:Enabled", "false"),
new KeyValuePair<string, string>("DB:1:Enabled", "false"),
new KeyValuePair<string, string>("DB:1:ProviderName", "SQLite"),
new KeyValuePair<string, string>("DB:1:ConnectionStrings:ba", SQLiteConnectionStrings),
new KeyValuePair<string, string>("DB:2:Enabled", "false"),
new KeyValuePair<string, string>("DB:2:ProviderName", "MySql"),
new KeyValuePair<string, string>("DB:2:ConnectionStrings:ba", MySqlConnectionStrings),
new KeyValuePair<string, string>("LongbowCache:Enabled", "false")
});
#if SQLite
config.AddInMemoryCollection(new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("DB:1:Enabled", "true")
});
CopySQLiteDBFile();
#endif
#if MySQL
config.AddInMemoryCollection(new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("DB:2:Enabled", "true")
});
#endif
return config.Build();
} }
} }
} }