diff --git a/.github/workflows/build_publish.yml b/.github/workflows/build_publish.yml index 5ab9a3e..dfe5f9c 100644 --- a/.github/workflows/build_publish.yml +++ b/.github/workflows/build_publish.yml @@ -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 @@ -82,7 +88,7 @@ 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 \ @@ -90,7 +96,18 @@ jobs: --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 @@ -98,19 +115,19 @@ jobs: 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" \ @@ -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" \ diff --git a/backend/Directory.Build.targets b/backend/Directory.Build.targets index fc7ebd2..b343325 100644 --- a/backend/Directory.Build.targets +++ b/backend/Directory.Build.targets @@ -11,6 +11,7 @@ + @@ -18,6 +19,7 @@ + diff --git a/backend/tests/LeanCode.AppRating.IntegrationTests/App/Startup.cs b/backend/tests/LeanCode.AppRating.IntegrationTests/App/Startup.cs index 1c6a3c6..47444c4 100644 --- a/backend/tests/LeanCode.AppRating.IntegrationTests/App/Startup.cs +++ b/backend/tests/LeanCode.AppRating.IntegrationTests/App/Startup.cs @@ -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>(); - services.AddDbContext( - opts => opts.UseSqlServer(Configuration.GetValue("SqlServer:ConnectionString")) - ); + services.AddDbContext(cfg => testDatabaseConfig.ConfigureDbContext(cfg, Configuration)); services.AddFluentValidation(Handlers); services.AddCQRS(Contracts, Handlers).AddAppRating(); diff --git a/backend/tests/LeanCode.AppRating.IntegrationTests/App/TestDbContext.cs b/backend/tests/LeanCode.AppRating.IntegrationTests/App/TestDbContext.cs index d6deb4a..a93a44f 100644 --- a/backend/tests/LeanCode.AppRating.IntegrationTests/App/TestDbContext.cs +++ b/backend/tests/LeanCode.AppRating.IntegrationTests/App/TestDbContext.cs @@ -5,14 +5,19 @@ namespace LeanCode.AppRating.IntegrationTests.App; public class TestDbContext : DbContext, IAppRatingStore { + private readonly TestDatabaseConfig config; + public TestDbContext(DbContextOptions options) - : base(options) { } + : base(options) + { + config = TestDatabaseConfig.Create(); + } public DbSet> AppRatings => Set>(); protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); - builder.ConfigureAppRatingEntity(SqlDbType.MsSql); + builder.ConfigureAppRatingEntity(config.DbType); } } diff --git a/backend/tests/LeanCode.AppRating.IntegrationTests/LeanCode.AppRating.IntegrationTests.csproj b/backend/tests/LeanCode.AppRating.IntegrationTests/LeanCode.AppRating.IntegrationTests.csproj index abfe092..ea49efe 100644 --- a/backend/tests/LeanCode.AppRating.IntegrationTests/LeanCode.AppRating.IntegrationTests.csproj +++ b/backend/tests/LeanCode.AppRating.IntegrationTests/LeanCode.AppRating.IntegrationTests.csproj @@ -8,10 +8,12 @@ + + diff --git a/backend/tests/LeanCode.AppRating.IntegrationTests/TestBase.cs b/backend/tests/LeanCode.AppRating.IntegrationTests/TestBase.cs index 12ced01..5110124 100644 --- a/backend/tests/LeanCode.AppRating.IntegrationTests/TestBase.cs +++ b/backend/tests/LeanCode.AppRating.IntegrationTests/TestBase.cs @@ -19,8 +19,7 @@ public class TestBase : LeanCodeTestFactory 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() { diff --git a/backend/tests/LeanCode.AppRating.IntegrationTests/TestDatabaseConfig.cs b/backend/tests/LeanCode.AppRating.IntegrationTests/TestDatabaseConfig.cs new file mode 100644 index 0000000..464b9c9 --- /dev/null +++ b/backend/tests/LeanCode.AppRating.IntegrationTests/TestDatabaseConfig.cs @@ -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("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("Postgres:ConnectionString")) + .EnableDynamicJson() + .Build(); + builder.UseNpgsql(dataSource); + } +} diff --git a/backend/tests/LeanCode.AppRating.IntegrationTests/docker-compose.yml b/backend/tests/LeanCode.AppRating.IntegrationTests/docker-compose.yml index 894b440..c388c79 100644 --- a/backend/tests/LeanCode.AppRating.IntegrationTests/docker-compose.yml +++ b/backend/tests/LeanCode.AppRating.IntegrationTests/docker-compose.yml @@ -7,3 +7,9 @@ services: - MSSQL_SA_PASSWORD=Passw12# ports: - "1433:1433" + postgres: + image: postgres:16 + environment: + POSTGRES_PASSWORD: Passw12# + ports: + - "5432:5432"