Skip to content

Commit

Permalink
Clean up files
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehats committed Dec 16, 2024
1 parent d1ec2fe commit 9fe668f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 308 deletions.
61 changes: 4 additions & 57 deletions src/api/Controllers/BoreholeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int
borehole.MarkAsNew();
borehole.Completions?.MarkAsNew();
borehole.Sections?.MarkAsNew();
borehole.Observations?.MarkAsNew();

foreach (var stratigraphy in borehole.Stratigraphies)
{
Expand All @@ -171,69 +170,17 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int
stratigraphy.ChronostratigraphyLayers?.MarkAsNew();
stratigraphy.LithostratigraphyLayers?.MarkAsNew();
}
foreach (var lithologicalDescription in stratigraphy.LithologicalDescriptions)
{
lithologicalDescription.Id = 0;
}

foreach (var faciesDescription in stratigraphy.FaciesDescriptions)
{
faciesDescription.Id = 0;
}

foreach (var chronostratigraphy in stratigraphy.ChronostratigraphyLayers)
{
chronostratigraphy.Id = 0;
}

foreach (var lithostratigraphy in stratigraphy.LithostratigraphyLayers)
{
lithostratigraphy.Id = 0;
}
}

foreach (var completion in borehole.Completions)
{
completion.Id = 0;
foreach (var casing in completion.Casings)
{
casing.Id = 0;
foreach (var casingElement in casing.CasingElements)
{
casingElement.Id = 0;
}
}

foreach (var instrumentation in completion.Instrumentations)
{
instrumentation.Id = 0;
}

foreach (var backfill in completion.Backfills)
{
backfill.Id = 0;
}
}

foreach (var section in borehole.Sections)
{
section.Id = 0;
foreach (var sectionElement in section.SectionElements)
{
sectionElement.Id = 0;
}
}

foreach (var observation in borehole.Observations)
{
observation.Id = 0;
observation.MarkAsNew();
if (observation is FieldMeasurement fieldMeasurement)
{
if (fieldMeasurement.FieldMeasurementResults != null)
{
foreach (var fieldMeasurementResult in fieldMeasurement.FieldMeasurementResults)
{
fieldMeasurementResult.Id = 0;
fieldMeasurementResult.MarkAsNew();
}
}
}
Expand All @@ -244,7 +191,7 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int
{
foreach (var hydrotestResult in hydrotest.HydrotestResults)
{
hydrotestResult.Id = 0;
hydrotestResult.MarkAsNew();
}
}

Expand Down Expand Up @@ -279,7 +226,7 @@ public async Task<ActionResult<int>> CopyAsync([Required] int id, [Required] int

foreach (var boreholeGeometry in borehole.BoreholeGeometry)
{
boreholeGeometry.Id = 0;
boreholeGeometry.MarkAsNew();
}

borehole.UpdatedBy = null;
Expand Down
60 changes: 3 additions & 57 deletions src/api/Controllers/UploadController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ public async Task<ActionResult<int>> UploadFileAsync(int workgroupId, IFormFile
return Problem("Error while importing borehole(s).");
}
}
{
return new List<int>();
}

internal static int GetPrecision(IReaderRow row, string fieldName)
{
Expand Down Expand Up @@ -329,6 +326,9 @@ private void ValidateAttachments(BoreholeImport borehole, IList<IFormFile>? atta
var boreholeFileNames = borehole.Attachments
.Split(",")
.Select(s => s.Trim())
.Where(s => !string.IsNullOrEmpty(s))
.ToList();

foreach (var boreholeFileName in boreholeFileNames)
{
// Check if the name of any attached file matches the name of the borehole file
Expand All @@ -339,60 +339,6 @@ private void ValidateAttachments(BoreholeImport borehole, IList<IFormFile>? atta
}
}

private void ValidateLithologyImports(List<int> importIds, List<LithologyImport> lithologyImports)
{
// Iterate over provided lithology imports, validate them, and create error messages when necessary.
foreach (var lithology in lithologyImports.Select((value, index) => (value, index)))
{
if (lithology.value.ImportId == 0) AddValidationErrorToModelState(lithology.index, string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "import_id"), false);

if (lithology.value.StratiImportId == 0) AddValidationErrorToModelState(lithology.index, string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "strati_import_id"), false);

if (lithology.value.FromDepth == null) AddValidationErrorToModelState(lithology.index, string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "from_depth"), false);

if (lithology.value.ToDepth == null) AddValidationErrorToModelState(lithology.index, string.Format(CultureInfo.InvariantCulture, nullOrEmptyMsg, "to_depth"), false);

// Check if all import ids exist in the list of provided import ids (from borehole import file)
if (!importIds.Contains(lithology.value.ImportId)) AddValidationErrorToModelState(lithology.index, $"Borehole with {nameof(LithologyImport.ImportId)} '{lithology.value.ImportId}' not found.", false);
{
// Check if all multi code list values are numbers
try
{
ParseMultiValueCodeListIds(lithology.value);
}
catch
{
AddValidationErrorToModelState(lithology.index, $"One or more invalid (not a number) code list id in any of the following properties: {nameof(LithologyImport.ColorIds)}, {nameof(LithologyImport.OrganicComponentIds)}, {nameof(LithologyImport.GrainShapeIds)}, {nameof(LithologyImport.GrainGranularityIds)}, {nameof(LithologyImport.Uscs3Ids)}, {nameof(LithologyImport.DebrisIds)}.", false);
}
}

// Group lithology records by import id to get lithologies per borehole.
var boreholeGroups = lithologyImports.GroupBy(l => l.ImportId);

foreach (var boreholeLithologies in boreholeGroups)
{
// Group lithology records per borehole by strati import id to get lithologies per stratigraphy.
var stratiGroups = boreholeLithologies.GroupBy(bhoGroup => bhoGroup.StratiImportId);
foreach (var stratiGroup in stratiGroups)
{
// Check if all records with the same strati import id have the same strati name.
if (stratiGroup.Select(s => s.StratiName).Distinct().Count() > 1)
{
ModelState.AddModelError($"import_id{stratiGroup.First().ImportId}", $"Lithology with {nameof(LithologyImport.StratiImportId)} '{stratiGroup.Key}' has various {nameof(LithologyImport.StratiName)}.");
}

// Check if all records with the same strati import id have the same strati date.
if (stratiGroup.Select(s => s.StratiDate).Distinct().Count() > 1)
{
ModelState.AddModelError($"import_id{stratiGroup.First().ImportId}", $"Lithology with {nameof(LithologyImport.StratiImportId)} '{stratiGroup.Key}' has various {nameof(LithologyImport.StratiDate)}.");
}
}
}
}

}
}

internal static bool CompareValuesWithTolerance(double? firstValue, double? secondValue, double tolerance)
{
if (firstValue == null && secondValue == null) return true;
Expand Down
194 changes: 0 additions & 194 deletions tests/api/Controllers/UploadControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,137 +479,6 @@ public async Task UploadJsonWithDuplicatesExistingBoreholeShouldReturnError()
CollectionAssert.AreEquivalent(new[] { $"Borehole with same Coordinates (+/- 2m) and same {nameof(Borehole.TotalDepth)} already exists in database.", }, problemDetails.Errors["Borehole0"]);
}

[TestMethod]
public async Task UploadLithologyShouldSaveData()
{
httpClientFactoryMock
.Setup(cf => cf.CreateClient(It.IsAny<string>()))
.Returns(() => new HttpClient())
.Verifiable();

var boreholeCsvFile = GetFormFileByExistingFile("data_sets/import_litho/borehole.csv");
var lithoCsvFile = GetFormFileByExistingFile("data_sets/import_litho/litho.csv");

ActionResult<int> response = await controller.UploadFileAsync(workgroupId: 1, boreholeCsvFile, lithologyFile: lithoCsvFile, attachments: null);

ActionResultAssert.IsOk(response.Result);
OkObjectResult okResult = (OkObjectResult)response.Result!;
Assert.AreEqual(1, okResult.Value);

// Assert imported values
var borehole = GetBoreholesWithIncludes(context.Boreholes).ToList().Find(b => b.OriginalName == "Seth Patel");
Assert.AreEqual(1, borehole.WorkgroupId);
Assert.AreEqual("Seth Patel", borehole.OriginalName);

// Assert imported stratigraphy & lithologies
Assert.AreEqual(2, borehole.Stratigraphies.Count);

// First stratigraphy
var stratigraphy = borehole.Stratigraphies.First();
Assert.AreEqual(new DateTime(2021, 8, 6), stratigraphy.Date?.Date);
Assert.AreEqual("Bennett", stratigraphy.Name);
Assert.AreEqual(2, stratigraphy.Layers.Count);
var lithology = stratigraphy.Layers.First(l => l.FromDepth == 0.125);
Assert.AreEqual(100, lithology.ToDepth);
Assert.AreEqual(false, lithology.IsLast);
Assert.AreEqual(9001, lithology.DescriptionQualityId);
Assert.AreEqual(15104915, lithology.LithologyId);
Assert.AreEqual(15302034, lithology.LithostratigraphyId);
Assert.AreEqual("Granite", lithology.OriginalUscs);
Assert.AreEqual(23107001, lithology.UscsDeterminationId);
Assert.AreEqual(23101005, lithology.Uscs1Id);
Assert.AreEqual(21101001, lithology.GrainSize1Id);
Assert.AreEqual(23101008, lithology.Uscs2Id);
Assert.AreEqual(21103008, lithology.GrainSize2Id);
Assert.AreEqual(false, lithology.IsStriae);
Assert.AreEqual(21103003, lithology.ConsistanceId);
Assert.AreEqual(21101001, lithology.PlasticityId);
Assert.AreEqual(21102007, lithology.CompactnessId);
Assert.AreEqual(21116005, lithology.CohesionId);
Assert.AreEqual(21105002, lithology.HumidityId);
Assert.AreEqual(21106004, lithology.AlterationId);
Assert.AreEqual("instruction set Dynamic backing up Lock", lithology.Notes);
Assert.AreEqual("trace back Peso", lithology.OriginalLithology);
Assert.AreEqual(30000018, lithology.GradationId);
Assert.AreEqual(15104916, lithology.LithologyTopBedrockId);
Assert.AreEqual(2, lithology.ColorCodelists.Count);
Assert.AreEqual(2, lithology.DebrisCodelists.Count);
Assert.AreEqual(2, lithology.GrainAngularityCodelists.Count);
Assert.AreEqual(2, lithology.GrainShapeCodelists.Count);
Assert.AreEqual(3, lithology.OrganicComponentCodelists.Count);
Assert.AreEqual(3, lithology.Uscs3Codelists.Count);

lithology = stratigraphy.Layers.First(l => l.FromDepth == 11);
Assert.AreEqual(12, lithology.ToDepth);

// Second stratigraphy
stratigraphy = borehole.Stratigraphies.Skip(1).First();
Assert.AreEqual(1, stratigraphy.Layers.Count);
lithology = stratigraphy.Layers.First();
Assert.AreEqual(55, lithology.FromDepth);
Assert.AreEqual(55.23, lithology.ToDepth);
}

[TestMethod]
public async Task UploadLithologyWithMultiCodeListPropertiesProvidedShouldSaveData()
{
httpClientFactoryMock
.Setup(cf => cf.CreateClient(It.IsAny<string>()))
.Returns(() => new HttpClient())
.Verifiable();

var boreholeCsvFile = GetFormFileByExistingFile("data_sets/import_litho_with_multi_code_list_properties/borehole.csv");
var lithoCsvFile = GetFormFileByExistingFile("data_sets/import_litho_with_multi_code_list_properties/litho.csv");

ActionResult<int> response = await controller.UploadFileAsync(workgroupId: 1, boreholeCsvFile, lithologyFile: lithoCsvFile, attachments: null);

ActionResultAssert.IsOk(response.Result);
OkObjectResult okResult = (OkObjectResult)response.Result!;
Assert.AreEqual(1, okResult.Value);

// Assert imported values
var borehole = GetBoreholesWithIncludes(context.Boreholes)
.ToList().Find(b => b.OriginalName == "Seth Patel");
Assert.AreEqual(1, borehole.WorkgroupId);
Assert.AreEqual("Seth Patel", borehole.OriginalName);

// Assert imported stratigraphy & lithologies
Assert.AreEqual(2, borehole.Stratigraphies.Count);

// First stratigraphy
var stratigraphy = borehole.Stratigraphies.First();
Assert.AreEqual(2, stratigraphy.Layers.Count);
var lithology = stratigraphy.Layers.First(l => l.FromDepth == 0.125);
Assert.AreEqual(100, lithology.ToDepth);
Assert.AreEqual(2, lithology.ColorCodelists.Count);
Assert.AreEqual(2, lithology.DebrisCodelists.Count);
Assert.AreEqual(3, lithology.GrainAngularityCodelists.Count);
Assert.AreEqual(3, lithology.GrainShapeCodelists.Count);
Assert.AreEqual(3, lithology.OrganicComponentCodelists.Count);
Assert.AreEqual(3, lithology.Uscs3Codelists.Count);
lithology = stratigraphy.Layers.First(l => l.FromDepth == 11);
Assert.AreEqual(12, lithology.ToDepth);
Assert.AreEqual(0, lithology.ColorCodelists.Count);
Assert.AreEqual(1, lithology.DebrisCodelists.Count);
Assert.AreEqual(0, lithology.GrainAngularityCodelists.Count);
Assert.AreEqual(0, lithology.GrainShapeCodelists.Count);
Assert.AreEqual(0, lithology.OrganicComponentCodelists.Count);
Assert.AreEqual(0, lithology.Uscs3Codelists.Count);

// Second stratigraphy
stratigraphy = borehole.Stratigraphies.Skip(1).First();
Assert.AreEqual(1, stratigraphy.Layers.Count);
lithology = stratigraphy.Layers.First();
Assert.AreEqual(55, lithology.FromDepth);
Assert.AreEqual(55.23, lithology.ToDepth);
Assert.AreEqual(0, lithology.ColorCodelists.Count);
Assert.AreEqual(0, lithology.DebrisCodelists.Count);
Assert.AreEqual(0, lithology.GrainAngularityCodelists.Count);
Assert.AreEqual(0, lithology.GrainShapeCodelists.Count);
Assert.AreEqual(0, lithology.OrganicComponentCodelists.Count);
Assert.AreEqual(2, lithology.Uscs3Codelists.Count);
}

[TestMethod]
public async Task UploadShouldSaveDataToDatabaseAsync()
{
Expand Down Expand Up @@ -1128,69 +997,6 @@ public async Task UploadRequiredHeadersMissingShouldReturnError()
StringAssert.Contains(problemDetails.Detail, "Header with name 'ImportId'[0] was not found.");
}

[TestMethod]
public async Task UploadLithologyRequiredHeadersMissingShouldReturnError()
{
var boreholeCsvFile = GetFormFileByExistingFile("data_sets/import_litho_missing_required_headers/borehole.csv");
var lithoCsvFile = GetFormFileByExistingFile("data_sets/import_litho_missing_required_headers/litho.csv");

ActionResult<int> response = await controller.UploadFileAsync(workgroupId: 1, boreholeCsvFile, lithoCsvFile);

Assert.IsInstanceOfType(response.Result, typeof(ObjectResult));
ObjectResult result = (ObjectResult)response.Result!;
ActionResultAssert.IsBadRequest(result);

ProblemDetails problemDetails = (ProblemDetails)result.Value!;
StringAssert.Contains(problemDetails.Detail, "Header with name 'ImportId'[0] was not found.");
StringAssert.Contains(problemDetails.Detail, "Header with name 'StratiImportId'[0] was not found.");
StringAssert.Contains(problemDetails.Detail, "Header with name 'FromDepth'[0] was not found.");
StringAssert.Contains(problemDetails.Detail, "Header with name 'ToDepth'[0] was not found.");
}

[TestMethod]
public async Task UploadLithologyDiffInStratiAttributesForSameStratiIdShouldReturnError()
{
var boreholeCsvFile = GetFormFileByExistingFile("data_sets/import_litho_diff_in_strati_attributes_for_same_starti_id/borehole.csv");
var lithoCsvFile = GetFormFileByExistingFile("data_sets/import_litho_diff_in_strati_attributes_for_same_starti_id/litho.csv");

ActionResult<int> response = await controller.UploadFileAsync(workgroupId: 1, boreholeCsvFile, lithoCsvFile);

Assert.IsInstanceOfType(response.Result, typeof(ObjectResult));
ObjectResult result = (ObjectResult)response.Result!;
ActionResultAssert.IsBadRequest(result);

ValidationProblemDetails problemDetails = (ValidationProblemDetails)result.Value!;
Assert.AreEqual(1, problemDetails.Errors.Count);

CollectionAssert.AreEquivalent(
new[]
{
$"Lithology with {nameof(LithologyImport.StratiImportId)} '1001' has various {nameof(LithologyImport.StratiName)}.",
$"Lithology with {nameof(LithologyImport.StratiImportId)} '1001' has various {nameof(LithologyImport.StratiDate)}.",
},
problemDetails.Errors["import_id1"]);
}

[TestMethod]
public async Task UploadLithologyWithImportIdNotPresentInBoreholeFileShouldReturnError()
{
var boreholeCsvFile = GetFormFileByExistingFile("data_sets/import_litho_import_id_not_present_in_borehole_file/borehole.csv");
var lithoCsvFile = GetFormFileByExistingFile("data_sets/import_litho_import_id_not_present_in_borehole_file/litho.csv");

ActionResult<int> response = await controller.UploadFileAsync(workgroupId: 1, boreholeCsvFile, lithoCsvFile);

Assert.IsInstanceOfType(response.Result, typeof(ObjectResult));
ObjectResult result = (ObjectResult)response.Result!;
ActionResultAssert.IsBadRequest(result);

ValidationProblemDetails problemDetails = (ValidationProblemDetails)result.Value!;
Assert.AreEqual(1, problemDetails.Errors.Count);

CollectionAssert.AreEquivalent(
new[] { "Borehole with ImportId '2' not found." },
problemDetails.Errors["Row2"]);
}

[TestMethod]
public async Task UploadDuplicateBoreholesInFileShouldReturnError()
{
Expand Down
Loading

0 comments on commit 9fe668f

Please sign in to comment.