Skip to content

Commit

Permalink
Improve consistency of tests by removing InitDb in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Christdej committed Oct 21, 2024
1 parent aa96881 commit 5b11d42
Show file tree
Hide file tree
Showing 34 changed files with 471 additions and 686 deletions.
2 changes: 1 addition & 1 deletion backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ We use [dotnet format](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet
to format and verify code style in backend based on the
[C# coding conventions](https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions).

Dotnet format is included in the .NET6 SDK.
Dotnet format is included in the .NET SDK.

To check the formatting, run the following command in the backend folder:

Expand Down
79 changes: 35 additions & 44 deletions backend/api.test/Client/AreaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Api.Controllers.Models;
using Api.Database.Context;
using Api.Database.Models;
using Api.Test.Database;
using Microsoft.AspNetCore.Mvc.Testing;
using Xunit;
namespace Api.Test
namespace Api.Test.Client
{
[Collection("Database collection")]
public class AreaTests : IClassFixture<TestWebApplicationFactory<Program>>
{
private readonly HttpClient _client;
private readonly DatabaseUtilities _databaseUtilities;
private readonly JsonSerializerOptions _serializerOptions =
new()
{
Expand All @@ -37,6 +40,10 @@ public AreaTests(TestWebApplicationFactory<Program> factory)
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
TestAuthHandler.AuthenticationScheme
);

object? context = factory.Services.GetService(typeof(FlotillaDbContext)) as FlotillaDbContext ?? throw new ArgumentNullException(nameof(factory));
_databaseUtilities = new DatabaseUtilities((FlotillaDbContext)context);

}

[Fact]
Expand Down Expand Up @@ -139,32 +146,15 @@ public async Task AreaTest()
[Fact]
public async Task MissionIsCreatedInArea()
{
// Arrange
// Robot
string robotUrl = "/robots";
var robotResponse = await _client.GetAsync(robotUrl);
Assert.True(robotResponse.IsSuccessStatusCode);
var robots = await robotResponse.Content.ReadFromJsonAsync<List<Robot>>(_serializerOptions);
Assert.NotNull(robots);
var robot = robots.Where(robot => robot.Name == "Shockwave").First();
string robotId = robot.Id;
// Arrange - Initialise area
var installation = await _databaseUtilities.ReadOrNewInstallation();
var plant = await _databaseUtilities.ReadOrNewPlant(installation.InstallationCode);
var deck = await _databaseUtilities.ReadOrNewDeck(installation.InstallationCode, plant.PlantCode);
var area = await _databaseUtilities.ReadOrNewArea(installation.InstallationCode, plant.PlantCode, deck.Name);

// Installation
string installationUrl = "/installations";
var installationResponse = await _client.GetAsync(installationUrl);
Assert.True(installationResponse.IsSuccessStatusCode);
var installations = await installationResponse.Content.ReadFromJsonAsync<List<Installation>>(_serializerOptions);
Assert.NotNull(installations);
var installation = installations.Where(installation => installation.InstallationCode == robot.CurrentInstallation?.InstallationCode).First();

// Area
string areaUrl = "/areas";
var areaResponse = await _client.GetAsync(areaUrl);
Assert.True(areaResponse.IsSuccessStatusCode);
var areas = await areaResponse.Content.ReadFromJsonAsync<List<AreaResponse>>(_serializerOptions);
Assert.NotNull(areas);
var area = areas.Where(area => area.InstallationCode == installation.InstallationCode).First();
string areaId = area.Id;
// Arrange - Robot
var robot = await _databaseUtilities.NewRobot(RobotStatus.Available, installation);
string robotId = robot.Id;

string testMissionName = "testMissionInAreaTest";

Expand All @@ -189,7 +179,7 @@ public async Task MissionIsCreatedInArea()
RobotId = robotId,
DesiredStartTime = DateTime.UtcNow,
InstallationCode = installation.InstallationCode,
AreaName = area.AreaName,
AreaName = area.Name,
Name = testMissionName,
Tasks = tasks
};
Expand All @@ -208,8 +198,8 @@ public async Task MissionIsCreatedInArea()
var mission = await missionResponse.Content.ReadFromJsonAsync<MissionRun>(_serializerOptions);
Assert.NotNull(mission);
Assert.NotNull(mission.MissionId);

var areaMissionsResponse = await _client.GetAsync(areaUrl + $"/{areaId}/mission-definitions");
string areaUrl = "/areas";
var areaMissionsResponse = await _client.GetAsync(areaUrl + $"/{area.Id}/mission-definitions");

// Assert
Assert.True(areaMissionsResponse.IsSuccessStatusCode);
Expand All @@ -222,14 +212,12 @@ public async Task MissionIsCreatedInArea()
public async Task SafePositionTest()
{
// Arrange
string areaUrl = "/areas";
var areaResponse = await _client.GetAsync(areaUrl);
Assert.True(areaResponse.IsSuccessStatusCode);
var areaResponses = await areaResponse.Content.ReadFromJsonAsync<List<AreaResponse>>(_serializerOptions);
Assert.NotNull(areaResponses);
var area = areaResponses[0];
string areaName = area.AreaName;
string installationCode = area.InstallationCode;
var installation = await _databaseUtilities.ReadOrNewInstallation();
var plant = await _databaseUtilities.ReadOrNewPlant(installation.InstallationCode);
var deck = await _databaseUtilities.ReadOrNewDeck(installation.InstallationCode, plant.PlantCode);
var area = await _databaseUtilities.ReadOrNewArea(installation.InstallationCode, plant.PlantCode, deck.Name);
string areaName = area.Name;
string installationCode = installation.InstallationCode;

string addSafePositionUrl = $"/areas/{installationCode}/{areaName}/safe-position";
var testPosition = new Position
Expand All @@ -255,7 +243,7 @@ public async Task SafePositionTest()
"application/json"
);

areaResponse = await _client.PostAsync(addSafePositionUrl, content);
var areaResponse = await _client.PostAsync(addSafePositionUrl, content);
Assert.True(areaResponse.IsSuccessStatusCode);
var areaContent = await areaResponse.Content.ReadFromJsonAsync<AreaResponse>(_serializerOptions);
Assert.NotNull(areaContent);
Expand All @@ -275,12 +263,11 @@ public async Task SafePositionTest()
[Fact]
public async Task UpdateDefaultLocalizationPoseOnDeck()
{
string deckUrl = "/decks";
var deckResponse = await _client.GetAsync(deckUrl);
Assert.True(deckResponse.IsSuccessStatusCode);
var decks = await deckResponse.Content.ReadFromJsonAsync<List<DeckResponse>>(_serializerOptions);
Assert.NotNull(decks);
var deck = decks[0];
// Arrange
var installation = await _databaseUtilities.ReadOrNewInstallation();
var plant = await _databaseUtilities.ReadOrNewPlant(installation.InstallationCode);
var deck = await _databaseUtilities.ReadOrNewDeck(installation.InstallationCode, plant.PlantCode);

string deckId = deck.Id;

string url = $"/decks/{deckId}/update-default-localization-pose";
Expand Down Expand Up @@ -308,9 +295,13 @@ public async Task UpdateDefaultLocalizationPoseOnDeck()
null,
"application/json"
);

// Act
var putResponse = await _client.PutAsync(url, content);
Assert.True(putResponse.IsSuccessStatusCode);
var putDeck = await putResponse.Content.ReadFromJsonAsync<DeckResponse>(_serializerOptions);

// Assert
Assert.NotNull(putDeck);
Assert.NotNull(putDeck.DefaultLocalizationPose);
Assert.True(putDeck.DefaultLocalizationPose.Position.Z.Equals(query.Pose.Position.Z));
Expand Down
Loading

0 comments on commit 5b11d42

Please sign in to comment.