Skip to content

Commit 8ed44cb

Browse files
chore: update tests to remove IMultiTenantContextAccessor constructor usage
1 parent 409c386 commit 8ed44cb

12 files changed

Lines changed: 46 additions & 64 deletions

File tree

test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/Extensions/EntityTypeBuilderExtensions/TestDbContext.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@
88

99
namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.Extensions.EntityTypeBuilderExtensions;
1010

11-
public class TestDbContext(Action<ModelBuilder>? config, TenantInfo tenantInfo, DbContextOptions options)
12-
: EntityFrameworkCore.MultiTenantDbContext(new StaticMultiTenantContextAccessor<TenantInfo>(tenantInfo), options)
11+
public class TestDbContext : EntityFrameworkCore.MultiTenantDbContext
1312
{
13+
private readonly Action<ModelBuilder>? _config;
14+
15+
public TestDbContext(Action<ModelBuilder>? config, TenantInfo tenantInfo, DbContextOptions options)
16+
: base(options)
17+
{
18+
_config = config;
19+
TenantInfo = tenantInfo;
20+
}
1421
public DbSet<MyMultiTenantThing>? MyMultiTenantThings { get; set; }
1522
public DbSet<MyThingWithTenantId>? MyThingsWithTenantIds { get; set; }
1623
public DbSet<MyThingWithIntTenantId>? MyThingsWithIntTenantId { get; set; }
@@ -20,8 +27,8 @@ public class TestDbContext(Action<ModelBuilder>? config, TenantInfo tenantInfo,
2027
protected override void OnModelCreating(ModelBuilder modelBuilder)
2128
{
2229
// if the test passed in a custom builder use it
23-
if (config != null)
24-
config(modelBuilder);
30+
if (_config != null)
31+
_config(modelBuilder);
2532
// or use the standard builder configuration
2633
else
2734
{

test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/Extensions/MultiTenantDbContextExtensions/TestDbContext.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ public class TestDbContext : EntityFrameworkCore.MultiTenantDbContext
1111
public DbSet<Blog>? Blogs { get; set; }
1212
public DbSet<Post>? Posts { get; set; }
1313

14-
public TestDbContext(TenantInfo tenantInfo,
15-
DbContextOptions options) :
16-
base(new StaticMultiTenantContextAccessor<TenantInfo>(tenantInfo), options)
14+
public TestDbContext(TenantInfo tenantInfo, DbContextOptions options) : base(options)
1715
{
16+
TenantInfo = tenantInfo;
1817
}
1918
}
2019

test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/Extensions/MultiTenantEntityTypeBuilderExtensions/TestDbContext.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright Finbuckle LLC, Andrew White, and Contributors.
22
// Refer to the solution LICENSE file for more information.
33

4-
using Finbuckle.MultiTenant.Abstractions;
54
using Microsoft.EntityFrameworkCore;
65
using Microsoft.EntityFrameworkCore.Infrastructure;
76

@@ -11,8 +10,7 @@ public class TestDbContext : EntityFrameworkCore.MultiTenantDbContext
1110
{
1211
private readonly Action<ModelBuilder> _config;
1312

14-
public TestDbContext(Action<ModelBuilder> config, DbContextOptions options) :
15-
base(new StaticMultiTenantContextAccessor<TenantInfo>(new TenantInfo { Id = "dummy", Identifier = "" }), options)
13+
public TestDbContext(Action<ModelBuilder> config, DbContextOptions options) : base(options)
1614
{
1715
_config = config;
1816
}

test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/Extensions/ServiceCollectionExtensions/ServiceCollectionExtensionsShould.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,8 @@ public void AddMultiTenantDbContext_SetsTenantInfoOnCustomContext_WithoutConstru
209209
}
210210
}
211211

212-
public class NonPooledTestDbContext(
213-
IMultiTenantContextAccessor multiTenantContextAccessor,
214-
DbContextOptions<NonPooledTestDbContext> options)
215-
: EntityFrameworkCore.MultiTenantDbContext(multiTenantContextAccessor, options)
212+
public class NonPooledTestDbContext(DbContextOptions<NonPooledTestDbContext> options)
213+
: EntityFrameworkCore.MultiTenantDbContext(options)
216214
{
217215
public DbSet<NonPooledBlog>? Blogs { get; set; }
218216
}
@@ -224,10 +222,8 @@ public class NonPooledBlog
224222
public string? Title { get; set; }
225223
}
226224

227-
public class PooledTestDbContext(
228-
IMultiTenantContextAccessor multiTenantContextAccessor,
229-
DbContextOptions<PooledTestDbContext> options)
230-
: EntityFrameworkCore.MultiTenantDbContext(multiTenantContextAccessor, options)
225+
public class PooledTestDbContext(DbContextOptions<PooledTestDbContext> options)
226+
: EntityFrameworkCore.MultiTenantDbContext(options)
231227
{
232228
public DbSet<PooledBlog>? Blogs { get; set; }
233229
}

test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantDbContext/MultiTenantDbContextShould.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,14 @@ public void WorkWithDependencyInjection()
2626
[Fact]
2727
public void WorkWithSingleParamCtor()
2828
{
29-
var tenant1 = new TenantInfo { Id = "abc", Identifier = "abc", Name = "abc" };
30-
var mca = new StaticMultiTenantContextAccessor<TenantInfo>(tenant1);
31-
var c = new TestBlogDbContext(mca);
32-
29+
var c = new TestBlogDbContext();
3330
Assert.NotNull(c);
3431
}
3532

3633
[Fact]
3734
public void WorkWithTwoParamCtor()
3835
{
39-
var tenant1 = new TenantInfo { Id = "abc", Identifier = "abc", Name = "abc" };
40-
var mca = new StaticMultiTenantContextAccessor<TenantInfo>(tenant1);
41-
var c = new TestBlogDbContext(mca, new DbContextOptions<TestBlogDbContext>());
42-
36+
var c = new TestBlogDbContext(new DbContextOptions<TestBlogDbContext>());
4337
Assert.NotNull(c);
4438
}
4539

@@ -91,9 +85,10 @@ public void WorkWithCreateNoOptions()
9185
[Fact]
9286
public void ThrowOnInvalidDbContext()
9387
{
88+
// Passing args that no constructor accepts should throw ArgumentException.
9489
var tenant1 = new TenantInfo { Id = "abc", Identifier = "abc", Name = "abc" };
9590

9691
Assert.Throws<ArgumentException>(() =>
97-
EntityFrameworkCore.MultiTenantDbContext.Create<DbContext, TenantInfo>(tenant1));
92+
EntityFrameworkCore.MultiTenantDbContext.Create<TestBlogDbContext, TenantInfo>(tenant1, new object(), new object()));
9893
}
9994
}

test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantDbContext/TestEntitities.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@ public class TestBlogDbContext : EntityFrameworkCore.MultiTenantDbContext
1111
public DbSet<Blog>? Blogs { get; set; }
1212
public DbSet<Post>? Posts { get; set; }
1313

14-
public TestBlogDbContext(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor)
14+
public TestBlogDbContext()
1515
{
1616
}
1717

18-
public TestBlogDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(
19-
multiTenantContextAccessor, options)
18+
public TestBlogDbContext(DbContextOptions options) : base(options)
2019
{
2120
}
2221

23-
public TestBlogDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, object dependency) : base(
24-
multiTenantContextAccessor)
22+
public TestBlogDbContext(object dependency)
2523
{
2624
}
2725
}

test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantEntityTypeBuilder/TestDbContext.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright Finbuckle LLC, Andrew White, and Contributors.
22
// Refer to the solution LICENSE file for more information.
33

4-
using Finbuckle.MultiTenant.Abstractions;
54
using Microsoft.EntityFrameworkCore;
65
using Microsoft.EntityFrameworkCore.Infrastructure;
76

@@ -11,8 +10,7 @@ public class TestDbContext : EntityFrameworkCore.MultiTenantDbContext
1110
{
1211
private readonly Action<ModelBuilder> _config;
1312

14-
public TestDbContext(Action<ModelBuilder> config, DbContextOptions options) :
15-
base(new StaticMultiTenantContextAccessor<TenantInfo>(new TenantInfo { Id = "dummy", Identifier = "" }), options)
13+
public TestDbContext(Action<ModelBuilder> config, DbContextOptions options) : base(options)
1614
{
1715
_config = config;
1816
}

test/Finbuckle.MultiTenant.Identity.EntityFrameworkCore.Test/MultiTenanIdentitytDbContextShould.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ namespace Finbuckle.MultiTenant.Identity.EntityFrameworkCore.Test;
1616

1717
public class MultiTenantIdentityDbContextShould
1818
{
19-
private TContext CreateDbContextViaDi<TContext>(int schemaVersion = 3) where TContext : DbContext
19+
private TContext CreateDbContextViaDi<TContext>(int schemaVersion = 3) where TContext : DbContext, IMultiTenantDbContext
2020
{
2121
var services = new ServiceCollection();
2222
services.AddOptions();
23-
services.Configure<IdentityOptions>(o => o.Stores.SchemaVersion = new Version(schemaVersion,0));
24-
services.AddMultiTenant<TenantInfo>();
25-
// override the generic accessor with a static one bound to a test tenant
23+
services.Configure<IdentityOptions>(o => o.Stores.SchemaVersion = new Version(schemaVersion, 0));
2624
var tenant = new TenantInfo { Id = "abc", Identifier = "abc", Name = "abc" };
27-
services.AddSingleton<IMultiTenantContextAccessor<TenantInfo>>(new StaticMultiTenantContextAccessor<TenantInfo>(tenant));
28-
services.AddDbContext<TContext>(o =>
25+
services.AddMultiTenant<TenantInfo>()
26+
.WithStaticStrategy(tenant.Identifier)
27+
.WithInMemoryStore(options => options.Tenants.Add(tenant));
28+
services.AddMultiTenantDbContext<TContext>(o =>
2929
{
3030
o.UseSqlite("DataSource=:memory:");
3131
o.ReplaceService<IModelCacheKeyFactory, DynamicModelCacheKeyFactory>();
@@ -39,8 +39,11 @@ private TContext CreateDbContextViaDi<TContext>(int schemaVersion = 3) where TCo
3939
public void WorkWithDependencyInjection()
4040
{
4141
var services = new ServiceCollection();
42-
services.AddMultiTenant<TenantInfo>();
43-
services.AddDbContext<TestIdentityDbContext>();
42+
var tenant = new TenantInfo { Id = "abc", Identifier = "abc", Name = "abc" };
43+
services.AddMultiTenant<TenantInfo>()
44+
.WithStaticStrategy(tenant.Identifier)
45+
.WithInMemoryStore(options => options.Tenants.Add(tenant));
46+
services.AddMultiTenantDbContext<TestIdentityDbContext>();
4447
var scope = services.BuildServiceProvider().CreateScope();
4548

4649
var context = scope.ServiceProvider.GetService<TestIdentityDbContext>();
@@ -50,7 +53,7 @@ public void WorkWithDependencyInjection()
5053
[Fact]
5154
public void WorkWithSingleParamCtor()
5255
{
53-
var c = CreateDbContextViaDi<TestIdentityDbContext>();
56+
var c = new TestIdentityDbContext();
5457
Assert.NotNull(c);
5558
}
5659

test/Finbuckle.MultiTenant.Identity.EntityFrameworkCore.Test/TestIdentityDbContext.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// Copyright Finbuckle LLC, Andrew White, and Contributors.
22
// Refer to the solution LICENSE file for more information.
33

4-
using Finbuckle.MultiTenant.Abstractions;
54
using Microsoft.EntityFrameworkCore;
65

76
namespace Finbuckle.MultiTenant.Identity.EntityFrameworkCore.Test;
87

98
public class TestIdentityDbContext : MultiTenantIdentityDbContext
109
{
11-
public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor) : base(
12-
multiTenantContextAccessor)
10+
public TestIdentityDbContext()
1311
{
1412
}
1513

16-
public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) :
17-
base(multiTenantContextAccessor, options)
14+
public TestIdentityDbContext(DbContextOptions options) : base(options)
1815
{
1916
}
2017

test/Finbuckle.MultiTenant.Identity.EntityFrameworkCore.Test/TestIdentityDbContextAll.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright Finbuckle LLC, Andrew White, and Contributors.
22
// Refer to the solution LICENSE file for more information.
33

4-
using Finbuckle.MultiTenant.Abstractions;
54
using Microsoft.AspNetCore.Identity;
65
using Microsoft.EntityFrameworkCore;
76

@@ -11,13 +10,11 @@ public class TestIdentityDbContextAll : MultiTenantIdentityDbContext<IdentityUse
1110
IdentityUserClaim<string>, IdentityUserRole<string>, IdentityUserLogin<string>, IdentityRoleClaim<string>,
1211
IdentityUserToken<string>, IdentityUserPasskey<string>>
1312
{
14-
public TestIdentityDbContextAll(IMultiTenantContextAccessor multiTenantContextAccessor) : base(
15-
multiTenantContextAccessor)
13+
public TestIdentityDbContextAll()
1614
{
1715
}
1816

19-
public TestIdentityDbContextAll(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) :
20-
base(multiTenantContextAccessor, options)
17+
public TestIdentityDbContextAll(DbContextOptions options) : base(options)
2118
{
2219
}
2320

0 commit comments

Comments
 (0)