Skip to content

Commit

Permalink
Merge pull request #22 from vladislav-karamfilov/master
Browse files Browse the repository at this point in the history
Implemented tests for IDataRecord.GetNullable<T>() extension method.
  • Loading branch information
Teodor92 authored Oct 9, 2016
2 parents 60dbad0 + 9337f8a commit df44e89
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<ArgumentNullException>(() => dataRecord.GetNullable<int>(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<IndexOutOfRangeException>(() => dataRecord.GetNullable<string>(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<string>(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<int>(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<string>(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<int>(1));
}
}
}
2 changes: 2 additions & 0 deletions Source/MoreDotNet.Test/MoreDotNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll</HintPath>
Expand Down Expand Up @@ -76,6 +77,7 @@
<Compile Include="Extensions\Collections\CollectionExtensions\IsNullOrEmptyTests.cs" />
<Compile Include="Extensions\Common\ColorExtensions\ToHexStringTests.cs" />
<Compile Include="Extensions\Common\ColorExtensions\ToRgbStringTests.cs" />
<Compile Include="Extensions\Common\DataExtensions\GetNullableTests.cs" />
<Compile Include="Extensions\Common\IntegerExtensions\RangeToTests.cs" />
<Compile Include="Extensions\Common\StringExtensions\CapitalizeTests.cs" />
<Compile Include="Extensions\Common\StringExtensions\IsLikeTests.cs" />
Expand Down

0 comments on commit df44e89

Please # to comment.