-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[PM-21079] Add support to integration tests for using sqlserver #5823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2bd74b3
Add support for running integration tests using sqlserver
Hinton e844c9c
Mark OrgUserPerTest as skipped
Hinton 3b52c59
Dynamically build connection string
Hinton 8225378
Update events application factory
Hinton 96284a7
Remove dispose
Hinton fe5e4e2
Fix tests
Hinton e197e80
Use sqlite test db by default
Hinton edc4b56
Update events as well
Hinton b4618b3
Fix tests
Hinton 0a43ae0
Simplify database handling
Hinton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
7 changes: 7 additions & 0 deletions
7
test/Api.IntegrationTest/Factories/SqlServerApiApplicationFactory.cs
This file contains hidden or 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,7 @@ | ||
๏ปฟusing Bit.IntegrationTestCommon; | ||
|
||
#nullable enable | ||
|
||
namespace Bit.Api.IntegrationTest.Factories; | ||
|
||
public class SqlServerApiApplicationFactory() : ApiApplicationFactory(new SqlServerTestDatabase()); |
This file contains hidden or 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 hidden or 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,19 @@ | ||
๏ปฟusing Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bit.IntegrationTestCommon; | ||
|
||
#nullable enable | ||
|
||
public interface ITestDatabase | ||
{ | ||
public void AddDatabase(IServiceCollection serviceCollection); | ||
|
||
public void Migrate(IServiceCollection serviceCollection); | ||
|
||
public void Dispose(); | ||
|
||
public void ModifyGlobalSettings(Dictionary<string, string?> config) | ||
{ | ||
// Default implementation does nothing | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,70 @@ | ||
๏ปฟusing Bit.Core.Settings; | ||
using Bit.Infrastructure.EntityFramework.Repositories; | ||
using Bit.Migrator; | ||
using Microsoft.Data.SqlClient; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Bit.IntegrationTestCommon; | ||
|
||
public class SqlServerTestDatabase : ITestDatabase | ||
{ | ||
public string SqlServerConnection { get; set; } | ||
|
||
public SqlServerTestDatabase() | ||
{ | ||
SqlServerConnection = "Server=localhost;Database=vault_test;User Id=SA;Password=SET_A_PASSWORD_HERE_123;Encrypt=True;TrustServerCertificate=True;"; | ||
Hinton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public void ModifyGlobalSettings(Dictionary<string, string> config) | ||
{ | ||
config["globalSettings:databaseProvider"] = "sqlserver"; | ||
config["globalSettings:sqlServer:connectionString"] = SqlServerConnection; | ||
} | ||
|
||
public void AddDatabase(IServiceCollection serviceCollection) | ||
{ | ||
serviceCollection.AddScoped(s => new DbContextOptionsBuilder<DatabaseContext>() | ||
.UseSqlServer(SqlServerConnection) | ||
.UseApplicationServiceProvider(s) | ||
.Options); | ||
} | ||
|
||
public void Migrate(IServiceCollection serviceCollection) | ||
{ | ||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
using var scope = serviceProvider.CreateScope(); | ||
var services = scope.ServiceProvider; | ||
var globalSettings = services.GetRequiredService<GlobalSettings>(); | ||
var logger = services.GetRequiredService<ILogger<DbMigrator>>(); | ||
|
||
var migrator = new SqlServerDbMigrator(globalSettings, logger); | ||
migrator.MigrateDatabase(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
var masterConnectionString = new SqlConnectionStringBuilder(SqlServerConnection) | ||
{ | ||
InitialCatalog = "master" | ||
}.ConnectionString; | ||
|
||
using var connection = new SqlConnection(masterConnectionString); | ||
var databaseName = new SqlConnectionStringBuilder(SqlServerConnection).InitialCatalog; | ||
|
||
connection.Open(); | ||
|
||
var databaseNameQuoted = new SqlCommandBuilder().QuoteIdentifier(databaseName); | ||
|
||
using (var cmd = new SqlCommand($"ALTER DATABASE {databaseNameQuoted} SET single_user WITH rollback IMMEDIATE", connection)) | ||
{ | ||
cmd.ExecuteNonQuery(); | ||
} | ||
|
||
using (var cmd = new SqlCommand($"DROP DATABASE {databaseNameQuoted}", connection)) | ||
{ | ||
cmd.ExecuteNonQuery(); | ||
} | ||
} | ||
} |
This file contains hidden or 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,41 @@ | ||
๏ปฟusing Bit.Infrastructure.EntityFramework.Repositories; | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bit.IntegrationTestCommon; | ||
|
||
public class SqliteTestDatabase : ITestDatabase | ||
{ | ||
private SqliteConnection SqliteConnection { get; set; } | ||
|
||
public SqliteTestDatabase() | ||
{ | ||
SqliteConnection = new SqliteConnection("DataSource=:memory:"); | ||
SqliteConnection.Open(); | ||
} | ||
|
||
public void AddDatabase(IServiceCollection serviceCollection) | ||
{ | ||
serviceCollection.AddScoped(s => new DbContextOptionsBuilder<DatabaseContext>() | ||
.UseSqlite(SqliteConnection) | ||
.UseApplicationServiceProvider(s) | ||
.Options); | ||
} | ||
|
||
public void Migrate(IServiceCollection serviceCollection) | ||
{ | ||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
using var scope = serviceProvider.CreateScope(); | ||
var services = scope.ServiceProvider; | ||
var context = services.GetRequiredService<DatabaseContext>(); | ||
|
||
context.Database.EnsureDeleted(); | ||
context.Database.EnsureCreated(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
SqliteConnection.Dispose(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.