diff --git a/test/AgileConfig.Server.ServiceTests/mongodb/ConfigServiceTests.cs b/test/AgileConfig.Server.ServiceTests/mongodb/ConfigServiceTests.cs index 0e5e0477..e40667fd 100644 --- a/test/AgileConfig.Server.ServiceTests/mongodb/ConfigServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/mongodb/ConfigServiceTests.cs @@ -1,45 +1,47 @@ -using AgileConfig.Server.Data.Entity; -using AgileConfig.Server.Data.Repository.Mongodb; +using AgileConfig.Server.Data.Repository.Mongodb; using AgileConfig.Server.ServiceTests.sqlite; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using System.Collections.Generic; using System.Threading.Tasks; +using Testcontainers.MongoDb; namespace AgileConfig.Server.ServiceTests.mongodb { + [TestClass()] public class ConfigServiceTests_mongo : ConfigServiceTests { public override void ClearData() { - var repository = new ConfigRepository(conn); + var repository = new ConfigRepository(GetConn()); var entities = repository.AllAsync().Result; foreach (var entity in entities) { repository.DeleteAsync(entity).Wait(); } - var configPublishedRepository = new ConfigPublishedRepository(conn); + var configPublishedRepository = new ConfigPublishedRepository(GetConn()); var configPublisheds = configPublishedRepository.AllAsync().Result; foreach (var entity in configPublisheds) { configPublishedRepository.DeleteAsync(entity).Wait(); } - var detailRepository = new PublishDetailRepository(conn); + var detailRepository = new PublishDetailRepository(GetConn()); var details = detailRepository.AllAsync().Result; foreach (var entity in details) { detailRepository.DeleteAsync(entity).Wait(); } - var app_repository = new AppRepository(conn); + var app_repository = new AppRepository(GetConn()); var apps = app_repository.AllAsync().Result; foreach (var entity in apps) { app_repository.DeleteAsync(entity).Wait(); } - var appref_repository = new AppInheritancedRepository(conn); + var appref_repository = new AppInheritancedRepository(GetConn()); var apprefs = appref_repository.AllAsync().Result; foreach (var entity in apprefs) { @@ -47,17 +49,37 @@ public override void ClearData() } } - string conn = "mongodb://192.168.0.125:27017/agile_config_1"; + static MongoDbContainer _container = new MongoDbBuilder().WithImage("mongo:6.0").Build(); + + [ClassInitialize] + public static async Task ClassInit(TestContext testContext) + { + await _container.StartAsync(); + Console.WriteLine($"MongoDbContainer started"); + } + + [ClassCleanup] + public static async Task ClassCleanup() + { + await _container.DisposeAsync(); + Console.WriteLine($"MongoDbContainer dispose"); + } + + private string GetConn() + { + return _container.GetConnectionString(); + } public override Task> GetConfigurationData() { - return - Task.FromResult( - new Dictionary - { - {"db:provider","mongodb" }, - {"db:conn",conn } - }); + var connstr = GetConn(); + Console.WriteLine($"MongoDbContainer connstr: {connstr}"); + + var dict = new Dictionary(); + dict["db:provider"] = "mongodb"; + dict["db:conn"] = connstr; + + return Task.FromResult(dict); } } } \ No newline at end of file diff --git a/test/AgileConfig.Server.ServiceTests/mongodb/ServerNodeServiceTests.cs b/test/AgileConfig.Server.ServiceTests/mongodb/ServerNodeServiceTests.cs index 89ba262f..7d71a838 100644 --- a/test/AgileConfig.Server.ServiceTests/mongodb/ServerNodeServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/mongodb/ServerNodeServiceTests.cs @@ -1,18 +1,19 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using AgileConfig.Server.Service; using System; using System.Collections.Generic; using AgileConfig.Server.ServiceTests.sqlite; using AgileConfig.Server.Data.Repository.Mongodb; using System.Threading.Tasks; +using Testcontainers.MongoDb; namespace AgileConfig.Server.ServiceTests.mongodb { + [TestClass()] public class ServerNodeServiceTests_mongo: ServerNodeServiceTests { public override void ClearData() { - var repository = new ServerNodeRepository(conn); + var repository = new ServerNodeRepository(_container.GetConnectionString()); var entities = repository.AllAsync().Result; foreach (var log in entities) { @@ -20,17 +21,33 @@ public override void ClearData() } } - string conn = "mongodb://192.168.0.125:27017/agile_config_1"; + static MongoDbContainer _container = new MongoDbBuilder().WithImage("mongo:6.0").Build(); + + [ClassInitialize] + public static async Task ClassInit(TestContext testContext) + { + await _container.StartAsync(); + Console.WriteLine($"MongoDbContainer started"); + } + + [ClassCleanup] + public static async Task ClassCleanup() + { + await _container.DisposeAsync(); + Console.WriteLine($"MongoDbContainer dispose"); + } + public override Task> GetConfigurationData() { - return - Task.FromResult( - new Dictionary - { - {"db:provider","mongodb" }, - {"db:conn",conn } - }); + var connstr = _container.GetConnectionString(); + Console.WriteLine($"MongoDbContainer connstr: {connstr}"); + + var dict = new Dictionary(); + dict["db:provider"] = "mongodb"; + dict["db:conn"] = connstr; + + return Task.FromResult(dict); } } } \ No newline at end of file diff --git a/test/AgileConfig.Server.ServiceTests/mongodb/SettingServiceTests.cs b/test/AgileConfig.Server.ServiceTests/mongodb/SettingServiceTests.cs index 9a7a7fc8..82197c1e 100644 --- a/test/AgileConfig.Server.ServiceTests/mongodb/SettingServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/mongodb/SettingServiceTests.cs @@ -2,26 +2,44 @@ using System.Collections.Generic; using AgileConfig.Server.ServiceTests.sqlite; using System.Threading.Tasks; +using System; +using Testcontainers.MongoDb; namespace AgileConfig.Server.ServiceTests.mongodb { + [TestClass()] public class SettingServiceTests_mongo : SettingServiceTests { public override void ClearData() { } + static MongoDbContainer _container = new MongoDbBuilder().WithImage("mongo:6.0").Build(); + + [ClassInitialize] + public static async Task ClassInit(TestContext testContext) + { + await _container.StartAsync(); + Console.WriteLine($"MongoDbContainer started"); + } + + [ClassCleanup] + public static async Task ClassCleanup() + { + await _container.DisposeAsync(); + Console.WriteLine($"MongoDbContainer dispose"); + } - string conn = "mongodb://192.168.0.125:27017/agile_config_1"; public override Task> GetConfigurationData() { - return - Task.FromResult( - new Dictionary - { - {"db:provider","mongodb" }, - {"db:conn",conn } - }); + var connstr = _container.GetConnectionString(); + Console.WriteLine($"MongoDbContainer connstr: {connstr}"); + + var dict = new Dictionary(); + dict["db:provider"] = "mongodb"; + dict["db:conn"] = connstr; + + return Task.FromResult(dict); } } } \ No newline at end of file diff --git a/test/AgileConfig.Server.ServiceTests/mongodb/SysLogServiceTests.cs b/test/AgileConfig.Server.ServiceTests/mongodb/SysLogServiceTests.cs index 027429f4..1a19cb0b 100644 --- a/test/AgileConfig.Server.ServiceTests/mongodb/SysLogServiceTests.cs +++ b/test/AgileConfig.Server.ServiceTests/mongodb/SysLogServiceTests.cs @@ -1,20 +1,19 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; -using AgileConfig.Server.Data.Abstraction; -using AgileConfig.Server.Data.Entity; using AgileConfig.Server.Data.Repository.Mongodb; -using AgileConfig.Server.IService; using AgileConfig.Server.ServiceTests.sqlite; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Testcontainers.MongoDb; namespace AgileConfig.Server.ServiceTests.mongodb; +[TestClass()] public class SysLogServiceTests_mongo : SysLogServiceTests { public override void ClearData() { - var repository = new SysLogRepository(conn); + var repository = new SysLogRepository(_container.GetConnectionString()); var syslogs = repository.AllAsync().Result; foreach (var log in syslogs) { @@ -22,16 +21,32 @@ public override void ClearData() } } - string conn = "mongodb://192.168.0.125:27017/agile_config_1"; + static MongoDbContainer _container = new MongoDbBuilder().WithImage("mongo:6.0").Build(); + + [ClassInitialize] + public static async Task ClassInit(TestContext testContext) + { + await _container.StartAsync(); + Console.WriteLine($"MongoDbContainer started"); + } + + [ClassCleanup] + public static async Task ClassCleanup() + { + await _container.DisposeAsync(); + Console.WriteLine($"MongoDbContainer dispose"); + } + public override Task> GetConfigurationData() { - return - Task.FromResult( - new Dictionary - { - {"db:provider","mongodb" }, - {"db:conn",conn } - }); + var connstr = _container.GetConnectionString(); + Console.WriteLine($"MongoDbContainer connstr: {connstr}"); + + var dict = new Dictionary(); + dict["db:provider"] = "mongodb"; + dict["db:conn"] = connstr; + + return Task.FromResult(dict); } } \ No newline at end of file