Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipeline tests #77

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,8 @@ jobs:
.\.sonar\scanner\dotnet-sonarscanner begin /k:"Clean-CaDET_tutor" /o:"clean-cadet" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.exclusions="**/Migrations/**, **/*.sql"
dotnet build --no-incremental
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
- name: Pull Docker Images
shell: powershell
run: docker pull postgres # hard-coded image
- name: Test
run: dotnet test --no-restore
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Shouldly;
using System.IO;
using Tutor.Infrastructure.DataImport;
using Xunit;

namespace Tutor.Infrastructure.Tests.Integration.DataImport;

public class ExcelToSqlTransformerTests
{
[Fact]
//[Fact]
public void Can_transform_to_sql()
{
const string sourceFolder = "C:/TUTOR-EDU/FTN/Add-RP-IF/domain";
Expand Down
113 changes: 93 additions & 20 deletions tests/Tutor.Web.Tests/Integration/TutorTestFactory.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,57 @@
using Microsoft.AspNetCore.Hosting;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Tutor.Infrastructure.Database;
using Tutor.Infrastructure.Database.EventStore.Postgres;
using Tutor.Infrastructure.Security;
using Xunit;

namespace Tutor.Web.Tests.Integration;

public class TutorApplicationTestFactory<TStartup> : WebApplicationFactory<Startup>
public class TutorApplicationTestFactory<TStartup> : WebApplicationFactory<Startup>, IAsyncLifetime
{
private readonly TestcontainerDatabase _dbContainer;
private readonly TestcontainerDatabase _dbEventContainer;
private readonly IConfiguration _config;

public TutorApplicationTestFactory()
{
_config = InitConfiguration();
var testContainers = _config.GetValue("TESTCONTAINERS", true);
if (testContainers)
{
_dbContainer = new TestcontainersBuilder<PostgreSqlTestcontainer>()
.WithDatabase(new PostgreSqlTestcontainerConfiguration
{
Database = "smart_tutor_test",
Username = "postgres",
Password = "postgres",
})
.WithImage("postgres")
.WithCleanUp(true)
.Build();
_dbEventContainer = new TestcontainersBuilder<PostgreSqlTestcontainer>()
.WithDatabase(new PostgreSqlTestcontainerConfiguration
{
Database = "smart_tutor_test_events",
Username = "postgres",
Password = "postgres",
})
.WithImage("postgres")
.WithCleanUp(true)
.Build();
}
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
Expand Down Expand Up @@ -45,7 +83,7 @@ private static void InitializeDatabase(DbContext context, string scriptFolder, I
}
}

private static ServiceProvider BuildServiceProvider(IServiceCollection services)
private ServiceProvider BuildServiceProvider(IServiceCollection services)
{
var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<TutorContext>));
services.Remove(descriptor);
Expand All @@ -57,31 +95,66 @@ private static ServiceProvider BuildServiceProvider(IServiceCollection services)
return services.BuildServiceProvider();
}

private static string CreateConnectionStringForTest()
private string CreateConnectionStringForTest()
{
var server = Environment.GetEnvironmentVariable("DATABASE_HOST") ?? "localhost";
var port = Environment.GetEnvironmentVariable("DATABASE_PORT") ?? "5432";
var database = EnvironmentConnection.GetSecret("DATABASE_SCHEMA") ?? "smart-tutor-test";
var user = EnvironmentConnection.GetSecret("DATABASE_USERNAME") ?? "postgres";
var password = EnvironmentConnection.GetSecret("DATABASE_PASSWORD") ?? "super";
var integratedSecurity = Environment.GetEnvironmentVariable("DATABASE_INTEGRATED_SECURITY") ?? "false";
var pooling = Environment.GetEnvironmentVariable("DATABASE_POOLING") ?? "true";
var testContainers = _config.GetValue("TESTCONTAINERS", true);
if (testContainers) return _dbContainer.ConnectionString;

var server = _config.GetValue("DATABASE_HOST", "localhost");
var port = _config.GetValue("DATABASE_PORT", "5432");
var database = _config.GetValue("DATABASE_SCHEMA", "smart-tutor-test");
var user = _config.GetValue("DATABASE_USERNAME", "postgres");
var password = _config.GetValue("DATABASE_PASSWORD", "super");
var integratedSecurity = _config.GetValue("DATABASE_INTEGRATED_SECURITY", "false");
var pooling = _config.GetValue("DATABASE_POOLING", "true");

return
$"Server={server};Port={port};Database={database};User ID={user};Password={password};Integrated Security={integratedSecurity};Pooling={pooling};Include Error Detail=True";
}

private static string CreateConnectionStringForEvents()
private string CreateConnectionStringForEvents()
{
var server = Environment.GetEnvironmentVariable("DATABASE_HOST") ?? "localhost";
var port = Environment.GetEnvironmentVariable("DATABASE_PORT") ?? "5432";
var database = EnvironmentConnection.GetSecret("EVENT_DATABASE_SCHEMA") ?? "smart-tutor-test-events";
var user = EnvironmentConnection.GetSecret("DATABASE_USERNAME") ?? "postgres";
var password = EnvironmentConnection.GetSecret("DATABASE_PASSWORD") ?? "super";
var integratedSecurity = Environment.GetEnvironmentVariable("DATABASE_INTEGRATED_SECURITY") ?? "false";
var pooling = Environment.GetEnvironmentVariable("DATABASE_POOLING") ?? "true";
var testContainers = _config.GetValue("TESTCONTAINERS", true);
if (testContainers) return _dbEventContainer.ConnectionString;

var server = _config.GetValue("DATABASE_HOST", "localhost");
var port = _config.GetValue("DATABASE_PORT", "5432");
var database = _config.GetValue("EVENT_DATABASE_SCHEMA", "smart-tutor-test-events");
var user = _config.GetValue("DATABASE_USERNAME", "postgres");
var password = _config.GetValue("DATABASE_PASSWORD", "super");
var integratedSecurity = _config.GetValue("DATABASE_INTEGRATED_SECURITY", "false");
var pooling = _config.GetValue("DATABASE_POOLING", "true");

return
$"Server={server};Port={port};Database={database};User ID={user};Password={password};Integrated Security={integratedSecurity};Pooling={pooling};";
}

public IConfiguration InitConfiguration()
{
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.test.json")
.AddEnvironmentVariables()
.Build();
return config;
}

public async Task InitializeAsync()
{
var testContainers = _config.GetValue("TESTCONTAINERS", true);
if (testContainers)
{
await _dbContainer.StartAsync();
await _dbEventContainer.StartAsync();
}
}

public new async Task DisposeAsync()
{
var testContainers = _config.GetValue("TESTCONTAINERS", true);
if (testContainers)
{
await _dbContainer.DisposeAsync();
await _dbEventContainer.DisposeAsync();
}
}
}
14 changes: 14 additions & 0 deletions tests/Tutor.Web.Tests/Tutor.Web.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<None Remove="appsettings.Test.json" />
</ItemGroup>

<ItemGroup>
<Content Include="appsettings.Test.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="Testcontainers" Version="2.3.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
Expand Down
3 changes: 3 additions & 0 deletions tests/Tutor.Web.Tests/appsettings.Test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"TESTCONTAINERS": true
}