Skip to content
Permalink
Browse files

Completes ExecuteAsync, ExecuteScalarAsync, and QueryAsync

  • Loading branch information...
pleb committed Mar 23, 2019
1 parent 9afff19 commit d03b1fa659bcb48373eec6b4e4b401bedf319ab3
@@ -1,10 +1,4 @@
// <copyright company="PetaPoco - CollaboratingPlatypus">
// Apache License, Version 2.0 https://github.com/CollaboratingPlatypus/PetaPoco/blob/master/LICENSE.txt
// </copyright>
// <author>PetaPoco - CollaboratingPlatypus</author>
// <date>2018/07/02</date>

using System;
using System;
using PetaPoco.Core;
using PetaPoco.Tests.Integration.Models;
using Shouldly;
@@ -26,8 +20,7 @@ protected BaseExecuteTests(DBTestProvider provider)
public void Execute_GivenSqlAndArgumentAffectsOneRow_ShouldReturnOne()
{
InsertNotes(5);
var sql = $"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" +
$"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} = @0";
var sql = $"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" + $"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} = @0";

var beforeCount = CountNotes();
var result = DB.Execute(sql, 1);
@@ -44,8 +37,8 @@ public void Execute_GivenSqlAndArgumentsAffectsTwoRows_ShouldReturnTwo()
InsertNotes(5);

var beforeCount = CountNotes();
var result = DB.Execute($"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" +
$"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} IN(@0,@1)", 1, 2);
var result = DB.Execute(
$"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" + $"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} IN(@0,@1)", 1, 2);
var afterCount = CountNotes();

beforeCount.ShouldBe(5);
@@ -59,8 +52,8 @@ public void Execute_GivenSqlAffectsOneRow_ShouldReturnOne()
InsertNotes(5);

var beforeCount = CountNotes();
var result = DB.Execute($"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" +
$"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} = 1");
var result = DB.Execute(
$"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" + $"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} = 1");
var afterCount = CountNotes();

beforeCount.ShouldBe(5);
@@ -83,6 +76,66 @@ public void Execute_GivenSqlAffectsTwoRows_ShouldReturnTwo()
afterCount.ShouldBe(3);
}

[Fact]
public async void ExecuteAsync_GivenSqlAndArgumentAffectsOneRow_ShouldReturnOne()
{
InsertNotes(5);
var sql = $"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" + $"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} = @0";

var beforeCount = CountNotes();
var result = await DB.ExecuteAsync(sql, 1);
var afterCount = CountNotes();

beforeCount.ShouldBe(5);
result.ShouldBe(1);
afterCount.ShouldBe(4);
}

[Fact]
public async void ExecuteAsync_GivenSqlAndArgumentsAffectsTwoRows_ShouldReturnTwo()
{
InsertNotes(5);

var beforeCount = CountNotes();
var result = await DB.ExecuteAsync(
$"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" + $"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} IN(@0,@1)", 1, 2);
var afterCount = CountNotes();

beforeCount.ShouldBe(5);
result.ShouldBe(2);
afterCount.ShouldBe(3);
}

[Fact]
public async void ExecuteAsync_GivenSqlAffectsOneRow_ShouldReturnOne()
{
InsertNotes(5);

var beforeCount = CountNotes();
var result = await DB.ExecuteAsync($"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" +
$"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} = 1");
var afterCount = CountNotes();

beforeCount.ShouldBe(5);
result.ShouldBe(1);
afterCount.ShouldBe(4);
}

[Fact]
public async void ExecuteAsync_GivenSqlAffectsTwoRows_ShouldReturnTwo()
{
InsertNotes(5);

var beforeCount = CountNotes();
var result = await DB.ExecuteAsync($"DELETE FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" +
$"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} IN(1,2)");
var afterCount = CountNotes();

beforeCount.ShouldBe(5);
result.ShouldBe(2);
afterCount.ShouldBe(3);
}

[Fact]
public void ExecuteScalar_GivenSql_ReturnShouldBeValid()
{
@@ -96,17 +149,47 @@ public void ExecuteScalar_GivenSqlAndParameter_ReturnShouldBeValid()
{
InsertNotes(4);

DB.ExecuteScalar<int>($"SELECT COUNT(*) FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" +
$"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} <= @0", 2).ShouldBe(2);
DB.ExecuteScalar<int>(
$"SELECT COUNT(*) FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" + $"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} <= @0", 2)
.ShouldBe(2);
}

[Fact]
public void ExecuteScalar_GivenSqlAndParameters_ReturnShouldBeValid()
{
InsertNotes(5);

DB.ExecuteScalar<int>($"SELECT COUNT(*) FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" +
$"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} IN(@0, @1)", 1, 2).ShouldBe(2);
DB.ExecuteScalar<int>(
$"SELECT COUNT(*) FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" + $"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} IN(@0, @1)", 1,
2).ShouldBe(2);
}

[Fact]
public async void ExecuteScalarAsync_GivenSql_ReturnShouldBeValid()
{
InsertNotes(3);

(await DB.ExecuteScalarAsync<int>($"SELECT COUNT(*) FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}")).ShouldBe(3);
}

[Fact]
public async void ExecuteScalarAsync_GivenSqlAndParameter_ReturnShouldBeValid()
{
InsertNotes(4);

(await DB.ExecuteScalarAsync<int>(
$"SELECT COUNT(*) FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" + $"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} <= @0", 2))
.ShouldBe(2);
}

[Fact]
public async void ExecuteScalarAsync_GivenSqlAndParameters_ReturnShouldBeValid()
{
InsertNotes(5);

(await DB.ExecuteScalarAsync<int>(
$"SELECT COUNT(*) FROM {DB.Provider.EscapeTableName(_pd.TableInfo.TableName)}" + $"WHERE {DB.Provider.EscapeSqlIdentifier(_pd.TableInfo.PrimaryKey)} IN(@0, @1)", 1,
2)).ShouldBe(2);
}

private int CountNotes()
Oops, something went wrong.

0 comments on commit d03b1fa

Please sign in to comment.
You can’t perform that action at this time.