2019-03-29 18:18:56 +08:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2019-01-19 15:58:24 +08:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-01-12 19:23:10 +08:00
|
|
|
using System.IO;
|
|
|
|
|
2019-01-15 18:22:06 +08:00
|
|
|
namespace UnitTest
|
2019-01-12 19:23:10 +08:00
|
|
|
{
|
|
|
|
public static class TestHelper
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// 获得当前工程解决方案目录
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static string RetrieveSolutionPath()
|
|
|
|
{
|
|
|
|
var dirSeparator = Path.DirectorySeparatorChar;
|
|
|
|
var paths = AppContext.BaseDirectory.SpanSplit($"{dirSeparator}.vs{dirSeparator}");
|
|
|
|
return paths.Count > 1 ? paths[0] : Path.Combine(AppContext.BaseDirectory, $"..{dirSeparator}..{dirSeparator}..{dirSeparator}..{dirSeparator}");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="folder"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static string RetrievePath(string folder)
|
|
|
|
{
|
|
|
|
var soluFolder = RetrieveSolutionPath();
|
|
|
|
return Path.Combine(soluFolder, folder);
|
|
|
|
}
|
2019-01-15 18:22:06 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
public static void CopyLicense()
|
|
|
|
{
|
2019-02-16 11:52:46 +08:00
|
|
|
var licFile = RetrievePath($"Scripts{Path.DirectorySeparatorChar}Longbow.lic");
|
2019-01-15 18:22:06 +08:00
|
|
|
|
|
|
|
var targetFile = Path.Combine(AppContext.BaseDirectory, "Longbow.lic");
|
2019-01-24 17:58:06 +08:00
|
|
|
if (!File.Exists(targetFile))
|
|
|
|
{
|
|
|
|
File.Copy(licFile, targetFile, true);
|
|
|
|
}
|
2019-01-19 15:58:24 +08:00
|
|
|
}
|
|
|
|
|
2019-01-20 17:10:47 +08:00
|
|
|
private const string SqlConnectionString = "Data Source=.;Initial Catalog=UnitTest;User ID=sa;Password=sa";
|
|
|
|
private const string SQLiteConnectionString = "Data Source=UnitTest.db;";
|
2019-02-27 01:38:04 +08:00
|
|
|
private const string MySqlConnectionString = "Server=localhost;Database=UnitTest;Uid=argozhang;Pwd=argo@163.com;SslMode=none;allowPublicKeyRetrieval=true";
|
2019-01-20 17:10:47 +08:00
|
|
|
private const string NpgSqlConnectionString = "Server=localhost;Database=UnitTest;User ID=argozhang;Password=sa;";
|
2019-01-19 15:58:24 +08:00
|
|
|
|
2019-01-24 17:58:06 +08:00
|
|
|
public static void ConfigureWebHost(IWebHostBuilder builder, string providerName = Longbow.Data.DatabaseProviderType.SqlServer)
|
2019-01-19 15:58:24 +08:00
|
|
|
{
|
|
|
|
builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair<string, string>[] {
|
2019-01-20 17:10:47 +08:00
|
|
|
new KeyValuePair<string, string>("ConnectionStrings:ba", SqlConnectionString),
|
2019-01-20 12:00:15 +08:00
|
|
|
new KeyValuePair<string, string>("DB:0:Enabled", "true")
|
2019-01-19 15:58:24 +08:00
|
|
|
}));
|
|
|
|
|
2019-01-24 17:58:06 +08:00
|
|
|
if (providerName == Longbow.Data.DatabaseProviderType.SQLite)
|
|
|
|
{
|
|
|
|
var dbPath = RetrievePath($"UnitTest{Path.DirectorySeparatorChar}DB{Path.DirectorySeparatorChar}UnitTest.db");
|
|
|
|
var dbFile = Path.Combine(AppContext.BaseDirectory, "UnitTest.db");
|
2019-03-29 18:18:56 +08:00
|
|
|
File.Copy(dbPath, dbFile, true);
|
2019-01-20 17:10:47 +08:00
|
|
|
|
2019-01-24 17:58:06 +08:00
|
|
|
builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair<string, string>[] {
|
|
|
|
new KeyValuePair<string, string>("DB:0:Enabled", "false"),
|
|
|
|
new KeyValuePair<string, string>("DB:1:Enabled", "true"),
|
|
|
|
new KeyValuePair<string, string>("DB:1:ConnectionStrings:ba", SQLiteConnectionString)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (providerName == Longbow.Data.DatabaseProviderType.MySql)
|
|
|
|
{
|
|
|
|
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:Enabled", "true"),
|
|
|
|
new KeyValuePair<string, string>("DB:2:ConnectionStrings:ba", MySqlConnectionString)
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (providerName == Longbow.Data.DatabaseProviderType.Npgsql)
|
|
|
|
{
|
|
|
|
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:Enabled", "false"),
|
|
|
|
new KeyValuePair<string, string>("DB:3:Enabled", "true"),
|
|
|
|
new KeyValuePair<string, string>("DB:3:ConnectionStrings:ba", NpgSqlConnectionString)
|
|
|
|
}));
|
|
|
|
}
|
2019-01-19 15:58:24 +08:00
|
|
|
}
|
2019-01-12 19:23:10 +08:00
|
|
|
}
|
|
|
|
}
|