Skip to content

Commit ec264cc

Browse files
Add the CreateEmptyDatabaseAsync() and DeleteDatabaseAsync() methods.
1 parent 1a1d9da commit ec264cc

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/Directory.Build.props

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
<PackageReadmeFile>README.md</PackageReadmeFile>
1212
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1313
<PackageReleaseNotes>
14+
2.2.0
15+
- Add SqlServer.CreateEmptyDatabaseAsync() method.
16+
- Add SqlServer.DeleteDatabaseAsync() method.
17+
1418
2.1.0
1519
- PosInformatique.Testing.Databases.SqlServer target the .NET Standard 2.0 platform.
1620
- PosInformatique.Testing.Databases.SqlServer.Dac target the .NET Core 6.0 and .NET Framework 4.6.2

src/Testing.Databases.SqlServer/SqlServer.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ public SqlServerDatabase CreateEmptyDatabase(string name)
5151
return this.GetDatabase(name);
5252
}
5353

54+
/// <summary>
55+
/// Creates an empty database asynchronously in the SQL Server instance with the specified <paramref name="name"/>.
56+
/// If the database already exists, it will be delete.
57+
/// </summary>
58+
/// <param name="name">Name of the database to create.</param>
59+
/// <param name="cancellationToken"><see cref="CancellationToken"/> used to cancel the asynchronous operation.</param>
60+
/// <returns>A <see cref="Task"/> which represents the asynchronous operation and contains an instance of <see cref="SqlServerDatabase"/> which allows to execute SQL commands/queries.</returns>
61+
public async Task<SqlServerDatabase> CreateEmptyDatabaseAsync(string name, CancellationToken cancellationToken = default)
62+
{
63+
await this.DeleteDatabaseAsync(name, cancellationToken);
64+
await this.Master.ExecuteNonQueryAsync($"CREATE DATABASE [{name}]", cancellationToken);
65+
66+
return this.GetDatabase(name);
67+
}
68+
5469
/// <summary>
5570
/// Deletes a database in the SQL server instance with the specified <paramref name="name"/>.
5671
/// If the database does not exists, no exception is thrown.
@@ -62,6 +77,19 @@ public void DeleteDatabase(string name)
6277
this.Master.ExecuteNonQuery($"IF EXISTS (SELECT 1 FROM [sys].[databases] WHERE [name] = '{name}') DROP DATABASE [{name}]");
6378
}
6479

80+
/// <summary>
81+
/// Deletes a database asynchronously in the SQL server instance with the specified <paramref name="name"/>.
82+
/// If the database does not exists, no exception is thrown.
83+
/// </summary>
84+
/// <param name="name">Name of the database to delete.</param>
85+
/// <param name="cancellationToken"><see cref="CancellationToken"/> used to cancel the asynchronous operation.</param>
86+
/// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
87+
public async Task DeleteDatabaseAsync(string name, CancellationToken cancellationToken = default)
88+
{
89+
await this.Master.ExecuteNonQueryAsync($"IF EXISTS (SELECT 1 FROM [sys].[databases] WHERE [name] = '{name}') ALTER DATABASE [{name}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", cancellationToken);
90+
await this.Master.ExecuteNonQueryAsync($"IF EXISTS (SELECT 1 FROM [sys].[databases] WHERE [name] = '{name}') DROP DATABASE [{name}]", cancellationToken);
91+
}
92+
6593
/// <summary>
6694
/// Gets an instance of the <see cref="SqlServerDatabase"/> for the database specified with the <paramref name="name"/>.
6795
/// </summary>

tests/Testing.Databases.SqlServer.Tests/SqlServerTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
namespace PosInformatique.Testing.Databases.SqlServer.Tests
88
{
9+
[Collection("PosInformatique.Testing.Databases.SqlServer.Tests")]
910
public class SqlServerTest
1011
{
12+
private const string ConnectionString = $"Data Source=(localDB)\\posinfo-tests; Initial Catalog={nameof(SqlServerTest)}; Integrated Security=True";
13+
1114
[Theory]
1215
[InlineData("Data Source=TheServer; Initial Catalog=TheDB; User ID=TheID; Password=ThePassword", "Data Source=TheServer;Initial Catalog=master;User ID=TheID;Password=ThePassword")]
1316
[InlineData("Data Source=TheServer; Initial Catalog=TheDB; Integrated Security=True", "Data Source=TheServer;Initial Catalog=master;Integrated Security=True")]
@@ -18,5 +21,24 @@ public void Constructor(string connectionString, string expectedMasterConnection
1821
server.Master.ConnectionString.Should().Be(expectedMasterConnectionString);
1922
server.Master.Server.Should().BeSameAs(server);
2023
}
24+
25+
[Fact]
26+
public async Task CreateAndDeleteAsync()
27+
{
28+
var server = new SqlServer(ConnectionString);
29+
30+
var database = await server.CreateEmptyDatabaseAsync("CreateAndDeleteDB", CancellationToken.None);
31+
32+
database.ConnectionString.Should().Be("Data Source=(localDB)\\posinfo-tests;Initial Catalog=CreateAndDeleteDB;Integrated Security=True");
33+
34+
var table = await server.Master.ExecuteQueryAsync("SELECT * FROM [sys].[databases] WHERE [name] = 'CreateAndDeleteDB'");
35+
table.Rows.Should().HaveCount(1);
36+
37+
// Delete the database
38+
await server.DeleteDatabaseAsync("CreateAndDeleteDB", CancellationToken.None);
39+
40+
table = await server.Master.ExecuteQueryAsync("SELECT * FROM [sys].[databases] WHERE [name] = 'CreateAndDeleteDB'");
41+
table.Rows.Should().BeEmpty();
42+
}
2143
}
2244
}

0 commit comments

Comments
 (0)