|
1 | 1 | using System.Threading.Tasks; |
2 | 2 | using EcoLens.Api.Data; |
| 3 | +using Microsoft.Data.SqlClient; |
3 | 4 | using Microsoft.EntityFrameworkCore; |
4 | 5 | using Testcontainers.MsSql; |
5 | 6 | using Xunit; |
@@ -48,7 +49,10 @@ private static bool LocalDbAvailable() |
48 | 49 | { |
49 | 50 | var container = new MsSqlBuilder().Build(); |
50 | 51 | await container.StartAsync(); |
51 | | - return (container.GetConnectionString(), container); |
| 52 | + // Use a dedicated database name; container default is 'master' and EnsureDeletedAsync cannot run on master. |
| 53 | + var dbName = "EcoLensMigrationCoverage_" + Guid.NewGuid().ToString("N")[..8]; |
| 54 | + var builder = new SqlConnectionStringBuilder(container.GetConnectionString()) { InitialCatalog = dbName }; |
| 55 | + return (builder.ConnectionString, container); |
52 | 56 | } |
53 | 57 | catch (Exception) |
54 | 58 | { |
@@ -81,9 +85,11 @@ public async Task ApplyAllMigrations_InitialCreate_And_AddPointAwardLogsEf_Execu |
81 | 85 | Assert.NotEmpty(applied); |
82 | 86 | } |
83 | 87 |
|
84 | | - await using (var context = new ApplicationDbContext(options)) |
| 88 | + // Only drop DB when using LocalDB; with container, disposing the container removes everything (cannot run EnsureDeleted on master). |
| 89 | + if (container == null) |
85 | 90 | { |
86 | | - await context.Database.EnsureDeletedAsync(); |
| 91 | + await using (var context = new ApplicationDbContext(options)) |
| 92 | + await context.Database.EnsureDeletedAsync(); |
87 | 93 | } |
88 | 94 | } |
89 | 95 | finally |
@@ -113,7 +119,8 @@ public async Task ApplyAllMigrations_Then_EnsureDatabase_CanBeUsed() |
113 | 119 | Assert.Contains(migrations, m => m.Contains("InitialCreate")); |
114 | 120 | Assert.Contains(migrations, m => m.Contains("AddPointAwardLogsEf")); |
115 | 121 |
|
116 | | - await context.Database.EnsureDeletedAsync(); |
| 122 | + if (container == null) |
| 123 | + await context.Database.EnsureDeletedAsync(); |
117 | 124 | } |
118 | 125 | finally |
119 | 126 | { |
|
0 commit comments