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

Add support for running tests towards a postgreSQL database #1966

Merged
merged 5 commits into from
Jan 22, 2025
Merged
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
14 changes: 8 additions & 6 deletions backend/api.test/Controllers/AreaControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,40 @@
using Api.Services;
using Api.Test.Database;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;

namespace Api.Test.Controllers
{
public class AreaControllerTests : IAsyncLifetime
{
public required DatabaseUtilities DatabaseUtilities;
public required PostgreSqlContainer Container;
public required HttpClient Client;
public required JsonSerializerOptions SerializerOptions;

public required IAreaService AreaService;

public async Task InitializeAsync()
{
string databaseName = Guid.NewGuid().ToString();
(string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase(
databaseName
(Container, string connectionString, var connection) =
await TestSetupHelpers.ConfigurePostgreSqlDatabase();
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(
postgreSqlConnectionString: connectionString
);
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName);
var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory);

Client = TestSetupHelpers.ConfigureHttpClient(factory);
SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions();

DatabaseUtilities = new DatabaseUtilities(
TestSetupHelpers.ConfigureFlotillaDbContext(connectionString)
TestSetupHelpers.ConfigurePostgreSqlContext(connectionString)
);

AreaService = serviceProvider.GetRequiredService<IAreaService>();
}

public Task DisposeAsync() => Task.CompletedTask;
public async Task DisposeAsync() => await Task.CompletedTask;

[Fact]
public async Task CheckThatAreaIsCorrectlyCreatedThroughEndpoint()
Expand Down
18 changes: 8 additions & 10 deletions backend/api.test/Controllers/EmergencyActionControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
using System;
using System.Net.Http;
using System.Text.Json;
using System.Net.Http;
using System.Threading.Tasks;
using Api.Services;
using Api.Test.Database;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;

namespace Api.Test.Controllers
{
public class EmergencyActionControllerTests : IAsyncLifetime
{
public required DatabaseUtilities DatabaseUtilities;
public required PostgreSqlContainer Container;
public required HttpClient Client;

public async Task InitializeAsync()
{
string databaseName = Guid.NewGuid().ToString();
(string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase(
databaseName
(Container, string connectionString, var connection) =
await TestSetupHelpers.ConfigurePostgreSqlDatabase();
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(
postgreSqlConnectionString: connectionString
);
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName);
Client = TestSetupHelpers.ConfigureHttpClient(factory);

DatabaseUtilities = new DatabaseUtilities(
TestSetupHelpers.ConfigureFlotillaDbContext(connectionString)
TestSetupHelpers.ConfigurePostgreSqlContext(connectionString)
);
}

Expand Down
12 changes: 7 additions & 5 deletions backend/api.test/Controllers/InspectionAreaControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
using Api.Services;
using Api.Test.Database;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;

namespace Api.Test.Controllers
{
public class InspectionAreaControllerTests : IAsyncLifetime
{
public required DatabaseUtilities DatabaseUtilities;
public required PostgreSqlContainer Container;
public required HttpClient Client;
public required JsonSerializerOptions SerializerOptions;

Expand All @@ -26,18 +28,18 @@ public class InspectionAreaControllerTests : IAsyncLifetime

public async Task InitializeAsync()
{
string databaseName = Guid.NewGuid().ToString();
(string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase(
databaseName
(Container, string connectionString, var connection) =
await TestSetupHelpers.ConfigurePostgreSqlDatabase();
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(
postgreSqlConnectionString: connectionString
);
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName);
var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory);

Client = TestSetupHelpers.ConfigureHttpClient(factory);
SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions();

DatabaseUtilities = new DatabaseUtilities(
TestSetupHelpers.ConfigureFlotillaDbContext(connectionString)
TestSetupHelpers.ConfigurePostgreSqlContext(connectionString)
);

InspectionAreaService = serviceProvider.GetRequiredService<IInspectionAreaService>();
Expand Down
11 changes: 6 additions & 5 deletions backend/api.test/Controllers/InstallationControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@
using System.Threading.Tasks;
using Api.Controllers.Models;
using Api.Services;
using Api.Test.Database;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;

namespace Api.Test.Controllers
{
public class InstallationControllerTests : IAsyncLifetime
{
public required PostgreSqlContainer Container;
public required HttpClient Client;

public required IInstallationService InstallationService;

public async Task InitializeAsync()
{
string databaseName = Guid.NewGuid().ToString();
(string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase(
databaseName
(Container, string connectionString, var connection) =
await TestSetupHelpers.ConfigurePostgreSqlDatabase();
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(
postgreSqlConnectionString: connectionString
);
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName);
var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory);

Client = TestSetupHelpers.ConfigureHttpClient(factory);
Expand Down
18 changes: 9 additions & 9 deletions backend/api.test/Controllers/MissionSchedulingControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,34 @@
using Api.Services;
using Api.Test.Database;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;

namespace Api.Test.Controllers
{
public class MissionSchedulingControllerTests : IAsyncLifetime
{
public required DatabaseUtilities DatabaseUtilities;
public required PostgreSqlContainer Container;
public required HttpClient Client;
public required JsonSerializerOptions SerializerOptions;

public required IMissionDefinitionService MissionDefinitionService;

public async Task InitializeAsync()
{
string databaseName = Guid.NewGuid().ToString();
(string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase(
databaseName
(Container, string connectionString, var connection) =
await TestSetupHelpers.ConfigurePostgreSqlDatabase();
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(
postgreSqlConnectionString: connectionString
);
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName);
var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory);

Client = TestSetupHelpers.ConfigureHttpClient(factory);
SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions();

DatabaseUtilities = new DatabaseUtilities(
TestSetupHelpers.ConfigureFlotillaDbContext(connectionString)
TestSetupHelpers.ConfigurePostgreSqlContext(connectionString)
);
MissionDefinitionService =
serviceProvider.GetRequiredService<IMissionDefinitionService>();
Expand Down Expand Up @@ -272,7 +274,7 @@ await missionRunThreeResponse.Content.ReadFromJsonAsync<MissionRun>(
Assert.Equal(missionRunOne!.MissionId, activeMissionRun.MissionId);
Assert.Equal(missionRunTwo!.MissionId, activeMissionRun.MissionId);
Assert.Equal(missionRunThree!.MissionId, activeMissionRun.MissionId);
Assert.True(nextMissionRun.Id == missionRunOne.Id);
Assert.True(nextMissionRun.Id == missionRunTwo.Id);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this in a previous PR but had to change it back. Suspect this assert might be a bit dependent on luck.

}

[Fact]
Expand All @@ -286,9 +288,7 @@ public async Task CheckThatMissionDoesNotStartIfRobotIsNotInSameInstallationAsMi
plant.PlantCode
);

var otherInstallation = await DatabaseUtilities.NewInstallation(
"otherInstallationCode"
);
var otherInstallation = await DatabaseUtilities.NewInstallation("OtherCode");
var robot = await DatabaseUtilities.NewRobot(RobotStatus.Available, otherInstallation);

var query = CreateDefaultCustomMissionQuery(
Expand Down
12 changes: 7 additions & 5 deletions backend/api.test/Controllers/PlantControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,31 @@
using Api.Services;
using Api.Test.Database;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;

namespace Api.Test.Controllers
{
public class PlantControllerTests : IAsyncLifetime
{
public required DatabaseUtilities DatabaseUtilities;
public required PostgreSqlContainer Container;
public required HttpClient Client;

public required IPlantService PlantService;

public async Task InitializeAsync()
{
string databaseName = Guid.NewGuid().ToString();
(string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase(
databaseName
(Container, string connectionString, var connection) =
await TestSetupHelpers.ConfigurePostgreSqlDatabase();
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(
postgreSqlConnectionString: connectionString
);
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName);
var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory);

Client = TestSetupHelpers.ConfigureHttpClient(factory);
DatabaseUtilities = new DatabaseUtilities(
TestSetupHelpers.ConfigureFlotillaDbContext(connectionString)
TestSetupHelpers.ConfigurePostgreSqlContext(connectionString)
);

PlantService = serviceProvider.GetRequiredService<IPlantService>();
Expand Down
15 changes: 8 additions & 7 deletions backend/api.test/Controllers/RobotControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,30 @@
using Api.Database.Models;
using Api.Test.Database;
using Microsoft.EntityFrameworkCore;
using Testcontainers.PostgreSql;
using Xunit;

namespace Api.Test.Controllers
{
public class RobotControllerTests : IAsyncLifetime
{
public required DatabaseUtilities DatabaseUtilities;
public required PostgreSqlContainer Container;
public required HttpClient Client;
public required JsonSerializerOptions SerializerOptions;

public async Task InitializeAsync()
{
string databaseName = Guid.NewGuid().ToString();
(string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase(
databaseName
(Container, string connectionString, var connection) =
await TestSetupHelpers.ConfigurePostgreSqlDatabase();
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(
postgreSqlConnectionString: connectionString
);
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName);

Client = TestSetupHelpers.ConfigureHttpClient(factory);
SerializerOptions = TestSetupHelpers.ConfigureJsonSerializerOptions();

DatabaseUtilities = new DatabaseUtilities(
TestSetupHelpers.ConfigureFlotillaDbContext(connectionString)
TestSetupHelpers.ConfigurePostgreSqlContext(connectionString)
);
}

Expand Down Expand Up @@ -90,7 +91,7 @@ public async Task CheckThatRobotIsNotCreatedWhenInspectionAreaIsNotInInstallatio
// Arrange
var installation = await DatabaseUtilities.NewInstallation();

var wrongInstallation = await DatabaseUtilities.NewInstallation("wrongInstallation");
var wrongInstallation = await DatabaseUtilities.NewInstallation("wrongCode");
var wrongPlant = await DatabaseUtilities.NewPlant(wrongInstallation.InstallationCode);
var wrongInspectionArea = await DatabaseUtilities.NewInspectionArea(
wrongInstallation.InstallationCode,
Expand Down
14 changes: 8 additions & 6 deletions backend/api.test/Controllers/RoleAccessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@
using Api.Test.Mocks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Testcontainers.PostgreSql;
using Xunit;

namespace Api.Test.Controllers
{
public class RoleAccessTests : IAsyncLifetime
{
public required DatabaseUtilities DatabaseUtilities;
public required PostgreSqlContainer Container;
public required HttpClient Client;
public required JsonSerializerOptions SerializerOptions;
public required MockHttpContextAccessor HttpContextAccessor;

public async Task InitializeAsync()
{
string databaseName = Guid.NewGuid().ToString();
(string connectionString, var connection) = await TestSetupHelpers.ConfigureDatabase(
databaseName
(Container, string connectionString, var connection) =
await TestSetupHelpers.ConfigurePostgreSqlDatabase();
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(
postgreSqlConnectionString: connectionString
);
var factory = TestSetupHelpers.ConfigureWebApplicationFactory(databaseName);
var serviceProvider = TestSetupHelpers.ConfigureServiceProvider(factory);

Client = TestSetupHelpers.ConfigureHttpClient(factory);
Expand All @@ -35,7 +37,7 @@ public async Task InitializeAsync()
serviceProvider.GetService<IHttpContextAccessor>()!;

DatabaseUtilities = new DatabaseUtilities(
TestSetupHelpers.ConfigureFlotillaDbContext(connectionString)
TestSetupHelpers.ConfigurePostgreSqlContext(connectionString)
);
}

Expand All @@ -45,7 +47,7 @@ public async Task InitializeAsync()
public async Task CheckThatRequestingPlantsWithUnauthorizedUserFails()
{
// Arrange
var installation = await DatabaseUtilities.NewInstallation("TestInstallationCode");
var installation = await DatabaseUtilities.NewInstallation();
var plant = await DatabaseUtilities.NewPlant(installation.InstallationCode);

var accessRoleQuery = new CreateAccessRoleQuery
Expand Down
12 changes: 6 additions & 6 deletions backend/api.test/Database/DatabaseUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class DatabaseUtilities
private readonly RobotService _robotService;
private readonly UserInfoService _userInfoService;
private readonly SourceService _sourceService;
private readonly string _testInstallationCode = "testInstallationCode";
private readonly string _testInstallationName = "testInstallation";
private readonly string _testPlantCode = "testPlantCode";
private readonly string _testInspectionAreaName = "testInspectionArea";
private readonly string _testAreaName = "testArea";
private readonly string _testInstallationCode = "InstCode";
private readonly string _testInstallationName = "Installation";
private readonly string _testPlantCode = "PlantCode";
private readonly string _testInspectionAreaName = "InspectionArea";
private readonly string _testAreaName = "Area";

public DatabaseUtilities(FlotillaDbContext context)
{
Expand Down Expand Up @@ -106,7 +106,7 @@ public async Task<MissionRun> NewMissionRun(
IsarMissionId = isarMissionId,
MissionRunType = missionRunType,
Status = missionStatus,
DesiredStartTime = DateTime.Now,
DesiredStartTime = DateTime.UtcNow,
InspectionArea = inspectionArea,
Tasks = [],
InstallationCode = installationCode,
Expand Down
Loading
Loading