-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
SqlServerConnectionTest.cs
110 lines (95 loc) · 4.56 KB
/
SqlServerConnectionTest.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities.FakeProvider;
namespace Microsoft.EntityFrameworkCore.Storage.Internal;
public class SqlServerConnectionTest
{
[ConditionalFact]
public void Creates_SQL_Server_connection_string()
{
using var connection = new SqlServerConnection(CreateDependencies());
Assert.IsType<SqlConnection>(connection.DbConnection);
}
[ConditionalFact]
public void Can_create_master_connection()
{
using var connection = new SqlServerConnection(CreateDependencies());
using var master = connection.CreateMasterConnection();
Assert.Equal(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master", master.ConnectionString);
Assert.Equal(60, master.CommandTimeout);
}
[ConditionalFact]
public void Master_connection_string_contains_filename()
{
var options = new DbContextOptionsBuilder()
.UseSqlServer(
@"Server=(localdb)\MSSQLLocalDB;Database=SqlServerConnectionTest;AttachDBFilename=C:\Narf.mdf",
b => b.CommandTimeout(55))
.Options;
using var connection = new SqlServerConnection(CreateDependencies(options));
using var master = connection.CreateMasterConnection();
Assert.Equal(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master", master.ConnectionString);
}
[ConditionalFact]
public void Master_connection_string_none_default_command_timeout()
{
var options = new DbContextOptionsBuilder()
.UseSqlServer(
@"Server=(localdb)\MSSQLLocalDB;Database=SqlServerConnectionTest",
b => b.CommandTimeout(55))
.Options;
using var connection = new SqlServerConnection(CreateDependencies(options));
using var master = connection.CreateMasterConnection();
Assert.Equal(55, master.CommandTimeout);
}
public static RelationalConnectionDependencies CreateDependencies(DbContextOptions options = null)
{
options ??= new DbContextOptionsBuilder()
.UseSqlServer(@"Server=(localdb)\MSSQLLocalDB;Database=SqlServerConnectionTest")
.Options;
return new RelationalConnectionDependencies(
options,
new DiagnosticsLogger<DbLoggerCategory.Database.Transaction>(
new LoggerFactory(),
new LoggingOptions(),
new DiagnosticListener("FakeDiagnosticListener"),
new SqlServerLoggingDefinitions(),
new NullDbContextLogger()),
new RelationalConnectionDiagnosticsLogger(
new LoggerFactory(),
new LoggingOptions(),
new DiagnosticListener("FakeDiagnosticListener"),
new SqlServerLoggingDefinitions(),
new NullDbContextLogger(),
CreateOptions()),
new NamedConnectionStringResolver(options),
new RelationalTransactionFactory(
new RelationalTransactionFactoryDependencies(
new RelationalSqlGenerationHelper(
new RelationalSqlGenerationHelperDependencies()))),
new CurrentDbContext(new FakeDbContext()),
new RelationalCommandBuilderFactory(
new RelationalCommandBuilderDependencies(
new TestRelationalTypeMappingSource(
TestServiceFactory.Instance.Create<TypeMappingSourceDependencies>(),
TestServiceFactory.Instance.Create<RelationalTypeMappingSourceDependencies>()),
new SqlServerExceptionDetector())));
}
private const string ConnectionString = "Fake Connection String";
private static IDbContextOptions CreateOptions(
RelationalOptionsExtension optionsExtension = null)
{
var optionsBuilder = new DbContextOptionsBuilder();
((IDbContextOptionsBuilderInfrastructure)optionsBuilder)
.AddOrUpdateExtension(
optionsExtension
?? new FakeRelationalOptionsExtension().WithConnectionString(ConnectionString));
return optionsBuilder.Options;
}
private class FakeDbContext : DbContext;
}