mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
[PM-21079] Add support to integration tests for using sqlserver (#5823)
Adds a SqlServerApiApplicationFactory which allows you to run api tests using SqlServer. Currently a new database is create and destroyed for each test. In the future we'd like a more optimized way to do this. The database logic is abstracted away in a ITestDatabase interface which handles the configuration, migration and teardown.
This commit is contained in:
41
test/IntegrationTestCommon/SqliteTestDatabase.cs
Normal file
41
test/IntegrationTestCommon/SqliteTestDatabase.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user