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

SqlBatch: Add missing overrides and tests #2223

Merged
merged 2 commits into from
Nov 22, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -1775,6 +1775,14 @@ public override DataTable GetSchema(string collectionName, string[] restrictionV
return InnerConnection.GetSchema(ConnectionFactory, PoolGroup, this, collectionName, restrictionValues);
}

#if NET6_0_OR_GREATER
/// <inheritdoc />
public override bool CanCreateBatch => true;

/// <inheritdoc />
protected override DbBatch CreateDbBatch() => new SqlBatch(this);
#endif

private class OpenAsyncRetry
{
private SqlConnection _parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.Data.SqlClient.ManualTesting.Tests
{
public static class BatchTests
{

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void MissingCommandTextThrows()
{
Expand All @@ -23,7 +24,6 @@ public static void MissingCommandTextThrows()
}
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void MissingConnectionThrows()
{
Expand All @@ -33,7 +33,46 @@ public static void MissingConnectionThrows()
}
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ConnectionCanCreateBatch()
{
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
{
Assert.True(connection.CanCreateBatch);
using (var batch = connection.CreateBatch())
{
Assert.NotNull(batch);
Assert.Equal(connection, batch.Connection);

batch.BatchCommands.Add(new SqlBatchCommand("SELECT @@SPID"));
connection.Open();
batch.ExecuteNonQuery();
}
}
}

#if NET8_0_OR_GREATER
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void SqlBatchCanCreateParameter()
{
using (var connection = new SqlConnection(DataTestUtility.TCPConnectionString))
using (var batch = connection.CreateBatch())
{
SqlBatchCommand batchCommand = new SqlBatchCommand("SELECT @p");

Assert.True(batchCommand.CanCreateParameter);
SqlParameter parameter = batchCommand.CreateParameter();
Assert.NotNull(parameter);
parameter.ParameterName = "@p";
parameter.Value = 1;
batchCommand.Parameters.Add(parameter);

batch.ExecuteNonQuery();

}
}
#endif

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void StoredProcedureBatchSupported()
{
Expand All @@ -45,7 +84,6 @@ public static void StoredProcedureBatchSupported()
}
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void CommandTextBatchSupported()
{
Expand All @@ -57,14 +95,12 @@ public static void CommandTextBatchSupported()
}
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void TableDirectBatchNotSupported()
{
Assert.Throws<ArgumentOutOfRangeException>(() => new SqlBatchCommand("Categories", CommandType.TableDirect));
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void MixedBatchSupported()
{
Expand All @@ -84,7 +120,6 @@ public static void MixedBatchSupported()
}
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void DisposedThrows()
{
Expand All @@ -93,7 +128,6 @@ public static void DisposedThrows()
Assert.Throws<ObjectDisposedException>(() => batch.ExecuteNonQuery());
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ProviderApi()
{
Expand Down Expand Up @@ -166,7 +200,6 @@ public static void ProviderApi()

}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void DirectApi()
{
Expand Down Expand Up @@ -238,7 +271,6 @@ public static void DirectApi()
Assert.NotNull(exception.BatchCommand);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ExceptionInBatchContainsBatch()
{
Expand Down Expand Up @@ -276,7 +308,6 @@ public static void ExceptionInBatchContainsBatch()
Assert.NotNull(exception.BatchCommand);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ExceptionWithoutBatchContainsNoBatch()
{
Expand Down Expand Up @@ -306,7 +337,6 @@ public static void ExceptionWithoutBatchContainsNoBatch()
Assert.Null(exception.BatchCommand);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ParameterInOutAndReturn()
{
Expand Down Expand Up @@ -359,7 +389,6 @@ RETURN @Input
Assert.Equal(2, Convert.ToInt32(returned.Value));
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ExecuteNonQuery()
{
Expand Down Expand Up @@ -422,7 +451,6 @@ public static void ExecuteNonQuery()
Assert.Equal(1, batch.Commands[2].RecordsAffected);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static async Task ExecuteNonQueryAsync()
{
Expand Down Expand Up @@ -485,7 +513,6 @@ public static async Task ExecuteNonQueryAsync()
Assert.Equal(1, batch.Commands[2].RecordsAffected);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ExecuteScalarMultiple()
{
Expand All @@ -505,7 +532,6 @@ public static void ExecuteScalarMultiple()
Assert.Equal(9, value);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static async Task ExecuteScalarAsyncMultiple()
{
Expand All @@ -525,7 +551,6 @@ public static async Task ExecuteScalarAsyncMultiple()
Assert.Equal(9, value);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static void ExecuteReaderMultiple()
{
Expand Down Expand Up @@ -557,7 +582,6 @@ public static void ExecuteReaderMultiple()
Assert.Equal(10, resultRowCount);
}

[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup))]
public static async Task ExecuteReaderAsyncMultiple()
{
Expand Down
Loading