Skip to content

Commit d1deadd

Browse files
authored
Test - Add tests for code coverage part 1 (#1377)
1 parent 397279b commit d1deadd

File tree

7 files changed

+581
-4
lines changed

7 files changed

+581
-4
lines changed

src/Microsoft.Data.SqlClient/tests/FunctionalTests/Microsoft.Data.SqlClient.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<Compile Include="DataCommon\AssemblyResourceManager.cs" />
2929
<Compile Include="DataCommon\SystemDataResourceManager.cs" />
3030
<Compile Include="MultipartIdentifierTests.cs" />
31+
<Compile Include="SqlClientLoggerTest.cs" />
32+
<Compile Include="SqlCommandSetTest.cs" />
3133
<Compile Include="SqlConfigurableRetryLogicTest.cs" />
3234
<Compile Include="SqlCommandBuilderTest.cs" />
3335
<Compile Include="SqlBulkCopyTest.cs" />
@@ -47,6 +49,7 @@
4749
<Compile Include="SqlExceptionTest.cs" />
4850
<Compile Include="SqlFacetAttributeTest.cs" />
4951
<Compile Include="SqlNotificationRequestTest.cs" />
52+
<Compile Include="SqlParameterCollectionTest.cs" />
5053
<Compile Include="SqlParameterTest.cs" />
5154
<Compile Include="SqlClientFactoryTest.cs" />
5255
<Compile Include="SqlErrorCollectionTest.cs" />

src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlClientFactoryTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Data.Sql;
7+
using System.Reflection;
68
using Xunit;
79

810
namespace Microsoft.Data.SqlClient.Tests
@@ -20,6 +22,7 @@ public void InstanceTest()
2022
public static readonly object[][] FactoryMethodTestData =
2123
{
2224
new object[] { new Func<object>(SqlClientFactory.Instance.CreateCommand), typeof(SqlCommand) },
25+
new object[] { new Func<object>(SqlClientFactory.Instance.CreateCommandBuilder), typeof(SqlCommandBuilder) },
2326
new object[] { new Func<object>(SqlClientFactory.Instance.CreateConnection), typeof(SqlConnection) },
2427
new object[] { new Func<object>(SqlClientFactory.Instance.CreateConnectionStringBuilder), typeof(SqlConnectionStringBuilder) },
2528
new object[] { new Func<object>(SqlClientFactory.Instance.CreateDataAdapter), typeof(SqlDataAdapter) },
@@ -40,5 +43,26 @@ public void FactoryMethodTest(Func<object> factory, Type expectedType)
4043

4144
Assert.NotSame(value1, value2);
4245
}
46+
47+
#if NETFRAMEWORK
48+
[Fact]
49+
public void FactoryCreateDataSourceEnumerator()
50+
{
51+
// Unable to cover the in the FactoryMethodTest because the SqlDataSourceEnumerator is a singleton so, it's always the same.
52+
object instance = SqlClientFactory.Instance.CreateDataSourceEnumerator();
53+
// SqlDataSourceEnumerator is not available for .NET core 3.1 and above, so the type check is only for .NET Framework.
54+
Assert.IsType<SqlDataSourceEnumerator>(instance);
55+
Assert.NotNull(instance);
56+
}
57+
58+
[Fact]
59+
public void FactoryGetService()
60+
{
61+
Type type = typeof(SqlClientFactory);
62+
MethodInfo method = type.GetMethod("System.IServiceProvider.GetService", BindingFlags.NonPublic | BindingFlags.Instance);
63+
object res = method.Invoke(SqlClientFactory.Instance, new object[] { null });
64+
Assert.Null(res);
65+
}
66+
#endif
4367
}
4468
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Xunit;
6+
7+
namespace Microsoft.Data.SqlClient.Tests
8+
{
9+
public class SqlClientLoggerTest
10+
{
11+
[Fact]
12+
public void LogWarning()
13+
{
14+
// There is not much to test here but to add the code coverage.
15+
SqlClientLogger logger = new();
16+
logger.LogWarning("test type", "test method", "test message");
17+
}
18+
19+
[Fact]
20+
public void LogAssert()
21+
{
22+
SqlClientLogger logger = new();
23+
logger.LogAssert(true, "test type", "test method", "test message");
24+
}
25+
26+
[Fact]
27+
public void LogError()
28+
{
29+
SqlClientLogger logger = new();
30+
logger.LogError("test type", "test method", "test message");
31+
}
32+
33+
[Fact]
34+
public void LogInfo()
35+
{
36+
SqlClientLogger logger = new();
37+
logger.LogInfo("test type", "test method", "test message");
38+
}
39+
}
40+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
using System;
2+
using System.Data;
3+
using System.Reflection;
4+
using Xunit;
5+
6+
namespace Microsoft.Data.SqlClient.Tests
7+
{
8+
public class SqlCommandSetTest
9+
{
10+
private static Assembly mds = Assembly.GetAssembly(typeof(SqlConnection));
11+
12+
[Theory]
13+
[InlineData("BatchCommand")]
14+
[InlineData("CommandList")]
15+
public void GetDisposedProperty_Throws(string propertyName)
16+
{
17+
var cmdSet = CreateInstance();
18+
CallMethod(cmdSet, "Dispose");
19+
Exception ex = GetProperty_Throws(cmdSet, propertyName);
20+
VerifyException<ObjectDisposedException>(ex, "disposed");
21+
}
22+
23+
[Fact]
24+
public void AppendCommandWithEmptyString_Throws()
25+
{
26+
var cmdSet = CreateInstance();
27+
SqlCommand cmd = new SqlCommand("");
28+
Exception ex = CallMethod_Throws(cmdSet, "Append", cmd);
29+
VerifyException<InvalidOperationException>(ex, "CommandText property has not been initialized");
30+
}
31+
32+
[Theory]
33+
[InlineData(CommandType.TableDirect)]
34+
[InlineData((CommandType)5)]
35+
public void AppendBadCommandType_Throws(CommandType commandType)
36+
{
37+
var cmdSet = CreateInstance();
38+
SqlCommand cmd = GenerateBadCommand(commandType);
39+
Exception ex = CallMethod_Throws(cmdSet, "Append", cmd);
40+
VerifyException<ArgumentOutOfRangeException>(ex, "CommandType");
41+
}
42+
43+
[Fact]
44+
public void AppendBadParameterName_Throws()
45+
{
46+
var cmdSet = CreateInstance();
47+
SqlCommand cmd = new SqlCommand("Test");
48+
cmd.CommandType = CommandType.Text;
49+
cmd.Parameters.Add(new SqlParameter("Test1;=", "1"));
50+
Exception ex = CallMethod_Throws(cmdSet, "Append", cmd);
51+
VerifyException<ArgumentException>(ex, "not valid");
52+
}
53+
54+
[Theory]
55+
[InlineData(new byte[] { 1, 2, 3 })]
56+
[InlineData(new char[] { '1', '2', '3' })]
57+
public void AppendParameterArrayWithSize(object array)
58+
{
59+
var cmdSet = CreateInstance();
60+
SqlCommand cmd = new SqlCommand("Test");
61+
cmd.CommandType = CommandType.StoredProcedure;
62+
SqlParameter parameter = new SqlParameter("@array", array);
63+
parameter.Size = 2;
64+
cmd.Parameters.Add(parameter);
65+
CallMethod(cmdSet, "Append", cmd);
66+
object p = CallMethod(cmdSet, "GetParameter", 0, 0);
67+
SqlParameter result = p as SqlParameter;
68+
Assert.NotNull(result);
69+
Assert.Equal("@array", result.ParameterName);
70+
Assert.Equal(2, result.Size);
71+
}
72+
73+
[Fact]
74+
public void GetParameter()
75+
{
76+
var cmdSet = CreateInstance();
77+
SqlCommand cmd = new SqlCommand("Test");
78+
cmd.CommandType = CommandType.Text;
79+
cmd.Parameters.Add(new SqlParameter("@text", "value"));
80+
CallMethod(cmdSet, "Append", cmd);
81+
object p = CallMethod(cmdSet, "GetParameter", 0, 0);
82+
SqlParameter result = p as SqlParameter;
83+
Assert.NotNull(result);
84+
Assert.Equal("@text", result.ParameterName);
85+
Assert.Equal("value", (string)result.Value);
86+
}
87+
88+
[Fact]
89+
public void GetParameterCount()
90+
{
91+
var commandSetType = mds.GetType("Microsoft.Data.SqlClient.SqlCommandSet");
92+
var cmdSet = Activator.CreateInstance(commandSetType, true);
93+
SqlCommand cmd = new SqlCommand("Test");
94+
cmd.CommandType = CommandType.Text;
95+
cmd.Parameters.Add(new SqlParameter("@abc", "1"));
96+
cmd.Parameters.Add(new SqlParameter("@test", "2"));
97+
commandSetType.GetMethod("Append", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(cmdSet, new object[] { cmd });
98+
int index = 0;
99+
int count = (int)commandSetType.GetMethod("GetParameterCount", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(cmdSet, new object[] { index });
100+
Assert.Equal(2, count);
101+
}
102+
103+
[Fact]
104+
public void InvalidCommandBehaviorValidateCommandBehavior_Throws()
105+
{
106+
var cmdSet = CreateInstance();
107+
Exception ex = CallMethod_Throws(cmdSet, "ValidateCommandBehavior", "ExecuteNonQuery", (CommandBehavior)64);
108+
VerifyException<ArgumentOutOfRangeException>(ex, "CommandBehavior");
109+
}
110+
111+
[Fact]
112+
public void NotSupportedCommandBehaviorValidateCommandBehavior_Throws()
113+
{
114+
var cmdSet = CreateInstance();
115+
Exception ex = CallMethod_Throws(cmdSet, "ValidateCommandBehavior", "ExecuteNonQuery", CommandBehavior.KeyInfo);
116+
VerifyException<ArgumentOutOfRangeException>(ex, "not supported");
117+
}
118+
119+
#region private methods
120+
121+
private object CallMethod(object instance, string methodName, params object[] values)
122+
{
123+
var commandSetType = mds.GetType("Microsoft.Data.SqlClient.SqlCommandSet");
124+
object returnValue = commandSetType.GetMethod(methodName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(instance, values);
125+
return returnValue;
126+
}
127+
128+
private object CallMethod(object instance, string methodName)
129+
{
130+
var commandSetType = mds.GetType("Microsoft.Data.SqlClient.SqlCommandSet");
131+
object returnValue = commandSetType.GetMethod(methodName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(instance, new object[] { });
132+
return returnValue;
133+
}
134+
135+
private Exception CallMethod_Throws(object instance, string methodName, params object[] values)
136+
{
137+
var commandSetType = mds.GetType("Microsoft.Data.SqlClient.SqlCommandSet");
138+
Exception ex = Assert.ThrowsAny<Exception>(() =>
139+
{
140+
commandSetType.GetMethod(methodName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(instance, values);
141+
});
142+
return ex;
143+
}
144+
145+
private object CreateInstance()
146+
{
147+
var commandSetType = mds.GetType("Microsoft.Data.SqlClient.SqlCommandSet");
148+
object cmdSet = Activator.CreateInstance(commandSetType, true);
149+
return cmdSet;
150+
}
151+
152+
private Exception GetProperty_Throws(object instance, string propertyName)
153+
{
154+
var commandSetType = mds.GetType("Microsoft.Data.SqlClient.SqlCommandSet");
155+
var cmdSet = instance;
156+
Exception ex = Assert.ThrowsAny<Exception>(() =>
157+
{
158+
commandSetType.GetProperty(propertyName, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetGetMethod(true).Invoke(cmdSet, new object[] { });
159+
});
160+
161+
return ex;
162+
}
163+
164+
private SqlCommand GenerateBadCommand(CommandType cType)
165+
{
166+
SqlCommand cmd = new SqlCommand("Test");
167+
Type sqlCommandType = cmd.GetType();
168+
// There's validation done on the CommandType property, but we need to create one that avoids the check for the test case.
169+
sqlCommandType.GetField("_commandType", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(cmd, cType);
170+
171+
return cmd;
172+
}
173+
174+
private void VerifyException<T>(Exception ex, string contains)
175+
{
176+
Assert.NotNull(ex);
177+
Assert.IsType<T>(ex.InnerException);
178+
Assert.Contains(contains, ex.InnerException.Message, StringComparison.OrdinalIgnoreCase);
179+
}
180+
#endregion
181+
}
182+
}

0 commit comments

Comments
 (0)