-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds some integration tests for schema management (#157)
* Add SchemaManager.Core.UnitTests project. Add tests for SqlSchemaManager * Add integration test project, tests for BaseSchemaRunner * Add integration tests for schema initializer, schema upgrade runner * Fix db cleanup * Dispose Connection * PR feedback * Filter out integration tests for now * Revert changes to SqlServer.Web project * PR feedback * Prefix assembly name with Microsoft.Health
- Loading branch information
Showing
21 changed files
with
548 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Microsoft.Health.SqlServer/Extensions/SqlConnectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Data.SqlClient; | ||
|
||
namespace Microsoft.Health.SqlServer.Extensions | ||
{ | ||
public static class SqlConnectionExtensions | ||
{ | ||
public static async Task TryOpenAsync(this SqlConnection connection, CancellationToken cancellationToken = default) | ||
{ | ||
if (connection.State != System.Data.ConnectionState.Open) | ||
{ | ||
await connection.OpenAsync(cancellationToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Microsoft.Health.SqlServer/Features/Schema/Manager/IBaseSchemaRunner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Health.SqlServer.Features.Schema.Manager | ||
{ | ||
public interface IBaseSchemaRunner | ||
{ | ||
public Task EnsureBaseSchemaExistsAsync(CancellationToken cancellationToken); | ||
|
||
public Task EnsureInstanceSchemaRecordExistsAsync(CancellationToken cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...osoft.Health.SqlServer.Tests.Integration/Features/Schema/Manager/BaseSchemaRunnerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.Health.SqlServer.Features.Schema.Manager; | ||
using Microsoft.Health.SqlServer.Features.Schema.Manager.Exceptions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Health.SqlServer.Tests.Integration.Features.Schema.Manager | ||
{ | ||
public class BaseSchemaRunnerTests : SqlIntegrationTestBase | ||
{ | ||
private readonly BaseSchemaRunner _runner; | ||
private readonly ISchemaManagerDataStore _dataStore; | ||
|
||
public BaseSchemaRunnerTests(ITestOutputHelper output) | ||
: base(output) | ||
{ | ||
var sqlConnectionFactory = new DefaultSqlConnectionFactory(ConnectionStringProvider); | ||
_dataStore = new SchemaManagerDataStore(sqlConnectionFactory); | ||
|
||
_runner = new BaseSchemaRunner(sqlConnectionFactory, _dataStore, ConnectionStringProvider, NullLogger<BaseSchemaRunner>.Instance); | ||
} | ||
|
||
[Fact] | ||
public async Task EnsureBaseSchemaExist_DoesNotExist_CreatesIt() | ||
{ | ||
Assert.False(await _dataStore.BaseSchemaExistsAsync(CancellationToken.None)); | ||
await _runner.EnsureBaseSchemaExistsAsync(CancellationToken.None); | ||
Assert.True(await _dataStore.BaseSchemaExistsAsync(CancellationToken.None)); | ||
} | ||
|
||
[Fact] | ||
public async Task EnsureBaseSchemaExist_Exists_DoesNothing() | ||
{ | ||
Assert.False(await _dataStore.BaseSchemaExistsAsync(CancellationToken.None)); | ||
await _runner.EnsureBaseSchemaExistsAsync(CancellationToken.None); | ||
Assert.True(await _dataStore.BaseSchemaExistsAsync(CancellationToken.None)); | ||
await _runner.EnsureBaseSchemaExistsAsync(CancellationToken.None); | ||
Assert.True(await _dataStore.BaseSchemaExistsAsync(CancellationToken.None)); | ||
} | ||
|
||
[Fact] | ||
public async Task EnsureInstanceSchemaRecordExists_WhenNotExists_Throws() | ||
{ | ||
await Assert.ThrowsAsync<SchemaManagerException>(() => _runner.EnsureInstanceSchemaRecordExistsAsync(CancellationToken.None)); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
test/Microsoft.Health.SqlServer.Tests.Integration/Features/Schema/SchemaInitializerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// ------------------------------------------------------------------------------------------------- | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------------------------------------------- | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Health.SqlServer.Features.Schema; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.Health.SqlServer.Tests.Integration.Features.Schema | ||
{ | ||
public class SchemaInitializerTests : SqlIntegrationTestBase | ||
{ | ||
public SchemaInitializerTests(ITestOutputHelper outputHelper) | ||
: base(outputHelper) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task DatabaseDoesNotExist_DoesDatabaseExistAsync_ReturnsFalse() | ||
{ | ||
Assert.False(await SchemaInitializer.DoesDatabaseExistAsync(Connection, "doesnotexist", CancellationToken.None)); | ||
} | ||
|
||
[Fact] | ||
public async Task DatabaseExists_DoesDatabaseExistAsync_ReturnsTrue() | ||
{ | ||
const string dbName = "willexist"; | ||
|
||
try | ||
{ | ||
Assert.False(await SchemaInitializer.DoesDatabaseExistAsync(Connection, dbName, CancellationToken.None)); | ||
Assert.True(await SchemaInitializer.CreateDatabaseAsync(Connection, dbName, CancellationToken.None)); | ||
Assert.True(await SchemaInitializer.DoesDatabaseExistAsync(Connection, dbName, CancellationToken.None)); | ||
} | ||
finally | ||
{ | ||
await DeleteDatabaseAsync(dbName); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.