// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser 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.Data;
using System.Globalization;
using Core.Common.IO.Exceptions;
using Core.Common.IO.Readers;
using Core.Common.TestUtil;
using NUnit.Framework;
using Rhino.Mocks;
namespace Core.Common.IO.Test.Readers
{
[TestFixture]
public class IDataReaderExtensionsTest
{
[Test]
public void Read_NullDataReader_ThrowsArgumentNullException()
{
// Call
TestDelegate test = () => ((IDataReader)null).Read("column");
// Assert
var paramName = Assert.Throws(test).ParamName;
Assert.AreEqual("dataReader", paramName);
}
[Test]
public void Read_NullColumnName_ThrowsArgumentNullException()
{
// Setup
var mockRepository = new MockRepository();
var dataReader = mockRepository.Stub();
mockRepository.ReplayAll();
// Call
TestDelegate test = () => dataReader.Read(null);
// Assert
var paramName = Assert.Throws(test).ParamName;
Assert.AreEqual("columnName", paramName);
mockRepository.VerifyAll();
}
[Test]
public void Read_NotExistingColumnName_ThrowsArgumentException()
{
// Setup
var mockRepository = new MockRepository();
var dataReader = mockRepository.Stub();
var columnName = "SomeColumn";
dataReader.Stub(dr => dr[columnName]).Throw(new IndexOutOfRangeException());
mockRepository.ReplayAll();
// Call
TestDelegate test = () => dataReader.Read(columnName);
// Assert
ArgumentException exception = TestHelper.AssertThrowsArgumentExceptionAndTestMessage(
test, "Column \'SomeColumn\' not defined for data row.");
var paramName = exception.ParamName;
Assert.AreEqual("columnName", paramName);
mockRepository.VerifyAll();
}
[Test]
public void Read_NumberWithInvalidFormat_ThrowsInvalidCastException()
{
// Setup
var mockRepository = new MockRepository();
var dataReader = mockRepository.Stub();
string columnName = "SomeColumn";
dataReader.Stub(dr => dr[columnName]).Return("3..2");
mockRepository.ReplayAll();
// Call
TestDelegate test = () => dataReader.Read(columnName);
// Assert
var message = Assert.Throws(test).Message;
Assert.AreEqual("Value read from data reader ('3..2') is an incorrect format to transform to type System.Int32.", message);
mockRepository.VerifyAll();
}
[Test]
public void Read_UsingTypeNotMatchingColumnType_ThrowsInvalidCastException()
{
// Setup
var mockRepository = new MockRepository();
var dataReader = mockRepository.Stub();
string columnName = "SomeColumn";
var value = 3.9;
dataReader.Stub(dr => dr[columnName]).Return(value);
mockRepository.ReplayAll();
// Call
TestDelegate test = () => dataReader.Read(columnName);
// Assert
var message = Assert.Throws(test).Message;
var expectedMessage = string.Format(CultureInfo.CurrentCulture,
"Value read from data reader ('{0}') could not be cast to desired type System.Data.IDataReader.",
value);
Assert.AreEqual(expectedMessage, message);
mockRepository.VerifyAll();
}
[Test]
public void Read_ColumnValueTooLargeForUsedType_ThrowsInvalidCastException()
{
// Setup
var mockRepository = new MockRepository();
var dataReader = mockRepository.Stub();
string columnName = "SomeColumn";
dataReader.Stub(dr => dr[columnName]).Return(3e139);
mockRepository.ReplayAll();
// Call
TestDelegate test = () => dataReader.Read(columnName);
// Assert
var message = Assert.Throws(test).Message;
Assert.AreEqual("Value read from data reader ('3E+139') was too large to convert to type System.Int32.", message);
mockRepository.VerifyAll();
}
[Test]
public void Read_UsingSameTypeAsColumnType_ReturnsTypedValue()
{
// Setup
string columnName = "SomeColumn";
var testValue = "testValue";
var mockRepository = new MockRepository();
var dataReader = mockRepository.Stub();
dataReader.Stub(dr => dr[columnName]).Return(testValue);
mockRepository.ReplayAll();
// Call
string result = dataReader.Read(columnName);
// Assert
Assert.AreEqual(testValue, result);
mockRepository.VerifyAll();
}
}
}