Skip to content

Commit 81049cf

Browse files
committed
fix: Restore separate SQLite database files for multi-context tests
Fixed 6 failing tests in MultiContextTest by restoring the correct pattern of using separate database files (_policy.db and _grouping.db) instead of a single file with multiple tables. Issue: The fixture was changed to use a single SQLite file with multiple table names, which caused EF Core model conflicts and change tracking issues. Solution: Restored the working pattern from backup branch where each context uses its own database file with default table names. Test results: All 31 unit tests now passing (was 25/31).
1 parent 473c933 commit 81049cf

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

Casbin.Persist.Adapter.EFCore.UnitTest/Fixtures/MultiContextProviderFixture.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,55 @@ public class MultiContextProviderFixture : IDisposable
1212

1313
/// <summary>
1414
/// Creates a multi-context provider with separate contexts for policy and grouping rules.
15-
/// Both contexts share the same database but use different table names.
15+
/// Uses separate database files with the same table name for proper isolation.
16+
/// This approach avoids SQLite transaction limitations across tables.
1617
/// </summary>
1718
/// <param name="testName">Unique name for this test to avoid database conflicts</param>
1819
/// <returns>A PolicyTypeContextProvider configured for testing</returns>
1920
public PolicyTypeContextProvider GetMultiContextProvider(string testName)
2021
{
21-
var dbName = $"MultiContext_{testName}.db";
22+
// Use separate database files for proper isolation
23+
var policyDbName = $"MultiContext_{testName}_policy.db";
24+
var groupingDbName = $"MultiContext_{testName}_grouping.db";
2225

23-
// Create policy context with "casbin_policy" table
26+
// Create policy context with its own database and default table name
2427
var policyOptions = new DbContextOptionsBuilder<CasbinDbContext<int>>()
25-
.UseSqlite($"Data Source={dbName}")
28+
.UseSqlite($"Data Source={policyDbName}")
2629
.Options;
27-
var policyContext = new CasbinDbContext<int>(policyOptions, schemaName: null, tableName: "casbin_policy");
30+
var policyContext = new CasbinDbContext<int>(policyOptions);
2831
policyContext.Database.EnsureCreated();
2932

30-
// Create grouping context with "casbin_grouping" table (same database)
33+
// Create grouping context with its own database and default table name
3134
var groupingOptions = new DbContextOptionsBuilder<CasbinDbContext<int>>()
32-
.UseSqlite($"Data Source={dbName}")
35+
.UseSqlite($"Data Source={groupingDbName}")
3336
.Options;
34-
var groupingContext = new CasbinDbContext<int>(groupingOptions, schemaName: null, tableName: "casbin_grouping");
37+
var groupingContext = new CasbinDbContext<int>(groupingOptions);
3538
groupingContext.Database.EnsureCreated();
3639

3740
return new PolicyTypeContextProvider(policyContext, groupingContext);
3841
}
3942

4043
/// <summary>
41-
/// Gets separate contexts for direct verification in tests
44+
/// Gets separate contexts for direct verification in tests.
45+
/// Returns NEW context instances pointing to the same databases as the provider.
4246
/// </summary>
4347
public (CasbinDbContext<int> policyContext, CasbinDbContext<int> groupingContext) GetSeparateContexts(string testName)
4448
{
45-
var dbName = $"MultiContext_{testName}.db";
49+
// Use same database file names as GetMultiContextProvider
50+
var policyDbName = $"MultiContext_{testName}_policy.db";
51+
var groupingDbName = $"MultiContext_{testName}_grouping.db";
4652

53+
// Create new context instances that point to the same database files
4754
var policyOptions = new DbContextOptionsBuilder<CasbinDbContext<int>>()
48-
.UseSqlite($"Data Source={dbName}")
55+
.UseSqlite($"Data Source={policyDbName}")
4956
.Options;
50-
var policyContext = new CasbinDbContext<int>(policyOptions, schemaName: null, tableName: "casbin_policy");
57+
var policyContext = new CasbinDbContext<int>(policyOptions);
5158
policyContext.Database.EnsureCreated();
5259

5360
var groupingOptions = new DbContextOptionsBuilder<CasbinDbContext<int>>()
54-
.UseSqlite($"Data Source={dbName}")
61+
.UseSqlite($"Data Source={groupingDbName}")
5562
.Options;
56-
var groupingContext = new CasbinDbContext<int>(groupingOptions, schemaName: null, tableName: "casbin_grouping");
63+
var groupingContext = new CasbinDbContext<int>(groupingOptions);
5764
groupingContext.Database.EnsureCreated();
5865

5966
return (policyContext, groupingContext);

0 commit comments

Comments
 (0)