//----------------------------------------------------------------------- // // Copyright (c) 2010 Deltares. All rights reserved. // // B.S.T.I.M. The // tom.the@deltares.nl // 05-10-2010 // Test for export to csv //----------------------------------------------------------------------- using Deltares.Geotechnics; using Deltares.Geotechnics.GeotechnicalGeometry; using System.Collections.Generic; using System.IO; using Deltares.Dam.Data; using Deltares.Geotechnics.Soils; using Deltares.Geotechnics.SurfaceLines; using NUnit.Framework; namespace Deltares.Dam.Tests { [TestFixture] public class CsvExporterTest { const string FileName = "test.csv"; [Test] [ExpectedException(typeof(CsvExporterException))] public void ThrowWhenFileNameIsNull() { new CsvExporter(null, null); } [Test] [ExpectedException(typeof(CsvExporterException))] public void ThrowWhenFileNameIsNullOrEmpty() { new CsvExporter("", null); } [Test] [ExpectedException(typeof(CsvExporterException))] public void ThrowWhenFileNameOnlyContainsWhitespaces() { new CsvExporter(" ", null); } [Test] public void OverwriteExistingFileShouldWorkWithoutException() { const string writtenContent = "something"; var file = File.CreateText(FileName); file.WriteLine(writtenContent); file.Close(); using(var surfaceLine2 = new SurfaceLine2 { Geometry = new LocalizedGeometryPointString(), CharacteristicPoints = { GeometryMustContainPoint = true } }) using (var dike = new Dike()) { dike.Locations.Add(new Location { SurfaceLine2 = surfaceLine2, Segment = new Dam.Data.Segment() }); var scenario = new Scenario() { Location = dike.Locations[0] }; var exportData = new CsvExportData( "Test", dike, null, scenario, new SoilProfile1D(), "", AnalysisType.NoAdaption, 0, ProbabilisticType.Deterministic); var creator = new CsvExporter(FileName, new List { exportData }); creator.WriteFile(); using (var reader = File.OpenText(FileName)) { var fileContent = reader.ReadToEnd(); Assert.IsFalse(fileContent.Contains((writtenContent))); } } } [Test] [ExpectedException(typeof(CsvExporterException))] public void ThrowsIndexAlreadyExistsWhenDeclaredMultipleTimes() { var creator = new CsvExporter(FileName, new List { new DataStubWithWrongMetaData() }); creator.WriteFile(); } [Test] public void ColumnHeaderIsWrittenCorrectly() { var dataItems = new List { new DataStub() { ID = 1, Name = "Barry", Average = 95.4 }, new DataStub() { ID = 2, Name = "Tom", Average = 97.3 }, new DataStub() { ID = 3, Name = "Remko", Average = 92.2} }; var creator = new CsvExporter(FileName, dataItems) { WriteHeaderInFirstLine = true }; creator.WriteFile(); using (var reader = File.OpenText(FileName)) { var fileContent = reader.ReadToEnd(); Assert.IsTrue(fileContent.Contains("ID;Naam")); } } class DataStub { [CsvExportColumn("ID", 1)] public int ID { get; set; } [CsvExportColumn("Naam", 2)] public string Name { get; set; } [CsvExportColumn("Gemiddelde", 3)] public double Average { get; set; } } class DataStubWithWrongMetaData { [CsvExportColumn("ID", 1)] public int ID { get; set; } [CsvExportColumn("Naam", 1)] public string Name { get; set; } } } }