-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
SqlAzureDatabaseCreationTest.cs
145 lines (114 loc) · 5.3 KB
/
SqlAzureDatabaseCreationTest.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// 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;
// ReSharper disable InconsistentNaming
namespace Microsoft.EntityFrameworkCore.SqlAzure;
[SqlServerCondition(SqlServerCondition.IsSqlAzure)]
public class SqlAzureDatabaseCreationTest
{
protected string StoreName { get; } = "SqlAzureDatabaseCreationTest";
[ConditionalFact]
public async Task Creates_database_in_elastic_pool()
{
using var testDatabase = SqlServerTestStore.Create(StoreName + "Elastic");
using var context = new ElasticPoolContext(testDatabase);
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
await AssertOptionsAsync(context.Database.GetDbConnection(), 1000 * (1L << 28), "Standard", "ElasticPool");
}
private class ElasticPoolContext : DbContext
{
private readonly string _connectionString;
public DbSet<FastUn> FastUns { get; set; }
public DbSet<BigUn> BigUns { get; set; }
public ElasticPoolContext(SqlServerTestStore testStore)
{
_connectionString = testStore.ConnectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(_connectionString, b => b.ApplyConfiguration());
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.HasPerformanceLevelSql("ELASTIC_POOL ( name = unicornhack )");
}
[ConditionalFact]
public async Task Creates_basic_database()
{
using var testDatabase = SqlServerTestStore.Create(StoreName + "Basic");
using var context = new BasicContext(testDatabase);
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
await AssertOptionsAsync(context.Database.GetDbConnection(), 1L << 30, "Basic", "Basic");
}
private class BasicContext : DbContext
{
private readonly string _connectionString;
public DbSet<FastUn> FastUns { get; set; }
public DbSet<BigUn> BigUns { get; set; }
public BasicContext(SqlServerTestStore testStore)
{
_connectionString = testStore.ConnectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(_connectionString, b => b.ApplyConfiguration());
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDatabaseMaxSize("1 GB");
modelBuilder.HasServiceTier("'basic'");
}
}
[ConditionalFact]
public async Task Creates_business_critical_database()
{
using var testDatabase = SqlServerTestStore.Create(StoreName + "BusinessCritical");
using var context = new BusinessCriticalContext(testDatabase);
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
await AssertOptionsAsync(context.Database.GetDbConnection(), 1L << 31, "BusinessCritical", "BC_Gen4_1");
}
private class BusinessCriticalContext : DbContext
{
private readonly string _connectionString;
public DbSet<FastUn> FastUns { get; set; }
public DbSet<BigUn> BigUns { get; set; }
public BusinessCriticalContext(SqlServerTestStore testStore)
{
_connectionString = testStore.ConnectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(_connectionString, b => b.ApplyConfiguration());
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDatabaseMaxSize("2 GB");
modelBuilder.HasServiceTier("BusinessCritical");
modelBuilder.HasPerformanceLevel("BC_Gen4_1");
}
}
private async Task AssertOptionsAsync(DbConnection connection, long? maxSize, string serviceTier, string performanceLevel)
{
var storeName = new SqlConnectionStringBuilder(connection.ConnectionString).InitialCatalog;
await Task.Delay(TimeSpan.FromMinutes(5));
await connection.OpenAsync();
using var command = connection.CreateCommand();
command.CommandText = $@"
SELECT DATABASEPROPERTYEX('{storeName}', 'EDITION'),
DATABASEPROPERTYEX('{storeName}', 'ServiceObjective'),
DATABASEPROPERTYEX('{storeName}', 'MaxSizeInBytes');";
command.CommandTimeout = 300;
using var reader = await command.ExecuteReaderAsync();
await reader.ReadAsync();
Assert.Equal(serviceTier, await reader.IsDBNullAsync(0) ? null : await reader.GetFieldValueAsync<string>(0));
Assert.Equal(performanceLevel, await reader.IsDBNullAsync(1) ? null : await reader.GetFieldValueAsync<string>(1));
Assert.Equal(maxSize, await reader.IsDBNullAsync(2) ? null : await reader.GetFieldValueAsync<long>(2));
}
private class BigUn
{
public int Id { get; set; }
public ICollection<FastUn> FastUns { get; set; }
}
private class FastUn
{
public int Id { get; set; }
public string Name { get; set; }
public BigUn BigUn { get; set; }
}
}