From 9337f8a492569d9c37b03df85aa0b6e56d4d54cd Mon Sep 17 00:00:00 2001 From: vladislav-karamfilov Date: Sun, 9 Oct 2016 22:47:18 +0300 Subject: [PATCH] Implemented test for IDataRecord.GetNullable() extension method. --- .../Common/DataExtensions/GetNullableTests.cs | 81 +++++++++++++++++++ .../MoreDotNet.Test/MoreDotNet.Tests.csproj | 2 + 2 files changed, 83 insertions(+) create mode 100644 Source/MoreDotNet.Test/Extensions/Common/DataExtensions/GetNullableTests.cs diff --git a/Source/MoreDotNet.Test/Extensions/Common/DataExtensions/GetNullableTests.cs b/Source/MoreDotNet.Test/Extensions/Common/DataExtensions/GetNullableTests.cs new file mode 100644 index 0000000..380678d --- /dev/null +++ b/Source/MoreDotNet.Test/Extensions/Common/DataExtensions/GetNullableTests.cs @@ -0,0 +1,81 @@ +namespace MoreDotNet.Tests.Extensions.Common.DataExtensions +{ + using System; + using System.Data; + + using Microsoft.SqlServer.Server; + + using MoreDotNet.Extensions.Common; + + using Xunit; + + public class GetNullableTests + { + [Fact] + public void GetNullable_NullDataRecord_ShouldThrowException() + { + IDataRecord dataRecord = null; + Assert.Throws(() => dataRecord.GetNullable(1)); + } + + [Fact] + public void GetNullable_InvalidOrdinal_ShouldThrowException() + { + var dataRecord = new SqlDataRecord( + new SqlMetaData("StringProp", SqlDbType.NVarChar, 20), + new SqlMetaData("IntProp", SqlDbType.Int)); + dataRecord.SetValue(0, "String value."); + dataRecord.SetValue(1, 123); + + Assert.Throws(() => dataRecord.GetNullable(3)); + } + + [Fact] + public void GetNullableString_DbNullDataRecordValue_ShouldReturnNull() + { + var dataRecord = new SqlDataRecord( + new SqlMetaData("StringProp", SqlDbType.NVarChar, 20), + new SqlMetaData("IntProp", SqlDbType.Int)); + dataRecord.SetValue(1, 123); + + Assert.Equal(null, dataRecord.GetNullable(0)); + } + + [Fact] + public void GetNullableInt_DbNullDataRecordValue_ShouldReturnZero() + { + var dataRecord = new SqlDataRecord( + new SqlMetaData("StringProp", SqlDbType.NVarChar, 20), + new SqlMetaData("IntProp", SqlDbType.Int)); + dataRecord.SetValue(0, "String value."); + + Assert.Equal(0, dataRecord.GetNullable(1)); + } + + [Fact] + public void GetNullableString_ValidStringValue_ShouldReturnStringValue() + { + const string StringValue = "String value."; + var dataRecord = new SqlDataRecord( + new SqlMetaData("StringProp", SqlDbType.NVarChar, 20), + new SqlMetaData("IntProp", SqlDbType.Int)); + dataRecord.SetValue(0, StringValue); + dataRecord.SetValue(1, 123); + + Assert.Equal(StringValue, dataRecord.GetNullable(0)); + } + + [Fact] + public void GetNullableInt_ValidIntValue_ShouldReturnIntValue() + { + const int IntValue = 123; + var dataRecord = new SqlDataRecord( + new SqlMetaData("StringProp", SqlDbType.NVarChar, 20), + new SqlMetaData("IntProp", SqlDbType.Int)); + dataRecord.SetValue(0, "String value."); + dataRecord.SetValue(1, IntValue); + + Assert.Equal(IntValue, dataRecord.GetNullable(1)); + } + } +} diff --git a/Source/MoreDotNet.Test/MoreDotNet.Tests.csproj b/Source/MoreDotNet.Test/MoreDotNet.Tests.csproj index 1854afb..187ac9e 100644 --- a/Source/MoreDotNet.Test/MoreDotNet.Tests.csproj +++ b/Source/MoreDotNet.Test/MoreDotNet.Tests.csproj @@ -41,6 +41,7 @@ + ..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll @@ -76,6 +77,7 @@ +