Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbra committed Jan 14, 2024
1 parent 2c80f35 commit c33d872
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 48 deletions.
3 changes: 2 additions & 1 deletion src/grate.core/Migration/AnsiSqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@ private static async Task Close(DbConnection? conn)

protected virtual async Task Open(DbConnection? conn)
{
if (conn != null && conn.State != ConnectionState.Open)
//if (conn != null && conn.State != ConnectionState.Open)
if (conn is not null && conn is not { State: ConnectionState.Open or ConnectionState.Connecting })
{
await conn.OpenAsync();
await conn.QueryAsync<string>(_syntax.CurrentDatabase);
Expand Down
6 changes: 3 additions & 3 deletions unittests/MariaDB/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class Database : GenericDatabase, IClassFixture<SimpleService>
protected override IGrateTestContext Context { get; }

protected ITestOutputHelper TestOutput { get; }

public Database(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public Database(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
4 changes: 2 additions & 2 deletions unittests/MariaDB/MigrationTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class MigrationTables : GenericMigrationTables, IClassFixture<SimpleServi

protected ITestOutputHelper TestOutput { get; }

public MigrationTables(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public MigrationTables(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
4 changes: 2 additions & 2 deletions unittests/MariaDB/Running_MigrationScripts/Anytime_scripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class Anytime_scripts : TestCommon.Generic.Running_MigrationScripts.Anyti

protected override ITestOutputHelper TestOutput { get; }

public Anytime_scripts(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public Anytime_scripts(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
4 changes: 2 additions & 2 deletions unittests/MariaDB/Running_MigrationScripts/DropDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class DropDatabase : TestCommon.Generic.Running_MigrationScripts.DropData

protected override ITestOutputHelper TestOutput { get; }

public DropDatabase(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public DropDatabase(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class Environment_scripts : TestCommon.Generic.Running_MigrationScripts.E

protected override ITestOutputHelper TestOutput { get; }

public Environment_scripts(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public Environment_scripts(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public class Everytime_scripts : TestCommon.Generic.Running_MigrationScripts.Eve
protected override IGrateTestContext Context { get; }

protected override ITestOutputHelper TestOutput { get; }

public Everytime_scripts(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public Everytime_scripts(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
4 changes: 2 additions & 2 deletions unittests/MariaDB/Running_MigrationScripts/Failing_Scripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class Failing_Scripts : TestCommon.Generic.Running_MigrationScripts.Faili

protected override ITestOutputHelper TestOutput { get; }

public Failing_Scripts(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public Failing_Scripts(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public class One_time_scripts : TestCommon.Generic.Running_MigrationScripts.One_

protected override ITestOutputHelper TestOutput { get; }

public One_time_scripts(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public One_time_scripts(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class Order_Of_Scripts : TestCommon.Generic.Running_MigrationScripts.Orde

protected override ITestOutputHelper TestOutput { get; }

public Order_Of_Scripts(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public Order_Of_Scripts(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class ScriptsRun_Table : TestCommon.Generic.Running_MigrationScripts.Scri

protected override ITestOutputHelper TestOutput { get; }

public ScriptsRun_Table(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public ScriptsRun_Table(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
6 changes: 3 additions & 3 deletions unittests/MariaDB/Running_MigrationScripts/TokenScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class TokenScripts : TestCommon.Generic.Running_MigrationScripts.TokenScr
protected override IGrateTestContext Context { get; }

protected override ITestOutputHelper TestOutput { get; }

public TokenScripts(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public TokenScripts(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class Versioning_The_Database : TestCommon.Generic.Running_MigrationScrip

protected override ITestOutputHelper TestOutput { get; }

public Versioning_The_Database(MariaDbTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
public Versioning_The_Database(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = new MariaDbGrateTestContext(simpleService.ServiceProvider, testContainer);
Context = testContext;
TestOutput = testOutput;
}
}
62 changes: 55 additions & 7 deletions unittests/MariaDB/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
using grate.Configuration;
using grate.Infrastructure;
using grate.MariaDb.Infrastructure;
using grate.MariaDb.Migration;
using grate.Migration;
using MariaDB.TestInfrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
Expand All @@ -9,12 +15,54 @@ public class Startup
{
public void ConfigureServices(IServiceCollection services, HostBuilderContext context)
{
services.AddLogging(
lb => lb
//.AddSimpleConsole()
.AddXUnit()
.AddConsole()
.SetMinimumLevel(TestConfig.GetLogLevel())
);
services
.AddLogging(
lb => lb
.AddXUnit()
.AddConsole()
.SetMinimumLevel(TestConfig.GetLogLevel())
)
.AddSingleton<MariaDbTestContainer>()
.AddSingleton<IGrateMigrator, GrateMigrator>()
// .AddSingleton<Func<GrateConfiguration, IDbMigrator>>(serviceProvider =>
// {
// using var scope = serviceProvider.CreateScope();
// var provider = scope.ServiceProvider;
//
// IDatabase database = provider.GetRequiredService<IDatabase>();
// ILogger<DbMigrator> logger = provider.GetRequiredService<ILogger<DbMigrator>>();
// IHashGenerator hashGenerator = provider.GetRequiredService<IHashGenerator>();
// return config => new DbMigrator(database, logger, hashGenerator, config);
// })
.AddSingleton<Func<GrateConfiguration, GrateMigrator>>(serviceProvider =>
config =>
{
//using var scope = serviceProvider.CreateScope();
//var provider = scope.ServiceProvider;
var provider = serviceProvider;

var db = provider.GetRequiredService<IDatabase>();
var dbMigrator = new DbMigrator(db, provider.GetRequiredService<ILogger<DbMigrator>>(), new HashGenerator(), config);
var migrator = new GrateMigrator(provider.GetRequiredService<ILogger<GrateMigrator>>(), dbMigrator);

return migrator;

// IDatabase database = provider.GetRequiredService<IDatabase>();
//
// ILogger<DbMigrator> dbLogger = provider.GetRequiredService<ILogger<DbMigrator>>();
// ILogger<GrateMigrator> grateLogger = provider.GetRequiredService<ILogger<GrateMigrator>>();
//
// IHashGenerator hashGenerator = provider.GetRequiredService<IHashGenerator>();
// return config =>
// new GrateMigrator(grateLogger,
// new DbMigrator(database, dbLogger, hashGenerator, config));
})
.AddSingleton<IHashGenerator, HashGenerator>()
.AddSingleton<IGrateTestContext, MariaDbGrateTestContext>()
.AddSingleton<IDatabase, MariaDbDatabase>()
.AddSingleton<ISyntax, MariaDbSyntax>()
.AddSingleton<IDatabaseConnectionFactory, MariaDbConnectionFactory>()
;

}
}
27 changes: 19 additions & 8 deletions unittests/MariaDB/TestInfrastructure/MariaDbGrateTestContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using grate.Configuration;
using grate.Infrastructure;
using grate.MariaDb.Migration;
using grate.Migration;
Expand All @@ -11,22 +12,32 @@ namespace MariaDB.TestInfrastructure;
public class MariaDbGrateTestContext : IGrateTestContext
{
public string AdminPassword => _testContainer.AdminPassword;
public int? Port => _testContainer.TestContainer!.GetMappedPublicPort(MariaDbTestContainer.Port);
public int? Port => _testContainer.TestContainer!.GetMappedPublicPort(_testContainer.Port);
public IServiceProvider ServiceProvider { get; private set; }
//private readonly IGrateMigrator _grateMigrator;
private readonly Func<GrateConfiguration, GrateMigrator> _getGrateMigrator;
private readonly MariaDbTestContainer _testContainer;
private readonly IDatabaseConnectionFactory _databaseConnectionFactory;
public MariaDbGrateTestContext(IServiceProvider serviceProvider, MariaDbTestContainer container)

//public MariaDbGrateTestContext(IServiceProvider serviceProvider, MariaDbTestContainer container)
public MariaDbGrateTestContext(
IServiceProvider serviceProvider,
Func<GrateConfiguration, GrateMigrator> getGrateMigrator,
IDatabase dbMigrator,
ISyntax syntax,
IDatabaseConnectionFactory databaseConnectionFactory,
MariaDbTestContainer container)
{
//ServiceProvider = null!;
ServiceProvider = serviceProvider;
_getGrateMigrator = getGrateMigrator;
_testContainer = container;
DatabaseMigrator = ServiceProvider.GetService<IDatabase>()!;
Syntax = ServiceProvider.GetService<ISyntax>()!;
_databaseConnectionFactory = ServiceProvider.GetService<IDatabaseConnectionFactory>()!;

DatabaseMigrator = dbMigrator;
Syntax = syntax;
_databaseConnectionFactory = databaseConnectionFactory;
}

// public string DockerCommand(string serverName, string adminPassword) =>
// $"run -d --name {serverName} -e MYSQL_ROOT_PASSWORD={adminPassword} -P mariadb:10.5.9";
//public IGrateMigrator GetMigrator(GrateConfiguration config) => _getGrateMigrator(config);

public string AdminConnectionString => $"Server={_testContainer.TestContainer!.Hostname};Port={Port};Database=mysql;Uid=root;Pwd={AdminPassword}";
public string ConnectionString(string database) => $"Server={_testContainer.TestContainer!.Hostname};Port={Port};Database={database};Uid=root;Pwd={AdminPassword}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace MariaDB.TestInfrastructure;
public class MariaDbTestContainer : ContainerFixture
{
public string DockerImage => "mariadb:10.10";
public const int Port = 3306;
public readonly int Port = 3306;

public MariaDbTestContainer(ILogger<MariaDbTestContainer> logger) : base()
{
Expand Down
4 changes: 0 additions & 4 deletions unittests/TestCommon/TestInfrastructure/IGrateTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ DefaultConfiguration with

public IGrateMigrator GetMigrator(GrateConfiguration config)
{
// var factory = Substitute.For<IFactory>();
// factory
// .GetService<string, IDatabase>(DatabaseType)
// .Returns(DatabaseMigrator);
var db = ServiceProvider.GetRequiredService<IDatabase>();
var dbMigrator = new DbMigrator(db, ServiceProvider.GetRequiredService<ILogger<DbMigrator>>(), new HashGenerator(), config);
var migrator = new GrateMigrator(ServiceProvider.GetRequiredService<ILogger<GrateMigrator>>(), dbMigrator);
Expand Down

0 comments on commit c33d872

Please sign in to comment.