-
Notifications
You must be signed in to change notification settings - Fork 8
/
DatabaseFixture.cs
84 lines (64 loc) · 2.4 KB
/
DatabaseFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.Threading.Tasks;
using FluentCommand.Caching;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Testcontainers.MsSql;
using Testcontainers.Redis;
using Xunit;
using XUnit.Hosting;
namespace FluentCommand.SqlServer.Tests;
public class DatabaseFixture : TestHostFixture, IAsyncLifetime
{
private readonly MsSqlContainer _msSqlContainer = new MsSqlBuilder()
.WithImage("mcr.microsoft.com/mssql/server:2022-latest")
.WithPassword("!P@ss0rd")
.Build();
private readonly RedisContainer _redisContainer = new RedisBuilder()
.Build();
public async Task InitializeAsync()
{
await _msSqlContainer.StartAsync();
await _redisContainer.StartAsync();
}
public async Task DisposeAsync()
{
await _msSqlContainer.DisposeAsync();
await _redisContainer.DisposeAsync();
}
protected override void ConfigureLogging(HostBuilderContext context, ILoggingBuilder builder)
{
base.ConfigureLogging(context, builder);
builder.SetMinimumLevel(LogLevel.Debug);
}
protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
// change database from container default
var connectionBuilder = new SqlConnectionStringBuilder(_msSqlContainer.GetConnectionString())
{
InitialCatalog = "TrackerDocker"
};
var trackerConnection = connectionBuilder.ToString();
var cacheConnection = _redisContainer.GetConnectionString();
services.AddHostedService<DatabaseInitializer>();
services.AddStackExchangeRedisCache(options =>
{
options.Configuration = cacheConnection;
options.InstanceName = "FluentCommand";
});
services.AddFluentCommand(builder => builder
.UseConnectionString(trackerConnection)
.UseSqlServer()
.AddDistributedDataCache()
);
// readonly intent connection
connectionBuilder.ApplicationIntent = ApplicationIntent.ReadOnly;
var readOnlyConnection = connectionBuilder.ToString();
services.AddFluentCommand<ReadOnlyIntent>(builder => builder
.UseConnectionString(readOnlyConnection)
.UseSqlServer()
.AddDistributedDataCache()
);
}
}