// 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; using System.Linq; using System.Text.RegularExpressions; using Deltares.Dam.Data; using NUnit.Framework; using System.IO; namespace Deltares.Dam.Tests { [TestFixture] public class CsvParserTest { private string testFile; [TestFixtureSetUp] public void FixtureSetup() { testFile = "test.csv"; using (var writer = File.CreateText(testFile)) { writer.WriteLine("name;age"); //<- header contains name and age writer.WriteLine("Anouar;31"); writer.WriteLine("Barry;39"); writer.WriteLine("Tom;48"); writer.WriteLine("Remko;39"); } } [Test] public void ThrowsArgumentExceptionWhenHeaderStringIsNullByCallingParseHeader() { var expectedMessage = "The header in the import file is not valid"; Assert.That(() => CsvParser.ParseHeader(null, new Regex("")), Throws.TypeOf(typeof(ArgumentException)).With.Message.EqualTo(expectedMessage)); } [Test] public void ThrowsArgumentExceptionSplitterIsNull() { var expectedMessage = "The given pattern to split the data values of the CSV files is not valid" + Environment.NewLine + "Parameter name: splitter"; Assert.That(() => CsvParser.ParseHeader("test;test", null), Throws.TypeOf(typeof(ArgumentNullException)).With.Message.EqualTo(expectedMessage)); } [Test] public void ThrowsWhenSplitPatternIsEmpty() { var expectedMessage = "The given pattern to split the data values of the CSV files is not valid"; Assert.That(() => CsvParser.ParseHeader("test", new Regex("")), Throws.TypeOf(typeof(ArgumentException)).With.Message.EqualTo(expectedMessage)); } //[Test] //[ExpectedException(typeof(CsvParserException))] //public void ThrowsWhenNoHeaderItemsAreParsed() //{ // CsvParser.ParseHeader("test", new Regex("")); //} [Test] public void CanParseHeaderWithSemicolonSeparatedItems() { var items = CsvParser.ParseHeader("header1;header 2", new Regex(CsvParser.CsvSplitPatternCommaAndSemicolonCombined)); Assert.AreEqual(2, items.Length); Assert.AreEqual("header1", items[0]); Assert.AreEqual("header 2", items[1]); } [Test] public void CanParseHeaderWithCommaSeparatedItems() { var items = CsvParser.ParseHeader("header1,header 2", new Regex(CsvParser.CsvSplitPatternCommaAndSemicolonCombined)); Assert.AreEqual(2, items.Length); Assert.AreEqual("header1", items[0]); Assert.AreEqual("header 2", items[1]); } [Test] [ExpectedException(typeof(ArgumentNullException))] public void ThrowsExceptionWhenObjectMaterializerNull() { CsvParser.LoadFromCsvFile(null, testFile).ToList(); } [Test] [ExpectedException(typeof(CsvParserException))] public void ThrowsExceptionWhenObjectMaterializerNotValid() { var loader = new ObjectMaterializer(); loader.LoadFromCsvFile(testFile).ToList(); } [Test] [ExpectedException(typeof(ArgumentException))] public void ThrowsExceptionWhenFileNameNull() { var loader = new ObjectMaterializer(); loader.LoadFromCsvFile(null).ToList(); } [Test] [ExpectedException(typeof(ArgumentException))] public void ThrowsExceptionWhenFileNameEmpty() { var loader = new ObjectMaterializer(); loader.LoadFromCsvFile("").ToList(); } [Test] [ExpectedException(typeof(ArgumentException))] public void ThrowsExceptionWhenFileNameContainsOnlySpaces() { var loader = new ObjectMaterializer(); loader.LoadFromCsvFile(" ").ToList(); } [Test] [ExpectedException(typeof(CsvParserException))] public void ThrowsExceptionWhenNumberOfItemsDoesntMatchWithHeader() { using (var writer = File.CreateText(testFile)) { writer.WriteLine("name;age"); //<- header contains name and age writer.WriteLine("Barry;39;20"); } var loader = new ObjectMaterializer(); loader.Add("name", (entity, value) => entity.Name = value); loader.Add("age", (entity, value) => entity.Age = int.Parse(value)); loader.LoadFromCsvFile(testFile).Count(); // <- use count() to force the parser to run because it uses defered execution } [Test] [ExpectedException(typeof(CsvParserException))] public void ThrowsExceptionWhenMappedKeyDoesntExistInCsvHeader() { using (var writer = File.CreateText(testFile)) { writer.WriteLine("name;age"); //<- header contains name and age writer.WriteLine("Barry;39;"); } var loader = new ObjectMaterializer(); loader.Add("name", (entity, value) => entity.Name = value); loader.Add("ages", (entity, value) => entity.Age = int.Parse(value)); loader.LoadFromCsvFile(testFile).Count(); // <- use count() to force the parser to run because it uses defered execution } [Test] public void CanParseTestFile() { using (var writer = File.CreateText(testFile)) { writer.WriteLine("name;age"); //<- header contains name and age writer.WriteLine("Anouar;31"); writer.WriteLine("Barry;39"); writer.WriteLine("Tom;48"); writer.WriteLine("Remko;39"); } var loader = new ObjectMaterializer(); loader.Add("name", (entity, value) => entity.Name = value); loader.Add("age", (entity, value) => entity.Age = int.Parse(value)); var items = loader.LoadFromCsvFile(testFile); Assert.AreEqual(4, items.Count()); Assert.AreEqual("Tom", items.ElementAt(2).Name); Assert.AreEqual(48, items.ElementAt(2).Age); } [TestFixtureTearDown] public void FixtureTearDown() { if (File.Exists(testFile)) File.Delete(testFile); } class Foo { public string Name { get; set; } public int Age { get; set; } } } }