using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.IO; namespace UnitTest { public static class TestHelper { /// /// 获得当前工程解决方案目录 /// /// 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}"); } /// /// /// /// /// public static string RetrievePath(string folder) { var soluFolder = RetrieveSolutionPath(); return Path.Combine(soluFolder, folder); } /// /// /// public static void CopyLicense() { var licFile = RetrievePath($"Scripts{Path.DirectorySeparatorChar}Longbow.lic"); var targetFile = Path.Combine(AppContext.BaseDirectory, "Longbow.lic"); if (!File.Exists(targetFile)) { File.Copy(licFile, targetFile, true); } } private const string SqlConnectionString = "Data Source=.;Initial Catalog=UnitTest;User ID=sa;Password=sa"; private const string SQLiteConnectionString = "Data Source=UnitTest.db;"; private const string MySqlConnectionString = "Server=localhost;Database=UnitTest;Uid=argozhang;Pwd=argo@163.com;SslMode=none;allowPublicKeyRetrieval=true"; private const string NpgSqlConnectionString = "Server=localhost;Database=UnitTest;User ID=argozhang;Password=sa;"; public static void ConfigureWebHost(IWebHostBuilder builder, string providerName = Longbow.Data.DatabaseProviderType.SqlServer) { builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair[] { new KeyValuePair("ConnectionStrings:ba", SqlConnectionString), new KeyValuePair("DB:0:Enabled", "true") })); 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"); File.Copy(dbPath, dbFile, true); builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair[] { new KeyValuePair("DB:0:Enabled", "false"), new KeyValuePair("DB:1:Enabled", "true"), new KeyValuePair("DB:1:ConnectionStrings:ba", SQLiteConnectionString) })); } if (providerName == Longbow.Data.DatabaseProviderType.MySql) { builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair[] { new KeyValuePair("DB:0:Enabled", "false"), new KeyValuePair("DB:1:Enabled", "false"), new KeyValuePair("DB:2:Enabled", "true"), new KeyValuePair("DB:2:ConnectionStrings:ba", MySqlConnectionString) })); } if (providerName == Longbow.Data.DatabaseProviderType.Npgsql) { builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair[] { new KeyValuePair("DB:0:Enabled", "false"), new KeyValuePair("DB:1:Enabled", "false"), new KeyValuePair("DB:2:Enabled", "false"), new KeyValuePair("DB:3:Enabled", "true"), new KeyValuePair("DB:3:ConnectionStrings:ba", NpgSqlConnectionString) })); } } } }