Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHabenicht committed Oct 22, 2022
1 parent 08e7fa5 commit af9043e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class MySqlTestcontainerConfiguration : TestcontainerDatabaseConfiguratio

private const int MySqlPort = 3306;

private const string RootUsername = "root";
private string _username = "root";

/// <summary>
/// Initializes a new instance of the <see cref="MySqlTestcontainerConfiguration" /> class.
/// </summary>
Expand Down Expand Up @@ -39,15 +42,36 @@ public override string Database
/// <inheritdoc />
public override string Username
{
get => this.Environments["MYSQL_USER"];
set => this.Environments["MYSQL_USER"] = value;
get {
return this._username;
}
set {
this._username = value;
if(value != RootUsername) {
this.Environments["MYSQL_USER"] = value;
} else {
// No need for setting a username, as it is defined as root for mysql.
}
}
}

/// <inheritdoc />
public override string Password
{
get => this.Environments["MYSQL_PASSWORD"];
set => this.Environments["MYSQL_PASSWORD"] = value;
get {
if(this.Environments.ContainsKey("MYSQL_USER")) {
return this.Environments["MYSQL_PASSWORD"];
} else {
return this.Environments["MYSQL_ROOT_PASSWORD"];
}
}
set {
if(this._username == RootUsername) {
this.Environments["MYSQL_ROOT_PASSWORD"] = value;
} else {
this.Environments["MYSQL_PASSWORD"] = value;
}
}
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace DotNet.Testcontainers.Tests.Fixtures
using JetBrains.Annotations;
using MySqlConnector;

[UsedImplicitly]
public sealed class MySqlFixture : DatabaseFixture<MySqlTestcontainer, DbConnection>
public abstract class MySqlBaseFixture : DatabaseFixture<MySqlTestcontainer, DbConnection>
{
private readonly TestcontainerDatabaseConfiguration configuration = new MySqlTestcontainerConfiguration { Database = "db", Username = "mysql", Password = "mysql" };
private readonly TestcontainerDatabaseConfiguration configuration;

public MySqlFixture()
public MySqlBaseFixture(string username, string password)
{
this.configuration = new MySqlTestcontainerConfiguration { Database = "db", Username = username, Password = password };
this.Container = new TestcontainersBuilder<MySqlTestcontainer>()
.WithDatabase(this.configuration)
.Build();
Expand All @@ -41,4 +41,20 @@ public override void Dispose()
this.configuration.Dispose();
}
}

[UsedImplicitly]
public sealed class MySqlNormalUserFixture : MySqlBaseFixture
{
public MySqlNormalUserFixture() : base("mysql", "mysql")
{
}
}

[UsedImplicitly]
public sealed class MySqlRootUserFixture : MySqlBaseFixture
{
public MySqlRootUserFixture() : base("root", "root")
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System.Data;
using System.Threading.Tasks;
using DotNet.Testcontainers.Tests.Fixtures;
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class MySqlTestcontainerNormalUserTest : IClassFixture<MySqlNormalUserFixture>
{
private readonly MySqlNormalUserFixture mySqlFixture;

public MySqlTestcontainerNormalUserTest(MySqlNormalUserFixture mySqlFixture)
{
this.mySqlFixture = mySqlFixture;
}

[Fact]
public async Task ConnectionEstablished()
{
// Given
var connection = this.mySqlFixture.Connection;

// When
await connection.OpenAsync()
.ConfigureAwait(false);

// Then
Assert.Equal(ConnectionState.Open, connection.State);
}

[Fact]
public async Task ExecScriptInRunningContainer()
{
// Given
const string script = @"
CREATE TABLE MyTable (
id INT(6) UNSIGNED PRIMARY KEY,
name VARCHAR(30) NOT NULL
);
INSERT INTO MyTable (id, name) VALUES (1, 'MyName');
SELECT * FROM MyTable;
";

// When
var result = await this.mySqlFixture.Container.ExecScriptAsync(script)
.ConfigureAwait(false);

// Then
Assert.DoesNotContain("ERROR", result.Stderr);
Assert.Equal(0, result.ExitCode);
Assert.Contains("MyName", result.Stdout);
}

[Fact]
public async Task ThrowErrorInRunningContainerWithInvalidScript()
{
// Given
const string script = "invalid SQL command";

// When
var result = await this.mySqlFixture.Container.ExecScriptAsync(script)
.ConfigureAwait(false);

// Then
Assert.NotEqual(0, result.ExitCode);
Assert.Contains("ERROR 1064 (42000)", result.Stderr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace DotNet.Testcontainers.Tests.Unit
using Xunit;

[Collection(nameof(Testcontainers))]
public sealed class MySqlTestcontainerTest : IClassFixture<MySqlFixture>
public sealed class MySqlTestcontainerRootUserTest : IClassFixture<MySqlRootUserFixture>
{
private readonly MySqlFixture mySqlFixture;
private readonly MySqlRootUserFixture mySqlFixture;

public MySqlTestcontainerTest(MySqlFixture mySqlFixture)
public MySqlTestcontainerRootUserTest(MySqlRootUserFixture mySqlFixture)
{
this.mySqlFixture = mySqlFixture;
}
Expand Down Expand Up @@ -47,6 +47,7 @@ public async Task ExecScriptInRunningContainer()
.ConfigureAwait(false);

// Then
Assert.DoesNotContain("ERROR", result.Stderr);
Assert.Equal(0, result.ExitCode);
Assert.Contains("MyName", result.Stdout);
}
Expand Down

0 comments on commit af9043e

Please sign in to comment.