-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathSqlExceptionTest.cs
More file actions
70 lines (61 loc) · 2.35 KB
/
SqlExceptionTest.cs
File metadata and controls
70 lines (61 loc) · 2.35 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.Json;
using Xunit;
namespace Microsoft.Data.SqlClient.Tests
{
public class SqlExceptionTest
{
private const string badServer = "92B96911A0BD43E8ADA4451031F7E7CF";
[Fact]
public void SerializationTest()
{
SqlException e = CreateException();
// Serialize the properties we want to validate round-trip through JSON.
// SqlException cannot be directly serialized by System.Text.Json because
// Exception.TargetSite (MethodBase) is not supported.
string json = JsonSerializer.Serialize(new
{
e.Message,
ClientConnectionId = e.ClientConnectionId.ToString(),
e.Number,
e.Class,
e.State,
});
using JsonDocument doc = JsonDocument.Parse(json);
JsonElement root = doc.RootElement;
Assert.Equal(e.Message, root.GetProperty("Message").GetString());
Assert.Equal(e.ClientConnectionId.ToString(), root.GetProperty("ClientConnectionId").GetString());
Assert.Equal(e.Number, root.GetProperty("Number").GetInt32());
Assert.Equal(e.Class, root.GetProperty("Class").GetByte());
Assert.Equal(e.State, root.GetProperty("State").GetByte());
}
private static SqlException CreateException()
{
var builder = new SqlConnectionStringBuilder()
{
DataSource = badServer,
ConnectTimeout = 1,
Pooling = false
};
using (var connection = new SqlConnection(builder.ConnectionString))
{
try
{
connection.Open();
}
catch (SqlException ex)
{
Assert.NotNull(ex.Errors);
Assert.Single(ex.Errors);
return ex;
}
}
throw new InvalidOperationException("SqlException should have been returned.");
}
}
}