Skip to content

Commit 996e460

Browse files
committed
Remove unnecessary reflection from SqlBufferTests
1 parent fae085a commit 996e460

File tree

1 file changed

+7
-153
lines changed
  • src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient

1 file changed

+7
-153
lines changed

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlBufferTests.cs

Lines changed: 7 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,14 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Collections.Generic;
76
using System.Data.SqlTypes;
8-
using System.Linq;
9-
using System.Reflection;
107
using Xunit;
118

129
namespace Microsoft.Data.SqlClient.UnitTests
1310
{
1411
public sealed class SqlBufferTests
1512
{
16-
static SqlBufferTests()
17-
{
18-
const string sqlBufferTypeFullName = "Microsoft.Data.SqlClient.SqlBuffer";
19-
const string storageTypeName = nameof(SqlBufferProxy.StorageType);
20-
21-
var assembly = typeof(SqlClientFactory).Assembly;
22-
_sqlBufferType = assembly.GetType(sqlBufferTypeFullName)
23-
?? throw new Exception($"Type not found [{sqlBufferTypeFullName}]");
24-
_storageTypeType = _sqlBufferType.GetNestedTypes(BindingFlags.NonPublic)
25-
.FirstOrDefault(x => x.Name == storageTypeName)
26-
?? throw new Exception($"Type not found [{sqlBufferTypeFullName}+{storageTypeName}]");
27-
}
28-
29-
private static readonly Type _sqlBufferType;
30-
private static readonly Type _storageTypeType;
31-
private readonly SqlBufferProxy _target = new();
32-
33-
public static IEnumerable<object[]> GetStorageTypeValues()
34-
{
35-
#if NET
36-
return Enum.GetValues<SqlBufferProxy.StorageType>()
37-
.Select(x => new object[] { x });
38-
#else
39-
return Enum.GetValues(typeof(SqlBufferProxy.StorageType))
40-
.OfType<SqlBufferProxy.StorageType>()
41-
.Select(x => new object[] { x });
42-
#endif
43-
}
44-
45-
[Theory]
46-
[MemberData(nameof(GetStorageTypeValues))]
47-
public void StorageTypeInProxyShouldHaveTheSameValueAsOriginal(SqlBufferProxy.StorageType expected)
48-
{
49-
var originalEnumName = Enum.GetName(_storageTypeType, (int)expected);
50-
51-
Assert.Equal(expected.ToString(), originalEnumName);
52-
}
13+
private readonly SqlBuffer _target = new();
5314

5415
[Fact]
5516
public void GuidShouldThrowWhenSqlGuidNullIsSet()
@@ -60,9 +21,9 @@ public void GuidShouldThrowWhenSqlGuidNullIsSet()
6021
}
6122

6223
[Theory]
63-
[InlineData(SqlBufferProxy.StorageType.Guid)]
64-
[InlineData(SqlBufferProxy.StorageType.SqlGuid)]
65-
public void GuidShouldThrowWhenSetToNullOfTypeIsCalled(SqlBufferProxy.StorageType storageType)
24+
[InlineData(SqlBuffer.StorageType.Guid)]
25+
[InlineData(SqlBuffer.StorageType.SqlGuid)]
26+
internal void GuidShouldThrowWhenSetToNullOfTypeIsCalled(SqlBuffer.StorageType storageType)
6627
{
6728
_target.SetToNullOfType(storageType);
6829

@@ -88,9 +49,9 @@ public void GuidShouldReturnExpectedWhenSqlGuidIsSet()
8849
}
8950

9051
[Theory]
91-
[InlineData(SqlBufferProxy.StorageType.Guid)]
92-
[InlineData(SqlBufferProxy.StorageType.SqlGuid)]
93-
public void SqlGuidShouldReturnSqlNullWhenSetToNullOfTypeIsCalled(SqlBufferProxy.StorageType storageType)
52+
[InlineData(SqlBuffer.StorageType.Guid)]
53+
[InlineData(SqlBuffer.StorageType.SqlGuid)]
54+
internal void SqlGuidShouldReturnSqlNullWhenSetToNullOfTypeIsCalled(SqlBuffer.StorageType storageType)
9455
{
9556
_target.SetToNullOfType(storageType);
9657

@@ -142,112 +103,5 @@ public void SqlValueShouldReturnExpectedWhenSqlGuidIsSet()
142103

143104
Assert.Equal(expected, _target.SqlValue);
144105
}
145-
146-
public sealed class SqlBufferProxy
147-
{
148-
public enum StorageType
149-
{
150-
Empty = 0,
151-
Boolean,
152-
Byte,
153-
DateTime,
154-
Decimal,
155-
Double,
156-
Int16,
157-
Int32,
158-
Int64,
159-
Guid,
160-
Money,
161-
Single,
162-
String,
163-
SqlBinary,
164-
SqlCachedBuffer,
165-
SqlGuid,
166-
SqlXml,
167-
Date,
168-
DateTime2,
169-
DateTimeOffset,
170-
Time,
171-
}
172-
173-
private static readonly PropertyInfo _guidProperty;
174-
private static readonly PropertyInfo _sqlGuidProperty;
175-
private static readonly PropertyInfo _sqlValueProperty;
176-
private static readonly MethodInfo _setToNullOfTypeMethod;
177-
private readonly object _instance;
178-
179-
static SqlBufferProxy()
180-
{
181-
var flags = BindingFlags.NonPublic | BindingFlags.Instance;
182-
_guidProperty = _sqlBufferType.GetProperty(nameof(Guid), flags);
183-
_sqlGuidProperty = _sqlBufferType.GetProperty(nameof(SqlGuid), flags);
184-
_sqlValueProperty = _sqlBufferType.GetProperty(nameof(SqlValue), flags);
185-
_setToNullOfTypeMethod = _sqlBufferType.GetMethod(nameof(SetToNullOfType), flags);
186-
}
187-
188-
public SqlBufferProxy()
189-
{
190-
_instance = Activator.CreateInstance(_sqlBufferType, true);
191-
}
192-
193-
public Guid Guid
194-
{
195-
get => GetPropertyValue<Guid>(_guidProperty);
196-
set => SetPropertyValue(_guidProperty, value);
197-
}
198-
199-
public SqlGuid SqlGuid
200-
{
201-
get => GetPropertyValue<SqlGuid>(_sqlGuidProperty);
202-
set => SetPropertyValue(_sqlGuidProperty, value);
203-
}
204-
205-
public object SqlValue
206-
{
207-
get => GetPropertyValue<object>(_sqlValueProperty);
208-
}
209-
210-
public void SetToNullOfType(StorageType storageType)
211-
{
212-
#if NET
213-
_setToNullOfTypeMethod
214-
.Invoke(_instance, BindingFlags.DoNotWrapExceptions, null, new object[] { (int)storageType }, null);
215-
#else
216-
_setToNullOfTypeMethod.Invoke(_instance, new object[] { (int)storageType });
217-
#endif
218-
}
219-
220-
private T GetPropertyValue<T>(PropertyInfo property)
221-
{
222-
#if NET
223-
return (T)property.GetValue(_instance, BindingFlags.DoNotWrapExceptions, null, null, null);
224-
#else
225-
try
226-
{
227-
return (T)property.GetValue(_instance);
228-
}
229-
catch (TargetInvocationException e)
230-
{
231-
throw e.InnerException!;
232-
}
233-
#endif
234-
}
235-
236-
private void SetPropertyValue(PropertyInfo property, object value)
237-
{
238-
#if NET
239-
property.SetValue(_instance, value, BindingFlags.DoNotWrapExceptions, null, null, null);
240-
#else
241-
try
242-
{
243-
property.SetValue(_instance, value);
244-
}
245-
catch (TargetInvocationException e)
246-
{
247-
throw e.InnerException!;
248-
}
249-
#endif
250-
}
251-
}
252106
}
253107
}

0 commit comments

Comments
 (0)