Skip to content

Commit

Permalink
Optimize code in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehats committed Dec 12, 2024
1 parent bea3e0c commit 9334985
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 105 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
- Filtering striae for `not specified` returned wrong results.
- Filtering by `borehole status` did not work.
- When saving with ctrl+s in the borehole sections, the form content was reset.
- When copying a borehole, the nested collection of observations were not copied.
- When copying a borehole, the nested collections of observations were not copied.
- There was a bug when changing the order, transparency or visibility of custom WMS user layers.
- The borehole status was not translated everywhere in the workflow panel.

Expand Down
163 changes: 59 additions & 104 deletions tests/api/Controllers/BoreholeControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ namespace BDMS.Controllers;
public class BoreholeControllerTest
{
private const int DefaultWorkgroupId = 1;
private const int MaxBoreholeSeedId = 1_002_999;
private int boreholeId;

private BdmsContext context;
private BoreholeController controller;
private static int testBoreholeId = 1000068;
private static int maxBoreholeId;

[TestInitialize]
public void TestInitialize()
{
context = ContextFactory.GetTestContext();
controller = GetTestController(context);
maxBoreholeId = context.Boreholes.Max(b => b.Id);
}

private BoreholeController GetTestController(BdmsContext testContext)
Expand Down Expand Up @@ -55,18 +54,16 @@ public static async Task ClassCleanup()
await cleanupContext.SaveChangesAsync();
}

// Remove all boreholes created during the test
var boreholesToDelete = cleanupContext.Boreholes.Where(b => b.Id > maxBoreholeId);
// This is necessary because the some tests work with multiple contexts and actually write to the database.
var boreholesToDelete = cleanupContext.Boreholes.Where(b => b.Id > MaxBoreholeSeedId);
cleanupContext.Boreholes.RemoveRange(boreholesToDelete);

await cleanupContext.DisposeAsync();
}

private async Task<List<Codelist>> GetCodelists(BdmsContext context, List<int> codelistIds)
{
return await context.Codelists
.Where(c => codelistIds.Contains(c.Id))
.ToListAsync().ConfigureAwait(false);
return await context.Codelists.Where(c => codelistIds.Contains(c.Id)).ToListAsync().ConfigureAwait(false);
}

[TestMethod]
Expand Down Expand Up @@ -100,54 +97,7 @@ public async Task EditBoreholeWithCompleteBorehole()

LoadBoreholeWithIncludes();

var newBorehole = new Borehole
{
Id = id,
CreatedById = 4,
UpdatedById = 4,
Locked = null,
LockedById = null,
WorkgroupId = 1,
IsPublic = true,
TypeId = 20101003,
LocationX = 2600000.0,
PrecisionLocationX = 5,
LocationY = 1200000.0,
PrecisionLocationY = 5,
LocationXLV03 = 600000.0,
PrecisionLocationXLV03 = 5,
LocationYLV03 = 200000.0,
PrecisionLocationYLV03 = 5,
OriginalReferenceSystem = ReferenceSystem.LV95,
ElevationZ = 450.5,
HrsId = 20106001,
TotalDepth = 100.0,
RestrictionId = 20111003,
RestrictionUntil = DateTime.UtcNow.AddYears(1),
NationalInterest = false,
OriginalName = "BH-257",
AlternateName = "Borehole 257",
LocationPrecisionId = 20113002,
ElevationPrecisionId = null,
ProjectName = "Project Alpha",
Country = "CH",
Canton = "ZH",
Municipality = "Zurich",
PurposeId = 22103002,
StatusId = 22104001,
QtDepthId = 22108005,
TopBedrockFreshMd = 10.5,
TopBedrockWeatheredMd = 8.0,
HasGroundwater = true,
Geometry = null,
Remarks = "Test borehole for project",
LithologyTopBedrockId = 15104934,
LithostratigraphyId = 15300259,
ChronostratigraphyId = 15001141,
ReferenceElevation = 500.0,
QtReferenceElevationId = 20114002,
ReferenceElevationTypeId = 20117003,
};
var newBorehole = GetBoreholeToAdd(id);

var boreholeToEdit = context.Boreholes.Single(c => c.Id == id);
Assert.AreEqual(1, boreholeToEdit.Stratigraphies.Count);
Expand Down Expand Up @@ -321,56 +271,9 @@ public async Task EditWithWrongCodelistCodesReturnsInternalServerError()
[TestMethod]
public async Task CopyBoreholeWithHydrotests()
{
boreholeId = maxBoreholeId + 1;
boreholeId = MaxBoreholeSeedId + 1;

var newBorehole = new Borehole
{
Id = boreholeId,
CreatedById = 4,
UpdatedById = 4,
Locked = null,
LockedById = null,
WorkgroupId = 1,
IsPublic = true,
TypeId = 20101003,
LocationX = 2600000.0,
PrecisionLocationX = 5,
LocationY = 1200000.0,
PrecisionLocationY = 5,
LocationXLV03 = 600000.0,
PrecisionLocationXLV03 = 5,
LocationYLV03 = 200000.0,
PrecisionLocationYLV03 = 5,
OriginalReferenceSystem = ReferenceSystem.LV95,
ElevationZ = 450.5,
HrsId = 20106001,
TotalDepth = 100.0,
RestrictionId = 20111003,
RestrictionUntil = DateTime.UtcNow.AddYears(1),
NationalInterest = false,
OriginalName = "BH-257",
AlternateName = "Borehole 257",
LocationPrecisionId = 20113002,
ElevationPrecisionId = null,
ProjectName = "Project Alpha",
Country = "CH",
Canton = "ZH",
Municipality = "Zurich",
PurposeId = 22103002,
StatusId = 22104001,
QtDepthId = 22108005,
TopBedrockFreshMd = 10.5,
TopBedrockWeatheredMd = 8.0,
HasGroundwater = true,
Geometry = null,
Remarks = "Test borehole for project",
LithologyTopBedrockId = 15104934,
LithostratigraphyId = 15300259,
ChronostratigraphyId = 15001141,
ReferenceElevation = 500.0,
QtReferenceElevationId = 20114002,
ReferenceElevationTypeId = 20117003,
};
var newBorehole = GetBoreholeToAdd(boreholeId);

var fieldMeasurementResult = new FieldMeasurementResult
{
Expand Down Expand Up @@ -639,6 +542,58 @@ private Borehole GetBorehole(int id)
return GetBoreholesWithIncludes(context.Boreholes).Single(b => b.Id == id);
}

private Borehole GetBoreholeToAdd(int id)
{
return new Borehole
{
Id = id,
CreatedById = 4,
UpdatedById = 4,
Locked = null,
LockedById = null,
WorkgroupId = 1,
IsPublic = true,
TypeId = 20101003,
LocationX = 2600000.0,
PrecisionLocationX = 5,
LocationY = 1200000.0,
PrecisionLocationY = 5,
LocationXLV03 = 600000.0,
PrecisionLocationXLV03 = 5,
LocationYLV03 = 200000.0,
PrecisionLocationYLV03 = 5,
OriginalReferenceSystem = ReferenceSystem.LV95,
ElevationZ = 450.5,
HrsId = 20106001,
TotalDepth = 100.0,
RestrictionId = 20111003,
RestrictionUntil = DateTime.UtcNow.AddYears(1),
NationalInterest = false,
OriginalName = "BH-257",
AlternateName = "Borehole 257",
LocationPrecisionId = 20113002,
ElevationPrecisionId = null,
ProjectName = "Project Alpha",
Country = "CH",
Canton = "ZH",
Municipality = "Zurich",
PurposeId = 22103002,
StatusId = 22104001,
QtDepthId = 22108005,
TopBedrockFreshMd = 10.5,
TopBedrockWeatheredMd = 8.0,
HasGroundwater = true,
Geometry = null,
Remarks = "Test borehole for project",
LithologyTopBedrockId = 15104934,
LithostratigraphyId = 15300259,
ChronostratigraphyId = 15001141,
ReferenceElevation = 500.0,
QtReferenceElevationId = 20114002,
ReferenceElevationTypeId = 20117003,
};
}

// Get the id of a borehole with certain conditions.
private int GetBoreholeIdToCopy()
{
Expand Down

0 comments on commit 9334985

Please sign in to comment.