Skip to content

Commit 6989347

Browse files
committed
Testing: isolation level is configurable
1 parent e2eeb3e commit 6989347

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProvider.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class SqlServerTestDbContextProvider<T> : ITestDbContextProvider<T>
3333
private T? _assertDbContext;
3434
private IDbContextTransaction? _tx;
3535
private bool _isAtLeastOneContextCreated;
36+
private readonly IsolationLevel _sharedTablesIsolationLevel;
3637

3738
/// <inheritdoc />
3839
public T ArrangeDbContext => _arrangeDbContext ??= CreateDbContext(true);
@@ -68,6 +69,7 @@ protected internal SqlServerTestDbContextProvider(SqlServerTestDbContextProvider
6869
ArgumentNullException.ThrowIfNull(options);
6970

7071
Schema = options.Schema ?? throw new ArgumentException($"The '{nameof(options.Schema)}' cannot be null.", nameof(options));
72+
_sharedTablesIsolationLevel = ValidateIsolationLevel(options);
7173
_isUsingSharedTables = options.IsUsingSharedTables;
7274
_masterConnection = options.MasterConnection ?? throw new ArgumentException($"The '{nameof(options.MasterConnection)}' cannot be null.", nameof(options));
7375
_masterDbContextOptions = options.MasterDbContextOptions ?? throw new ArgumentException($"The '{nameof(options.MasterDbContextOptions)}' cannot be null.", nameof(options));
@@ -79,6 +81,20 @@ protected internal SqlServerTestDbContextProvider(SqlServerTestDbContextProvider
7981
_contextFactory = options.ContextFactory;
8082
}
8183

84+
private static IsolationLevel ValidateIsolationLevel(SqlServerTestDbContextProviderOptions<T> options)
85+
{
86+
if (!options.SharedTablesIsolationLevel.HasValue)
87+
return IsolationLevel.ReadCommitted;
88+
89+
if (Enum.IsDefined(options.SharedTablesIsolationLevel.Value))
90+
throw new ArgumentException($"The provided isolation level '{options.SharedTablesIsolationLevel}' is invalid.");
91+
92+
if (options.SharedTablesIsolationLevel < IsolationLevel.ReadCommitted)
93+
throw new ArgumentException($"The isolation level '{options.SharedTablesIsolationLevel}' cannot be less than '{nameof(IsolationLevel.ReadCommitted)}'.");
94+
95+
return options.SharedTablesIsolationLevel.Value;
96+
}
97+
8298
/// <inheritdoc />
8399
public T CreateDbContext()
84100
{
@@ -166,7 +182,7 @@ protected virtual IDbContextTransaction BeginTransaction(T ctx)
166182
{
167183
ArgumentNullException.ThrowIfNull(ctx);
168184

169-
return ctx.Database.BeginTransaction(IsolationLevel.ReadCommitted);
185+
return ctx.Database.BeginTransaction(_sharedTablesIsolationLevel);
170186
}
171187

172188
/// <summary>

src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProviderOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Data;
12
using System.Data.Common;
23
using Thinktecture.Logging;
34

@@ -26,6 +27,11 @@ public class SqlServerTestDbContextProviderOptions<T> : TestDbContextProviderOpt
2627
/// </summary>
2728
public Func<DbContextOptions<T>, IDbDefaultSchema, T>? ContextFactory { get; set; }
2829

30+
/// <summary>
31+
/// Isolation level to be used with shared tables.
32+
/// </summary>
33+
public IsolationLevel? SharedTablesIsolationLevel { get; set; }
34+
2935
/// <summary>
3036
/// Initializes new instance of <see cref="SqlServerTestDbContextProviderOptions{T}"/>.
3137
/// </summary>

0 commit comments

Comments
 (0)