Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Single, SingleOrDefault, First and FirstOrDefault #53

Merged
merged 7 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 123 additions & 1 deletion Moq.Dapper.Test/DapperQueryAsyncTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Numerics;
using System.Threading.Tasks;
using Dapper;
using NUnit.Framework;

Expand Down Expand Up @@ -42,7 +44,127 @@ public void QueryAsyncGenericUsingDbConnectionInterface()
Assert.That(actual.Count, Is.EqualTo(expected.Length));
Assert.That(actual, Is.EquivalentTo(expected));
}


[Test]
public void QuerySingleAsyncGeneric()
{
var connection = new Mock<DbConnection>();

var expected = 7;

connection.SetupDapperAsync(c => c.QuerySingleAsync<int>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync(expected);

var actual = connection.Object.QuerySingleAsync<int>("").GetAwaiter().GetResult();

Assert.AreEqual(actual, expected);
}

[Test]
public void QuerySingleAsyncGenericUsingDbConnectionInterface()
{
var connection = new Mock<IDbConnection>();

var expected = 7;

connection.SetupDapperAsync(c => c.QuerySingleAsync<int>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync(expected);

var actual = connection.Object.QuerySingleAsync<int>("").GetAwaiter().GetResult();

Assert.AreEqual(actual, expected);
}

[Test]
public void QuerySingleOrDefaultAsyncGeneric()
{
var connection = new Mock<DbConnection>();

var expected = 7;

connection.SetupDapperAsync(c => c.QuerySingleOrDefaultAsync<int>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync(expected);

var actual = connection.Object.QuerySingleOrDefaultAsync<int>("").GetAwaiter().GetResult();

Assert.AreEqual(actual, expected);
}

[Test]
public void QuerySingleOrDefaultAsyncGenericUsingDbConnectionInterface()
{
var connection = new Mock<IDbConnection>();

var expected = 7;

connection.SetupDapperAsync(c => c.QuerySingleOrDefaultAsync<int>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync(expected);

var actual = connection.Object.QuerySingleOrDefaultAsync<int>("").GetAwaiter().GetResult();

Assert.AreEqual(actual, expected);
}

[Test]
public void QueryFirstAsyncGeneric()
{
var connection = new Mock<DbConnection>();

var expected = 7;

connection.SetupDapperAsync(c => c.QueryFirstAsync<int>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync(expected);

var actual = connection.Object.QueryFirstAsync<int>("").GetAwaiter().GetResult();

Assert.AreEqual(actual, expected);
}

[Test]
public void QueryFirstAsyncGenericUsingDbConnectionInterface()
{
var connection = new Mock<DbConnection>();

var expected = 7;

connection.SetupDapperAsync(c => c.QueryFirstAsync<int>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync(expected);

var actual = connection.Object.QueryFirstAsync<int>("").GetAwaiter().GetResult();

Assert.AreEqual(actual, expected);
}

[Test]
public void QueryFirstOrDefaultAsyncGeneric()
{
var connection = new Mock<DbConnection>();

var expected = 7;

connection.SetupDapperAsync(c => c.QueryFirstOrDefaultAsync<int>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync(expected);

var actual = connection.Object.QueryFirstOrDefaultAsync<int>("").GetAwaiter().GetResult();

Assert.AreEqual(actual, expected);
}

[Test]
public void QueryFirstOrDefaultAsyncGenericUsingDbConnectionInterface()
{
var connection = new Mock<DbConnection>();

var expected = 7;

connection.SetupDapperAsync(c => c.QueryFirstOrDefaultAsync<int>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync(expected);

var actual = connection.Object.QueryFirstOrDefaultAsync<int>("").GetAwaiter().GetResult();

Assert.AreEqual(actual, expected);
}

[Test]
public void QueryAsyncGenericComplexType()
{
Expand Down
64 changes: 55 additions & 9 deletions Moq.Dapper.Test/DapperQueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Data;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using Dapper;
using NUnit.Framework;

Expand All @@ -11,34 +12,79 @@ namespace Moq.Dapper.Test
public class DapperQueryTest
{
[Test]
public void QueryFirstOrDefault()
public void QueryGeneric()
{
var connection = new Mock<IDbConnection>();

var expected = new[] { 7, 77, 777 };

connection.SetupDapper(c => c.Query<int>(It.IsAny<string>(), null, null, true, null, null))
.Returns(expected);

var actual = connection.Object.Query<int>("").ToList();

Assert.That(actual.Count, Is.EqualTo(expected.Length));
Assert.That(actual, Is.EquivalentTo(expected));
}

[Test]
public void QuerySingle()
{
var connection = new Mock<IDbConnection>();

const int expected = 7;

connection.SetupDapper(c => c.QueryFirstOrDefault<int>(It.IsAny<string>(), null, null, null, null))
connection.SetupDapper(c => c.QuerySingle<int>(It.IsAny<string>(), null, null, null, null))
.Returns(expected);

var actual = connection.Object.QueryFirstOrDefault<int>("");
var actual = connection.Object.QuerySingle<int>("");

Assert.That(actual, Is.EqualTo(expected));
}

[Test]
public void QueryGeneric()
public void QuerySingleOrDefault()
{
var connection = new Mock<IDbConnection>();

var expected = new[] { 7, 77, 777 };
const int expected = 7;

connection.SetupDapper(c => c.Query<int>(It.IsAny<string>(), null, null, true, null, null))
connection.SetupDapper(c => c.QuerySingleOrDefault<int>(It.IsAny<string>(), null, null, null, null))
.Returns(expected);

var actual = connection.Object.Query<int>("").ToList();
var actual = connection.Object.QuerySingleOrDefault<int>("");

Assert.That(actual.Count, Is.EqualTo(expected.Length));
Assert.That(actual, Is.EquivalentTo(expected));
Assert.That(actual, Is.EqualTo(expected));
}

[Test]
public void QueryFirst()
{
var connection = new Mock<IDbConnection>();

const int expected = 7;

connection.SetupDapper(c => c.QueryFirst<int>(It.IsAny<string>(), null, null, null, null))
.Returns(expected);

var actual = connection.Object.QueryFirst<int>("");

Assert.That(actual, Is.EqualTo(expected));
}

[Test]
public void QueryFirstOrDefault()
{
var connection = new Mock<IDbConnection>();

const int expected = 7;

connection.SetupDapper(c => c.QueryFirstOrDefault<int>(It.IsAny<string>(), null, null, null, null))
.Returns(expected);

var actual = connection.Object.QueryFirstOrDefault<int>("");

Assert.That(actual, Is.EqualTo(expected));
}

[Test]
Expand Down
10 changes: 9 additions & 1 deletion Moq.Dapper/DbConnectionInterfaceMockExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public static ISetup<IDbConnection, TResult> SetupDapper<TResult>(this Mock<IDbC
return SetupExecuteScalar<TResult>(mock);

case nameof(SqlMapper.Query):
case nameof(SqlMapper.QueryFirst):
case nameof(SqlMapper.QueryFirstOrDefault):
case nameof(SqlMapper.QuerySingle):
case nameof(SqlMapper.QuerySingleOrDefault):
return SetupQuery<TResult>(mock);

default:
Expand All @@ -46,6 +49,10 @@ public static ISetup<IDbConnection, Task<TResult>> SetupDapperAsync<TResult>(thi
switch (call.Method.Name)
{
case nameof(SqlMapper.QueryAsync):
case nameof(SqlMapper.QueryFirstAsync):
case nameof(SqlMapper.QueryFirstOrDefaultAsync):
case nameof(SqlMapper.QuerySingleAsync):
case nameof(SqlMapper.QuerySingleOrDefaultAsync):
return SetupQueryAsync<TResult>(mock);

default:
Expand All @@ -58,7 +65,8 @@ static ISetup<IDbConnection, Task<TResult>> SetupQueryAsync<TResult>(Mock<IDbCon
{
commandMock.Protected()
.Setup<Task<DbDataReader>>("ExecuteDbDataReaderAsync", ItExpr.IsAny<CommandBehavior>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(() => DbDataReaderFactory.DbDataReader(result));
.ReturnsAsync(() => result().ToDataTable(typeof(TResult))
.ToDataTableReader());
});

static ISetup<IDbConnection, TResult> SetupQuery<TResult>(Mock<IDbConnection> mock) =>
Expand Down
7 changes: 6 additions & 1 deletion Moq.Dapper/DbConnectionMockExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public static ISetup<DbConnection, Task<TResult>> SetupDapperAsync<TResult>(this
switch (call.Method.Name)
{
case nameof(SqlMapper.QueryAsync):
case nameof(SqlMapper.QueryFirstAsync):
case nameof(SqlMapper.QueryFirstOrDefaultAsync):
case nameof(SqlMapper.QuerySingleAsync):
case nameof(SqlMapper.QuerySingleOrDefaultAsync):
return SetupQueryAsync<TResult>(mock);
case nameof(SqlMapper.ExecuteAsync) when typeof(TResult) == typeof(int):
return (ISetup<DbConnection, Task<TResult>>)SetupExecuteAsync(mock);
Expand All @@ -37,7 +41,8 @@ static ISetup<DbConnection, Task<TResult>> SetupQueryAsync<TResult>(Mock<DbConne
{
commandMock.Protected()
.Setup<Task<DbDataReader>>("ExecuteDbDataReaderAsync", ItExpr.IsAny<CommandBehavior>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(() => DbDataReaderFactory.DbDataReader(result));
.ReturnsAsync(() => result().ToDataTable(typeof(TResult))
.ToDataTableReader());
});

static ISetup<DbConnection, Task<int>> SetupExecuteAsync(Mock<DbConnection> mock) =>
Expand Down
72 changes: 0 additions & 72 deletions Moq.Dapper/DbDataReaderFactory.cs

This file was deleted.