Permalink
Browse files

Async-ing things

  • Loading branch information...
jbogard committed Jun 30, 2017
1 parent 5da7dc0 commit 2f08dbd309b5850acf87f263034d48d96e131752
@@ -1,4 +1,5 @@
using Xunit;
using System.Threading.Tasks;
using Xunit;
namespace Respawn.Tests
{
@@ -48,7 +49,7 @@ public PostgresTests()
}
[Fact]
public void ShouldDeleteData()
public async Task ShouldDeleteData()
{
_database.Execute("create table \"foo\" (value int)");
@@ -64,13 +65,13 @@ public void ShouldDeleteData()
DbAdapter = DbAdapter.Postgres,
SchemasToInclude = new [] { "public" }
};
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM \"foo\"").ShouldBe(0);
}
[Fact]
public void ShouldIgnoreTables()
public async Task ShouldIgnoreTables()
{
_database.Execute("create table foo (value int)");
_database.Execute("create table bar (value int)");
@@ -87,14 +88,14 @@ public void ShouldIgnoreTables()
SchemasToInclude = new[] { "public" },
TablesToIgnore = new[] { "foo" }
};
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM bar").ShouldBe(0);
}
[Fact]
public void ShouldExcludeSchemas()
public async Task ShouldExcludeSchemas()
{
_database.Execute("create schema a");
_database.Execute("create schema b");
@@ -112,14 +113,14 @@ public void ShouldExcludeSchemas()
DbAdapter = DbAdapter.Postgres,
SchemasToExclude = new [] { "a", "pg_catalog" }
};
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM a.foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM b.bar").ShouldBe(0);
}
[Fact]
public void ShouldIncludeSchemas()
public async Task ShouldIncludeSchemas()
{
_database.Execute("create schema a");
_database.Execute("create schema b");
@@ -137,7 +138,7 @@ public void ShouldIncludeSchemas()
DbAdapter = DbAdapter.Postgres,
SchemasToInclude = new [] { "b" }
};
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM a.foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM b.bar").ShouldBe(0);
@@ -3,6 +3,7 @@
using System.Data.SqlServerCe;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NPoco;
using Shouldly;
using Xunit;
@@ -41,7 +42,7 @@ public SqlServerCeTests()
}
[Fact]
public void ShouldDeleteData()
public async Task ShouldDeleteData()
{
_database.Execute("create table Foo (Value [int])");
@@ -53,13 +54,13 @@ public void ShouldDeleteData()
{
DbAdapter = DbAdapter.SqlServerCe
};
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(0);
}
[Fact]
public void ShouldIgnoreTables()
public async Task ShouldIgnoreTables()
{
_database.Execute("create table Foo (Value [int])");
_database.Execute("create table Bar (Value [int])");
@@ -72,7 +73,7 @@ public void ShouldIgnoreTables()
DbAdapter = DbAdapter.SqlServerCe,
TablesToIgnore = new[] { "Foo" }
};
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Bar").ShouldBe(0);
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Threading.Tasks;
using Xunit;
namespace Respawn.Tests
@@ -42,7 +43,7 @@ public SqlServerTests()
}
[Fact]
public void ShouldDeleteData()
public async Task ShouldDeleteData()
{
_database.Execute("drop table if exists Foo");
_database.Execute("create table Foo (Value [int])");
@@ -52,13 +53,13 @@ public void ShouldDeleteData()
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(100);
var checkpoint = new Checkpoint();
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(0);
}
[Fact]
public void ShouldIgnoreTables()
public async Task ShouldIgnoreTables()
{
_database.Execute("drop table if exists Foo");
_database.Execute("drop table if exists Bar");
@@ -72,14 +73,14 @@ public void ShouldIgnoreTables()
{
TablesToIgnore = new[] {"Foo"}
};
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM Bar").ShouldBe(0);
}
[Fact]
public void ShouldExcludeSchemas()
public async Task ShouldExcludeSchemas()
{
_database.Execute("drop table if exists A.Foo");
_database.Execute("drop table if exists B.Bar");
@@ -100,14 +101,14 @@ public void ShouldExcludeSchemas()
{
SchemasToExclude = new [] { "A" }
};
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM A.Foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM B.Bar").ShouldBe(0);
}
[Fact]
public void ShouldIncludeSchemas()
public async Task ShouldIncludeSchemas()
{
_database.Execute("drop table if exists A.Foo");
_database.Execute("drop table if exists B.Bar");
@@ -128,7 +129,7 @@ public void ShouldIncludeSchemas()
{
SchemasToInclude = new [] { "B" }
};
checkpoint.Reset(_connection);
await checkpoint.Reset(_connection);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM A.Foo").ShouldBe(100);
_database.ExecuteScalar<int>("SELECT COUNT(1) FROM B.Bar").ShouldBe(0);
View
@@ -1,4 +1,5 @@
namespace Respawn
namespace Respawn
{
using System;
using System.Collections.Generic;
@@ -7,6 +8,7 @@
using System.Data.SqlClient;
#endif
using System.Linq;
using System.Threading.Tasks;
public class Checkpoint
{
@@ -30,28 +32,28 @@ private class Relationship
}
#if !NETSTANDARD1_2
public virtual void Reset(string nameOrConnectionString)
public virtual async Task Reset(string nameOrConnectionString)
{
using (var connection = new SqlConnection(nameOrConnectionString))
{
connection.Open();
await connection.OpenAsync();
Reset(connection);
await Reset(connection);
}
}
#endif
public virtual void Reset(DbConnection connection)
public virtual async Task Reset(DbConnection connection)
{
if (string.IsNullOrWhiteSpace(_deleteSql))
{
BuildDeleteTables(connection);
await BuildDeleteTables(connection);
}
ExecuteDeleteSql(connection);
await ExecuteDeleteSqlAsync(connection);
}
private void ExecuteDeleteSql(DbConnection connection)
private async Task ExecuteDeleteSqlAsync(DbConnection connection)
{
using (var tx = connection.BeginTransaction())
using (var cmd = connection.CreateCommand())
@@ -60,17 +62,17 @@ private void ExecuteDeleteSql(DbConnection connection)
cmd.CommandText = _deleteSql;
cmd.Transaction = tx;
cmd.ExecuteNonQuery();
await cmd.ExecuteNonQueryAsync();
tx.Commit();
}
}
private void BuildDeleteTables(DbConnection connection)
private async Task BuildDeleteTables(DbConnection connection)
{
var allTables = GetAllTables(connection);
var allTables = await GetAllTables(connection);
var allRelationships = GetRelationships(connection);
var allRelationships = await GetRelationships(connection);
_tablesToDelete = BuildTableList(allTables, allRelationships);
@@ -114,17 +116,17 @@ private void BuildDeleteTables(DbConnection connection)
return tablesToDelete.ToArray();
}
private IList<Relationship> GetRelationships(DbConnection connection)
private async Task<IList<Relationship>> GetRelationships(DbConnection connection)
{
var rels = new List<Relationship>();
var commandText = DbAdapter.BuildRelationshipCommandText(this);
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = commandText;
using (var reader = cmd.ExecuteReader())
using (var reader = await cmd.ExecuteReaderAsync())
{
while (reader.Read())
while (await reader.ReadAsync())
{
var rel = new Relationship
{
@@ -139,7 +141,7 @@ private IList<Relationship> GetRelationships(DbConnection connection)
return rels;
}
private IList<string> GetAllTables(DbConnection connection)
private async Task<IList<string>> GetAllTables(DbConnection connection)
{
var tables = new List<string>();
@@ -148,9 +150,9 @@ private IList<string> GetAllTables(DbConnection connection)
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = commandText;
using (var reader = cmd.ExecuteReader())
using (var reader = await cmd.ExecuteReaderAsync())
{
while (reader.Read())
while (await reader.ReadAsync())
{
if (!reader.IsDBNull(0))
{
View
@@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Intelligent checkpoints for database tests</Description>
<Copyright>Copyright Jimmy Bogard</Copyright>
<VersionPrefix>0.3.0-preview1</VersionPrefix>
<VersionPrefix>0.3.0-preview2</VersionPrefix>
<Authors>Jimmy Bogard</Authors>
<TargetFrameworks>net452;netstandard1.2;netstandard2.0</TargetFrameworks>
<AssemblyName>Respawn</AssemblyName>

0 comments on commit 2f08dbd

Please sign in to comment.