Skip to content

Commit

Permalink
Builder method for setting the SharedTablesIsolationLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Jan 23, 2022
1 parent 896abbf commit 0246ea2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected internal SqlServerTestDbContextProvider(SqlServerTestDbContextProvider
ArgumentNullException.ThrowIfNull(options);

Schema = options.Schema ?? throw new ArgumentException($"The '{nameof(options.Schema)}' cannot be null.", nameof(options));
_sharedTablesIsolationLevel = ValidateIsolationLevel(options);
_sharedTablesIsolationLevel = ValidateIsolationLevel(options.SharedTablesIsolationLevel);
_isUsingSharedTables = options.IsUsingSharedTables;
_masterConnection = options.MasterConnection ?? throw new ArgumentException($"The '{nameof(options.MasterConnection)}' cannot be null.", nameof(options));
_masterDbContextOptions = options.MasterDbContextOptions ?? throw new ArgumentException($"The '{nameof(options.MasterDbContextOptions)}' cannot be null.", nameof(options));
Expand All @@ -81,18 +81,18 @@ protected internal SqlServerTestDbContextProvider(SqlServerTestDbContextProvider
_contextFactory = options.ContextFactory;
}

private static IsolationLevel ValidateIsolationLevel(SqlServerTestDbContextProviderOptions<T> options)
private static IsolationLevel ValidateIsolationLevel(IsolationLevel? isolationLevel)
{
if (!options.SharedTablesIsolationLevel.HasValue)
if (!isolationLevel.HasValue)
return IsolationLevel.ReadCommitted;

if (Enum.IsDefined(options.SharedTablesIsolationLevel.Value))
throw new ArgumentException($"The provided isolation level '{options.SharedTablesIsolationLevel}' is invalid.");
if (Enum.IsDefined(isolationLevel.Value))
throw new ArgumentException($"The provided isolation level '{isolationLevel}' is invalid.", nameof(isolationLevel));

if (options.SharedTablesIsolationLevel < IsolationLevel.ReadCommitted)
throw new ArgumentException($"The isolation level '{options.SharedTablesIsolationLevel}' cannot be less than '{nameof(IsolationLevel.ReadCommitted)}'.");
if (isolationLevel < IsolationLevel.ReadCommitted)
throw new ArgumentException($"The isolation level '{isolationLevel}' cannot be less than '{nameof(IsolationLevel.ReadCommitted)}'.", nameof(isolationLevel));

return options.SharedTablesIsolationLevel.Value;
return isolationLevel.Value;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Data;
using System.Data.Common;
using System.Globalization;
using Microsoft.Data.SqlClient;
Expand Down Expand Up @@ -25,6 +26,7 @@ public class SqlServerTestDbContextProviderBuilder<T> : TestDbContextProviderBui

private bool _useThinktectureSqlServerMigrationsSqlGenerator = true;
private string? _sharedTablesSchema;
private IsolationLevel? _sharedTablesIsolationLevel;
private Func<DbContextOptions<T>, IDbDefaultSchema, T?>? _contextFactory;
private Func<SqlServerTestDbContextProviderOptions<T>, SqlServerTestDbContextProvider<T>?>? _providerFactory;

Expand All @@ -42,6 +44,19 @@ public SqlServerTestDbContextProviderBuilder(string connectionString, bool useSh
_ctxInitializations = new List<Action<T>>();
}

/// <summary>
/// Specifies the isolation level to use with shared tables.
/// Default is <see cref="IsolationLevel.ReadCommitted"/>.
/// </summary>
/// <param name="sharedTablesIsolationLevel">Isolation level to use.</param>
/// <returns>Current builder for chaining</returns>
public SqlServerTestDbContextProviderBuilder<T> UseSharedTablesIsolationLevel(IsolationLevel sharedTablesIsolationLevel)
{
_sharedTablesIsolationLevel = sharedTablesIsolationLevel;

return this;
}

/// <summary>
/// Specifies the migration strategy to use.
/// Default is <see cref="IMigrationExecutionStrategy.Migrations"/>.
Expand Down Expand Up @@ -319,7 +334,8 @@ public SqlServerTestDbContextProvider<T> Build()
{
IsUsingSharedTables = _useSharedTables,
ContextFactory = _contextFactory,
ExecutedCommands = state.CommandCapturingInterceptor?.Commands
ExecutedCommands = state.CommandCapturingInterceptor?.Commands,
SharedTablesIsolationLevel = _sharedTablesIsolationLevel
};

return _providerFactory?.Invoke(options) ?? new SqlServerTestDbContextProvider<T>(options);
Expand Down

0 comments on commit 0246ea2

Please sign in to comment.