-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add unit tests for shared user covering shared and separate database #25319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ecb23b6
Add unit tests for shared user covering shared and separate database
maliming 77bc52e
Address Copilot review feedback
maliming d85d710
Address Copilot review feedback
maliming 5bb367e
Address Copilot review feedback
maliming b344bcc
Address Copilot review feedback
maliming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...y.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_SharedUser_SeparateDatabase_Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| using Volo.Abp.Identity.EntityFrameworkCore; | ||
|
|
||
| namespace Volo.Abp.Identity; | ||
|
|
||
| public class IdentityUserManager_SharedUser_SeparateDatabase_Tests : IdentityUserManager_SharedUser_SeparateDatabase_Tests<AbpIdentitySharedUserSeparateDbEntityFrameworkCoreTestModule> | ||
| { | ||
| } |
5 changes: 5 additions & 0 deletions
5
.../Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityUserManager_SharedUser_Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| namespace Volo.Abp.Identity; | ||
|
|
||
| public class IdentityUserManager_SharedUser_Tests : IdentityUserManager_SharedUser_Tests<AbpIdentityDomainTestModule> | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...ntity/EntityFrameworkCore/AbpIdentitySharedUserSeparateDbEntityFrameworkCoreTestModule.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.EntityFrameworkCore.Infrastructure; | ||
| using Microsoft.EntityFrameworkCore.Storage; | ||
| using Volo.Abp.Data; | ||
| using Volo.Abp.EntityFrameworkCore; | ||
| using Volo.Abp.EntityFrameworkCore.Sqlite; | ||
| using Volo.Abp.Modularity; | ||
| using Volo.Abp.MultiTenancy; | ||
| using Volo.Abp.MultiTenancy.ConfigurationStore; | ||
| using Volo.Abp.PermissionManagement.EntityFrameworkCore; | ||
| using Volo.Abp.Uow; | ||
|
|
||
| namespace Volo.Abp.Identity.EntityFrameworkCore; | ||
|
|
||
| // EF/SQLite equivalent of the MongoDB separate-database test module: each predefined tenant | ||
| // has its own keep-alive in-memory SQLite connection. Each test method (and therefore each | ||
| // AbpApplication) gets a unique connection-string suffix so the test-data seeder runs into | ||
| // fresh databases instead of duplicating into shared cache. | ||
| [DependsOn( | ||
| typeof(AbpIdentityTestBaseModule), | ||
| typeof(AbpPermissionManagementEntityFrameworkCoreModule), | ||
| typeof(AbpIdentityEntityFrameworkCoreModule), | ||
| typeof(AbpEntityFrameworkCoreSqliteModule))] | ||
| public class AbpIdentitySharedUserSeparateDbEntityFrameworkCoreTestModule : AbpModule | ||
| { | ||
| public static readonly Guid TenantAId = IdentitySharedUserSeparateDbConstants.TenantAId; | ||
| public static readonly Guid TenantBId = IdentitySharedUserSeparateDbConstants.TenantBId; | ||
|
|
||
| // Per-app keep-alive connections so the in-memory SQLite databases survive for the test's | ||
| // lifetime (without an open connection, shared-cache in-memory databases are discarded). | ||
| // Uses AbpUnitTestSqliteConnection (SemaphoreSlim around CreateCommand) — SQLite isn't | ||
| // thread-safe and parallel xUnit collections would otherwise race. Disposed in | ||
| // OnApplicationShutdown so connections do not accumulate across tests. | ||
| private readonly List<AbpUnitTestSqliteConnection> _keepAlive = new(); | ||
|
|
||
| public override void PreConfigureServices(ServiceConfigurationContext context) | ||
| { | ||
| PreConfigure<AbpSqliteOptions>(x => x.BusyTimeout = null); | ||
| } | ||
|
|
||
| public override void ConfigureServices(ServiceConfigurationContext context) | ||
| { | ||
| // Unique-per-app suffix so each test method gets a fresh trio of databases (the seeder | ||
| // in AbpIdentityTestBaseModule.OnApplicationInitialization expects to write into empty | ||
| // tables, which would fail if test methods reused the same shared-cache database). | ||
| var suffix = Guid.NewGuid().ToString("N"); | ||
| var hostConnection = $"Data Source=AbpIdentity_SeparateDb_Host_{suffix};Mode=Memory;Cache=Shared"; | ||
| var tenantAConnection = $"Data Source=AbpIdentity_SeparateDb_TenantA_{suffix};Mode=Memory;Cache=Shared"; | ||
| var tenantBConnection = $"Data Source=AbpIdentity_SeparateDb_TenantB_{suffix};Mode=Memory;Cache=Shared"; | ||
|
|
||
| EnsureDatabase(hostConnection); | ||
| EnsureDatabase(tenantAConnection); | ||
| EnsureDatabase(tenantBConnection); | ||
|
|
||
| Configure<AbpDbConnectionOptions>(options => | ||
| { | ||
| options.ConnectionStrings.Default = hostConnection; | ||
| }); | ||
|
|
||
| Configure<AbpDbContextOptions>(options => | ||
| { | ||
| options.Configure(ctx => | ||
| { | ||
| ctx.DbContextOptions.UseSqlite(ctx.ConnectionString); | ||
| }); | ||
| }); | ||
|
|
||
| Configure<AbpMultiTenancyOptions>(options => | ||
| { | ||
| options.IsEnabled = true; | ||
| options.UserSharingStrategy = TenantUserSharingStrategy.Shared; | ||
| }); | ||
|
|
||
| Configure<AbpDefaultTenantStoreOptions>(options => | ||
| { | ||
| options.Tenants = new[] | ||
| { | ||
| new TenantConfiguration(TenantAId, "tenant-a") | ||
| { | ||
| ConnectionStrings = new ConnectionStrings | ||
| { | ||
| { ConnectionStrings.DefaultConnectionStringName, tenantAConnection } | ||
| } | ||
| }, | ||
| new TenantConfiguration(TenantBId, "tenant-b") | ||
| { | ||
| ConnectionStrings = new ConnectionStrings | ||
| { | ||
| { ConnectionStrings.DefaultConnectionStringName, tenantBConnection } | ||
| } | ||
| } | ||
| }; | ||
| }); | ||
|
|
||
| context.Services.AddAlwaysDisableUnitOfWorkTransaction(); | ||
| } | ||
|
|
||
| public override void OnApplicationShutdown(ApplicationShutdownContext context) | ||
| { | ||
| foreach (var connection in _keepAlive) | ||
| { | ||
| connection.Dispose(); | ||
| } | ||
| _keepAlive.Clear(); | ||
| } | ||
|
|
||
| private void EnsureDatabase(string connectionString) | ||
| { | ||
| var keepAlive = new AbpUnitTestSqliteConnection(connectionString); | ||
| keepAlive.Open(); | ||
| _keepAlive.Add(keepAlive); | ||
|
maliming marked this conversation as resolved.
|
||
|
|
||
| new IdentityDbContext( | ||
| new DbContextOptionsBuilder<IdentityDbContext>().UseSqlite(connectionString).Options) | ||
| .GetService<IRelationalDatabaseCreator>().CreateTables(); | ||
|
|
||
| new PermissionManagementDbContext( | ||
| new DbContextOptionsBuilder<PermissionManagementDbContext>().UseSqlite(connectionString).Options) | ||
| .GetService<IRelationalDatabaseCreator>().CreateTables(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.