Skip to content

Tests | Move various unit tests to UnitTests project #3458

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -114,7 +114,7 @@ protected override void AfterCleared(SqlCommand owner)
}

private string _commandText;
private CommandType _commandType;
internal CommandType _commandType;
private int? _commandTimeout;
private UpdateRowSource _updatedRowSource = UpdateRowSource.Both;
private bool _designTimeInvisible;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected override void AfterCleared(SqlCommand owner)
}

private string _commandText;
private CommandType _commandType;
internal CommandType _commandType;
private int? _commandTimeout;
private UpdateRowSource _updatedRowSource = UpdateRowSource.Both;
private bool _designTimeInvisible;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal SqlCommandSet() : base()
_commandList = new List<SqlBatchCommand>();
}

private SqlCommand BatchCommand
internal SqlCommand BatchCommand
{
get
{
Expand All @@ -45,7 +45,7 @@ private SqlCommand BatchCommand

internal int CommandCount => CommandList.Count;

private List<SqlBatchCommand> CommandList
internal List<SqlBatchCommand> CommandList
{
get
{
Expand Down Expand Up @@ -279,7 +279,7 @@ internal bool GetBatchedAffected(int commandIdentifier, out int recordsAffected,
internal int GetParameterCount(int commandIndex)
=> CommandList[commandIndex].Parameters.Count;

private void ValidateCommandBehavior(string method, CommandBehavior behavior)
internal void ValidateCommandBehavior(string method, CommandBehavior behavior)
{
if (0 != (behavior & ~(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using System;
using System.Data;
using System.Reflection;
using Xunit;

namespace Microsoft.Data.SqlClient.UnitTests;
Expand All @@ -17,18 +16,16 @@ public class SqlCommandSetTest
/// <summary>
/// Verifies that key properties throw an ObjectDisposedException after the SqlCommandSet has been disposed.
/// </summary>
/// <remarks>
/// These properties are private, requiring reflection to access.
/// </remarks>
[Theory]
[InlineData("BatchCommand")]
[InlineData("CommandList")]
public void GetDisposedProperty_Throws(string propertyName)
[Fact]
public void GetDisposedProperty_Throws()
{
SqlCommandSet cmdSet = new();
cmdSet.Dispose();

ObjectDisposedException ex = GetProperty_Throws<ObjectDisposedException>(cmdSet, propertyName);
ObjectDisposedException ex = Assert.Throws<ObjectDisposedException>(() => _ = cmdSet.BatchCommand);
Assert.Contains("disposed", ex.Message, StringComparison.OrdinalIgnoreCase);

ex = Assert.Throws<ObjectDisposedException>(() => _ = cmdSet.CommandList);
Assert.Contains("disposed", ex.Message, StringComparison.OrdinalIgnoreCase);
}

Expand Down Expand Up @@ -152,7 +149,7 @@ public void InvalidCommandBehaviorValidateCommandBehavior_Throws()
{
SqlCommandSet cmdSet = new();

ArgumentOutOfRangeException ex = InvokeMethod_Throws<ArgumentOutOfRangeException>(cmdSet, "ValidateCommandBehavior", "ExecuteNonQuery", (CommandBehavior)64);
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => cmdSet.ValidateCommandBehavior("ExecuteNonQuery", (CommandBehavior)64));
Assert.Contains("CommandBehavior", ex.Message, StringComparison.OrdinalIgnoreCase);
}

Expand All @@ -164,50 +161,16 @@ public void NotSupportedCommandBehaviorValidateCommandBehavior_Throws()
{
SqlCommandSet cmdSet = new();

ArgumentOutOfRangeException ex = InvokeMethod_Throws<ArgumentOutOfRangeException>(cmdSet, "ValidateCommandBehavior", "ExecuteNonQuery", CommandBehavior.KeyInfo);
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => cmdSet.ValidateCommandBehavior("ExecuteNonQuery", CommandBehavior.KeyInfo));
Assert.Contains("not supported", ex.Message, StringComparison.OrdinalIgnoreCase);
}

#region private methods

private static T GetProperty_Throws<T>(SqlCommandSet instance, string propertyName)
where T : Exception
=> InvokeMethod_Throws<T>(instance,
typeof(SqlCommandSet)
.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance)
.GetGetMethod(true),
[]);

private static T InvokeMethod_Throws<T>(SqlCommandSet instance, string methodName, params object[] values)
where T : Exception
=> InvokeMethod_Throws<T>(instance,
typeof(SqlCommandSet)
.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance),
values);

private static T InvokeMethod_Throws<T>(SqlCommandSet instance, MethodInfo methodInfo, params object[] values)
where T : Exception
{
return Assert.Throws<T>(() =>
{
try
{
methodInfo.Invoke(instance, values);
}
catch (TargetInvocationException e)
{
throw e.InnerException;
}
});
}

private static SqlCommand GenerateBadCommand(CommandType cType)
{
SqlCommand cmd = new("Test");
// There's validation done on the CommandType property, but we need to create one that avoids the check for the test case.
typeof(SqlCommand).GetField("_commandType", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(cmd, cType);
cmd._commandType = cType;

return cmd;
}
#endregion
}
Loading