using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using Deltares.Geotechnics.IO.Importers; using Deltares.Standard.Language; using NUnit.Framework; namespace Deltares.Geotechnics.IO.Tests { [TestFixture] public class SoilProfilesImporterTest { /// /// Test that the soil profiles importer returns a warning message if the file is empty. /// [Test] public void EmptySoilProfilesFileResultsInWarningMessage() { // empty file (header only) var testFile = "TestFiles/empty_soilprofiles.csv"; File.WriteAllText(testFile, @"soilprofile_id;top_level;soil_name;soiltype", Encoding.ASCII); // setup importer, not using factory var importer = new SoilProfilesImporter(); var soilProfiles = new List(); // error messages are returned as list of strings, which are to be logged etc. by the application Assert.AreEqual(0, importer.ErrorMessages.Count); var segments = importer.ReadSoilProfiles(testFile, new SoilList()); Assert.Greater(importer.ErrorMessages.Count, 0); Assert.NotNull(importer.ErrorMessages.FirstOrDefault(m => m.Message.Contains( LocalizationManager.GetTranslatedText(typeof(SoilSegmentsImporter), "SoilprofilesCsvFileEmptyWarning").Replace("{0}", "")))); File.Delete(testFile); } /// /// Check of Import() returns correct soil profiles. /// The values that are checked in this test were looked up in the testfile "soilprofiles.csv" /// [Test] public void ImportReturnsCorrectSoilProfiles() { var soils = new SoilList(); var importer = new SoilProfilesImporter(); var soilProfiles = importer.ReadSoilProfiles(Path.Combine(mapTestData, soilProfilesFilename), soils); Assert.AreEqual(32, soilProfiles.Count, "Read incorrect number of soil profiles"); SoilProfile1D soilProfile = soilProfiles.FirstOrDefault(x => x.Name.Equals("10Z_400_STBI")); Assert.IsNotNull(soilProfile); Assert.AreEqual(3, soilProfile.Layers.Count); importer.Registration.PopulateRegisteredItems(); Assert.AreEqual("Zand Pleistoceen", soilProfile.Layers[2].Soil.Name); } /// /// Check if ReadSoilProfiles returns correct soil profiles. /// The values that are checked in this test were looked up in the testfile "soilprofiles.csv" /// [Test] public void ReadSoilProfilesReturnsCorrectNumberOfSoilProfiles() { var soils = new SoilList(); var importer = new SoilProfilesImporter(); var soilProfiles = importer.ReadSoilProfiles(Path.Combine(mapTestData, soilProfilesFilename), soils); Assert.AreEqual(32, soilProfiles.Count, "Read incorrect number of soil profiles"); Assert.AreEqual(32, soilProfiles.Count, "Read incorrect number of soil profiles"); SoilProfile1D soilProfile = soilProfiles.FirstOrDefault(x => x.Name.Equals("10Z_400_STBI")); Assert.IsNotNull(soilProfile); Assert.AreEqual(3, soilProfile.Layers.Count); Assert.AreEqual("Zand Pleistoceen", soilProfile.Layers[2].Soil.Name); } /// /// Check whether matching Soils via the classic (not using factory) method is not case sensitive /// [Test] public void LookupOfSoilsIsNotCaseSensitive() { var soilProfilesImporter = new SoilProfilesImporter(); var soils = new SoilList(); var soilProfiles = soilProfilesImporter.ReadSoilProfiles(Path.Combine(mapTestData, soilProfilesFilename), soils); Assert.AreEqual(32, soilProfiles.Count, "Read incorrect number of soil profiles"); Assert.AreEqual(26, soils.Soils.Count , "Read incorrect number of soils"); foreach (var soil in soils.Soils) { soil.Name = soil.Name.ToUpper(); } soilProfiles = soilProfilesImporter.ReadSoilProfiles(Path.Combine(mapTestData, soilProfilesFilename), soils); // both the number of profiles and soils remain the same ? Assert.AreEqual(32, soilProfiles.Count, "Read incorrect number of soil profiles"); Assert.AreEqual(26, soils.Soils.Count, "Read incorrect number of soils"); } private const string mapTestData = @"..\..\..\data\Geotechnics\CSVImportTest"; private const string soilProfilesFilename = "soilprofiles.csv"; } }