Skip to content
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
3 changes: 2 additions & 1 deletion API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions API/Services/SoilServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static void Add(SoilDbContext context, Models.Soil[] soils)
/// <param name="latitude">The latitude of the point.</param>
/// <param name="longitude">The longitude of the point.</param>
/// <param name="fullName">The full name of the soil.</param>
/// <param name="recordNumber">The record number of the soil.</param>
/// <param name="cropName">The name of the crop to use when cll or pawc is provided.</param>
/// <param name="thickness">The thickness of the cll or PAWC values.</param>
/// <param name="cll">The crop lower limit (volumetric).</param>
Expand All @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -135,7 +139,7 @@ public static SoilsFromDb Search(SoilDbContext context, string name = null, stri
/// <returns>The plant available water(mm).</returns>
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)
Expand All @@ -159,7 +163,7 @@ public static double PAWC(SoilDbContext context, string fullName, string cropNam
/// <returns>The plant available water(mm).</returns>
public static double PAW(SoilDbContext context, string fullName, string cropName, IReadOnlyList<double> thickness, IReadOnlyList<double> 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)
Expand Down
43 changes: 42 additions & 1 deletion Tests/UnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<SoilDbContext>();
using var context = new SoilDbContext(options);
API.Services.Soil.Add(context, [
ResourceFile.FromResourceXML<API.Models.Soil>("Tests.testsoil1.xml"),
ResourceFile.FromResourceXML<API.Models.Soil>("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()
Expand Down Expand Up @@ -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<SoilDbContext>();
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()
{
Expand All @@ -246,6 +273,20 @@ public void PAW_ShouldReturnCorrectValue()
}


[Test]
public void PAW_ShouldReturnCorrectValueFromSoilWithTrailingSpacesInName()
{
var options = MockDb.CreateOptions<SoilDbContext>();
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()
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/testsoil1-trailing-spaces.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<folder version="36" creator="Apsim 7.7-r3678" name="Soils">
<Folder name="Tests">
<Soil name="Clay (Kerikeri No1353)">
<Soil name="Clay (Kerikeri No1353) ">
<RecordNumber>1265</RecordNumber>
<FullName>Soils/Tests/Clay (Kerikeri No1353) </FullName>
<ASCOrder />
Expand Down