Skip to content

Commit

Permalink
Use testcontainers to run mysql intergration testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Jan 23, 2024
1 parent dda52c8 commit e054b2b
Show file tree
Hide file tree
Showing 32 changed files with 281 additions and 185 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Testcontainers.MySql" Version="3.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using AgileConfig.Server.ServiceTests.sqlite;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.PostgreSQL
{
Expand All @@ -9,13 +10,14 @@ public class AppServiceTests_pg : AppServiceTests
{
string conn = "Host=192.168.0.125;Port=15432;Database=agileconfig;Username=postgres;Password=123456";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "npgsql";
var dict = new Dictionary<string, string>();
dict["db:provider"] = "pg";
dict["db:conn"] = conn;

return dict;

return Task.FromResult(dict);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AgileConfig.Server.ServiceTests.sqlite;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.PostgreSQL
{
Expand All @@ -9,13 +10,15 @@ public class ConfigServiceTests_pg: ConfigServiceTests
{
string conn = "Host=192.168.0.125;Port=15432;Database=agileconfig;Username=postgres;Password=123456";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "npgsql";
dict["db:conn"] = conn;

return dict;
return
Task.FromResult(
new Dictionary<string, string>
{
{"db:provider","pg" },
{"db:conn",conn }
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using AgileConfig.Server.ServiceTests.sqlite;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.PostgreSQL
{
Expand All @@ -11,13 +12,15 @@ public class ServerNodeServiceTests_pg: ServerNodeServiceTests
{
string conn = "Host=192.168.0.125;Port=15432;Database=agileconfig;Username=postgres;Password=123456";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "npgsql";
dict["db:conn"] = conn;

return dict;
return
Task.FromResult(
new Dictionary<string, string>
{
{"db:provider","pg" },
{"db:conn",conn }
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using AgileConfig.Server.ServiceTests.sqlite;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.PostgreSQL
{
Expand All @@ -9,13 +10,15 @@ public class SettingServiceTests_pg : SettingServiceTests
{
string conn = "Host=192.168.0.125;Port=15432;Database=agileconfig;Username=postgres;Password=123456";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "npgsql";
dict["db:conn"] = conn;

return dict;
return
Task.FromResult(
new Dictionary<string, string>
{
{"db:provider","pg" },
{"db:conn",conn }
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using AgileConfig.Server.ServiceTests.sqlite;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.PostgreSQL
{
Expand All @@ -10,13 +11,15 @@ public class SysLogServiceTests_pg: SysLogServiceTests
{
string conn = "Host=192.168.0.125;Port=15432;Database=agileconfig;Username=postgres;Password=123456";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "npgsql";
dict["db:conn"] = conn;

return dict;
return
Task.FromResult(
new Dictionary<string, string>
{
{"db:provider","pg" },
{"db:conn",conn }
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using AgileConfig.Server.ServiceTests.sqlite;
using AgileConfig.Server.Data.Repository.Mongodb;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.mongodb
{
Expand All @@ -20,13 +21,13 @@ public override void ClearData()

string conn = "mongodb://192.168.0.125:27017/agile_config_1";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
var dict = new Dictionary<string, string>();
dict["db:provider"] = "mongodb";
dict["db:conn"] = conn;

return dict;
return Task.FromResult(dict);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AgileConfig.Server.ServiceTests.sqlite;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.mongodb
{
Expand Down Expand Up @@ -49,13 +50,15 @@ public override void ClearData()

string conn = "mongodb://192.168.0.125:27017/agile_config_1";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "mongodb";
dict["db:conn"] = conn;

return dict;
return
Task.FromResult(
new Dictionary<string, string>
{
{"db:provider","mongodb" },
{"db:conn",conn }
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using AgileConfig.Server.ServiceTests.sqlite;
using AgileConfig.Server.Data.Repository.Mongodb;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.mongodb
{
Expand All @@ -22,13 +23,15 @@ public override void ClearData()

string conn = "mongodb://192.168.0.125:27017/agile_config_1";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "mongodb";
dict["db:conn"] = conn;

return dict;
return
Task.FromResult(
new Dictionary<string, string>
{
{"db:provider","mongodb" },
{"db:conn",conn }
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using AgileConfig.Server.ServiceTests.sqlite;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.mongodb
{
Expand All @@ -13,13 +14,15 @@ public override void ClearData()

string conn = "mongodb://192.168.0.125:27017/agile_config_1";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "mongodb";
dict["db:conn"] = conn;

return dict;
return
Task.FromResult(
new Dictionary<string, string>
{
{"db:provider","mongodb" },
{"db:conn",conn }
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ public override void ClearData()

string conn = "mongodb://192.168.0.125:27017/agile_config_1";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "mongodb";
dict["db:conn"] = conn;

return dict;
return
Task.FromResult(
new Dictionary<string, string>
{
{"db:provider","mongodb" },
{"db:conn",conn }
});
}
}
31 changes: 26 additions & 5 deletions test/AgileConfig.Server.ServiceTests/mysql/AppServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AgileConfig.Server.ServiceTests.sqlite;
using System.Collections.Generic;
using Testcontainers.MySql;
using System.Threading.Tasks;
using System;

namespace AgileConfig.Server.ServiceTests.mysql
{
[TestClass()]
public class AppServiceTests_mysql : AppServiceTests
{
string conn = "Database=agile_config_test;Data Source=192.168.0.125;User Id=root;Password=x;port=13306";
static MySqlContainer _container = new MySqlBuilder().WithImage("mysql:8.0").Build();

public override Dictionary<string, string> GetConfigurationData()
[ClassInitialize]
public static async Task ClassInit(TestContext testContext)
{
var dict = base.GetConfigurationData();
await _container.StartAsync();
Console.WriteLine($"MySqlContainer started");
}

[ClassCleanup]
public static async Task ClassCleanup()
{
await _container.DisposeAsync();
Console.WriteLine($"MySqlContainer dispose");
}


public override Task<Dictionary<string, string>> GetConfigurationData()
{
var connstr = _container.GetConnectionString();
Console.WriteLine($"MySqlContainer connstr: {connstr}");

var dict = new Dictionary<string, string>();
dict["db:provider"] = "mysql";
dict["db:conn"] = conn;
dict["db:conn"] = connstr;

return dict;
return Task.FromResult(dict);
}

}
Expand Down
15 changes: 9 additions & 6 deletions test/AgileConfig.Server.ServiceTests/mysql/ConfigServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using AgileConfig.Server.ServiceTests.sqlite;
using System.Threading.Tasks;

namespace AgileConfig.Server.ServiceTests.mysql
{
Expand All @@ -9,13 +10,15 @@ public class ConfigServiceTests_mysql : ConfigServiceTests
{
string conn = "Database=agile_config_test;Data Source=192.168.0.125;User Id=root;Password=x;port=13306";

public override Dictionary<string, string> GetConfigurationData()
public override Task<Dictionary<string, string>> GetConfigurationData()
{
var dict = base.GetConfigurationData();
dict["db:provider"] = "mysql";
dict["db:conn"] = conn;

return dict;
return
Task.FromResult(
new Dictionary<string, string>
{
{"db:provider","mysql" },
{"db:conn",conn }
});
}
}
}
Loading

0 comments on commit e054b2b

Please sign in to comment.