Skip to content

Commit

Permalink
Test DbDataReader after DELETE. Fixes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Jun 14, 2019
1 parent 9e31859 commit 78b9724
Show file tree
Hide file tree
Showing 19 changed files with 189 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/AdoNet.Databases/MySqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ insert into select_value values

public static string SelectNoRows => "SELECT * FROM mysql.user WHERE 0 = 1;";

public static string DeleteNoRows => "DELETE FROM select_value WHERE 0 = 1;";

public static IReadOnlyCollection<DbType> SupportedDbTypes { get; } = new ReadOnlyCollection<DbType>(new[]
{
DbType.Binary,
Expand Down
2 changes: 2 additions & 0 deletions src/AdoNet.Databases/PostgreSqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ insert into select_value values

public static string SelectNoRows => "SELECT 1 WHERE 0 = 1;";

public static string DeleteNoRows => "DELETE FROM select_value WHERE 0 = 1;";

public static IReadOnlyCollection<DbType> SupportedDbTypes { get; } = new ReadOnlyCollection<DbType>(new[]
{
DbType.Binary,
Expand Down
2 changes: 2 additions & 0 deletions src/AdoNet.Databases/SqlServerDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ insert into select_value values

public static string SelectNoRows => "SELECT 1 WHERE 0 = 1;";

public static string DeleteNoRows => "DELETE FROM select_value WHERE 0 = 1;";

public static IReadOnlyCollection<DbType> SupportedDbTypes { get; } = new ReadOnlyCollection<DbType>(new[]
{
DbType.Binary,
Expand Down
2 changes: 2 additions & 0 deletions src/AdoNet.Databases/SqliteDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ insert into select_value values

public static string SelectNoRows => "SELECT 1 WHERE 0 = 1;";

public static string DeleteNoRows => "DELETE FROM select_value WHERE 0 = 1;";

public static IReadOnlyCollection<DbType> SupportedDbTypes { get; } = new ReadOnlyCollection<DbType>(new[]
{
DbType.Binary,
Expand Down
136 changes: 135 additions & 1 deletion src/AdoNet.Specification.Tests/DataReaderTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace AdoNet.Specification.Tests
{
[Collection("ISelectValueFixture Collection")]
public class DataReaderTestBase<TFixture> : DbFactoryTestBase<TFixture>
where TFixture : class, ISelectValueFixture
where TFixture : class, ISelectValueFixture, IDeleteFixture
{
public DataReaderTestBase(TFixture fixture)
: base(fixture)
Expand Down Expand Up @@ -806,6 +806,114 @@ public virtual void GetFieldValue_for_TextReader_throws_for_null_String()
}
}

[Fact]
public virtual void FieldCount_is_zero_after_Delete() => Test_X_after_Delete(x => Assert.Equal(0, x.FieldCount));

[Fact]
public virtual void IsDBNull_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.IsDBNull(0)));

[Fact]
public virtual async Task IsDBNullAsync_throws_after_Delete() => await Test_X_after_Delete(async x => await Assert.ThrowsAsync<InvalidOperationException>(async () => await x.IsDBNullAsync(0)));

[Fact]
public virtual void Read_returns_false_after_Delete() => Test_X_after_Delete(x => Assert.False(x.Read()));

[Fact]
public virtual void NextResult_returns_false_after_Delete() => Test_X_after_Delete(x => Assert.False(x.NextResult()));

[Fact]
public virtual void GetDataTypeName_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetDataTypeName(0)));

[Fact]
public virtual void GetFieldType_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetFieldType(0)));

[Fact]
public virtual void GetFieldValue_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetFieldValue<object>(0)));

[Fact]
public virtual async Task GetFieldValueAsync_throws_after_Delete() => await Test_X_after_Delete(async x => await Assert.ThrowsAsync<InvalidOperationException>(async () => await x.GetFieldValueAsync<object>(0)));

[Fact]
public virtual void GetBoolean_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetBoolean(0)));

[Fact]
public virtual void GetByte_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetByte(0)));

[Fact]
public virtual void GetBytes_throws_after_Delete()
{
Test_X_after_Delete(x =>
{
var bytes = new byte[1];
Assert.Throws<InvalidOperationException>(() => x.GetBytes(0, 0, bytes, 0, 1));
});
}

[Fact]
public virtual void GetChar_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetChar(0)));

[Fact]
public virtual void GetChars_throws_after_Delete()
{
Test_X_after_Delete(x =>
{
var chars = new char[1];
Assert.Throws<InvalidOperationException>(() => x.GetChars(0, 0, chars, 0, 1));
});
}

[Fact]
public virtual void GetDataTime_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetDateTime(0)));

[Fact]
public virtual void GetDecimal_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetDecimal(0)));

[Fact]
public virtual void GetDouble_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetDouble(0)));

[Fact]
public virtual void GetFloat_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetFloat(0)));

[Fact]
public virtual void GetGuid_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetGuid(0)));

[Fact]
public virtual void GetInt16_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetInt16(0)));

[Fact]
public virtual void GetInt32_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetInt32(0)));

[Fact]
public virtual void GetInt64_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetInt64(0)));

[Fact]
public virtual void GetSchemaTable_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetSchemaTable()));

[Fact]
public virtual void GetStream_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetStream(0)));

[Fact]
public virtual void GetString_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetString(0)));

[Fact]
public virtual void GetTextReader_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetTextReader(0)));

[Fact]
public virtual void GetName_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() => x.GetName(0)));

[Fact]
public virtual void GetValue_throws_after_Delete() => Test_X_after_Delete(x => Assert.Throws<InvalidOperationException>(() =>x.GetValue(0)));

[Fact]
public virtual void GetValues_throws_after_Delete()
{
Test_X_after_Delete(x =>
{
var values = new object[1];
Assert.Throws<InvalidOperationException>(() => x.GetValues(values));
});
}

private void TestGetTextReader(ValueKind valueKind, string expected)
{
using (var connection = CreateOpenConnection())
Expand Down Expand Up @@ -881,5 +989,31 @@ private void X_throws_when_closed(Action<DbDataReader> action)
Assert.Throws<InvalidOperationException>(() => action(reader));
}
}

private void Test_X_after_Delete(Action<DbDataReader> action)
{
using (var connection = CreateOpenConnection())
using (var command = connection.CreateCommand())
{
command.CommandText = Fixture.DeleteNoRows;
using (var reader = command.ExecuteReader())
{
action(reader);
}
}
}

private async Task Test_X_after_Delete(Func<DbDataReader, Task> action)
{
using (var connection = CreateOpenConnection())
using (var command = connection.CreateCommand())
{
command.CommandText = Fixture.DeleteNoRows;
using (var reader = command.ExecuteReader())
{
await action(reader);
}
}
}
}
}
13 changes: 13 additions & 0 deletions src/AdoNet.Specification.Tests/IDeleteFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace AdoNet.Specification.Tests
{
/// <summary>
/// Provides methods to DELETE rows from a particular database server.
/// </summary>
public interface IDeleteFixture
{
/// <summary>
/// Returns a <c>DELETE</c> statement that deletes no rows.
/// </summary>
string DeleteNoRows { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace DotConnectMySql.Tests
{
public class DotConnectMySqlSelectValueFixture : DotConnectMySqlDbFactoryFixture, ISelectValueFixture
public class DotConnectMySqlSelectValueFixture : DotConnectMySqlDbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public DotConnectMySqlSelectValueFixture() => MySqlDatabase.CreateSelectValueTable(this);

Expand All @@ -21,5 +21,7 @@ public class DotConnectMySqlSelectValueFixture : DotConnectMySqlDbFactoryFixture
public IReadOnlyCollection<DbType> SupportedDbTypes => MySqlDatabase.SupportedDbTypes;

public Type NullValueExceptionType => MySqlDatabase.NullValueExceptionType;

public string DeleteNoRows => MySqlDatabase.DeleteNoRows;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace DotConnectPostgres.Tests
{
public class DotConnectPostgresSelectValueFixture : DotConnectPostgresDbFactoryFixture, ISelectValueFixture, IDisposable
public class DotConnectPostgresSelectValueFixture : DotConnectPostgresDbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public DotConnectPostgresSelectValueFixture() => PostgreSqlDatabase.CreateSelectValueTable(this);
public void Dispose() => PostgreSqlDatabase.DropSelectValueTable(this);
Expand All @@ -15,5 +15,6 @@ public class DotConnectPostgresSelectValueFixture : DotConnectPostgresDbFactoryF
public string SelectNoRows => PostgreSqlDatabase.SelectNoRows;
public IReadOnlyCollection<DbType> SupportedDbTypes => PostgreSqlDatabase.SupportedDbTypes;
public Type NullValueExceptionType => PostgreSqlDatabase.NullValueExceptionType;
public string DeleteNoRows => PostgreSqlDatabase.DeleteNoRows;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MicrosoftSqlClient.Tests
{
public class MicrosoftSqlClientSelectValueFixture : MicrosoftSqlClientDbFactoryFixture, ISelectValueFixture, IDisposable
public class MicrosoftSqlClientSelectValueFixture : MicrosoftSqlClientDbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public MicrosoftSqlClientSelectValueFixture()
{
Expand All @@ -24,5 +24,7 @@ public MicrosoftSqlClientSelectValueFixture()
public IReadOnlyCollection<DbType> SupportedDbTypes => SqlServerDatabase.SupportedDbTypes;

public Type NullValueExceptionType => SqlServerDatabase.NullValueExceptionType;

public string DeleteNoRows => SqlServerDatabase.DeleteNoRows;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MicrosoftSqlite.Tests
{
public class MicrosoftSqliteSelectValueFixture : MicrosoftSqliteDbFactoryFixture, ISelectValueFixture, IDisposable
public class MicrosoftSqliteSelectValueFixture : MicrosoftSqliteDbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public MicrosoftSqliteSelectValueFixture() => SqliteDatabase.CreateSelectValueTable(this);
public void Dispose() => SqliteDatabase.DropSelectValueTable(this);
Expand All @@ -23,5 +23,6 @@ public class MicrosoftSqliteSelectValueFixture : MicrosoftSqliteDbFactoryFixture
};

public Type NullValueExceptionType => SqliteDatabase.NullValueExceptionType;
public string DeleteNoRows => SqliteDatabase.DeleteNoRows;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MicrosoftSqlite22.Tests
{
public class MicrosoftSqlite22SelectValueFixture : MicrosoftSqlite22DbFactoryFixture, ISelectValueFixture, IDisposable
public class MicrosoftSqlite22SelectValueFixture : MicrosoftSqlite22DbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public MicrosoftSqlite22SelectValueFixture() => SqliteDatabase.CreateSelectValueTable(this);
public void Dispose() => SqliteDatabase.DropSelectValueTable(this);
Expand All @@ -15,5 +15,6 @@ public class MicrosoftSqlite22SelectValueFixture : MicrosoftSqlite22DbFactoryFix
public string SelectNoRows => SqliteDatabase.SelectNoRows;
public IReadOnlyCollection<DbType> SupportedDbTypes => SqliteDatabase.SupportedDbTypes;
public Type NullValueExceptionType => SqliteDatabase.NullValueExceptionType;
public string DeleteNoRows => SqliteDatabase.DeleteNoRows;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MySqlConnector.Tests
{
public class MySqlConnectorSelectValueFixture : MySqlConnectorDbFactoryFixture, ISelectValueFixture, IDisposable
public class MySqlConnectorSelectValueFixture : MySqlConnectorDbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public MySqlConnectorSelectValueFixture() => MySqlDatabase.CreateSelectValueTable(this);

Expand All @@ -21,5 +21,7 @@ public class MySqlConnectorSelectValueFixture : MySqlConnectorDbFactoryFixture,
public IReadOnlyCollection<DbType> SupportedDbTypes => MySqlDatabase.SupportedDbTypes;

public Type NullValueExceptionType => MySqlDatabase.NullValueExceptionType;

public string DeleteNoRows => MySqlDatabase.DeleteNoRows;
}
}
4 changes: 3 additions & 1 deletion tests/MySqlData.Tests/MySqlDataSelectValueFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MySqlData.Tests
{
public class MySqlDataSelectValueFixture : MySqlDataDbFactoryFixture, ISelectValueFixture, IDisposable
public class MySqlDataSelectValueFixture : MySqlDataDbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public MySqlDataSelectValueFixture() => MySqlDatabase.CreateSelectValueTable(this);

Expand All @@ -21,5 +21,7 @@ public class MySqlDataSelectValueFixture : MySqlDataDbFactoryFixture, ISelectVal
public IReadOnlyCollection<DbType> SupportedDbTypes => MySqlDatabase.SupportedDbTypes;

public Type NullValueExceptionType => MySqlDatabase.NullValueExceptionType;

public string DeleteNoRows => MySqlDatabase.DeleteNoRows;
}
}
4 changes: 3 additions & 1 deletion tests/MySqlData8.Tests/MySqlData8SelectValueFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace MySqlData8.Tests
{
public class MySqlData8SelectValueFixture : MySqlData8DbFactoryFixture, ISelectValueFixture, IDisposable
public class MySqlData8SelectValueFixture : MySqlData8DbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public MySqlData8SelectValueFixture() => MySqlDatabase.CreateSelectValueTable(this);

Expand All @@ -21,5 +21,7 @@ public class MySqlData8SelectValueFixture : MySqlData8DbFactoryFixture, ISelectV
public IReadOnlyCollection<DbType> SupportedDbTypes => MySqlDatabase.SupportedDbTypes;

public Type NullValueExceptionType => MySqlDatabase.NullValueExceptionType;

public string DeleteNoRows => MySqlDatabase.DeleteNoRows;
}
}
3 changes: 2 additions & 1 deletion tests/Npgsql3.Tests/Npgsql3SelectValueFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Npgsql3.Tests
{
public class Npgsql3SelectValueFixture : Npgsql3DbFactoryFixture, ISelectValueFixture, IDisposable
public class Npgsql3SelectValueFixture : Npgsql3DbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public Npgsql3SelectValueFixture() => PostgreSqlDatabase.CreateSelectValueTable(this);
public void Dispose() => PostgreSqlDatabase.DropSelectValueTable(this);
Expand All @@ -15,5 +15,6 @@ public class Npgsql3SelectValueFixture : Npgsql3DbFactoryFixture, ISelectValueFi
public string SelectNoRows => PostgreSqlDatabase.SelectNoRows;
public IReadOnlyCollection<DbType> SupportedDbTypes => PostgreSqlDatabase.SupportedDbTypes;
public Type NullValueExceptionType => PostgreSqlDatabase.NullValueExceptionType;
public string DeleteNoRows => PostgreSqlDatabase.DeleteNoRows;
}
}
3 changes: 2 additions & 1 deletion tests/Npgsql4.Tests/Npgsql4SelectValueFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Npgsql4.Tests
{
public class Npgsql4SelectValueFixture : Npgsql4DbFactoryFixture, ISelectValueFixture, IDisposable
public class Npgsql4SelectValueFixture : Npgsql4DbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public Npgsql4SelectValueFixture() => PostgreSqlDatabase.CreateSelectValueTable(this);
public void Dispose() => PostgreSqlDatabase.DropSelectValueTable(this);
Expand All @@ -15,5 +15,6 @@ public class Npgsql4SelectValueFixture : Npgsql4DbFactoryFixture, ISelectValueFi
public string SelectNoRows => PostgreSqlDatabase.SelectNoRows;
public IReadOnlyCollection<DbType> SupportedDbTypes => PostgreSqlDatabase.SupportedDbTypes;
public Type NullValueExceptionType => PostgreSqlDatabase.NullValueExceptionType;
public string DeleteNoRows => PostgreSqlDatabase.DeleteNoRows;
}
}
4 changes: 3 additions & 1 deletion tests/SqlClient.Tests/SqlClientSelectValueFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace SqlClient.Tests
{
public class SqlClientSelectValueFixture : SqlClientDbFactoryFixture, ISelectValueFixture, IDisposable
public class SqlClientSelectValueFixture : SqlClientDbFactoryFixture, ISelectValueFixture, IDeleteFixture, IDisposable
{
public SqlClientSelectValueFixture()
{
Expand All @@ -24,5 +24,7 @@ public SqlClientSelectValueFixture()
public IReadOnlyCollection<DbType> SupportedDbTypes => SqlServerDatabase.SupportedDbTypes;

public Type NullValueExceptionType => SqlServerDatabase.NullValueExceptionType;

public string DeleteNoRows => SqlServerDatabase.DeleteNoRows;
}
}
Loading

0 comments on commit 78b9724

Please sign in to comment.