diff --git a/API/Program.cs b/API/Program.cs index 00903ba..8babbea 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -66,11 +66,12 @@ private static void Main(string[] args) app.MapGet("/search", (SoilDbContext context, string name = null, string folder = null, string soilType = null, string country = null, double latitude = double.NaN, double longitude = double.NaN, double radius = double.NaN, string fullName = null, + int recordNumber = int.MinValue, string cropName = null, Values thickness = null, Values cll = null, bool cllIsGrav = false, Values pawc = null, int numToReturn = 0, OutputFormatEnum output = OutputFormatEnum.Names) => Soil.Search(context, name, folder, soilType, country, latitude, longitude, radius, - fullName, cropName, thickness?.Doubles, cll?.Doubles, cllIsGrav, pawc?.Doubles, numToReturn) + fullName, recordNumber, cropName, thickness?.Doubles, cll?.Doubles, cllIsGrav, pawc?.Doubles, numToReturn) .ToXMLResult(output)); // Endpoint: Get graph of soil. diff --git a/API/Services/SoilServices.cs b/API/Services/SoilServices.cs index f0bdb4a..c601979 100644 --- a/API/Services/SoilServices.cs +++ b/API/Services/SoilServices.cs @@ -42,6 +42,7 @@ public static void Add(SoilDbContext context, Models.Soil[] soils) /// The latitude of the point. /// The longitude of the point. /// The full name of the soil. + /// The record number of the soil. /// The name of the crop to use when cll or pawc is provided. /// The thickness of the cll or PAWC values. /// The crop lower limit (volumetric). @@ -51,6 +52,7 @@ public static void Add(SoilDbContext context, Models.Soil[] soils) public static SoilsFromDb Search(SoilDbContext context, string name = null, string folder = null, string soilType = null, string country = null, double latitude = double.NaN, double longitude = double.NaN, double radius = double.NaN, string fullName = null, + int recordNumber = int.MinValue, string cropName = null, double[] thickness = null, double[] cll = null, bool cllIsGrav = false, double[] pawc = null, int numToReturn = 0) { @@ -67,6 +69,8 @@ public static SoilsFromDb Search(SoilDbContext context, string name = null, stri soils = soils.Where(s => s.Country == country); if (cropName != null) soils = soils.Where(s => s.Water.SoilCrops.Any(sc => sc.Name == cropName.ToLower())); + if (recordNumber != int.MinValue) + soils = soils.Where(s => s.RecordNumber == recordNumber); // The above where clauses can be converted to SQL by EntityFramework. // The lat/long, cll and pawc orderby clauses below cannot be converted to SQL so must be done in memory by LINQ @@ -135,7 +139,7 @@ public static SoilsFromDb Search(SoilDbContext context, string name = null, stri /// The plant available water(mm). public static double PAWC(SoilDbContext context, string fullName, string cropName) { - var soils = context.Soils.Where(s => s.FullName == fullName); + var soils = context.Soils.Where(s => s.FullName.Trim() == fullName); if (!soils.Any()) throw new Exception($"Soil with full name {fullName} not found."); return soils.Include(s => s.Water) @@ -159,7 +163,7 @@ public static double PAWC(SoilDbContext context, string fullName, string cropNam /// The plant available water(mm). public static double PAW(SoilDbContext context, string fullName, string cropName, IReadOnlyList thickness, IReadOnlyList sw, bool swIsGrav) { - var soils = context.Soils.Where(s => s.FullName == fullName); + var soils = context.Soils.Where(s => s.FullName.Trim() == fullName); if (!soils.Any()) throw new Exception($"Soil with full name {fullName} not found."); var soil = soils.Include(s => s.Water) diff --git a/Tests/UnitTest.cs b/Tests/UnitTest.cs index 6884769..9071e03 100644 --- a/Tests/UnitTest.cs +++ b/Tests/UnitTest.cs @@ -64,7 +64,7 @@ public void GetUsingFullName_ShouldIgnoreTrailingSpaces() var soils = API.Services.Soil.Search(context, fullName: "Soils/Tests/Clay (Kerikeri No1353)") .ToSoils(); Assert.That(soils.Length, Is.EqualTo(1)); - Assert.That(soils[0].Name, Is.EqualTo("Clay (Kerikeri No1353)")); + Assert.That(soils[0].Name.Trim(), Is.EqualTo("Clay (Kerikeri No1353)")); } [Test] @@ -172,6 +172,21 @@ public void GetWithCLL_ShouldReturnSoilWithClosestCLL() Assert.That(soils[0].Name, Is.EqualTo("Clay (Kerikeri No1353)")); } + [Test] + public void GetWithRecordNumber_ShouldReturnSoil() + { + var options = MockDb.CreateOptions(); + using var context = new SoilDbContext(options); + API.Services.Soil.Add(context, [ + ResourceFile.FromResourceXML("Tests.testsoil1.xml"), + ResourceFile.FromResourceXML("Tests.testsoil2.xml") + ]); + + var soils = API.Services.Soil.Search(context, recordNumber: 1) + .ToSoils(); + Assert.That(soils.Length, Is.EqualTo(1)); + Assert.That(soils[0].Name, Is.EqualTo("Red Chromosol (Billa Billa No066)")); + } [Test] public void SoilsToXML_ShouldReturnValidXML() @@ -230,6 +245,18 @@ public void PAWC_ShouldReturnCorrectValue() Assert.That(pawc, Is.EqualTo(214.89).Within(0.000001)); } + [Test] + public void PAWC_ShouldReturnCorrectValueFromSoilWithTrailingSpacesInName() + { + var options = MockDb.CreateOptions(); + using var context = new SoilDbContext(options); + var folder = ResourceFile.Get("Tests.testsoil1-trailing-spaces.xml").ToSoils(); + API.Services.Soil.Add(context, folder); + + var pawc = API.Services.Soil.PAWC(context, "Soils/Tests/Clay (Kerikeri No1353)", cropName: "wheat"); + Assert.That(pawc, Is.EqualTo(214.89).Within(0.000001)); + } + [Test] public void PAW_ShouldReturnCorrectValue() { @@ -246,6 +273,20 @@ public void PAW_ShouldReturnCorrectValue() } + [Test] + public void PAW_ShouldReturnCorrectValueFromSoilWithTrailingSpacesInName() + { + var options = MockDb.CreateOptions(); + using var context = new SoilDbContext(options); + var folder = ResourceFile.Get("Tests.testsoil1-trailing-spaces.xml").ToSoils(); + API.Services.Soil.Add(context, folder); + + var paw = API.Services.Soil.PAW(context, "Soils/Tests/Clay (Kerikeri No1353)", cropName: "wheat", + thickness: [ 150, 500 ], + sw: [ 0.4, 0.3 ], swIsGrav: false); + Assert.That(paw, Is.EqualTo(21.72).Within(0.000001)); + } + [Test] public void PAWFromGravimetric_ShouldReturnCorrectValue() { diff --git a/Tests/testsoil1-trailing-spaces.xml b/Tests/testsoil1-trailing-spaces.xml index 8cadf60..0457f62 100644 --- a/Tests/testsoil1-trailing-spaces.xml +++ b/Tests/testsoil1-trailing-spaces.xml @@ -1,6 +1,6 @@ - + 1265 Soils/Tests/Clay (Kerikeri No1353)