Skip to content

Commit

Permalink
Switching integration tests to use TestContainers
Browse files Browse the repository at this point in the history
  • Loading branch information
artiomchi committed Nov 19, 2023
1 parent 152a890 commit 60ad9a0
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 162 deletions.
35 changes: 1 addition & 34 deletions .github/workflows/build-full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,15 @@ on: [push, pull_request]
jobs:
build-ubuntu:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:alpine
ports:
- 25432:5432
env:
POSTGRES_DB: testuser
POSTGRES_USER: testuser
POSTGRES_PASSWORD: Password12!
mysql:
image: mysql
ports:
- 23306:3306
env:
MYSQL_DATABASE: testuser
MYSQL_USER: testuser
MYSQL_ROOT_PASSWORD: Password12!
MYSQL_PASSWORD: Password12!
mssql:
image: mcr.microsoft.com/mssql/server
ports:
- 21433:1433
env:
ACCEPT_EULA: Y
SA_PASSWORD: Password12!
steps:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0'
include-prerelease: True
- uses: actions/checkout@v2
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore /p:CORE_ONLY=true
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build

Expand All @@ -49,15 +23,8 @@ jobs:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0'
include-prerelease: True
- uses: actions/checkout@v2
- name: Enable Postgres
run: sc config postgresql-x64-14 start= demand
- name: Start Postgres
run: net start postgresql-x64-14
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore /p:POSTGRES_ONLY=true
- name: Test
run: dotnet test --no-build
3 changes: 1 addition & 2 deletions .github/workflows/build-quick.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ jobs:
- uses: actions/setup-dotnet@v1
with:
dotnet-version: '7.0'
include-prerelease: True
- uses: actions/checkout@v2
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore /p:CORE_ONLY=true
run: dotnet build --no-restore
- name: Test
run: dotnet test ./test/FlexLabs.EntityFrameworkCore.Upsert.Tests --no-build
5 changes: 0 additions & 5 deletions appveyor.signed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ image: Visual Studio 2022
configuration: Release
skip_tags: true

services:
- mssql2017
- mysql
- postgresql13

environment:
skey_secret:
secure: tZ1CLa1H+KzjeZAks/d3fkM3mp7ymDd0hnSmmKa+qe19K1kW2Tt+hMt38tNFMpJIYaOBSAcQ7bwvvqFQwVFNXA==
Expand Down
5 changes: 0 additions & 5 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ image: Visual Studio 2022
configuration: Debug
skip_tags: true

services:
- mssql2017
- mysql
- postgresql13

before_build:
- ps: dotnet --info
- ps: dotnet restore -v Minimal
Expand Down
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
}
}
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests.Base;
using FlexLabs.EntityFrameworkCore.Upsert.Tests.EF;
using Microsoft.EntityFrameworkCore;
using Xunit;
using Xunit.Abstractions;

namespace FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests
{
Expand All @@ -11,9 +11,12 @@ public class DbTests_InMemory : DbTestsBase, IClassFixture<DbTests_InMemory.Data
{
public sealed class DatabaseInitializer : DatabaseInitializerFixture
{
public DatabaseInitializer(IMessageSink diagnosticMessageSink)
: base(diagnosticMessageSink, DbDriver.InMemory)
public DatabaseInitializer()
: base(DbDriver.InMemory)
{ }

protected override void ConfigureContextOptions(DbContextOptionsBuilder<TestDbContext> builder)
=> builder.UseInMemoryDatabase("Upsert_TestDbContext_Tests");
}

public DbTests_InMemory(DatabaseInitializer contexts)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests.Base;
using DotNet.Testcontainers.Containers;
using FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests.Base;
using FlexLabs.EntityFrameworkCore.Upsert.Tests.EF;
using Microsoft.EntityFrameworkCore;
using Testcontainers.MySql;
using Xunit;
using Xunit.Abstractions;

namespace FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests
{
Expand All @@ -10,9 +12,15 @@ public class DbTests_MySql : DbTestsBase, IClassFixture<DbTests_MySql.DatabaseIn
{
public sealed class DatabaseInitializer : DatabaseInitializerFixture
{
public DatabaseInitializer(IMessageSink diagnosticMessageSink)
: base(diagnosticMessageSink, DbDriver.MySQL)
public DatabaseInitializer()
: base(DbDriver.MySQL, new MySqlBuilder().Build())
{ }

protected override void ConfigureContextOptions(DbContextOptionsBuilder<TestDbContext> builder)
{
var connectionString = (TestContainer as IDatabaseContainer).GetConnectionString();
builder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
}

public DbTests_MySql(DatabaseInitializer contexts)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Linq;
using DotNet.Testcontainers.Containers;
using FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests.Base;
using FlexLabs.EntityFrameworkCore.Upsert.Tests.EF;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Testcontainers.PostgreSql;
using Xunit;
using Xunit.Abstractions;

namespace FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests
{
Expand All @@ -13,9 +14,15 @@ public class DbTests_Postgres : DbTestsBase, IClassFixture<DbTests_Postgres.Data
{
public sealed class DatabaseInitializer : DatabaseInitializerFixture
{
public DatabaseInitializer(IMessageSink diagnosticMessageSink)
: base(diagnosticMessageSink, DbDriver.Postgres)
public DatabaseInitializer()
: base(DbDriver.Postgres, new PostgreSqlBuilder().Build())
{ }

protected override void ConfigureContextOptions(DbContextOptionsBuilder<TestDbContext> builder)
{
var connectionString = (TestContainer as IDatabaseContainer).GetConnectionString();
builder.UseNpgsql(connectionString);
}
}

public DbTests_Postgres(DatabaseInitializer contexts)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests.Base;
using DotNet.Testcontainers.Containers;
using FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests.Base;
using FlexLabs.EntityFrameworkCore.Upsert.Tests.EF;
using Microsoft.EntityFrameworkCore;
using Testcontainers.MsSql;
using Xunit;
using Xunit.Abstractions;

namespace FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests
{
Expand All @@ -10,9 +12,15 @@ public class DbTests_SqlServer : DbTestsBase, IClassFixture<DbTests_SqlServer.Da
{
public sealed class DatabaseInitializer : DatabaseInitializerFixture
{
public DatabaseInitializer(IMessageSink diagnosticMessageSink)
: base(diagnosticMessageSink, DbDriver.MSSQL)
public DatabaseInitializer()
: base(DbDriver.MSSQL, new MsSqlBuilder().Build())
{ }

protected override void ConfigureContextOptions(DbContextOptionsBuilder<TestDbContext> builder)
{
var connectionString = (TestContainer as IDatabaseContainer).GetConnectionString();
builder.UseSqlServer(connectionString);
}
}

public DbTests_SqlServer(DatabaseInitializer contexts)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests.Base;
using FlexLabs.EntityFrameworkCore.Upsert.Tests.EF;
using Microsoft.EntityFrameworkCore;
using Xunit;
using Xunit.Abstractions;

namespace FlexLabs.EntityFrameworkCore.Upsert.IntegrationTests
{
public class DbTests_Sqlite : DbTestsBase, IClassFixture<DbTests_Sqlite.DatabaseInitializer>
{
public sealed class DatabaseInitializer : DatabaseInitializerFixture
{
public DatabaseInitializer(IMessageSink diagnosticMessageSink)
: base(diagnosticMessageSink, DbDriver.Sqlite)
public DatabaseInitializer()
: base(DbDriver.Sqlite)
{ }

protected override void ConfigureContextOptions(DbContextOptionsBuilder<TestDbContext> builder)
=> builder.UseSqlite("Data Source=testdb.db");
}

public DbTests_Sqlite(DatabaseInitializer contexts)
Expand Down
Loading

0 comments on commit 60ad9a0

Please sign in to comment.