Skip to content

Commit

Permalink
Cleaning up tests - finished PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
erikbra committed Jan 18, 2024
1 parent 34e66d4 commit 661ac85
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 108 deletions.
11 changes: 3 additions & 8 deletions unittests/MariaDB/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
namespace MariaDB;

[Collection(nameof(MariaDbTestContainer))]
public class Database : GenericDatabase, IClassFixture<SimpleService>
{
public Database(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = testContext;
TestOutput = testOutput;
}
}
public class Database(IGrateTestContext testContext, ITestOutputHelper testOutput)
: GenericDatabase(testContext, testOutput);

24 changes: 2 additions & 22 deletions unittests/MariaDB/MariaDbDatabase_.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
using System.Data.Common;
using FluentAssertions;
using FluentAssertions;
using grate.Configuration;
using grate.MariaDb.Migration;
using MariaDB.TestInfrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MySqlConnector;

namespace Basic_tests.Infrastructure.MariaDB;

// ReSharper disable once InconsistentNaming
public class MariaDbDatabase_ : IClassFixture<SimpleService>
public class MariaDbDatabase_(InspectableMariaDbDatabase mariaDb)
{
private IServiceProvider _serviceProvider;

public MariaDbDatabase_(SimpleService simpleService)
{
_serviceProvider = simpleService.ServiceProvider;
}
[Fact]
public async Task Disables_pipelining_if_not_explicitly_set_in_connection_string()
{
var connStr = "Server=dummy";
var cfg = new GrateConfiguration() { ConnectionString = connStr };
var mariaDb = new InspectableMariaDbDatabase(_serviceProvider);
await mariaDb.InitializeConnections(cfg);

var conn = mariaDb.GetConnection();
Expand All @@ -36,20 +25,11 @@ public async Task Leaves_pipelining_as_configured_if_set_explicitly_in_connectio
{
var connStr = "Server=dummy;Pipelining=true";
var cfg = new GrateConfiguration() { ConnectionString = connStr };
var mariaDb = new InspectableMariaDbDatabase(_serviceProvider);
await mariaDb.InitializeConnections(cfg);

var conn = mariaDb.GetConnection();
var builder = new MySqlConnectionStringBuilder(conn.ConnectionString);
builder.Pipelining.Should().BeTrue();
}

private class InspectableMariaDbDatabase : MariaDbDatabase
{
public InspectableMariaDbDatabase(IServiceProvider serviceProvider) : base(serviceProvider.GetRequiredService<ILogger<InspectableMariaDbDatabase>>())
{
}

public DbConnection GetConnection() => base.Connection;
}
}
17 changes: 2 additions & 15 deletions unittests/MariaDB/MigrationTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,5 @@
namespace MariaDB;

[Collection(nameof(MariaDbTestContainer))]
public class MigrationTables : GenericMigrationTables, IClassFixture<SimpleService>
{
public MigrationTables(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = testContext;
TestOutput = testOutput;
}

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

}
public class MigrationTables(IGrateTestContext testContext, ITestOutputHelper testOutput) :
GenericMigrationTables(testContext, testOutput);
12 changes: 4 additions & 8 deletions unittests/MariaDB/ServiceCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@
namespace MariaDB.DependencyInjection;

[Collection(nameof(MariaDbTestContainer))]
public class ServiceCollectionTest : TestCommon.DependencyInjection.GrateServiceCollectionTest
// ReSharper disable once UnusedType.Global
public class ServiceCollectionTest(MariaDbTestContainer mariaDbTestContainer)
: TestCommon.DependencyInjection.GrateServiceCollectionTest
{
private readonly MariaDbTestContainer _mariaDbTestContainer;

public ServiceCollectionTest(MariaDbTestContainer mariaDbTestContainer)
{
_mariaDbTestContainer = mariaDbTestContainer; ;
}
protected override void ConfigureService(GrateConfigurationBuilder grateConfigurationBuilder)
{
var connectionString = $"Server={_mariaDbTestContainer.TestContainer!.Hostname};Port={_mariaDbTestContainer.TestContainer!.GetMappedPublicPort(_mariaDbTestContainer.Port)};Database={TestConfig.RandomDatabase()};Uid=root;Pwd={_mariaDbTestContainer.AdminPassword}";
var connectionString = $"Server={mariaDbTestContainer.TestContainer!.Hostname};Port={mariaDbTestContainer.TestContainer!.GetMappedPublicPort(mariaDbTestContainer.Port)};Database={TestConfig.RandomDatabase()};Uid=root;Pwd={mariaDbTestContainer.AdminPassword}";
grateConfigurationBuilder.WithConnectionString(connectionString);
grateConfigurationBuilder.UseMariaDb();
grateConfigurationBuilder.ServiceCollection.AddSingleton<IDatabaseConnectionFactory, MariaDbConnectionFactory>();
Expand Down
2 changes: 2 additions & 0 deletions unittests/MariaDB/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Basic_tests.Infrastructure.MariaDB;
using grate.Configuration;
using grate.Infrastructure;
using grate.MariaDb.Infrastructure;
Expand Down Expand Up @@ -47,6 +48,7 @@ public void ConfigureServices(IServiceCollection services, HostBuilderContext co
.AddTransient<IDatabase, MariaDbDatabase>()
.AddSingleton<ISyntax, MariaDbSyntax>()
.AddSingleton<IDatabaseConnectionFactory, MariaDbConnectionFactory>()
.AddTransient<InspectableMariaDbDatabase>()
;

}
Expand Down
16 changes: 16 additions & 0 deletions unittests/MariaDB/TestInfrastructure/InspectableMariaDbDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Data.Common;
using grate.MariaDb.Migration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace MariaDB.TestInfrastructure;

public class InspectableMariaDbDatabase : MariaDbDatabase
{
public InspectableMariaDbDatabase(IServiceProvider serviceProvider)
: base(serviceProvider.GetRequiredService<ILogger<InspectableMariaDbDatabase>>())
{
}

public DbConnection GetConnection() => base.Connection;
}
18 changes: 2 additions & 16 deletions unittests/PostgreSQL/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,5 @@
namespace PostgreSQL;

[Collection(nameof(PostgreSqlTestContainer))]
public class Database : GenericDatabase, IClassFixture<SimpleService>
{

// public Database(PostgreSqlTestContainer testContainer, SimpleService simpleService, ITestOutputHelper testOutput)
// {
// Context = new PostgreSqlGrateTestContext(simpleService.ServiceProvider, testContainer);
// TestOutput = testOutput;
// }

public Database(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = testContext;
TestOutput = testOutput;
}

}
public class Database(IGrateTestContext testContext, ITestOutputHelper testOutput) :
GenericDatabase(testContext, testOutput);
13 changes: 3 additions & 10 deletions unittests/PostgreSQL/MigrationTables.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
using PostgreSQL.TestInfrastructure;
using TestCommon.Generic;
using TestCommon.Generic;
using TestCommon.TestInfrastructure;

namespace PostgreSQL;

[Collection(nameof(PostgreSqlTestContainer))]
public class MigrationTables : GenericMigrationTables, IClassFixture<SimpleService>
{
public MigrationTables(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = testContext;
TestOutput = testOutput;
}
}
public class MigrationTables(IGrateTestContext testContext, ITestOutputHelper testOutput) :
GenericMigrationTables(testContext, testOutput);
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ namespace PostgreSQL.Running_MigrationScripts;

[Collection(nameof(PostgreSqlTestContainer))]
// ReSharper disable once InconsistentNaming
public class Failing_Scripts : TestCommon.Generic.Running_MigrationScripts.Failing_Scripts
public class Failing_Scripts(IGrateTestContext testContext, ITestOutputHelper testOutput)
: TestCommon.Generic.Running_MigrationScripts.Failing_Scripts(testContext, testOutput)
{
public Failing_Scripts(IGrateTestContext testContext, ITestOutputHelper testOutput)
{
Context = testContext;
TestOutput = testOutput;
}

protected override string ExpectedErrorMessageForInvalidSql =>
@"42703: column ""top"" does not exist
Expand Down
10 changes: 3 additions & 7 deletions unittests/PostgreSQL/ServiceCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@
namespace PostgreSQL.DependencyInjection;

[Collection(nameof(PostgreSqlTestContainer))]
public class ServiceCollectionTest : TestCommon.DependencyInjection.GrateServiceCollectionTest
public class ServiceCollectionTest(PostgreSqlTestContainer postgreSqlTestContainer)
: TestCommon.DependencyInjection.GrateServiceCollectionTest
{
private readonly PostgreSqlTestContainer _postgreSqlTestContainer;
public ServiceCollectionTest(PostgreSqlTestContainer postgreSqlTestContainer)
{
_postgreSqlTestContainer = postgreSqlTestContainer;
}
protected override void ConfigureService(GrateConfigurationBuilder grateConfigurationBuilder)
{
var connectionString = $"Host={_postgreSqlTestContainer.TestContainer!.Hostname};Port={_postgreSqlTestContainer.TestContainer!.GetMappedPublicPort(_postgreSqlTestContainer.Port)};Database={TestConfig.RandomDatabase()};Username=postgres;Password={_postgreSqlTestContainer.AdminPassword};Include Error Detail=true;Pooling=false";
var connectionString = $"Host={postgreSqlTestContainer.TestContainer!.Hostname};Port={postgreSqlTestContainer.TestContainer!.GetMappedPublicPort(postgreSqlTestContainer.Port)};Database={TestConfig.RandomDatabase()};Username=postgres;Password={postgreSqlTestContainer.AdminPassword};Include Error Detail=true;Pooling=false";
grateConfigurationBuilder.WithConnectionString(connectionString);
grateConfigurationBuilder.UsePostgreSql();
grateConfigurationBuilder.ServiceCollection.AddSingleton<IDatabaseConnectionFactory, PostgreSqlConnectionFactory>();
Expand Down
6 changes: 6 additions & 0 deletions unittests/PostgreSQL/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public void ConfigureServices(IServiceCollection services, HostBuilderContext co
.AddTransient<IDatabase, PostgreSqlDatabase>()
.AddSingleton<ISyntax, PostgreSqlSyntax>()
.AddSingleton<IDatabaseConnectionFactory, PostgreSqlConnectionFactory>()
.AddSingleton<StatementSplitter>()
.AddSingleton(service =>
{
var database = service.GetService<IDatabase>()!;
return new StatementSplitter(database.StatementSeparatorRegex);
});
;

}
Expand Down
22 changes: 7 additions & 15 deletions unittests/PostgreSQL/Statement_Splitting/StatementSplitter_.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@ namespace Basic_tests.Infrastructure.PostgreSQL.Statement_Splitting;


// ReSharper disable once InconsistentNaming
public class StatementSplitter_ : IClassFixture<SimpleService>
public class StatementSplitter_(StatementSplitter statementSplitter)
{
private StatementSplitter Splitter;

public StatementSplitter_(StatementSplitter statementSplitter)
{
Splitter = statementSplitter;
}


[Fact]
public void Splits_and_removes_semicolons()
{
Expand All @@ -35,7 +27,7 @@ USING btree
column2
);
";
var batches = Splitter.Split(original);
var batches = statementSplitter.Split(original);

batches.Should().HaveCount(4);
}
Expand All @@ -57,7 +49,7 @@ FROM information_schema.schemata
END
';
";
var batches = Splitter.Split(original);
var batches = statementSplitter.Split(original);

batches.Should().HaveCount(1);
}
Expand All @@ -82,15 +74,15 @@ FROM information_schema.schemata
END
{tag};
";
var batches = Splitter.Split(original);
var batches = statementSplitter.Split(original);
batches.Should().HaveCount(1);
}

[Fact]
public void Splits_on_semicolon_after_single_quotes_when_there_is_another_semicolon_in_the_quote()
{
var original = @"SELECT 1 WHERE whatnot = '; ' ; MOO";
var batches = Splitter.Split(original).ToList();
var batches = statementSplitter.Split(original).ToList();
batches.Should().HaveCount(2);

batches.First().Should().Be("SELECT 1 WHERE whatnot = '; ' ");
Expand All @@ -101,7 +93,7 @@ public void Splits_on_semicolon_after_single_quotes_when_there_is_another_semico
public void Ignores_semicolon_in_single_quotes_when_there_is_no_other_semicolon()
{
var original = @"SELECT 1 WHERE whatnot = '; '";
var batches = Splitter.Split(original);
var batches = statementSplitter.Split(original);
batches.Should().HaveCount(1);
}

Expand All @@ -122,7 +114,7 @@ FROM information_schema.schemata
END
';
";
var batches = Splitter.Split(original);
var batches = statementSplitter.Split(original);

batches.Should().HaveCount(1);
}
Expand Down

0 comments on commit 661ac85

Please sign in to comment.