forked from MoonStorm/FastCrud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseSteps.cs
682 lines (580 loc) · 28.9 KB
/
DatabaseSteps.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
namespace Dapper.FastCrud.Tests
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlLocalDb;
using System.Data.SqlServerCe;
using System.Data.SQLite;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Dapper.FastCrud.Tests.Models;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using MySql.Data.MySqlClient;
using Npgsql;
using NUnit.Framework;
using TechTalk.SpecFlow;
[Binding]
public class DatabaseSteps
{
private DatabaseTestContext _testContext;
private const string DatabaseName = "FastCrudTestDatabase";
//private static string OriginalDatabaseFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data");
//private static string FinalDatabaseFolder = Path.Combine(OriginalDatabaseFolder, $"FastCrudDatabaseTests");
public DatabaseSteps(DatabaseTestContext testContext)
{
this._testContext = testContext;
}
[AfterScenario]
public void Cleanup()
{
_testContext.DatabaseConnection?.Close();
_testContext.DatabaseConnection?.Dispose();
OrmConfiguration.ClearEntityRegistrations();
}
[Given(@"I have initialized a PostgreSql database")]
public void GivenIHaveInitializedPostgreSqlDatabase()
{
this.CleanupPostgreSqlDatabase(ConfigurationManager.ConnectionStrings["PostgreSql"].ConnectionString);
this.SetupPostgreSqlDatabase(ConfigurationManager.ConnectionStrings["PostgreSql"].ConnectionString);
}
[Given(@"I have initialized a Benchmark LocalDb database")]
public void GivenIHaveInitializedBenchmarkLocalDbDatabase()
{
this.CleanupLocalDbDatabase();
this.SetupLocalDbDatabase();
}
[Given(@"I have initialized a LocalDb database")]
public void GivenIHaveInitializedLocalDbDatabase()
{
this.CleanupMsSqlDatabase(ConfigurationManager.ConnectionStrings["LocalDb"].ConnectionString);
this.SetupMsSqlDatabase(ConfigurationManager.ConnectionStrings["LocalDb"].ConnectionString);
}
[Given(@"I have initialized a SqLite database")]
public void GivenIHaveInitializedSqlLiteDatabase()
{
this.SetupSqLiteDatabase(ConfigurationManager.ConnectionStrings["SqLite"].ConnectionString);
}
[Given(@"I have initialized a MySql database")]
public void GivenIHaveInitializedMySqlDatabase()
{
this.CleanupMySqlDatabase(ConfigurationManager.ConnectionStrings["MySql"].ConnectionString);
this.SetupMySqlDatabase(ConfigurationManager.ConnectionStrings["MySql"].ConnectionString);
}
[Given(@"I have initialized a SQLCE database")]
public void GivenIHaveInitializedSqlCeDatabase()
{
this.CleanupSqlCeDatabase(ConfigurationManager.ConnectionStrings["SqlCompact"].ConnectionString);
this.SetupSqlCeDatabase(ConfigurationManager.ConnectionStrings["SqlCompact"].ConnectionString);
}
[Then(@"I cleanup the SQLCE database")]
public void ThenICleanupTheSqlCeDatabase()
{
this.CleanupSqlCeDatabase(ConfigurationManager.ConnectionStrings["SqlCompact"].ConnectionString);
}
[Then(@"I cleanup the Benchmark LocalDb database")]
public void ThenICleanupTheBenchmarkLocalDbDatabase()
{
this.CleanupLocalDbDatabase();
}
[Then(@"I cleanup the LocalDb database")]
public void ThenICleanupTheLocalDbDatabase()
{
this.CleanupMsSqlDatabase(ConfigurationManager.ConnectionStrings["LocalDb"].ConnectionString);
}
[Given(@"I have initialized a MsSqlServer database")]
public void GivenIHaveInitializedAnMsSqlServerDatabase()
{
this.CleanupMsSqlDatabase(ConfigurationManager.ConnectionStrings["MsSqlServer"].ConnectionString);
this.SetupMsSqlDatabase(ConfigurationManager.ConnectionStrings["MsSqlServer"].ConnectionString);
}
[Then(@"I cleanup the MsSqlServer database")]
public void ThenICleanupTheMsSqlServerDatabase()
{
this.CleanupMsSqlDatabase(ConfigurationManager.ConnectionStrings["MsSqlServer"].ConnectionString);
}
[When(@"I refresh the database connection")]
public void WhenIRefreshTheDatabaseConnection()
{
_testContext.DatabaseConnection.Close();
_testContext.DatabaseConnection.Open();
}
[Given(@"I started the stopwatch")]
[When(@"I start the stopwatch")]
public void GivenIStartedTheStopwatch()
{
_testContext.Stopwatch.Restart();
}
[When(@"I stop the stopwatch")]
public void WhenIStopTheStopwatch()
{
_testContext.Stopwatch.Stop();
}
[When(@"I report the stopwatch value for (.*) finished processing (.*) operations of type (.*)")]
public void WhenIReportTheStopwatchValueFor(string ormType, int opCount, string operation)
{
Trace.WriteLine($"Stopwatch reported: {_testContext.Stopwatch.Elapsed.TotalMilliseconds:0,0.00} milliseconds for {ormType}");
// automatically update the docs
var docsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../README.MD");
var docsContents = File.ReadAllText(docsPath);
var reportTitle = $"| {ormType} | {operation} | {opCount} |";
var report = $"{reportTitle} {_testContext.Stopwatch.Elapsed.TotalMilliseconds:0,0.00} | {_testContext.Stopwatch.Elapsed.TotalMilliseconds*1000/opCount:0,0.00} |{Environment.NewLine}";
var benchmarkHeaderRegex = new Regex($@"(?<=#+\s*?Automatic Benchmark Report)[^{Environment.NewLine}]*", RegexOptions.Singleline);
var emptySpaceInsertRegex = new Regex($@"(?<=#+\s*?Automatic Benchmark Report(.*?{Environment.NewLine}){{3,3}})\s*?", RegexOptions.Singleline);
var reportReplaceRegex = new Regex($@"{reportTitle.Replace("|",@"\|")}.*?{Environment.NewLine}", RegexOptions.Singleline);
if (reportReplaceRegex.Match(docsContents).Success)
{
docsContents = reportReplaceRegex.Replace(docsContents, report, 1);
}
else
{
docsContents = emptySpaceInsertRegex.Replace(docsContents, report, 1);
}
docsContents = benchmarkHeaderRegex.Replace(docsContents, $" (Last Run: {DateTime.Now:D})", 1);
File.WriteAllText(docsPath, docsContents);
}
[Then(@"I should have queried (.*) entities")]
public void ThenIShouldHaveQueriedEntities(int queryEntitiesCount)
{
Assert.AreEqual(queryEntitiesCount, _testContext.QueriedEntities.Count());
}
[Then(@"the queried entities should be the same as the ones I inserted, in reverse order, starting from (.*) counting (.*)")]
public void ThenTheQueriedEntitiesShouldBeTheSameAsTheOnesIInsertedReverseStartingFromCounting(int? skip, int? max)
{
var expectedEntities = ((IEnumerable<object>)_testContext.LocalInsertedEntities).Reverse().Skip(skip??0).Take(max??int.MaxValue);
CollectionAssert.AreEqual(expectedEntities, _testContext.QueriedEntities);
}
[Then(@"the queried entities should be the same as the local ones")]
public void ThenTheQueriedEntitiesShouldBeTheSameAsTheLocalOnes()
{
this.CompareQueriedEntitiesWithLocalEntities<object>();
}
[Then(@"the queried workstation entities should be the same as the local ones")]
public void ThenTheQueriedWorkstationEntitiesShouldBeTheSameAsTheLocalOnes()
{
this.CompareQueriedEntitiesWithLocalEntities<Workstation>();
}
[Then(@"the queried employee entities should be the same as the local ones")]
public void ThenTheQueriedEmployeeEntitiesShouldBeTheSameAsTheLocalOnes()
{
this.CompareQueriedEntitiesWithLocalEntities<Employee>();
}
[Then(@"the queried building entities should be the same as the local ones")]
public void ThenTheQueriedBuildingEntitiesShouldBeTheSameAsTheLocalOnes()
{
this.CompareQueriedEntitiesWithLocalEntities<Building>();
}
[When(@"I clear all the queried entities")]
public void WhenIClearAllTheQueriedEntities()
{
_testContext.QueriedEntities.Clear();
}
[When(@"I clear all the local entities")]
public void WhenIClearAllTheInsertedEntities()
{
_testContext.LocalInsertedEntities.Clear();
}
private void SetupSqlCeDatabase(string connectionString)
{
this.SetupOrmConfiguration(SqlDialect.MsSql);
connectionString = string.Format(
CultureInfo.InvariantCulture,
connectionString,
Path.Combine(Path.GetDirectoryName(typeof(DatabaseSteps).Assembly.Location), $"{DatabaseName}.sdf"));
// create the database
using(var sqlCeEngine = new SqlCeEngine(connectionString))
{
sqlCeEngine.CreateDatabase();
}
_testContext.DatabaseConnection = new SqlCeConnection(connectionString);
_testContext.DatabaseConnection.Open();
using(var command = _testContext.DatabaseConnection.CreateCommand())
{
command.CommandText = @"CREATE TABLE [SimpleBenchmarkEntities](
[Id] [int] IDENTITY(2,1) PRIMARY KEY,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NOT NULL,
[DateOfBirth] [datetime] NULL
)";
command.ExecuteNonQuery();
}
}
private void SetupSqLiteDatabase(string connectionString)
{
this.SetupOrmConfiguration(SqlDialect.SqLite);
_testContext.DatabaseConnection = new SQLiteConnection(connectionString);
_testContext.DatabaseConnection.Open();
using (var command = _testContext.DatabaseConnection.CreateCommand())
{
command.CommandText =
$@"CREATE TABLE Workstations (
WorkstationId integer primary key AUTOINCREMENT,
InventoryIndex int NOT NULL,
Name nvarchar(100) NULL,
AccessLevel int NOT NULL DEFAULT(1),
BuildingId int NULL,
""10PinSlots"" int NULL
)";
command.ExecuteNonQuery();
}
////using (var command = _testContext.DatabaseConnection.CreateCommand())
////{
//// command.CommandText =
//// $@"CREATE TABLE SimpleBenchmarkEntities(
//// Id integer primary key AUTOINCREMENT,
//// FirstName nvarchar(50) NULL,
//// LastName nvarchar(50) NOT NULL,
//// DateOfBirth datetime NULL
//// ";
//// command.ExecuteNonQuery();
////}
using (var command = _testContext.DatabaseConnection.CreateCommand())
{
command.CommandText = $@"CREATE TABLE Buildings (
Id integer primary key AUTOINCREMENT,
BuildingName nvarchar(100) NULL,
Description nvarchar(100) NULL
)";
command.ExecuteNonQuery();
}
}
private void CleanupPostgreSqlDatabase(string connectionString)
{
using (var dataConnection = new NpgsqlConnection(connectionString))
{
dataConnection.Open();
using (var command = dataConnection.CreateCommand())
{
command.CommandText =
$@"
select pg_terminate_backend(pid)
from pg_stat_activity
where datname = '{
DatabaseName.ToLowerInvariant()}'";
command.ExecuteNonQuery();
}
using (var command = dataConnection.CreateCommand())
{
command.CommandText = $@"DROP DATABASE IF EXISTS { DatabaseName.ToLowerInvariant()}";
command.ExecuteNonQuery();
}
}
}
private void SetupPostgreSqlDatabase(string connectionString)
{
this.SetupOrmConfiguration(SqlDialect.PostgreSql);
using (var dataConnection = new NpgsqlConnection(connectionString))
{
dataConnection.Open();
using (var command = dataConnection.CreateCommand())
{
command.CommandText = $@"CREATE DATABASE {DatabaseName}";
command.ExecuteNonQuery();
}
}
using (var dataConnection = new NpgsqlConnection(connectionString + $";Database={DatabaseName.ToLowerInvariant()}"))
{
// uuid_in((md5((random())::text))::cstring)
dataConnection.Open();
using (var command = dataConnection.CreateCommand())
{
command.CommandText =$@"
CREATE TABLE ""Employee"" (
""Id"" SERIAL,
""EmployeeId"" uuid NOT NULL DEFAULT (md5(random()::text || clock_timestamp()::text)::uuid),
""KeyPass"" uuid NOT NULL DEFAULT (md5(random()::text || clock_timestamp()::text)::uuid),
""LastName"" varchar(100) NOT NULL,
""FirstName"" varchar(100) NOT NULL,
""FullName"" varchar(200) NOT NULL,
""BirthDate"" timestamp NOT NULL,
""WorkstationId"" int NULL,
PRIMARY KEY (""Id"", ""EmployeeId"")
);
CREATE OR REPLACE FUNCTION computed_full_name()
RETURNS trigger
LANGUAGE plpgsql
SECURITY DEFINER
AS $BODY$
DECLARE
payload text;
BEGIN
NEW.""FullName"" = NEW.""FirstName"" || NEW.""LastName"";
RETURN NEW;
END
$BODY$;
CREATE TRIGGER ""computed_full_name_trigger""
BEFORE INSERT OR UPDATE
ON ""Employee""
FOR EACH ROW
EXECUTE PROCEDURE computed_full_name();
CREATE TABLE ""Workstations"" (
""WorkstationId"" BIGSERIAL,
""Name"" varchar(100) NOT NULL,
""InventoryIndex"" int NOT NULL,
""AccessLevel"" int NOT NULL DEFAULT 1,
""BuildingId"" int NULL,
""10PinSlots"" int NULL,
PRIMARY KEY (""WorkstationId"")
);
CREATE TABLE ""Buildings"" (
""Id"" SERIAL,
""BuildingName"" varchar(100) NULL,
""Description"" varchar(100) NULL,
PRIMARY KEY (""Id"")
);
";
command.ExecuteNonQuery();
}
}
_testContext.DatabaseConnection = new NpgsqlConnection(connectionString + $";Database={DatabaseName.ToLowerInvariant()}");
_testContext.DatabaseConnection.Open();
}
private void CleanupMySqlDatabase(string connectionString)
{
using (var dataConnection = new MySqlConnection(connectionString))
{
dataConnection.Open();
using (var command = dataConnection.CreateCommand())
{
command.CommandText = $@"DROP DATABASE IF EXISTS {DatabaseName}";
command.ExecuteNonQuery();
}
}
}
private void SetupMySqlDatabase(string connectionString)
{
this.SetupOrmConfiguration(SqlDialect.MySql);
using (var dataConnection = new MySqlConnection(connectionString))
{
dataConnection.Open();
using (var command = dataConnection.CreateCommand())
{
command.CommandText = $@"CREATE DATABASE {DatabaseName}";
command.ExecuteNonQuery();
}
using (var command = dataConnection.CreateCommand())
{
command.CommandText = $@"USE {DatabaseName};
CREATE TABLE `Employee` (
Id int NOT NULL AUTO_INCREMENT,
EmployeeId CHAR(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
KeyPass CHAR(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000',
LastName nvarchar(100) NOT NULL,
FirstName nvarchar(100) NOT NULL,
FullName nvarchar(200) AS (CONCAT(FirstName,LastName)),
BirthDate datetime NOT NULL,
WorkstationId int NULL,
PRIMARY KEY (Id, EmployeeId)
);
ALTER TABLE `Employee` auto_increment=2;
CREATE TRIGGER `Employee_Assign_UUID`
BEFORE INSERT ON Employee
FOR EACH ROW
SET NEW.EmployeeId = UUID(),
New.KeyPass = UUID();
CREATE TABLE `Workstations` (
WorkstationId bigint NOT NULL AUTO_INCREMENT,
Name nvarchar(100) NOT NULL,
InventoryIndex int NOT NULL,
AccessLevel int NOT NULL DEFAULT 1,
BuildingId int NULL,
10PinSlots int NULL,
PRIMARY KEY (WorkstationId)
);
ALTER TABLE `Workstations` auto_increment=2;
CREATE TABLE `Buildings` (
Id int NOT NULL AUTO_INCREMENT,
BuildingName nvarchar(100) NULL,
Description nvarchar(100) NULL,
PRIMARY KEY (Id)
);
ALTER TABLE `Buildings` auto_increment=2;
";
command.ExecuteNonQuery();
}
}
_testContext.DatabaseConnection = new MySqlConnection(connectionString+$";Database={DatabaseName}");
_testContext.DatabaseConnection.Open();
}
private void SetupLocalDbDatabase()
{
var localDbProvider = new SqlLocalDbProvider();
var localInstance = localDbProvider.CreateInstance(DatabaseName); // instance name = db name, the api will always use the latest version
localInstance.Start();
this.SetupMsSqlDatabase(localInstance.CreateConnectionStringBuilder().ConnectionString);
}
private void SetupMsSqlDatabase(string connectionString)
{
this.SetupOrmConfiguration(SqlDialect.MsSql);
using (var dataConnection = new SqlConnection(connectionString))
{
dataConnection.Open();
var server = new Server(new ServerConnection(dataConnection));
var database = new Database(server, DatabaseName);
try
{
database.Create();
}
catch (FailedOperationException ex)
{
// sometimes db test files are left behind
var fileInUseErrorMatch =
new Regex("Cannot create file '(.*?)' because it already exists.").Match(
((ex.InnerException as ExecutionFailureException)?.InnerException as SqlException)?.Message ?? string.Empty);
if (fileInUseErrorMatch.Success)
{
var dbFilePath = fileInUseErrorMatch.Groups[1].Value;
var dbLogFilePath = Path.Combine(Path.GetDirectoryName(dbFilePath), $"{Path.GetFileNameWithoutExtension(dbFilePath)}_log.ldf");
if (File.Exists(dbFilePath))
{
File.Delete(dbFilePath);
}
if (File.Exists(dbLogFilePath))
{
File.Delete(dbLogFilePath);
}
database.Create();
}
else
{
throw;
}
}
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET SINGLE_USER"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET COMPATIBILITY_LEVEL = 110"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET RECOVERY BULK_LOGGED"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET AUTO_CREATE_STATISTICS OFF"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET AUTO_UPDATE_STATISTICS OFF"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} MODIFY FILE(NAME=[{DatabaseName}], SIZE=100MB, FILEGROWTH=10%)"); // for benchmarking purposes 5MB approx 20k records
database.ExecuteNonQuery(@"CREATE TABLE [dbo].[SimpleBenchmarkEntities](
[Id] [int] IDENTITY(2,1) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NOT NULL,
[DateOfBirth] [datetime] NULL,
CONSTRAINT [PK_SimpleBenchmarkEntities] PRIMARY KEY CLUSTERED
(
[Id] ASC
))");
database.ExecuteNonQuery(@"CREATE TABLE [dbo].[Workstations](
[WorkstationId] [bigint] IDENTITY(2,1) NOT NULL,
[Name] [nvarchar](100) NULL,
[InventoryIndex] [int] NOT NULL,
[AccessLevel] [int] NOT NULL DEFAULT(1),
[BuildingId] [int] NULL,
-- Verify that column names that begin with numbers will work
[10PinSlots] [int] NULL,
CONSTRAINT [PK_Workstations] PRIMARY KEY CLUSTERED
(
[WorkstationId] ASC
))");
database.ExecuteNonQuery(@"CREATE TABLE [dbo].[Employee](
[Id] [int] IDENTITY(2,1) NOT NULL,
[EmployeeId] [uniqueidentifier] NOT NULL DEFAULT(newid()),
[KeyPass] [uniqueidentifier] NOT NULL DEFAULT(newid()),
[LastName] [nvarchar](100) NOT NULL,
[FirstName] [nvarchar](100) NULL,
[BirthDate] [datetime] NOT NULL,
[WorkstationId] [bigint] NULL,
[FullName] AS ([FirstName] + [LastName]),
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[Id] ASC,
[EmployeeId] ASC
),
CONSTRAINT [FK_Workstations_Employee] FOREIGN KEY (WorkstationId) REFERENCES [dbo].[Workstations] (WorkstationId))");
database.ExecuteNonQuery(@"CREATE TABLE [dbo].[Buildings](
[Id] [int] IDENTITY(2,1) NOT NULL,
[BuildingName] [nvarchar](100) NULL,
[Description] [nvarchar](100) NULL,
CONSTRAINT [PK_Buildings] PRIMARY KEY CLUSTERED
(
[Id] ASC
))");
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET MULTI_USER"); // for benchmarking purposes
// no longer required. local db instances are destroyed and re-created
//database.ExecuteNonQuery(@"CHECKPOINT;");
//database.ExecuteNonQuery(@"DBCC DROPCLEANBUFFERS;");
//database.ExecuteNonQuery(@"DBCC FREESYSTEMCACHE('ALL') WITH NO_INFOMSGS;");
//database.ExecuteNonQuery(@"DBCC FREESESSIONCACHE WITH NO_INFOMSGS;");
//database.ExecuteNonQuery(@"DBCC FREEPROCCACHE WITH NO_INFOMSGS;");
}
_testContext.DatabaseConnection = new SqlConnection(connectionString+$";Initial Catalog={DatabaseName};"); //Max Pool Size=1; Pooling = False
_testContext.DatabaseConnection.Open();
}
private void CleanupSqlCeDatabase(string connectionString)
{
_testContext.DatabaseConnection?.Close();
var dbFile = Path.Combine(Path.GetDirectoryName(typeof(DatabaseSteps).Assembly.Location), $"{DatabaseName}.sdf");
if (File.Exists(dbFile))
{
File.Delete(dbFile);
}
}
private void CleanupLocalDbDatabase()
{
SqlLocalDbApi.AutomaticallyDeleteInstanceFiles = true;
SqlLocalDbApi.StopOptions = StopInstanceOptions.KillProcess;
var localDbProvider = new SqlLocalDbProvider();
var localDbInstanceInfo = localDbProvider.GetInstances().FirstOrDefault(instance => instance.Name==DatabaseName);
if (localDbInstanceInfo != null)
{
var localDbInstance = localDbProvider.GetInstance(DatabaseName);
if (!localDbInstance.IsRunning)
{
localDbInstance.Start();
}
this.CleanupMsSqlDatabase(localDbInstance.CreateConnectionStringBuilder().ConnectionString);
SqlLocalDbApi.StopInstance(DatabaseName, TimeSpan.FromSeconds(20.0));
SqlLocalDbApi.DeleteInstance(DatabaseName);
}
}
private void CleanupMsSqlDatabase(string connectionString)
{
_testContext.DatabaseConnection?.Close();
using (var dataConnection = new SqlConnection(connectionString))
{
dataConnection.Open();
var serverManagement = new Server(new ServerConnection(dataConnection));
var database = serverManagement.Databases[DatabaseName];
if (database != null)
{
try
{
// drop any active connections
database.UserAccess = DatabaseUserAccess.Single;
database.Alter(TerminationClause.RollbackTransactionsImmediately);
database.Refresh();
database.UserAccess = DatabaseUserAccess.Multiple;
database.Alter(TerminationClause.RollbackTransactionsImmediately);
database.Refresh();
}
catch
{
// this may fail if the db files are no longer there
}
database.Drop();
//serverManagement.DetachDatabase(MsSqlDatabaseName, false, true);
}
}
}
private void SetupOrmConfiguration(SqlDialect dialect)
{
OrmConfiguration.DefaultDialect = dialect;
OrmConfiguration.RegisterEntity<Building>()
.SetTableName("Buildings")
.SetProperty(building => building.BuildingId,propMapping => propMapping.SetPrimaryKey().SetDatabaseGenerated(DatabaseGeneratedOption.Identity).SetDatabaseColumnName("Id"))
.SetProperty(building => building.Name, propMapping=> propMapping.SetDatabaseColumnName("BuildingName"))
.SetProperty(building => building.Description); // test mapping w/o name
}
private void CompareQueriedEntitiesWithLocalEntities<TEntity>()
{
CollectionAssert.AreEquivalent(_testContext.QueriedEntities.OfType<TEntity>(), _testContext.LocalInsertedEntities.OfType<TEntity>());
}
}
}