test: 支持Appveyor数据库单元测试

This commit is contained in:
Argo Zhang 2019-05-12 14:28:56 +08:00
parent 9f7e559f1b
commit 66edea15b2
6 changed files with 76 additions and 14 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.Mvc.Testing.Handlers; using Microsoft.AspNetCore.Mvc.Testing.Handlers;
using Microsoft.Extensions.Configuration;
using System; using System;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
@ -91,6 +92,24 @@ namespace Bootstrap.Admin
{ {
base.ConfigureWebHost(builder); base.ConfigureWebHost(builder);
var config = new ConfigurationBuilder();
config.AddJsonFile(TestHelper.RetrievePath("UnitTest\\appsettings.json"), false, true);
config.AddEnvironmentVariables();
var con = config.Build();
if (con.GetValue("Appveyor", false))
{
TestHelper.SQLServerConnectionString = con.GetConnectionString("sqlserver-app");
TestHelper.MySqlConnectionString = con.GetConnectionString("mysql-app");
TestHelper.NpgSqlConnectionString = con.GetConnectionString("npgsql-app");
}
else
{
TestHelper.SQLServerConnectionString = con.GetConnectionString("sqlserver");
TestHelper.MySqlConnectionString = con.GetConnectionString("mysql");
TestHelper.NpgSqlConnectionString = con.GetConnectionString("npgsql");
}
TestHelper.SQLiteConnectionString = con.GetConnectionString("sqlite");
TestHelper.ConfigureWebHost(builder); TestHelper.ConfigureWebHost(builder);
} }
} }

View File

@ -1,5 +1,5 @@
using Bootstrap.DataAccess; using Bootstrap.DataAccess;
using System.Collections.Generic; using Longbow.Web.Mvc;
using System.Net.Http; using System.Net.Http;
using Xunit; using Xunit;
@ -12,8 +12,8 @@ namespace Bootstrap.Admin.Api
[Fact] [Fact]
public async void Login_Get() public async void Login_Get()
{ {
var users = await Client.GetAsJsonAsync<IEnumerable<LoginUser>>(); var users = await Client.GetAsJsonAsync<QueryData<LoginUser>>();
Assert.NotEmpty(users); Assert.NotEmpty(users.rows);
} }
[Fact] [Fact]

View File

@ -3,7 +3,7 @@
namespace Bootstrap.Admin.Api.SQLite namespace Bootstrap.Admin.Api.SQLite
{ {
[Collection("SQLiteContext")] [Collection("SQLiteContext")]
public class AppsTest : Api.CategoryTest public class AppsTest : Api.AppsTest
{ {
public AppsTest(SQLiteBAWebHost factory) : base(factory) { } public AppsTest(SQLiteBAWebHost factory) : base(factory) { }
} }

View File

@ -14,7 +14,7 @@ namespace Bootstrap.Admin.Api
[Fact] [Fact]
public async void Get_Ok() public async void Get_Ok()
{ {
var resp = await Client.GetAsJsonAsync<IEnumerable<ICacheCorsItem>>(); var resp = await Client.GetAsJsonAsync<IEnumerable<CacheCorsItem>>();
Assert.NotNull(resp); Assert.NotNull(resp);
} }
@ -40,5 +40,33 @@ namespace Bootstrap.Admin.Api
ids = dict.RetrieveDicts().Where(d => d.Category == "UnitTest-Settings").Select(d => d.Id); ids = dict.RetrieveDicts().Where(d => d.Category == "UnitTest-Settings").Select(d => d.Id);
dict.Delete(ids); dict.Delete(ids);
} }
internal class CacheCorsItem : ICacheCorsItem
{
/// <summary>
///
/// </summary>
public bool Enabled { get; set; }
/// <summary>
///
/// </summary>
public string Key { get; set; }
/// <summary>
///
/// </summary>
public string Url { get; set; }
/// <summary>
///
/// </summary>
public string Desc { get; set; }
/// <summary>
///
/// </summary>
public bool Self { get; set; }
}
} }
} }

View File

@ -9,6 +9,14 @@ namespace UnitTest
{ {
public static class TestHelper public static class TestHelper
{ {
public static string SQLServerConnectionString { get; set; }
public static string SQLiteConnectionString { get; set; }
public static string MySqlConnectionString { get; set; }
public static string NpgSqlConnectionString { get; set; }
/// <summary> /// <summary>
/// 获得当前工程解决方案目录 /// 获得当前工程解决方案目录
/// </summary> /// </summary>
@ -45,17 +53,15 @@ namespace UnitTest
} }
} }
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, DatabaseProviderType providerName = DatabaseProviderType.SqlServer) public static void ConfigureWebHost(IWebHostBuilder builder, DatabaseProviderType providerName = DatabaseProviderType.SqlServer)
{ {
builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair<string, string>[] { if (providerName == DatabaseProviderType.SqlServer)
new KeyValuePair<string, string>("ConnectionStrings:ba", SqlConnectionString), {
new KeyValuePair<string, string>("DB:0:Enabled", "true") builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair<string, string>[] {
})); new KeyValuePair<string, string>("ConnectionStrings:ba", SQLServerConnectionString),
new KeyValuePair<string, string>("DB:0:Enabled", "true")
}));
}
if (providerName == DatabaseProviderType.SQLite) if (providerName == DatabaseProviderType.SQLite)
{ {

View File

@ -0,0 +1,9 @@
{
"ConnectionStrings": {
"sqlite": "Data Source=UnitTest.db;",
"sqlserver": "Data Source=.;Initial Catalog=UnitTest;User ID=sa;Password=sa",
"mysql": "Server=localhost;Database=UnitTest;Uid=argozhang;Pwd=argo@163.com;SslMode=none;allowPublicKeyRetrieval=true",
"sqlserver-app": "Data Source=(local)\\SQL2014;Initial Catalog=BootstrapAdmin;User ID=sa;Password=Password12!",
"mysql-app": "Server=localhost;Database=BootstrapAdmin;Uid=root;Pwd=Password12!;SslMode=none;allowPublicKeyRetrieval=true"
}
}