Added initial test cases. Fixed circle search provider logic.
This commit is contained in:
89
JSMR.Tests/Integration/MariaDbFixture.cs
Normal file
89
JSMR.Tests/Integration/MariaDbFixture.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using DotNet.Testcontainers.Builders;
|
||||
using JSMR.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Testcontainers.MariaDb;
|
||||
|
||||
namespace JSMR.Tests.Integration;
|
||||
|
||||
public sealed class MariaDbFixture : IAsyncLifetime
|
||||
{
|
||||
public MariaDbContainer? MariaDbContainer { get; private set; }
|
||||
|
||||
public string ConnectionString { get; private set; } = default!;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
MariaDbContainer = new MariaDbBuilder()
|
||||
.WithImage("mariadb:10.11.6")
|
||||
.WithEnvironment("MARIADB_ROOT_PASSWORD", "rootpwd")
|
||||
.WithEnvironment("MARIADB_DATABASE", "appdb")
|
||||
.WithPortBinding(3307, 3306) // host:container; avoid conflicts
|
||||
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(3306))
|
||||
.Build();
|
||||
|
||||
await MariaDbContainer.StartAsync();
|
||||
|
||||
ConnectionString =
|
||||
"Server=localhost;Port=3307;Database=appdb;User=root;Password=rootpwd;SslMode=None;AllowPublicKeyRetrieval=True;";
|
||||
|
||||
//ConnectionString = MariaDbContainer.GetConnectionString();
|
||||
|
||||
// Run migrations here to create schema
|
||||
await using AppDbContext context = CreateDbContext();
|
||||
await context.Database.EnsureCreatedAsync();
|
||||
//await context.Database.MigrateAsync();
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
if (MariaDbContainer is not null)
|
||||
{
|
||||
await MariaDbContainer.StopAsync();
|
||||
await MariaDbContainer.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public AppDbContext CreateDbContext()
|
||||
{
|
||||
MySqlServerVersion serverVersion = new(new Version(10, 11, 6));
|
||||
|
||||
DbContextOptions<AppDbContext> options = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseMySql(ConnectionString, serverVersion,
|
||||
o => o.EnableRetryOnFailure())
|
||||
.EnableSensitiveDataLogging()
|
||||
.Options;
|
||||
|
||||
return new AppDbContext(options);
|
||||
}
|
||||
|
||||
/// <summary>Clean tables between tests; use Respawn or manual TRUNCATE in correct FK order.</summary>
|
||||
public async Task ResetAsync()
|
||||
{
|
||||
await using AppDbContext context = CreateDbContext();
|
||||
|
||||
await context.Database.EnsureDeletedAsync();
|
||||
await context.Database.EnsureCreatedAsync();
|
||||
|
||||
//await using var connection = context.Database.GetDbConnection();
|
||||
//await connection.OpenAsync();
|
||||
|
||||
//using var cmd = connection.CreateCommand();
|
||||
//cmd.CommandText = "SELECT DATABASE()";
|
||||
|
||||
//var dbName = (string?)await cmd.ExecuteScalarAsync();
|
||||
//Console.WriteLine($"[TEST] Connected to DB: {dbName}");
|
||||
|
||||
// Fast reset (example): disable FK checks, truncate, re-enable
|
||||
//await context.Database.ExecuteSqlRawAsync("SET FOREIGN_KEY_CHECKS = 0;");
|
||||
//await context.Database.ExecuteSqlRawAsync("TRUNCATE TABLE voice_work_creators;");
|
||||
//await context.Database.ExecuteSqlRawAsync("TRUNCATE TABLE voice_work_tags;");
|
||||
//await context.Database.ExecuteSqlRawAsync("TRUNCATE TABLE english_tags;");
|
||||
//await context.Database.ExecuteSqlRawAsync("TRUNCATE TABLE english_voice_works;");
|
||||
//await context.Database.ExecuteSqlRawAsync("TRUNCATE TABLE voice_work_searches;");
|
||||
//await context.Database.ExecuteSqlRawAsync("TRUNCATE TABLE voice_works;");
|
||||
//await context.Database.ExecuteSqlRawAsync("TRUNCATE TABLE creators;");
|
||||
//await context.Database.ExecuteSqlRawAsync("TRUNCATE TABLE tags;");
|
||||
//await context.Database.ExecuteSqlRawAsync("TRUNCATE TABLE circles;");
|
||||
//await context.Database.ExecuteSqlRawAsync("SET FOREIGN_KEY_CHECKS = 1;");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user