-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switching integration tests to use TestContainers
- Loading branch information
Showing
12 changed files
with
82 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
test/FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests/BuildEnvironment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests | ||
{ | ||
public static class BuildEnvironment | ||
{ | ||
public static bool IsAppVeyor => Environment.GetEnvironmentVariable("APPVEYOR") != null; | ||
public static bool IsGitHub => Environment.GetEnvironmentVariable("GITHUB_ACTIONS") != null; | ||
public const bool IsGitHubLocalPostgres = | ||
#if POSTGRES_ONLY | ||
true; | ||
#else | ||
false; | ||
#endif | ||
} | ||
} |
118 changes: 30 additions & 88 deletions
118
test/FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests/DatabaseInitializerFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,51 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using DotNet.Testcontainers.Containers; | ||
using FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests.Base; | ||
using Microsoft.EntityFrameworkCore; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
using Xunit; | ||
|
||
namespace FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests | ||
{ | ||
public abstract class DatabaseInitializerFixture | ||
public abstract class DatabaseInitializerFixture : IAsyncLifetime | ||
{ | ||
/* Docker commands for the test containers | ||
docker run --name flexlabs_upsert_test_postgres -e POSTGRES_USER=testuser -e POSTGRES_PASSWORD=Password12! -e POSTGRES_DB=testuser -p 25432:5432 -d postgres:alpine | ||
docker run --name flexlabs_upsert_test_mysql -e MYSQL_ROOT_PASSWORD=Password12! -e MYSQL_USER=testuser -e MYSQL_PASSWORD=Password12! -e MYSQL_DATABASE=testuser -p 23306:3306 -d mysql | ||
docker run --name flexlabs_upsert_test_mssql -e ACCEPT_EULA=Y -e SA_PASSWORD=Password12! -p 21433:1433 -d mcr.microsoft.com/mssql/server | ||
*/ | ||
|
||
private const string Username = "testuser"; | ||
private const string Password = "Password12!"; | ||
|
||
private static readonly string ConnString_InMemory = "Upsert_TestDbContext_Tests"; | ||
private static readonly string ConnString_Sqlite = $"Data Source={Username}.db"; | ||
|
||
private static readonly string ConnString_Postgres_GitHub = $"Server=localhost;Port=5432;Database={Username};Username=postgres;Password=root"; | ||
|
||
private static readonly string ConnString_Postgres_Docker = $"Server=localhost;Port=25432;Database={Username};Username={Username};Password={Password}"; | ||
private static readonly string ConnString_MySql_Docker = $"Server=localhost;Port=23306;Database={Username};Uid=root;Pwd={Password}"; | ||
private static readonly string ConnString_SqlServer_Docker = $"Server=localhost,21433;User=sa;Password={Password};Initial Catalog=FlexLabsUpsertTests;Trust Server Certificate=true"; | ||
|
||
private static readonly string ConnString_Postgres_AppVeyor = $"Server=localhost;Port=5432;Database={Username};Username=postgres;Password={Password}"; | ||
private static readonly string ConnString_MySql_AppVeyor = $"Server=localhost;Port=3306;Database={Username};Uid=root;Pwd={Password}"; | ||
private static readonly string ConnString_SqlServer_AppVeyor = $"Server=(local)\\SQL2017;Database={Username};User Id=sa;Password={Password};Trust Server Certificate=true"; | ||
|
||
private static readonly string ConnString_SqlServer_LocalDb = $"Server=(localdb)\\MSSqlLocalDB;Integrated Security=SSPI;Initial Catalog=FlexLabsUpsertTests;"; | ||
|
||
private readonly IMessageSink _diagnosticMessageSink; | ||
public DbContextOptions<TestDbContext> DataContextOptions { get; } | ||
public IContainer TestContainer { get; } | ||
public DbDriver DbDriver { get; } | ||
public DbContextOptions<TestDbContext> DataContextOptions { get; private set; } | ||
|
||
public DatabaseInitializerFixture(IMessageSink diagnosticMessageSink, DbDriver driver) | ||
public DatabaseInitializerFixture(DbDriver dbDriver) | ||
{ | ||
_diagnosticMessageSink = diagnosticMessageSink; | ||
DbDriver = driver; | ||
DbDriver = dbDriver; | ||
} | ||
|
||
var connectionString = driver switch | ||
{ | ||
DbDriver.InMemory => ConnString_InMemory, | ||
DbDriver.Sqlite => ConnString_Sqlite, | ||
DbDriver.Postgres when BuildEnvironment.IsAppVeyor => ConnString_Postgres_AppVeyor, | ||
DbDriver.Postgres when BuildEnvironment.IsGitHub && BuildEnvironment.IsGitHubLocalPostgres => ConnString_Postgres_GitHub, | ||
DbDriver.Postgres => ConnString_Postgres_Docker, | ||
DbDriver.MySQL when BuildEnvironment.IsAppVeyor => ConnString_MySql_AppVeyor, | ||
DbDriver.MySQL => ConnString_MySql_Docker, | ||
DbDriver.MSSQL when BuildEnvironment.IsAppVeyor => ConnString_SqlServer_AppVeyor, | ||
DbDriver.MSSQL when BuildEnvironment.IsGitHub || Environment.OSVersion.Platform != PlatformID.Win32NT => ConnString_SqlServer_Docker, | ||
DbDriver.MSSQL => ConnString_SqlServer_LocalDb, | ||
_ => throw new ArgumentException("Can't get a connection string for driver " + driver) | ||
}; | ||
public DatabaseInitializerFixture(DbDriver dbDriver, IContainer testContainer) | ||
: this(dbDriver) | ||
{ | ||
TestContainer = testContainer; | ||
} | ||
|
||
protected abstract void ConfigureContextOptions(DbContextOptionsBuilder<TestDbContext> builder); | ||
|
||
DataContextOptions = GetContextOptions(connectionString, driver); | ||
var startTime = DateTime.Now; | ||
var isSuccess = false; | ||
while (DateTime.Now.Subtract(startTime) < TimeSpan.FromSeconds(200)) | ||
public async Task InitializeAsync() | ||
{ | ||
if (TestContainer is not null) | ||
{ | ||
try | ||
{ | ||
using var context = new TestDbContext(DataContextOptions); | ||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
isSuccess = true; | ||
break; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_diagnosticMessageSink.OnMessage(new DiagnosticMessage("Connecting to {0} failed! Error: {1}", driver, ex.GetBaseException().Message)); | ||
System.Threading.Thread.Sleep(1000); | ||
continue; | ||
} | ||
await TestContainer.StartAsync(); | ||
} | ||
if (!isSuccess) | ||
throw new Exception("Could not connect to the database " + driver); | ||
|
||
var builder = new DbContextOptionsBuilder<TestDbContext>(); | ||
ConfigureContextOptions(builder); | ||
DataContextOptions = builder.Options; | ||
|
||
using var context = new TestDbContext(DataContextOptions); | ||
await context.Database.EnsureCreatedAsync(); | ||
} | ||
|
||
private static DbContextOptions<TestDbContext> GetContextOptions(string connectionString, DbDriver driver) | ||
public async Task DisposeAsync() | ||
{ | ||
var options = new DbContextOptionsBuilder<TestDbContext>(); | ||
switch (driver) | ||
if (TestContainer is not null) | ||
{ | ||
case DbDriver.Postgres: | ||
options.UseNpgsql(connectionString); | ||
break; | ||
case DbDriver.MSSQL: | ||
options.UseSqlServer(connectionString); | ||
break; | ||
case DbDriver.MySQL: | ||
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)); | ||
break; | ||
case DbDriver.InMemory: | ||
options.UseInMemoryDatabase(connectionString); | ||
break; | ||
case DbDriver.Sqlite: | ||
options.UseSqlite(connectionString); | ||
break; | ||
default: | ||
throw new InvalidOperationException("Invalid database driver: " + driver); | ||
await TestContainer.StopAsync(); | ||
} | ||
return options.Options; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
test/FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests/DbTests_Sqlite.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.