Skip to content

Commit

Permalink
Merge pull request #5 from leancodepl/feature/postgres-integration-tests
Browse files Browse the repository at this point in the history
Add postgres integration test
  • Loading branch information
lukaszgarstecki authored Nov 27, 2023
2 parents 349da0d + 0ecf72f commit a31dd76
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 13 deletions.
27 changes: 22 additions & 5 deletions .github/workflows/build_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ jobs:
MSSQL_SA_PASSWORD: Passw12#
ports:
- "1433:1433"
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: Passw12#
ports:
- "5432:5432"
env:
DOTNET_VERSION: 8.0
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
Expand Down Expand Up @@ -82,35 +88,46 @@ jobs:
- name: Test
run: dotnet msbuild -t:RunTests -p:Configuration=Release -p:LogFileName="$PWD/../TestResults/tests.trx"
working-directory: ./backend/tests
- name: Integration test
- name: Integration Test SqlServer
run: |
dotnet test \
./tests/LeanCode.AppRating.IntegrationTests/LeanCode.AppRating.IntegrationTests.csproj \
--no-build \
--logger "trx;LogFileName=LeanCode.AppRating.IntegrationTests.trx" \
--results-directory TestResults
env:
AppReviewIntegrationTests__Database: sqlserver
SqlServer__ConnectionStringBase: Server=localhost,1433;User Id=sa;Password=Passw12#;Encrypt=false
- name: Integration Test Postgres
run: |
dotnet test \
./tests/LeanCode.AppRating.IntegrationTests/LeanCode.AppRating.IntegrationTests.csproj \
--no-build \
--logger "trx;LogFileName=LeanCode.AppRating.IntegrationTests.Postgres.trx" \
--results-directory TestResults
env:
AppReviewIntegrationTests__Database: postgres
Postgres__ConnectionStringBase: Host=localhost;Username=postgres;Password=Passw12#
- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v3
with:
name: test_results.zip
path: backend/TestResults/*.trx
- name: Pack AppRating Contracts
if: ${{ needs.prepare.outputs.publish_artifacts == '1' }}
if: ${{ steps.version.outputs.publish_artifacts == '1' }}
env:
BUILD_VERSION: ${{ steps.version.outputs.version }}
run: dotnet pack --no-build -c Release -p:Version=$BUILD_VERSION
working-directory: ./backend/src/LeanCode.AppRating.Contracts
- name: Pack AppRating
if: ${{ needs.prepare.outputs.publish_artifacts == '1' }}
if: ${{ steps.version.outputs.publish_artifacts == '1' }}
env:
BUILD_VERSION: ${{ steps.version.outputs.version }}
run: dotnet pack --no-build -c Release -p:Version=$BUILD_VERSION
working-directory: ./backend/src/LeanCode.AppRating
- name: Publish to Feedz
if: ${{ needs.prepare.outputs.publish_artifacts == '1' }}
if: ${{ steps.version.outputs.publish_artifacts == '1' }}
run: |
dotnet nuget push \
"src/LeanCode.AppRating.Contracts/bin/Release/LeanCode.AppRating.Contracts.${BUILD_VERSION}.nupkg" \
Expand All @@ -122,7 +139,7 @@ jobs:
BUILD_VERSION: ${{ steps.version.outputs.version }}
FEEDZ_API_KEY: ${{ secrets.FEEDZ_API_KEY }}
- name: Publish to Nuget
if: ${{ needs.prepare.outputs.publish_nuget == '1' }}
if: ${{ steps.version.outputs.publish_nuget == '1' }}
run: |
dotnet nuget push \
"src/LeanCode.AppRating.Contracts/bin/Release/LeanCode.AppRating.Contracts.${BUILD_VERSION}.nupkg" \
Expand Down
2 changes: 2 additions & 0 deletions backend/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
<PackageReference Update="LeanCode.CQRS.AspNetCore" Version="$(CoreLibVersion)" />
<PackageReference Update="LeanCode.CQRS.Execution" Version="$(CoreLibVersion)" />
<PackageReference Update="LeanCode.CQRS.Validation.Fluent" Version="$(CoreLibVersion)" />
<PackageReference Update="LeanCode.DomainModels.EF" Version="$(CoreLibVersion)" />
<PackageReference Update="LeanCode.TimeProvider" Version="$(CoreLibVersion)" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="FluentValidation" Version="11.5.1" />
<PackageReference Update="Microsoft.EntityFrameworkCore" Version="$(EFCoreVersion)" />
<PackageReference Update="Microsoft.EntityFrameworkCore.Relational" Version="$(EFCoreVersion)" />
<PackageReference Update="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Update="Serilog" Version="3.0.1" />
</ItemGroup>

Expand Down
10 changes: 6 additions & 4 deletions backend/tests/LeanCode.AppRating.IntegrationTests/App/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ public class Startup : LeanStartup
{
public static readonly TypesCatalog Contracts = new(typeof(Startup)); // nothing
public static readonly TypesCatalog Handlers = new(typeof(Startup)); // nothing
private readonly TestDatabaseConfig testDatabaseConfig;

public Startup(IConfiguration config)
: base(config) { }
: base(config)
{
testDatabaseConfig = TestDatabaseConfig.Create();
}

public override void ConfigureServices(IServiceCollection services)
{
services.AddHostedService<DbContextInitializer<TestDbContext>>();
services.AddDbContext<TestDbContext>(
opts => opts.UseSqlServer(Configuration.GetValue<string>("SqlServer:ConnectionString"))
);
services.AddDbContext<TestDbContext>(cfg => testDatabaseConfig.ConfigureDbContext(cfg, Configuration));
services.AddFluentValidation(Handlers);
services.AddCQRS(Contracts, Handlers).AddAppRating<Guid, TestDbContext, UserIdExtractor>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ namespace LeanCode.AppRating.IntegrationTests.App;

public class TestDbContext : DbContext, IAppRatingStore<Guid>
{
private readonly TestDatabaseConfig config;

public TestDbContext(DbContextOptions<TestDbContext> options)
: base(options) { }
: base(options)
{
config = TestDatabaseConfig.Create();
}

public DbSet<AppRatingEntity<Guid>> AppRatings => Set<AppRatingEntity<Guid>>();

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ConfigureAppRatingEntity<Guid>(SqlDbType.MsSql);
builder.ConfigureAppRatingEntity<Guid>(config.DbType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" />
<PackageReference Include="LeanCode.CQRS.AspNetCore" />
<PackageReference Include="LeanCode.DomainModels.EF" />
<PackageReference Include="LeanCode.IntegrationTestHelpers" />
<PackageReference Include="LeanCode.Startup.MicrosoftDI" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public class TestBase : LeanCodeTestFactory<App.Startup>

protected JsonSerializerOptions JsonSerializerOptions { get; } = new() { };

protected override ConfigurationOverrides Configuration { get; } =
new("SqlServer__ConnectionStringBase", "SqlServer:ConnectionString", Serilog.Events.LogEventLevel.Verbose);
protected override ConfigurationOverrides Configuration => TestDatabaseConfig.Create().GetConfigurationOverrides();

public TestBase()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using LeanCode.AppRating.DataAccess;
using LeanCode.CQRS.MassTransitRelay.LockProviders;
using LeanCode.DomainModels.EF;
using LeanCode.IntegrationTestHelpers;
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Npgsql;

namespace LeanCode.AppRating.IntegrationTests;

public abstract class TestDatabaseConfig
{
public const string ConfigEnvName = "AppReviewIntegrationTests__Database";

public abstract SqlDbType DbType { get; }
public abstract ConfigurationOverrides GetConfigurationOverrides();
public abstract void ConfigureDbContext(DbContextOptionsBuilder builder, IConfiguration config);

public static TestDatabaseConfig Create()
{
return Environment.GetEnvironmentVariable(ConfigEnvName) switch
{
"sqlserver" => new SqlServerTestDatabaseConfig(),
"postgres" => new PostgresTestConfig(),
_
=> throw new InvalidOperationException(
$"Set the database provider (sqlserver|postgres) via {ConfigEnvName} env variable"
),
};
}
}

public class SqlServerTestDatabaseConfig : TestDatabaseConfig
{
public override SqlDbType DbType => SqlDbType.MsSql;

public override ConfigurationOverrides GetConfigurationOverrides() =>
new("SqlServer__ConnectionStringBase", "SqlServer:ConnectionString");

public override void ConfigureDbContext(DbContextOptionsBuilder builder, IConfiguration config)
{
builder.UseSqlServer(config.GetValue<string>("SqlServer:ConnectionString"));
}
}

public class PostgresTestConfig : TestDatabaseConfig
{
public override SqlDbType DbType => SqlDbType.PostgreSql;

public override ConfigurationOverrides GetConfigurationOverrides() =>
new("Postgres__ConnectionStringBase", "Postgres:ConnectionString");

public override void ConfigureDbContext(DbContextOptionsBuilder builder, IConfiguration config)
{
var dataSource = new NpgsqlDataSourceBuilder(config.GetValue<string>("Postgres:ConnectionString"))
.EnableDynamicJson()
.Build();
builder.UseNpgsql(dataSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ services:
- MSSQL_SA_PASSWORD=Passw12#
ports:
- "1433:1433"
postgres:
image: postgres:16
environment:
POSTGRES_PASSWORD: Passw12#
ports:
- "5432:5432"

0 comments on commit a31dd76

Please sign in to comment.