-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathUsage.cs
245 lines (191 loc) · 6.66 KB
/
Usage.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using Npgsql;
public class Usage :
LocalDbTestBase
{
public static void Suffix(WebApplicationBuilder builder)
{
#region Suffix
var app = builder.Build();
app.UseDelta(suffix: httpContext => "MySuffix");
#endregion
}
public static void ShouldExecute(WebApplicationBuilder builder)
{
#region ShouldExecute
var app = builder.Build();
app.UseDelta(
shouldExecute: httpContext =>
{
var path = httpContext.Request.Path.ToString();
return path.Contains("match");
});
#endregion
}
[Test]
public async Task LastTimeStampRowVersion()
{
await using var database = await LocalDb();
var timeStamp = await DeltaExtensions.GetLastTimeStamp(database.Connection, null);
IsNotEmpty(timeStamp);
IsNotNull(timeStamp);
Recording.Start();
await using var command = database.Connection.CreateCommand();
command.CommandText =
$"""
insert into [Companies] (Id, Content)
values ('{Guid.NewGuid()}', 'The company')
""";
await command.ExecuteNonQueryAsync();
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(database.Connection, null);
IsNotEmpty(newTimeStamp);
IsNotNull(newTimeStamp);
}
[Test]
public async Task GetLastTimeStampSqlServer()
{
await using var database = await LocalDb();
var sqlConnection = database.Connection;
#region GetLastTimeStampSqlConnection
var timeStamp = await sqlConnection.GetLastTimeStamp();
#endregion
IsNotNull(timeStamp);
}
[Test]
public async Task GetLastTimeStampPostgres()
{
await using var connection = new NpgsqlConnection(PostgresConnection.ConnectionString);
await connection.OpenAsync();
await PostgresDbBuilder.Create(connection);
await using var command = connection.CreateCommand();
command.CommandText =
$"""
insert into "Companies"("Id", "Content")
values ('{Guid.NewGuid()}', 'The company')
""";
await command.ExecuteNonQueryAsync();
var timeStamp = await connection.GetLastTimeStamp();
IsNotNull(timeStamp);
IsNotEmpty(timeStamp);
}
[Test]
public async Task LastTimeStampRowVersionAndTracking()
{
await using var database = await LocalDb();
var connection = database.Connection;
await connection.EnableTracking();
var timeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
IsNotEmpty(timeStamp);
IsNotNull(timeStamp);
await using var command = connection.CreateCommand();
command.CommandText =
$"""
insert into [Companies] (Id, Content)
values ('{Guid.NewGuid()}', 'The company')
""";
await command.ExecuteNonQueryAsync();
var newTimeStamp = await DeltaExtensions.GetLastTimeStamp(connection, null);
IsNotEmpty(newTimeStamp);
IsNotNull(newTimeStamp);
AreNotEqual(timeStamp, newTimeStamp);
}
[Test]
public async Task GetDatabasesWithTracking()
{
await using var database = await LocalDb();
var sqlConnection = database.Connection;
await sqlConnection.EnableTracking();
#region GetDatabasesWithTracking
var trackedDatabases = await sqlConnection.GetTrackedDatabases();
foreach (var db in trackedDatabases)
{
Trace.WriteLine(db);
}
#endregion
IsNotEmpty(trackedDatabases);
}
[Test]
public async Task GetTrackedTables()
{
var database = await LocalDb();
var sqlConnection = database.Connection;
await sqlConnection.DisableTracking();
#region SetTrackedTables
await sqlConnection.SetTrackedTables(["Companies"]);
#endregion
#region GetTrackedTables
var trackedTables = await sqlConnection.GetTrackedTables();
foreach (var db in trackedTables)
{
Trace.WriteLine(db);
}
#endregion
await Verify(sqlConnection.GetTrackedTables());
}
[Test]
public async Task DuplicateSetTrackedTables()
{
await using var database = await LocalDb();
var sqlConnection = database.Connection;
await sqlConnection.SetTrackedTables(["Companies"]);
await sqlConnection.SetTrackedTables(["Companies"]);
}
[Test]
public async Task EmptySetTrackedTables()
{
await using var database = await LocalDb();
var sqlConnection = database.Connection;
await sqlConnection.SetTrackedTables([]);
}
[Test]
public async Task Schema()
{
await using var database = await LocalDb();
await Verify(database.Connection).SchemaAsSql();
}
[Test]
public async Task DisableTracking()
{
await using var database = await LocalDb();
var sqlConnection = database.Connection;
await sqlConnection.SetTrackedTables(["Companies"]);
#region DisableTracking
await sqlConnection.DisableTracking();
#endregion
IsFalse(await sqlConnection.IsTrackingEnabled());
}
[Test]
public async Task IsTrackingEnabled()
{
await using var database = await LocalDb();
var sqlConnection = database.Connection;
#region EnableTracking
await sqlConnection.EnableTracking();
#endregion
#region IsTrackingEnabled
var isTrackingEnabled = await sqlConnection.IsTrackingEnabled();
#endregion
IsTrue(isTrackingEnabled);
}
static void CustomDiscoveryConnection(WebApplicationBuilder webApplicationBuilder)
{
#region CustomDiscoveryConnection
var application = webApplicationBuilder.Build();
application.UseDelta(
getConnection: httpContext => httpContext.RequestServices.GetRequiredService<SqlConnection>());
#endregion
}
static void CustomDiscoveryConnectionAndTransaction(WebApplicationBuilder webApplicationBuilder)
{
#region CustomDiscoveryConnectionAndTransaction
var webApplication = webApplicationBuilder.Build();
webApplication.UseDelta(
getConnection: httpContext =>
{
var provider = httpContext.RequestServices;
var sqlConnection = provider.GetRequiredService<SqlConnection>();
var sqlTransaction = provider.GetService<SqlTransaction>();
return new(sqlConnection, sqlTransaction);
});
#endregion
}
}