Skip to content

Commit

Permalink
Merge pull request #98 from abe545/fix-null-binary-result
Browse files Browse the repository at this point in the history
Fix null binary result
  • Loading branch information
abe545 authored Feb 14, 2017
2 parents 2a3c3ef + ee3475d commit bb06c3b
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CodeOnlyStoredProcedure/RowFactory/ValueAccessorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public override Expression CreateExpressionToGetValueFromReader(IDataReader read
else
res = Expression.NotEqual(res, Zero(dbColumnType));
}
else if (expectedType == typeof(byte[]))
{
//BitConverter.GetBytes()
res = Expression.Condition(
Expression.Equal(res, Expression.Constant(null)),
Expression.Constant(default(byte[]), typeof(byte[])),
Expression.Call(null, typeof(BitConverter).GetMethod(nameof(BitConverter.GetBytes), new[] { dbColumnType }), Expression.Property(res, "Value")));
}
else
res = Expression.Convert(res, type);
}
Expand Down
57 changes: 57 additions & 0 deletions CodeOnlyTests/RowFactory/SimpleTypeRowFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,63 @@ public void ReturnsMultipleStrings()
.Should().ContainInOrder("Foo", null, "Bar");
}

[TestMethod]
public void ParsesByteArray_AsObject()
{
var rdr = new Mock<IDataReader>();
rdr.Setup(r => r.GetFieldType(0)).Returns(typeof(byte[]));
rdr.SetupSequence(r => r.IsDBNull(0))
.Returns(false)
.Returns(true)
.Returns(false);
rdr.SetupSequence(r => r.Read())
.Returns(true)
.Returns(true)
.Returns(true)
.Returns(false);
rdr.SetupSequence(r => r.GetValue(0))
.Returns(new byte[] { 0 })
.Returns(new byte[] { 1 });

var toTest = new SimpleTypeRowFactory<byte[]>();

var res = toTest.ParseRows(rdr.Object, Enumerable.Empty<IDataTransformer>(), CancellationToken.None);
res.First().Should().ContainSingle().Which.Should().Be(0);
res.Skip(1).First().Should().BeNull();
res.Last().Should().ContainSingle().Which.Should().Be(1);
}

[TestMethod]
public void ParsesByteArray_AsInt_WhenGlobalConvert()
{
using (GlobalSettings.UseTestInstance())
{
GlobalSettings.Instance.ConvertAllNumericValues = true;

var rdr = new Mock<IDataReader>();
rdr.Setup(r => r.GetFieldType(0)).Returns(typeof(int));
rdr.SetupSequence(r => r.IsDBNull(0))
.Returns(false)
.Returns(true)
.Returns(false);
rdr.SetupSequence(r => r.Read())
.Returns(true)
.Returns(true)
.Returns(true)
.Returns(false);
rdr.SetupSequence(r => r.GetValue(0))
.Returns(0)
.Returns(1);

var toTest = new SimpleTypeRowFactory<byte[]>();

var res = toTest.ParseRows(rdr.Object, Enumerable.Empty<IDataTransformer>(), CancellationToken.None);
res.First().Should().ContainInOrder(BitConverter.GetBytes(0));
res.Skip(1).First().Should().BeNull();
res.Last().Should().ContainInOrder(BitConverter.GetBytes(1));
}
}

[TestMethod]
public void HelpfulExceptionWhenColumnWrongType()
{
Expand Down
70 changes: 70 additions & 0 deletions SmokeTests/DynamicSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,26 @@ Tuple<bool, string> ExecuteBinarySync(IDbConnection db)
return Tuple.Create(true, "");
}

[SmokeTest("Dynamic Syntax Empty Binary ResultSet")]
Tuple<bool, string> ExecutEmptyBinarySync(IDbConnection db)
{
byte[] res = db.Execute(Program.timeout).usp_GetEmptyBinary(@includeResults: false);
if (res != null)
return Tuple.Create(false, "The stored procedure returned an empty result set, so should not have a value");

return Tuple.Create(true, "");
}

[SmokeTest("Dynamic Syntax NULL Binary ResultSet")]
Tuple<bool, string> ExecutNullBinarySync(IDbConnection db)
{
byte[] res = db.Execute(Program.timeout).usp_GetEmptyBinary(@includeResults: true);
if (res != null)
return Tuple.Create(false, "The stored procedure returned an empty result set, so should not have a value");

return Tuple.Create(true, "");
}

[SmokeTest("Dynamic Syntax Binary ResultSet (await)")]
async Task<Tuple<bool, string>> ExecuteBinaryAsync(IDbConnection db)
{
Expand All @@ -717,6 +737,26 @@ async Task<Tuple<bool, string>> ExecuteBinaryAsync(IDbConnection db)
return Tuple.Create(true, "");
}

[SmokeTest("Dynamic Syntax Empty Binary ResultSet (await)")]
async Task<Tuple<bool, string>> ExecutEmptyBinaryAsync(IDbConnection db)
{
byte[] res = await db.ExecuteAsync(Program.timeout).usp_GetEmptyBinary(@includeResults: false);
if (res != null)
return Tuple.Create(false, "The stored procedure returned an empty result set, so should not have a value");

return Tuple.Create(true, "");
}

[SmokeTest("Dynamic Syntax NULL Binary ResultSet (await)")]
async Task<Tuple<bool, string>> ExecutNullBinaryAsync(IDbConnection db)
{
byte[] res = await db.ExecuteAsync(Program.timeout).usp_GetEmptyBinary(@includeResults: true);
if (res != null)
return Tuple.Create(false, "The stored procedure returned an empty result set, so should not have a value");

return Tuple.Create(true, "");
}

[SmokeTest("Dynamic Syntax Binary ResultSet (task)")]
Task<Tuple<bool, string>> ExecuteBinaryTask(IDbConnection db)
{
Expand All @@ -737,6 +777,36 @@ Task<Tuple<bool, string>> ExecuteBinaryTask(IDbConnection db)
});
}

[SmokeTest("Dynamic Syntax Empty Binary ResultSet (task)")]
Task<Tuple<bool, string>> ExecutEmptyBinaryTask(IDbConnection db)
{
Task<byte[]> t = db.ExecuteAsync(Program.timeout).usp_GetEmptyBinary(@includeResults: false);

return t.ContinueWith(r =>
{
var res = t.Result;
if (res != null)
return Tuple.Create(false, "The stored procedure returned an empty result set, so should not have a value");

return Tuple.Create(true, "");
});
}

[SmokeTest("Dynamic Syntax NULL Binary ResultSet (task)")]
Task<Tuple<bool, string>> ExecutNullBinaryTask(IDbConnection db)
{
Task<byte[]> t = db.ExecuteAsync(Program.timeout).usp_GetEmptyBinary(@includeResults: true);

return t.ContinueWith(r =>
{
var res = t.Result;
if (res != null)
return Tuple.Create(false, "The stored procedure returned an empty result set, so should not have a value");

return Tuple.Create(true, "");
});
}

[SmokeTest("Dynamic Syntax Binary Parameter")]
Tuple<bool, string> ExecuteBinaryParameter(IDbConnection db)
{
Expand Down
98 changes: 98 additions & 0 deletions SmokeTests/FluentSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,36 @@ Tuple<bool, string> ExecuteBinarySync(IDbConnection db)
return Tuple.Create(true, "");
}

[SmokeTest("Fluent Syntax Empty Binary ResultSet")]
Tuple<bool, string> ExecuteEmptyBinarySync(IDbConnection db)
{
byte[] res = StoredProcedure.Create("usp_GetEmptyBinary")
.WithParameter("includeResults", false)
.WithResults<byte[]>()
.Execute(db, Program.timeout)
.SingleOrDefault();

if (res != null)
return Tuple.Create(false, "The bytes returned from the stored procedure were not null");

return Tuple.Create(true, "");
}

[SmokeTest("Fluent Syntax NULL Binary ResultSet")]
Tuple<bool, string> ExecuteNullBinarySync(IDbConnection db)
{
byte[] res = StoredProcedure.Create("usp_GetEmptyBinary")
.WithParameter("includeResults", true)
.WithResults<byte[]>()
.Execute(db, Program.timeout)
.Single();

if (res != null)
return Tuple.Create(false, "The bytes returned from the stored procedure were not null");

return Tuple.Create(true, "");
}

[SmokeTest("Fluent Syntax Binary ResultSet (await)")]
async Task<Tuple<bool, string>> ExecuteBinaryAsync(IDbConnection db)
{
Expand All @@ -994,6 +1024,36 @@ async Task<Tuple<bool, string>> ExecuteBinaryAsync(IDbConnection db)
return Tuple.Create(true, "");
}

[SmokeTest("Fluent Syntax Empty Binary ResultSet (await)")]
async Task<Tuple<bool, string>> ExecuteEmptyBinaryAsync(IDbConnection db)
{
byte[] res = (await StoredProcedure.Create("usp_GetEmptyBinary")
.WithParameter("includeResults", false)
.WithResults<byte[]>()
.ExecuteAsync(db, Program.timeout))
.SingleOrDefault();

if (res != null)
return Tuple.Create(false, "The bytes returned from the stored procedure were not null");

return Tuple.Create(true, "");
}

[SmokeTest("Fluent Syntax NULL Binary ResultSet (await)")]
async Task<Tuple<bool, string>> ExecuteNullBinaryAsync(IDbConnection db)
{
byte[] res = (await StoredProcedure.Create("usp_GetEmptyBinary")
.WithParameter("includeResults", true)
.WithResults<byte[]>()
.ExecuteAsync(db, Program.timeout))
.Single();

if (res != null)
return Tuple.Create(false, "The bytes returned from the stored procedure were not null");

return Tuple.Create(true, "");
}

[SmokeTest("Fluent Syntax Binary ResultSet (task)")]
Task<Tuple<bool, string>> ExecuteBinaryTask(IDbConnection db)
{
Expand All @@ -1017,6 +1077,44 @@ Task<Tuple<bool, string>> ExecuteBinaryTask(IDbConnection db)
});
}

[SmokeTest("Fluent Syntax Empty Binary ResultSet (task)")]
Task<Tuple<bool, string>> ExecuteEmptyBinaryTask(IDbConnection db)
{
var t = StoredProcedure.Create("usp_GetEmptyBinary")
.WithParameter("includeResults", false)
.WithResults<byte[]>()
.ExecuteAsync(db, Program.timeout);

return t.ContinueWith(r =>
{
var res = r.Result.SingleOrDefault();

if (res != null)
return Tuple.Create(false, "The bytes returned from the stored procedure were not null");

return Tuple.Create(true, "");
});
}

[SmokeTest("Fluent Syntax NULL Binary ResultSet")]
Task<Tuple<bool, string>> ExecuteNullBinaryTask(IDbConnection db)
{
var t = StoredProcedure.Create("usp_GetEmptyBinary")
.WithParameter("includeResults", true)
.WithResults<byte[]>()
.ExecuteAsync(db, Program.timeout);

return t.ContinueWith(r =>
{
var res = r.Result.Single();

if (res != null)
return Tuple.Create(false, "The bytes returned from the stored procedure were not null");

return Tuple.Create(true, "");
});
}

[SmokeTest("Fluent Syntax Binary Parameter")]
Tuple<bool, string> ExecuteBinaryParameter(IDbConnection db)
{
Expand Down
Binary file modified SmokeTests/Smoke.mdf
Binary file not shown.
Binary file modified SmokeTests/Smoke_log.ldf
Binary file not shown.

0 comments on commit bb06c3b

Please sign in to comment.