-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from vladislav-karamfilov/master
Implemented tests for IDataRecord.GetNullable<T>() extension method.
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
Source/MoreDotNet.Test/Extensions/Common/DataExtensions/GetNullableTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters