test: 修复单元测试一起运行时出错问题

This commit is contained in:
Argo Zhang 2019-05-18 17:48:40 +08:00
parent d2e56e85e9
commit 8a32aecbf5
18 changed files with 75 additions and 65 deletions

2
.gitignore vendored
View File

@ -353,5 +353,5 @@ Bootstrap.Admin.xml
###### -- Custom Ignore Section, Make sure all files you add to the git repo are below this line -- ######
# Coverage
coverage.xml
coverage*.xml
tools/

View File

@ -126,11 +126,6 @@ namespace Bootstrap.Admin
TestHelper.SQLServerConnectionString = con.GetConnectionString("sqlserver-app");
TestHelper.MySqlConnectionString = con.GetConnectionString("mysql-app");
TestHelper.NpgSqlConnectionString = con.GetConnectionString("npgsql-app");
builder.ConfigureAppConfiguration(app => app.AddInMemoryCollection(new KeyValuePair<string, string>[] {
new KeyValuePair<string, string>("Logging:LogLevel:Default", "Error"),
new KeyValuePair<string, string>("Logging:LogLevel:System", "Error"),
new KeyValuePair<string, string>("Logging:LogLevel:Microsoft", "Error")
}));
}
else
{
@ -141,6 +136,7 @@ namespace Bootstrap.Admin
new KeyValuePair<string, string>("MongoDB", con.GetValue("MongoDB", "UnitTest"))
}));
}
builder.ConfigureAppConfiguration(app => app.AddJsonFile(TestHelper.RetrievePath($"UnitTest{Path.DirectorySeparatorChar}appsettings.unittest.json"), false, true));
TestHelper.SQLiteConnectionString = con.GetConnectionString("sqlite");
TestHelper.ConfigureWebHost(builder);
}

View File

@ -12,7 +12,7 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void Get_Ok()
{
var rid = new Role().Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var cates = await Client.GetAsJsonAsync<IEnumerable<App>>(rid);
Assert.NotEmpty(cates);
}

View File

@ -23,13 +23,10 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void PostAndDelete_Ok()
{
var dict = new Dict();
dict.Delete(new Dict().RetrieveDicts().Where(d => d.Category == "UnitTest-Category").Select(d => d.Id));
var ret = await Client.PostAsJsonAsync<BootstrapDict, bool>("", new BootstrapDict() { Name = "UnitTest-Dict", Category = "UnitTest-Category", Code = "0", Define = 0 });
Assert.True(ret);
var ids = dict.RetrieveDicts().Where(d => d.Name == "UnitTest-Dict").Select(d => d.Id);
var ids = DictHelper.RetrieveDicts().Where(d => d.Name == "UnitTest-Dict").Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>(ids));
}
}

View File

@ -22,7 +22,7 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void GetById_Ok()
{
var id = new Group().Retrieves().Where(gp => gp.GroupName == "Admin").First().Id;
var id = GroupHelper.Retrieves().Where(gp => gp.GroupName == "Admin").First().Id;
var g = await Client.GetAsJsonAsync<Group>(id);
Assert.Equal("Admin", g.GroupName);
}
@ -33,18 +33,18 @@ namespace Bootstrap.Admin.Api.SqlServer
var ret = await Client.PostAsJsonAsync<Group, bool>("", new Group() { GroupName = "UnitTest-Group", Description = "UnitTest-Desc" });
Assert.True(ret);
var ids = new Group().Retrieves().Where(d => d.GroupName == "UnitTest-Group").Select(d => d.Id);
var ids = GroupHelper.Retrieves().Where(d => d.GroupName == "UnitTest-Group").Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>(ids));
}
[Fact]
public async void PostById_Ok()
{
var uid = new User().Retrieves().Where(u => u.UserName == "Admin").First().Id;
var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id;
var ret = await Client.PostAsJsonAsync<string, IEnumerable<Group>>($"{uid}?type=user", string.Empty);
Assert.NotEmpty(ret);
var rid = new Role().Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PostAsJsonAsync<string, IEnumerable<Group>>($"{rid}?type=role", string.Empty);
Assert.NotEmpty(ret);
}
@ -52,12 +52,12 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void PutById_Ok()
{
var ids = new Group().Retrieves().Select(g => g.Id);
var uid = new User().Retrieves().Where(u => u.UserName == "Admin").First().Id;
var ids = GroupHelper.Retrieves().Select(g => g.Id);
var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id;
var ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{uid}?type=user", ids);
Assert.True(ret);
var rid = new Role().Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{rid}?type=role", ids);
Assert.True(ret);
}

View File

@ -26,8 +26,7 @@ namespace Bootstrap.Admin.Api.SqlServer
var ret = await Client.PostAsJsonAsync<BootstrapMenu, bool>(new BootstrapMenu() { Name = "UnitTest-Menu", Application = "0", Category = "0", ParentId = "0", Url = "#", Target = "_self", IsResource = 0 });
Assert.True(ret);
var menu = new Menu();
var ids = menu.RetrieveAllMenus("Admin").Where(d => d.Name == "UnitTest-Menu").Select(d => d.Id);
var ids = MenuHelper.RetrieveAllMenus("Admin").Where(d => d.Name == "UnitTest-Menu").Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>("", ids));
}
@ -35,11 +34,11 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void PostById_Ok()
{
var uid = new User().Retrieves().Where(u => u.UserName == "Admin").First().Id;
var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id;
var ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{uid}?type=user", string.Empty);
Assert.NotEmpty(ret);
var rid = new Role().Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{rid}?type=role", string.Empty);
Assert.NotEmpty(ret);
}
@ -47,8 +46,8 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void PutById_Ok()
{
var ids = new Menu().RetrieveAllMenus("Admin").Select(g => g.Id);
var rid = new Role().Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var ids = MenuHelper.RetrieveAllMenus("Admin").Select(g => g.Id);
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{rid}", ids);
Assert.True(ret);
}

View File

@ -33,7 +33,7 @@ namespace Bootstrap.Admin.Api.SqlServer
Assert.True(resp);
// 删除新用户
nusr.Delete(new string[] { nusr.Id });
UserHelper.Delete(new string[] { nusr.Id });
// Reject
nusr = InsertNewUser();
@ -48,15 +48,15 @@ namespace Bootstrap.Admin.Api.SqlServer
private User InsertNewUser()
{
// 插入新用户
var nusr = new User() { UserName = "UnitTest-Register", DisplayName = "UnitTest", Password = "1", Description = "UnitTest" };
Assert.True(new User().Save(nusr));
var nusr = new User() { UserName = "UnitTest_New", DisplayName = "UnitTest", Password = "1", Description = "UnitTest" };
Assert.True(UserHelper.Save(nusr));
return nusr;
}
private void DeleteUnitTestUser()
{
var ids = new User().RetrieveNewUsers().Where(u => u.UserName == "UnitTest-Register").Select(u => u.Id);
new User().Delete(ids);
var ids = UserHelper.RetrieveNewUsers().Where(u => u.UserName == "UnitTest_New").Select(u => u.Id);
UserHelper.Delete(ids);
}
}
}

View File

@ -22,7 +22,7 @@ namespace Bootstrap.Admin.Api.SqlServer
var nusr = new User() { UserName = "UnitTest_RegisterController", DisplayName = "UnitTest", Password = "1", Description = "UnitTest" };
var resp = await Client.PostAsJsonAsync<User, bool>(nusr);
Assert.True(resp);
nusr.Delete(nusr.RetrieveNewUsers().Where(u => u.UserName == nusr.UserName).Select(u => u.Id));
UserHelper.Delete(nusr.RetrieveNewUsers().Where(u => u.UserName == nusr.UserName).Select(u => u.Id));
}
[Fact]

View File

@ -25,16 +25,16 @@ namespace Bootstrap.Admin.Api.SqlServer
var ret = await Client.PostAsJsonAsync<Role, bool>(new Role() { RoleName = "UnitTest-Role", Description = "UnitTest-Desc" });
Assert.True(ret);
var ids = new Role().Retrieves().Where(d => d.RoleName == "UnitTest-Role").Select(d => d.Id);
var ids = RoleHelper.Retrieves().Where(d => d.RoleName == "UnitTest-Role").Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>(ids));
}
[Fact]
public async void PostById_Ok()
{
var uid = new User().Retrieves().Where(u => u.UserName == "Admin").First().Id;
var gid = new Group().Retrieves().Where(g => g.GroupName == "Admin").First().Id;
var mid = new Menu().RetrieveAllMenus("Admin").Where(m => m.Url == "~/Admin/Index").First().Id;
var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id;
var gid = GroupHelper.Retrieves().Where(g => g.GroupName == "Admin").First().Id;
var mid = MenuHelper.RetrieveAllMenus("Admin").Where(m => m.Url == "~/Admin/Index").First().Id;
var ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{uid}?type=user", string.Empty);
Assert.NotEmpty(ret);
@ -49,10 +49,10 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void PutById_Ok()
{
var uid = new User().Retrieves().Where(u => u.UserName == "Admin").First().Id;
var gid = new Group().Retrieves().Where(g => g.GroupName == "Admin").First().Id;
var mid = new Menu().RetrieveAllMenus("Admin").Where(m => m.Url == "~/Admin/Index").First().Id;
var ids = new Role().Retrieves().Select(r => r.Id);
var uid = UserHelper.Retrieves().Where(u => u.UserName == "Admin").First().Id;
var gid = GroupHelper.Retrieves().Where(g => g.GroupName == "Admin").First().Id;
var mid = MenuHelper.RetrieveAllMenus("Admin").Where(m => m.Url == "~/Admin/Index").First().Id;
var ids = RoleHelper.Retrieves().Select(r => r.Id);
var ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{uid}?type=user", ids);
Assert.True(ret);

View File

@ -21,24 +21,23 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void Post_Ok()
{
var dict = new Dict();
var dicts = dict.RetrieveDicts();
var dicts = DictHelper.RetrieveDicts();
var ids = dicts.Where(d => d.Category == "UnitTest-Settings").Select(d => d.Id);
dict.Delete(ids);
DictHelper.Delete(ids);
Assert.True(dict.Save(new Dict() { Category = "UnitTest-Settings", Name = "UnitTest", Code = "0", Define = 0 }));
Assert.True(DictHelper.Save(new BootstrapDict() { Category = "UnitTest-Settings", Name = "UnitTest", Code = "0", Define = 0 }));
// 获得原来值
var resp = await Client.PostAsJsonAsync<BootstrapDict, bool>(new Dict() { Category = "UnitTest-Settings", Name = "UnitTest", Code = "UnitTest" });
var resp = await Client.PostAsJsonAsync<BootstrapDict, bool>(new BootstrapDict() { Category = "UnitTest-Settings", Name = "UnitTest", Code = "UnitTest" });
Assert.True(resp);
var code = dict.RetrieveDicts().FirstOrDefault(d => d.Category == "UnitTest-Settings").Code;
var code = DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == "UnitTest-Settings").Code;
Assert.Equal("UnitTest", code);
// Delete
ids = dict.RetrieveDicts().Where(d => d.Category == "UnitTest-Settings").Select(d => d.Id);
dict.Delete(ids);
ids = DictHelper.RetrieveDicts().Where(d => d.Category == "UnitTest-Settings").Select(d => d.Id);
DictHelper.Delete(ids);
}
internal class CacheCorsItem : ICacheCorsItem

View File

@ -31,30 +31,29 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void PostAndDelete_Ok()
{
var user = new User();
user.Delete(user.Retrieves().Where(usr => usr.UserName == "UnitTest-Delete").Select(usr => usr.Id));
UserHelper.Delete(UserHelper.Retrieves().Where(usr => usr.UserName == "UnitTest_Delete").Select(usr => usr.Id));
var nusr = new User { UserName = "UnitTest_Delete", Password = "1", DisplayName = "DisplayName", ApprovedBy = "System", ApprovedTime = DateTime.Now, Description = "Desc", Icon = "default.jpg" };
var resp = await Client.PostAsJsonAsync<User, bool>("", nusr);
Assert.True(resp);
nusr.Id = user.Retrieves().First(u => u.UserName == nusr.UserName).Id;
nusr.Id = UserHelper.Retrieves().First(u => u.UserName == nusr.UserName).Id;
resp = await Client.PostAsJsonAsync<User, bool>(nusr);
Assert.True(resp);
var ids = user.Retrieves().Where(d => d.UserName == nusr.UserName).Select(d => d.Id);
var ids = UserHelper.Retrieves().Where(d => d.UserName == nusr.UserName).Select(d => d.Id);
Assert.True(await Client.DeleteAsJsonAsync<IEnumerable<string>, bool>(ids));
}
[Fact]
public async void PostById_Ok()
{
var rid = new Role().Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{rid}?type=role", string.Empty);
Assert.NotNull(ret);
var gid = new Group().Retrieves().Where(r => r.GroupName == "Admin").First().Id;
var gid = GroupHelper.Retrieves().Where(r => r.GroupName == "Admin").First().Id;
ret = await Client.PostAsJsonAsync<string, IEnumerable<object>>($"{gid}?type=group", string.Empty);
Assert.NotNull(ret);
@ -65,12 +64,12 @@ namespace Bootstrap.Admin.Api.SqlServer
[Fact]
public async void PutById_Ok()
{
var ids = new User().Retrieves().Where(u => u.UserName == "Admin").Select(u => u.Id);
var gid = new Group().Retrieves().Where(r => r.GroupName == "Admin").First().Id;
var ids = UserHelper.Retrieves().Where(u => u.UserName == "Admin").Select(u => u.Id);
var gid = GroupHelper.Retrieves().Where(r => r.GroupName == "Admin").First().Id;
var ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{gid}?type=group", ids);
Assert.True(ret);
var rid = new Role().Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
var rid = RoleHelper.Retrieves().Where(r => r.RoleName == "Administrators").First().Id;
ret = await Client.PutAsJsonAsync<IEnumerable<string>, bool>($"{rid}?type=role", ids);
Assert.True(ret);
}

View File

@ -1,4 +1,4 @@
using Bootstrap.Security;
using Bootstrap.Security;
using System;
using System.Linq;
using System.Net.Http;
@ -9,6 +9,8 @@ namespace Bootstrap.DataAccess.SqlServer
[Collection("SQLServerContext")]
public class DictsTest
{
protected virtual string DatabaseName { get; set; } = "SQLServer";
[Fact]
public void SaveAndDelete_Ok()
{
@ -168,6 +170,14 @@ namespace Bootstrap.DataAccess.SqlServer
Assert.Equal("10", DictHelper.RetrieveLocaleIPSvrCachePeriod());
}
[Fact]
public void DatabaseCheck_Ok()
{
var dict = new BootstrapDict() { Category = "系统检查", Name = "系统设置", Code = DatabaseName, Define = 0 };
Assert.True(DictHelper.Save(dict));
Assert.Equal(DatabaseName, DictHelper.RetrieveDicts().FirstOrDefault(d => d.Category == dict.Category && d.Name == dict.Name)?.Code ?? "unknown");
}
#region Private Class For Test
/// <summary>
///

View File

@ -5,6 +5,6 @@ namespace Bootstrap.DataAccess.MongoDB
[Collection("MongoContext")]
public class DictsTest : SqlServer.DictsTest
{
protected override string DatabaseName { get; set; } = "MongoDB";
}
}

View File

@ -5,6 +5,6 @@ namespace Bootstrap.DataAccess.MySql
[Collection("MySqlContext")]
public class DictsTest : SqlServer.DictsTest
{
protected override string DatabaseName { get; set; } = "MySql";
}
}

View File

@ -5,6 +5,6 @@ namespace Bootstrap.DataAccess.SQLite
[Collection("SQLiteContext")]
public class DictsTest : SqlServer.DictsTest
{
protected override string DatabaseName { get; set; } = "SQLite";
}
}

View File

@ -8,7 +8,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="2.0.0" />
<PackageReference Include="coverlet.msbuild" Version="2.6.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="2.2.4" />

View File

@ -1,9 +1,4 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug"
}
},
"MongoDB": "UnitTest",
"ConnectionStrings": {
"sqlite": "Data Source=UnitTest.db;",

View File

@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Error",
"System": "Error",
"Microsoft": "Error"
}
},
"LongbowCache": {
"Enabled": false
}
}