Skip to content

Commit

Permalink
Update test cases;
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Sep 8, 2024
1 parent e4204e7 commit 03d2148
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 98 deletions.
37 changes: 23 additions & 14 deletions test/AgileConfig.Server.ServiceTests/sqlite/AppServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Moq;
using AgileConfig.Server.Common;
using AgileConfig.Server.Data.Freesql;
using AgileConfig.Server.Data.Repository.Selector;

Expand All @@ -19,6 +18,8 @@ namespace AgileConfig.Server.ServiceTests.sqlite
[TestClass()]
public class AppServiceTests : BasicTestService
{
IServiceProvider _serviceProvider = null;
IServiceScope _serviceScope = null;
IAppService _appservice = null;
public override Task<Dictionary<string, string>> GetConfigurationData()
{
Expand All @@ -30,16 +31,8 @@ public override Task<Dictionary<string, string>> GetConfigurationData()
});
}

[TestInitialize]
public async Task TestInitialize()
public virtual async Task<ServiceProvider> NewGlobalSp()
{
if (_appservice != null)
{
Console.WriteLine("IAppService already there.");

return;
}

Console.WriteLine("Try get configration data");
var dict = await GetConfigurationData();

Expand All @@ -57,8 +50,6 @@ public async Task TestInitialize()
Console.WriteLine($"key: {item.Key} value: {item.Value}");
}

ClearData();

var cache = new Mock<IMemoryCache>();
IServiceCollection services = new ServiceCollection();
services.AddScoped(_ => cache.Object);
Expand All @@ -68,18 +59,36 @@ public async Task TestInitialize()
services.AddRepositories();
services.AddBusinessServices();

_serviceProvider = services.BuildServiceProvider();
return services.BuildServiceProvider();
}

[TestInitialize]
public async Task TestInitialize()
{
this.GlobalServiceProvider = await NewGlobalSp();

ClearData();

_serviceScope = this.GlobalServiceProvider.CreateScope();
_serviceProvider = _serviceScope.ServiceProvider;

var systeminitializationService = _serviceProvider.GetService<ISystemInitializationService>();
systeminitializationService.TryInitDefaultEnvironment();//初始化环境 DEV TEST STAGE PROD
systeminitializationService.TryInitJwtSecret();//初始化 jwt secret


_appservice = _serviceProvider.GetService<IAppService>();

Console.WriteLine("Run TestInitialize");
}


[TestCleanup]
public void TestCleanup()
{
_appservice.Dispose();
_serviceScope.Dispose();
}

[TestMethod()]
public async Task AddAsyncTest()
{
Expand Down
20 changes: 2 additions & 18 deletions test/AgileConfig.Server.ServiceTests/sqlite/BasicTestService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using AgileConfig.Server.Common;
using AgileConfig.Server.Data.Abstraction;
using AgileConfig.Server.Data.Abstraction.DbProvider;
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.Data.Freesql;
using AgileConfig.Server.Data.Repository.Freesql;
using AgileConfig.Server.Data.Repository.Selector;
using AgileConfig.Server.IService;
using AgileConfig.Server.Service;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand All @@ -19,8 +12,8 @@ namespace AgileConfig.Server.ServiceTests.sqlite
{
public class BasicTestService
{
protected ServiceProvider _serviceProvider;

protected ServiceProvider GlobalServiceProvider { get; set; }
public virtual Task<Dictionary<string, string>> GetConfigurationData()
{
return
Expand All @@ -44,15 +37,6 @@ public IFreeSql GetFreeSql()

public virtual void ClearData()
{
var dict = new Dictionary<string, string>
{
{"db:provider","sqlite" },
{"db:conn","Data Source=agile_config.db" }
};
var config = new ConfigurationBuilder()
.AddInMemoryCollection(dict)
.Build();

var fsq = GetFreeSql();

fsq.Delete<ServerNode>().Where("1=1").ExecuteAffrows();
Expand Down
43 changes: 25 additions & 18 deletions test/AgileConfig.Server.ServiceTests/sqlite/ConfigServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
using AgileConfig.Server.Data.Abstraction;
using AgileConfig.Server.Service;
using Microsoft.Extensions.Configuration;
using AgileConfig.Server.Common;
using AgileConfig.Server.Data.Repository.Selector;

namespace AgileConfig.Server.ServiceTests.sqlite
{
[TestClass()]
public class ConfigServiceTests : BasicTestService
{
IServiceProvider _serviceProvider = null;
IServiceScope _serviceScope = null;
IConfigService _service = null;
public override Task<Dictionary<string, string>> GetConfigurationData()
{
Expand All @@ -34,13 +35,24 @@ public override Task<Dictionary<string, string>> GetConfigurationData()
[TestInitialize]
public async Task TestInitialize()
{
if (_service != null)
{
Console.WriteLine("IConfigService already there.");
await NewGloablSp();

return;
}
ClearData();

_serviceScope = this.GlobalServiceProvider.CreateScope();
_serviceProvider = this.GlobalServiceProvider.CreateScope().ServiceProvider;

_service = this._serviceProvider.GetService<IConfigService>();

var systeminitializationService = this._serviceProvider.GetService<ISystemInitializationService>();
systeminitializationService.TryInitDefaultEnvironment();//初始化环境 DEV TEST STAGE PROD
systeminitializationService.TryInitJwtSecret();//初始化 jwt secret

Console.WriteLine("Run TestInitialize");
}

private async Task NewGloablSp()
{
Console.WriteLine("Try get configration data");
var dict = await GetConfigurationData();

Expand All @@ -57,9 +69,6 @@ public async Task TestInitialize()
{
Console.WriteLine($"key: {item.Key} value: {item.Value}");
}

ClearData();

var cache = new Mock<IMemoryCache>();
IServiceCollection services = new ServiceCollection();
services.AddScoped(_ => cache.Object);
Expand All @@ -69,17 +78,15 @@ public async Task TestInitialize()
services.AddRepositories();
services.AddBusinessServices();

_serviceProvider = services.BuildServiceProvider();
var systeminitializationService = _serviceProvider.GetService<ISystemInitializationService>();
systeminitializationService.TryInitDefaultEnvironment();//初始化环境 DEV TEST STAGE PROD
systeminitializationService.TryInitJwtSecret();//初始化 jwt secret


_service = _serviceProvider.GetService<IConfigService>();

Console.WriteLine("Run TestInitialize");
this.GlobalServiceProvider = services.BuildServiceProvider();
}

[TestCleanup]
public void TestCleanup()
{
_service.Dispose();
_serviceScope.Dispose();
}

[TestMethod()]
public async Task AddAsyncTest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ namespace AgileConfig.Server.ServiceTests.sqlite
[TestClass()]
public class ServerNodeServiceTests : BasicTestService
{
IServiceProvider _serviceProvider = null;
IServiceScope _serviceScope = null;
IServerNodeService _serverNodeService = null;

public override Task<Dictionary<string, string>> GetConfigurationData()
{
return
Expand All @@ -34,13 +37,24 @@ public override Task<Dictionary<string, string>> GetConfigurationData()
[TestInitialize]
public async Task TestInitialize()
{
if (_serverNodeService != null)
{
Console.WriteLine("IServerNodeService already there.");
await NewGlobalSp();

return;
}
_serviceScope = GlobalServiceProvider.CreateScope();
_serviceProvider = _serviceScope.ServiceProvider;

var systeminitializationService = _serviceProvider.GetService<ISystemInitializationService>();
systeminitializationService.TryInitDefaultEnvironment();//初始化环境 DEV TEST STAGE PROD
systeminitializationService.TryInitJwtSecret();//初始化 jwt secret

_serverNodeService = _serviceProvider.GetService<IServerNodeService>();

Console.WriteLine($"IServerNodeService type is {_serverNodeService.GetType().FullName}");

Console.WriteLine("Run TestInitialize");
}

private async Task NewGlobalSp()
{
Console.WriteLine("Try get configration data");
var dict = await GetConfigurationData();

Expand All @@ -67,17 +81,14 @@ public async Task TestInitialize()
services.AddRepositories();
services.AddBusinessServices();

_serviceProvider = services.BuildServiceProvider();
var systeminitializationService = _serviceProvider.GetService<ISystemInitializationService>();
systeminitializationService.TryInitDefaultEnvironment();//初始化环境 DEV TEST STAGE PROD
systeminitializationService.TryInitJwtSecret();//初始化 jwt secret


_serverNodeService = _serviceProvider.GetService<IServerNodeService>();

Console.WriteLine($"IServerNodeService type is {_serverNodeService.GetType().FullName}");
this.GlobalServiceProvider = services.BuildServiceProvider();
}

Console.WriteLine("Run TestInitialize");
[TestCleanup]
public void TestCleanup()
{
_serverNodeService.Dispose();
_serviceScope.Dispose();
}

[TestMethod()]
Expand Down
42 changes: 26 additions & 16 deletions test/AgileConfig.Server.ServiceTests/sqlite/SettingServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ namespace AgileConfig.Server.ServiceTests.sqlite
[TestClass()]
public class SettingServiceTests : BasicTestService
{
IServiceProvider _serviceProvider = null;
IServiceScope _serviceScope = null;
ISettingService _settingService = null;

public override Task<Dictionary<string, string>> GetConfigurationData()
{
return
Expand All @@ -34,13 +37,24 @@ public override Task<Dictionary<string, string>> GetConfigurationData()
[TestInitialize]
public async Task TestInitialize()
{
if (_settingService != null)
{
Console.WriteLine("ISettingService already there.");
await NewGlobalSp();

return;
}
ClearData();

_serviceScope = GlobalServiceProvider.CreateScope();
_serviceProvider = _serviceScope.ServiceProvider;

var systeminitializationService = _serviceProvider.GetService<ISystemInitializationService>();
systeminitializationService.TryInitDefaultEnvironment();//初始化环境 DEV TEST STAGE PROD
systeminitializationService.TryInitJwtSecret();//初始化 jwt secret

_settingService = _serviceProvider.GetService<ISettingService>();

Console.WriteLine("Run TestInitialize");
}

private async Task NewGlobalSp()
{
Console.WriteLine("Try get configration data");
var dict = await GetConfigurationData();

Expand All @@ -58,8 +72,6 @@ public async Task TestInitialize()
Console.WriteLine($"key: {item.Key} value: {item.Value}");
}

ClearData();

var cache = new Mock<IMemoryCache>();
IServiceCollection services = new ServiceCollection();
services.AddScoped(_ => cache.Object);
Expand All @@ -69,17 +81,15 @@ public async Task TestInitialize()
services.AddRepositories();
services.AddBusinessServices();

_serviceProvider = services.BuildServiceProvider();
var systeminitializationService = _serviceProvider.GetService<ISystemInitializationService>();
systeminitializationService.TryInitDefaultEnvironment();//初始化环境 DEV TEST STAGE PROD
systeminitializationService.TryInitJwtSecret();//初始化 jwt secret


_settingService = _serviceProvider.GetService<ISettingService>();

Console.WriteLine("Run TestInitialize");
this.GlobalServiceProvider = services.BuildServiceProvider();
}

[TestCleanup]
public void TestCleanup()
{
_settingService.Dispose();
_serviceScope.Dispose();
}

[TestMethod()]
public async Task AddAsyncTest()
Expand Down
Loading

0 comments on commit 03d2148

Please sign in to comment.