diff --git a/src/Tests/SlimMessageBus.Host.Outbox.Sql.DbContext.Test/OutboxTests.cs b/src/Tests/SlimMessageBus.Host.Outbox.Sql.DbContext.Test/OutboxTests.cs index e283eaba..a8113167 100644 --- a/src/Tests/SlimMessageBus.Host.Outbox.Sql.DbContext.Test/OutboxTests.cs +++ b/src/Tests/SlimMessageBus.Host.Outbox.Sql.DbContext.Test/OutboxTests.cs @@ -145,6 +145,8 @@ void ConfigureExternalBus(MessageBusBuilder mbb) } public const string InvalidLastname = "Exception"; + private string[] _surnames = ["Doe", "Smith", InvalidLastname]; + [Theory] [InlineData([TransactionType.SqlTransaction, BusType.AzureSB, 100, SqlOutboxMessageIdGenerationMode.ClientGuidGenerator])] @@ -160,15 +162,9 @@ public async Task Given_CommandHandlerInTransaction_When_ExceptionThrownDuringHa _testParamBusType = busType; _testParamIdGenerationMode = mode; - await PerformDbOperation(async (context, _) => - { - // migrate db - await context.Database.DropSchemaIfExistsAsync(context.Model.GetDefaultSchema()); - await context.Database.MigrateAsync(); - }); + await PrepareDatabase(); - var surnames = new[] { "Doe", "Smith", InvalidLastname }; - var commands = Enumerable.Range(0, messageCount).Select(x => new CreateCustomerCommand($"John {x:000}", surnames[x % surnames.Length])); + var commands = Enumerable.Range(0, messageCount).Select(x => new CreateCustomerCommand($"John {x:000}", _surnames[x % _surnames.Length])).ToList(); var validCommands = commands.Where(x => !string.Equals(x.Lastname, InvalidLastname, StringComparison.InvariantCulture)).ToList(); await EnsureConsumersStarted(); @@ -219,6 +215,17 @@ await PerformDbOperation(async (context, _) => customerCountWithValidLastname.Should().Be(validCommands.Count); } + private Task PrepareDatabase() + => PerformDbOperation(async (context, _) => + { + // migrate db + await context.Database.MigrateAsync(); + await context.Customers.ExecuteDeleteAsync(); +#pragma warning disable EF1002 // Risk of vulnerability to SQL injection. + await context.Database.ExecuteSqlRawAsync($"delete from {context.Model.GetDefaultSchema()}.Outbox"); +#pragma warning restore EF1002 // Risk of vulnerability to SQL injection. + }); + [Theory] [InlineData([BusType.AzureSB, 100, SqlOutboxMessageIdGenerationMode.ClientGuidGenerator])] [InlineData([BusType.AzureSB, 100, SqlOutboxMessageIdGenerationMode.DatabaseGeneratedGuid])] @@ -231,15 +238,10 @@ public async Task Given_PublishExternalEventInTransaction_When_ExceptionThrownDu _testParamTransactionType = TransactionType.TransactionScope; _testParamBusType = busType; - await PerformDbOperation(async (context, _) => - { - // migrate db - await context.Database.DropSchemaIfExistsAsync(context.Model.GetDefaultSchema()); - await context.Database.MigrateAsync(); - }); + await PrepareDatabase(); var surnames = new[] { "Doe", "Smith", InvalidLastname }; - var events = Enumerable.Range(0, messageCount).Select(x => new CustomerCreatedEvent(Guid.NewGuid(), $"John {x:000}", surnames[x % surnames.Length])); + var events = Enumerable.Range(0, messageCount).Select(x => new CustomerCreatedEvent(Guid.NewGuid(), $"John {x:000}", surnames[x % surnames.Length])).ToList(); var validEvents = events.Where(x => !string.Equals(x.Lastname, InvalidLastname, StringComparison.InvariantCulture)).ToList(); await EnsureConsumersStarted();