Skip to content

Commit d91d2f8

Browse files
committed
test: Modify unit tests
1 parent 329bc8b commit d91d2f8

File tree

4 files changed

+13
-50
lines changed

4 files changed

+13
-50
lines changed

src/Contrib/Data/Orm/EFCore/Masa.Contrib.Data.EFCore.Tests/DbContextTest.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public async Task TestSoftDeleteAsync()
8181
dbContext.Set<Student>().Remove(student);
8282
await dbContext.SaveChangesAsync();
8383

84-
Assert.IsTrue(await dbContext.Set<Student>().CountAsync() == 0);
84+
Assert.IsFalse(await dbContext.Set<Student>().AnyAsync());
8585

8686
var dataFilter = serviceProvider.GetRequiredService<IDataFilter>();
8787
using (dataFilter.Disable<ISoftDelete>())
@@ -150,8 +150,8 @@ public async Task TestAddMultiDbContextAsync()
150150
dbContext.Set<Student>().Remove(student);
151151
await dbContext.SaveChangesAsync();
152152

153-
Assert.IsTrue(await dbContext.Set<Student>().CountAsync() == 0);
154-
Assert.IsTrue(await queryDbContext.Set<Student>().CountAsync() == 0);
153+
Assert.IsFalse(await dbContext.Set<Student>().AnyAsync());
154+
Assert.IsFalse(await queryDbContext.Set<Student>().AnyAsync());
155155

156156
var dataFilter = serviceProvider.GetRequiredService<IDataFilter>();
157157
using (dataFilter.Disable<ISoftDelete>())
@@ -217,7 +217,7 @@ public void TestAddMultiMasaDbContextReturnSaveChangeFilterEqual1()
217217
.AddMasaDbContext<CustomDbContext>();
218218

219219
var serviceProvider = services.BuildServiceProvider();
220-
Assert.IsTrue(serviceProvider.GetServices<ISaveChangesFilter>().Count() == 2);
220+
Assert.IsTrue(serviceProvider.GetServices<ISaveChangesFilter<CustomDbContext>>().Count() == 2);
221221
}
222222

223223
[TestMethod]
@@ -231,7 +231,7 @@ public void TestAddMasaDbContextReturnSaveChangeFilterEqual2()
231231

232232
var serviceProvider = services.BuildServiceProvider();
233233

234-
var filters = serviceProvider.GetServices<ISaveChangesFilter>();
234+
var filters = serviceProvider.GetServices<ISaveChangesFilter<CustomDbContext>>();
235235
Assert.IsTrue(filters.Count() == 2);
236236
}
237237

src/Contrib/Isolation/UoW/Masa.Contrib.Isolation.UoW.EFCore/IsolationDbContext.cs

+3-42
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,13 @@ namespace Masa.Contrib.Isolation.UoW.EFCore;
88
/// </summary>
99
/// <typeparam name="TKey">tenant id type</typeparam>
1010
/// <typeparam name="TDbContext"></typeparam>
11-
public abstract class IsolationDbContext<TDbContext, TKey> : MasaDbContext<TDbContext>
11+
public abstract class IsolationDbContext<TDbContext, TKey> : IsolationDbContext<TKey>
1212
where TKey : IComparable
1313
where TDbContext : MasaDbContext, IMasaDbContext
1414
{
15-
private readonly IMultiEnvironmentContext? _environmentContext;
16-
private readonly IMultiTenantContext? _tenantContext;
17-
18-
public IsolationDbContext(MasaDbContextOptions<TDbContext> options) : base(options)
19-
{
20-
_environmentContext = options.ServiceProvider?.GetService<IMultiEnvironmentContext>();
21-
_tenantContext = options.ServiceProvider?.GetService<IMultiTenantContext>();
22-
}
23-
24-
protected override Expression<Func<TEntity, bool>>? CreateFilterExpression<TEntity>()
25-
where TEntity : class
15+
protected IsolationDbContext(MasaDbContextOptions<TDbContext> options) : base(options)
2616
{
27-
Expression<Func<TEntity, bool>>? expression = null;
28-
29-
if (typeof(IMultiTenant<>).IsGenericInterfaceAssignableFrom(typeof(TEntity)) && _tenantContext != null)
30-
{
31-
string defaultTenantId = default(TKey)?.ToString() ?? string.Empty;
32-
Expression<Func<TEntity, bool>> tenantFilter = entity => !IsTenantFilterEnabled ||
33-
(EF.Property<TKey>(entity, nameof(IMultiTenant<TKey>.TenantId)).ToString() ?? string.Empty)
34-
.Equals(_tenantContext.CurrentTenant != null ? _tenantContext.CurrentTenant.Id : defaultTenantId);
35-
36-
expression = tenantFilter.And(expression != null, expression);
37-
}
38-
39-
if (typeof(IMultiEnvironment).IsAssignableFrom(typeof(TEntity)) && _environmentContext != null)
40-
{
41-
Expression<Func<TEntity, bool>> envFilter = entity => !IsEnvironmentFilterEnabled ||
42-
EF.Property<string>(entity, nameof(IMultiEnvironment.Environment))
43-
.Equals(_environmentContext != null ? _environmentContext.CurrentEnvironment : default);
44-
expression = envFilter.And(expression != null, expression);
45-
}
46-
47-
var secondExpression = base.CreateFilterExpression<TEntity>();
48-
if (secondExpression != null)
49-
expression = secondExpression.And(expression != null, expression);
50-
51-
return expression;
5217
}
53-
54-
protected virtual bool IsEnvironmentFilterEnabled => DataFilter?.IsEnabled<IMultiEnvironment>() ?? false;
55-
56-
protected virtual bool IsTenantFilterEnabled => DataFilter?.IsEnabled<IMultiTenant<TKey>>() ?? false;
5718
}
5819

5920
/// <summary>
@@ -76,7 +37,7 @@ public abstract class IsolationDbContext<TKey> : MasaDbContext
7637
private readonly IMultiEnvironmentContext? _environmentContext;
7738
private readonly IMultiTenantContext? _tenantContext;
7839

79-
public IsolationDbContext(MasaDbContextOptions options) : base(options)
40+
protected IsolationDbContext(MasaDbContextOptions options) : base(options)
8041
{
8142
_environmentContext = options.ServiceProvider?.GetService<IMultiEnvironmentContext>();
8243
_tenantContext = options.ServiceProvider?.GetService<IMultiTenantContext>();

src/Contrib/Isolation/UoW/Tests/Masa.Contrib.Isolation.UoW.EFCore.Web.Tests/EdgeDriverTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void Initialize()
1919
_services.AddSingleton<IConfiguration>(configurationRoot);
2020
_services.AddEventBus(eventBusBuilder => eventBusBuilder.UseIsolationUoW<CustomDbContext, int>(
2121
isolationBuilder => isolationBuilder.UseMultiTenant("tenant").UseMultiEnvironment("env"), dbOptions => dbOptions.UseSqlite().UseFilter()));
22-
System.Environment.SetEnvironmentVariable("env", "pro");
22+
Environment.SetEnvironmentVariable("env", "pro");
2323
}
2424

2525
[TestMethod]
@@ -39,7 +39,7 @@ public async Task TestTenantAsync()
3939
#endregion
4040

4141
var registerUserEvent = new RegisterUserEvent("jim", "123456");
42-
var eventBus = serviceProvider.GetRequiredService<IEventBus>();
42+
var eventBus = serviceProvider.CreateScope().ServiceProvider.GetRequiredService<IEventBus>();
4343
await eventBus.PublishAsync(registerUserEvent);
4444
}
4545

src/Contrib/Isolation/UoW/Tests/Masa.Contrib.Isolation.UoW.EFCore.Web.Tests/EventHandlers/RegisterUserEventHandler.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ public class RegisterUserEventHandler
1010
private readonly IMultiEnvironmentSetter _environmentSetter;
1111
private readonly IMultiEnvironmentContext _environmentContext;
1212

13-
public RegisterUserEventHandler(CustomDbContext customDbContext, IDataFilter dataFilter, IMultiEnvironmentSetter environmentSetter,
13+
public RegisterUserEventHandler(CustomDbContext customDbContext,
14+
IDataFilter dataFilter,
15+
IMultiEnvironmentSetter environmentSetter,
1416
IMultiEnvironmentContext environmentContext)
1517
{
1618
_customDbContext = customDbContext;

0 commit comments

Comments
 (0)