-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInferDbTypeTests.cs
More file actions
84 lines (74 loc) · 3.38 KB
/
Copy pathInferDbTypeTests.cs
File metadata and controls
84 lines (74 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Microsoft.Data.SqlClient;
using Xunit;
namespace DbaClientX.Tests;
public class InferDbTypeTests
{
private class TestClient : DBAClientX.DatabaseClientBase
{
public void InvokeAddParameters(DbCommand command, IDictionary<string, object?> parameters)
=> base.AddParameters(command, parameters, null);
public void InvokeAddParameters(
DbCommand command,
IDictionary<string, object?> parameters,
IDictionary<string, DbType>? parameterTypes,
IDictionary<string, ParameterDirection>? parameterDirections)
=> base.AddParameters(command, parameters, parameterTypes, parameterDirections);
}
[Fact]
public void AddParameters_InfersGuidType()
{
using var client = new TestClient();
using var command = new SqlCommand();
var guid = Guid.NewGuid();
client.InvokeAddParameters(command, new Dictionary<string, object?> { ["@id"] = guid });
var parameter = Assert.IsType<SqlParameter>(Assert.Single(command.Parameters));
Assert.Equal(DbType.Guid, parameter.DbType);
Assert.Equal(guid, parameter.Value);
}
[Fact]
public void AddParameters_InfersBinaryType()
{
using var client = new TestClient();
using var command = new SqlCommand();
var bytes = new byte[] { 1, 2, 3 };
client.InvokeAddParameters(command, new Dictionary<string, object?> { ["@data"] = bytes });
var parameter = Assert.IsType<SqlParameter>(Assert.Single(command.Parameters));
Assert.Equal(DbType.Binary, parameter.DbType);
Assert.Equal(bytes, parameter.Value);
}
[Fact]
public void AddParameters_MatchesExplicitTypeAndDirection_CaseInsensitively()
{
using var client = new TestClient();
using var command = new SqlCommand();
var parameters = new Dictionary<string, object?> { ["@Id"] = 7 };
var types = new Dictionary<string, DbType> { ["@id"] = DbType.Int64 };
var directions = new Dictionary<string, ParameterDirection> { ["@ID"] = ParameterDirection.InputOutput };
client.InvokeAddParameters(command, parameters, types, directions);
var parameter = Assert.IsType<SqlParameter>(Assert.Single(command.Parameters));
Assert.Equal(DbType.Int64, parameter.DbType);
Assert.Equal(ParameterDirection.InputOutput, parameter.Direction);
}
public static IEnumerable<object[]> InferDbTypeData => new[]
{
new object[] { TimeSpan.FromMinutes(1), DbType.Time },
new object[] { DateTimeOffset.UtcNow, DbType.DateTimeOffset },
new object[] { (sbyte)1, DbType.SByte },
new object[] { (ushort)1, DbType.UInt16 },
new object[] { (uint)1, DbType.UInt32 },
new object[] { (ulong)1, DbType.UInt64 },
new object[] { 'a', DbType.StringFixedLength }
};
[Theory]
[MemberData(nameof(InferDbTypeData))]
public void InferDbType_ReturnsExpected(object value, DbType expected)
{
var method = typeof(DBAClientX.DatabaseClientBase).GetMethod("InferDbType", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)!;
var result = (DbType)method.Invoke(null, new[] { value })!;
Assert.Equal(expected, result);
}
}