Skip to content

Commit

Permalink
Merge pull request #77 from FMSC-Measurements/Development
Browse files Browse the repository at this point in the history
Version 3.6.6
  • Loading branch information
BenCamps committed Apr 25, 2024
2 parents c0ba995 + a842b15 commit 21aefaa
Show file tree
Hide file tree
Showing 41 changed files with 1,668 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<Compile Include="$(MSBuildThisFileDirectory)DatabaseInitializer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TableDefinitionsTestDataProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TableNamesTestDataProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TemplateDatabaseInitializer.cs" />
</ItemGroup>
</Project>
224 changes: 224 additions & 0 deletions src/CruiseDAL.TestCommon.V3/TemplateDatabaseInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
using Bogus;
using CruiseDAL.V3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CruiseDAL.TestCommon.V3
{
public class TemplateDatabaseInitializer
{
public string CruiseID { get; set; }
public string SaleID { get; set; }
public string CreatedBy { get; set; }
public string[] Species { get; set; }

public (string field, string heading)[] TreeFieldHeadings { get; set; }

public (string field, string heading)[] LogFieldHeadings { get; set; }

public string[] StratumTemplates { get; set; }

public TreeDefaultValue[] TreeDefaults { get; set; }

public TreeAuditRule[] TreeAuditRules { get; set; }

public LogGradeAuditRule[] LogGradeAuditRules { get; set; }

public Reports[] Reports { get; set; }

public VolumeEquation[] VolumeEquations { get; set; }

public ValueEquation[] ValueEquations { get; set; }

public BiomassEquation[] BiomassEquations { get; set; }

public TemplateDatabaseInitializer()
{
CruiseID = Guid.NewGuid().ToString();
SaleID = Guid.NewGuid().ToString();

CreatedBy = nameof(TemplateDatabaseInitializer);

Species = new[] { "sp1", "sp2", "sp3" };

TreeFieldHeadings = new[]
{
(nameof(TreeMeasurment.DBH), "MyDBH"),
//(nameof(TreeMeasurment.Aspect), null),
};

LogFieldHeadings = new[]
{
(nameof(Log.DIBClass), "MyDibClass"),
//(nameof(Log.Grade), null),
};

StratumTemplates = new[] { "StTemplate1", "StTemplate2", };

TreeDefaults = new TreeDefaultValue[]
{
new TreeDefaultValue {SpeciesCode = "sp1", PrimaryProduct = "01", },
new TreeDefaultValue {SpeciesCode = null, PrimaryProduct = "01", },
new TreeDefaultValue {SpeciesCode = "sp1", PrimaryProduct = null, },
new TreeDefaultValue {SpeciesCode = null, PrimaryProduct = null, },
};

TreeAuditRules = new[]
{
new TreeAuditRule { Field = nameof(TreeMeasurment.DBH), Min = 0, Max = 100, },
new TreeAuditRule { Field = nameof(TreeMeasurment.TotalHeight), Min = 0, Max = 100, },
};

LogGradeAuditRules = new[]
{
new LogGradeAuditRule {Grade = "1", DefectMax = 101.0 },
new LogGradeAuditRule {Grade = "2", DefectMax = 102.0 },
};

Reports = new[]
{
new Reports {ReportID = "A1", Selected = true},
new Reports {ReportID = "A2", Selected = true},
};

VolumeEquations = new[]
{
new VolumeEquation {Species = "sp1", PrimaryProduct = "01", VolumeEquationNumber = "1234", },
new VolumeEquation {Species = "sp2", PrimaryProduct = "01", VolumeEquationNumber = "1234", },
};

ValueEquations = new[]
{
new ValueEquation {Species = "sp1", PrimaryProduct = "01", },
new ValueEquation {Species = "sp2", PrimaryProduct = "01", },
};

BiomassEquations = new[]
{
new BiomassEquation {Species = "sp1", Product = "01", Component = "something", LiveDead = "L", },
new BiomassEquation {Species = "sp2", Product = "01", Component = "something", LiveDead = "L", },
};

}

public CruiseDatastore_V3 CreateDatabase()
{
var database = new CruiseDatastore_V3();
InitializeDatabase(database, this);
return database;
}

public CruiseDatastore_V3 CreateDatabaseFile(string path)
{
var database = new CruiseDatastore_V3(path, true);
InitializeDatabase(database, this);
return database;
}

public static void InitializeDatabase(CruiseDatastore db, TemplateDatabaseInitializer init)
{
var cruiseID = init.CruiseID;

var insertOverrides = new Dictionary<string, object>()
{
{ "CreatedBy", init.CreatedBy },
};

var sale = new Sale()
{
SaleID = init.SaleID,
SaleNumber = "00000",
CreatedBy = init.CreatedBy,
};
db.Insert(sale);

var cruise = new Cruise()
{
CruiseID = cruiseID,
SaleID = init.SaleID,
CruiseNumber = "00000",
SaleNumber = "00000",
CreatedBy = init.CreatedBy,
};
db.Insert(cruise);

foreach (var sp in init.Species)
{
db.Insert(new Species { CruiseID = cruiseID, SpeciesCode = sp, });

db.Insert(new Species_Product { CruiseID = cruiseID, SpeciesCode = sp, PrimaryProduct = null, ContractSpecies = "123" });
db.Insert(new Species_Product { CruiseID = cruiseID, SpeciesCode = sp, PrimaryProduct = "01", ContractSpecies = "234" });
}

foreach (var (field, heading) in init.TreeFieldHeadings)
{
db.Insert(new TreeFieldHeading { CruiseID = cruiseID, Field = field, Heading = heading, CreatedBy = init.CreatedBy, });
}

foreach (var (field, heading) in init.LogFieldHeadings)
{
db.Insert(new LogFieldHeading { CruiseID = cruiseID, Field = field, Heading = heading, CreatedBy = init.CreatedBy, });
}

foreach (var stTemp in init.StratumTemplates)
{
db.Insert(new StratumTemplate { CruiseID = cruiseID, StratumTemplateName = stTemp, CreatedBy = init.CreatedBy, });
}

foreach (var tdv in init.TreeDefaults)
{
tdv.CruiseID = cruiseID;
tdv.CreatedBy = init.CreatedBy;
db.Insert(tdv);
}

foreach (var tar in init.TreeAuditRules)
{
tar.CruiseID = cruiseID;
tar.TreeAuditRuleID = Guid.NewGuid().ToString();
db.Insert(tar);

db.Insert(new TreeAuditRuleSelector { CruiseID = cruiseID, TreeAuditRuleID = tar.TreeAuditRuleID, SpeciesCode = null, PrimaryProduct = null });
db.Insert(new TreeAuditRuleSelector { CruiseID = cruiseID, TreeAuditRuleID = tar.TreeAuditRuleID, SpeciesCode = null, PrimaryProduct = "01" });
db.Insert(new TreeAuditRuleSelector { CruiseID = cruiseID, TreeAuditRuleID = tar.TreeAuditRuleID, SpeciesCode = init.Species[0], PrimaryProduct = null });
}

foreach (var lgar in init.LogGradeAuditRules)
{
lgar.CruiseID = cruiseID;
db.Insert(lgar);
}

foreach (var report in init.Reports)
{
report.CruiseID = cruiseID;
report.CreatedBy = init.CreatedBy;
db.Insert(report);
}

foreach (var volEq in init.VolumeEquations)
{
volEq.CruiseID = cruiseID;
volEq.CreatedBy = init.CreatedBy;
db.Insert(volEq);
}

foreach (var valEq in init.ValueEquations)
{
valEq.CruiseID = cruiseID;
valEq.CreatedBy = init.CreatedBy;
db.Insert(valEq);
}

foreach (var bioEq in init.BiomassEquations)
{
bioEq.CruiseID = cruiseID;
bioEq.CreatedBy = init.CreatedBy;
db.Insert(bioEq);
}
}
}
}
5 changes: 5 additions & 0 deletions src/CruiseDAL.TestCommon/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public string InitializeTestFile(string fileName)
if (File.Exists(sourcePath) == false) { throw new FileNotFoundException(sourcePath); }

var targetPath = Path.Combine(TestTempPath, fileName);
var targetDir = Path.GetDirectoryName(targetPath);
if (!Directory.Exists(targetDir))
{
Directory.CreateDirectory(targetDir);
}

RegesterFileForCleanUp(targetPath);
File.Copy(sourcePath, targetPath, true);
Expand Down
17 changes: 17 additions & 0 deletions src/CruiseDAL.V3.DownConvert.Test/DownMigrator_Issues_Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,22 @@ public void Goshen_Blueberry_TS()
var toSale = toDb.From<V2.Models.Sale>().Query().SingleOrDefault();
toSale.Should().NotBeNull();
}

[Fact]
public void DublinTSRecon()
{
var fromPath = GetTestFile("40483_DublinTS_ Recon.crz3");
var fromDb = new CruiseDatastore_V3(fromPath);
var cruiseID = fromDb.From<Cruise>().Query().Single().CruiseID;

var toPath = GetTempFilePathWithExt(".cruise");
var toDb = new DAL(toPath, true);

var downMigrator = new DownMigrator();
downMigrator.MigrateFromV3ToV2(cruiseID, fromDb, toDb);

//var toSale = toDb.From<V2.Models.Sale>().Query().SingleOrDefault();
//toSale.Should().NotBeNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void MigrateTreeErrors_multipleWarningsPerTreeField()
TreeID = tree.TreeID,
DBH = 101,
// we need to set height, otherwise we will get an error on the tree and we are just testing warnings
TotalHeight = 1,
TotalHeight = 1,
};
db.Insert(tm);

Expand Down Expand Up @@ -240,7 +240,39 @@ public void MigrateTreeErrors_multipleWarningsPerTreeField()

var error = errors.Single();
error.Suppress.Should().Be(false);
}

//for some plot errors the error applies to the whole plot
// so it doesn't make sence for the field column to have a
// this can be an issue in V2 so needs to be handled in conversion
[Fact]
public void MigratePlotErrors_blankPlotField()
{
var toPath = GetTempFilePath("MigratePlotErrors_blankPlotField.cruise");
var fromPath = GetTempFilePath("MigratePlotErrors_blankPlotField.crz3");

var init = new DatabaseInitializer();
var cruiseID = init.CruiseID;
using var db = init.CreateDatabaseFile(fromPath);

var plot = new Plot
{
CruiseID = cruiseID,
PlotID = Guid.NewGuid().ToString(),
PlotNumber = 1,
CuttingUnitCode = "u1",
};

db.Insert(plot);

using var toDb = new DAL(toPath, true);

var downMigrator = new DownMigrator();
downMigrator.MigrateFromV3ToV2(cruiseID, db, toDb);

var errorLogs = toDb.From<CruiseDAL.V2.Models.ErrorLog>().Query().ToArray();

errorLogs.Should().Contain(x => x.Message.EndsWith("no strata in plot"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CruiseDAL.DownMigrators;
using CruiseDAL.TestCommon;
using CruiseDAL.UpConvert;
using CruiseDAL.V3.Models;
using FluentAssertions;
using System.Linq;
Expand Down Expand Up @@ -187,5 +188,70 @@ public void NoContractSpecies()

tdvs.Should().OnlyContain(x => x.ContractSpecies == null);
}

[Fact]
public void RoundTripContractSpecies()
{
var v3Path = GetTempFilePath("RoundTripContractSpecies.crz3");
var v2Path = GetTempFilePath("RoundTripContractSpecies.cruise");
var v3AgainPath = GetTempFilePath("RoundTripContractSpecies_v3Again.crz3");

var specifProd = "01";

var init = new DatabaseInitializer()
{
Species = new[] { "sp1", "sp2" },
TreeDefaults = new[]
{
new TreeDefaultValue {SpeciesCode = "sp1", PrimaryProduct = specifProd, },
new TreeDefaultValue {SpeciesCode = null, PrimaryProduct = specifProd, },
new TreeDefaultValue {SpeciesCode = "sp1", PrimaryProduct = null, },
new TreeDefaultValue {SpeciesCode = null, PrimaryProduct = null, },

new TreeDefaultValue {SpeciesCode = "sp2", PrimaryProduct = specifProd, },
new TreeDefaultValue {SpeciesCode = "sp2", PrimaryProduct = null, },
},
Subpops = new SubPopulation[] { },
SpProds = null,
};

using var v3Db = init.CreateDatabaseFile(v3Path);
using var v2Db = new DAL(v2Path, true);

var spProds = new[]
{
new Species_Product
{
CruiseID = init.CruiseID,
SpeciesCode = "sp1",
ContractSpecies = "something",

},
new Species_Product
{
CruiseID = init.CruiseID,
SpeciesCode = "sp2",
ContractSpecies = "something2",
PrimaryProduct = specifProd,
},
};

foreach (var spProd in spProds)
{
v3Db.Insert(spProd);
}



var downMigrator = new DownMigrator();
downMigrator.MigrateFromV3ToV2(init.CruiseID, v3Db, v2Db);

var v3AgainDb = new CruiseDatastore_V3(v3AgainPath, true);
var upconverter = new Migrator();
upconverter.MigrateFromV2ToV3(v2Db, v3AgainDb);

var spProdsAgain = v3AgainDb.From<V3.Models.Species_Product>().Query().ToArray();
spProdsAgain.Should().HaveCount(spProds.Length);
}
}
}
Binary file not shown.

0 comments on commit 21aefaa

Please sign in to comment.