// Copyright (C) Stichting Deltares 2018. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System.Collections.Generic; using System.IO; using Deltares.Dam.Data; 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(); var exportData = new CsvExportData("locatioName", "scenarioName"); 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; } } } }