|
| 1 | +using System.Threading; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using AltaSoft.Storm.Exceptions; |
| 4 | +using AltaSoft.Storm.TestModels; |
| 5 | +using AltaSoft.Storm.TestModels.VeryBadNamespace; |
| 6 | +using FluentAssertions; |
| 7 | +using Microsoft.Data.SqlClient; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace AltaSoft.Storm.Tests; |
| 11 | + |
| 12 | +public class BatchTransactionRollbackTests : IClassFixture<DatabaseFixture> |
| 13 | +{ |
| 14 | + private readonly DatabaseFixture _fixture; |
| 15 | + |
| 16 | + public BatchTransactionRollbackTests(DatabaseFixture fixture) |
| 17 | + { |
| 18 | + _fixture = fixture; |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public async Task Batch_WithPrimaryKeyViolation_ShouldThrowAndRollbackAllCommands() |
| 23 | + { |
| 24 | + var account1 = NewAccount(100_001, "US12345678901234560001"); |
| 25 | + var account2 = NewAccount(100_001, "US12345678901234560002"); |
| 26 | + var account3 = NewAccount(100_002, "US12345678901234560003"); |
| 27 | + |
| 28 | + using (var _ = new StormTransactionScope()) |
| 29 | + { |
| 30 | + await using var context = new TestStormContext(_fixture.ConnectionString); |
| 31 | + await using var batch = context.CreateBatch(); |
| 32 | + |
| 33 | + batch.Add(context.InsertIntoAccount().Values(account1)); |
| 34 | + batch.Add(context.InsertIntoAccount().Values(account2)); |
| 35 | + batch.Add(context.InsertIntoAccount().Values(account3)); |
| 36 | + |
| 37 | + var act = async () => await batch.ExecuteAsync(CancellationToken.None); |
| 38 | + await act.Should().ThrowAsync<StormPrimaryKeyViolationException>(); |
| 39 | + } |
| 40 | + |
| 41 | + await using var verifyContext = new TestStormContext(_fixture.ConnectionString); |
| 42 | + |
| 43 | + var inserted1 = await verifyContext.SelectFromAccount(account1.IbanAccount, account1.Ccy).GetAsync(); |
| 44 | + var inserted2 = await verifyContext.SelectFromAccount(account2.IbanAccount, account2.Ccy).GetAsync(); |
| 45 | + var inserted3 = await verifyContext.SelectFromAccount(account3.IbanAccount, account3.Ccy).GetAsync(); |
| 46 | + |
| 47 | + inserted1.Should().BeNull(); |
| 48 | + inserted2.Should().BeNull(); |
| 49 | + inserted3.Should().BeNull(); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task Batch_WithUsersPrimaryKeyViolation_ShouldThrowAndRollbackAllCommands() |
| 54 | + { |
| 55 | + var user1 = DatabaseHelper.NewUser(100_100); |
| 56 | + var user2 = DatabaseHelper.NewUser(100_100); // same PK (UserId + BranchId) |
| 57 | + var user3 = DatabaseHelper.NewUser(100_101); // New PK, should not cause violation if executed alone |
| 58 | + |
| 59 | + |
| 60 | + using (var _ = new StormTransactionScope()) |
| 61 | + { |
| 62 | + await using var context = new TestStormContext(_fixture.ConnectionString); |
| 63 | + await using var batch = context.CreateBatch(); |
| 64 | + |
| 65 | + batch.Add(context.InsertIntoUsersTable().Values(user1)); |
| 66 | + batch.Add(context.InsertIntoUsersTable().Values(user2)); |
| 67 | + batch.Add(context.InsertIntoUsersTable().Values(user3)); |
| 68 | + |
| 69 | + var act = async () => await batch.ExecuteAsync(CancellationToken.None); |
| 70 | + await act.Should().ThrowAsync<StormPrimaryKeyViolationException>(); |
| 71 | + } |
| 72 | + |
| 73 | + await using var verifyContext = new TestStormContext(_fixture.ConnectionString); |
| 74 | + |
| 75 | + var inserted1 = await verifyContext.SelectFromUsersTable(user1.UserId, user1.BranchId).GetAsync(); |
| 76 | + var inserted2 = await verifyContext.SelectFromUsersTable(user2.UserId, user2.BranchId).GetAsync(); |
| 77 | + var inserted3 = await verifyContext.SelectFromUsersTable(user3.UserId, user3.BranchId).GetAsync(); |
| 78 | + |
| 79 | + inserted1.Should().BeNull(); |
| 80 | + inserted2.Should().BeNull(); |
| 81 | + inserted3.Should().BeNull(); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public async Task Batch_WithNotNullViolation_HigherSeverity_ShouldThrowAndRollbackAllCommands() |
| 86 | + { |
| 87 | + var user1 = DatabaseHelper.NewUser(100_200); |
| 88 | + var user2 = DatabaseHelper.NewUser(100_201); |
| 89 | + var user3 = DatabaseHelper.NewUser(100_202); |
| 90 | + user2.LoginName = null!; // force NOT NULL violation (SqlException class 16) |
| 91 | + |
| 92 | + SqlException? thrown; |
| 93 | + |
| 94 | + using (var _ = new StormTransactionScope()) |
| 95 | + { |
| 96 | + await using var context = new TestStormContext(_fixture.ConnectionString); |
| 97 | + await using var batch = context.CreateBatch(); |
| 98 | + |
| 99 | + batch.Add(context.InsertIntoUsersTable().Values(user1)); |
| 100 | + batch.Add(context.InsertIntoUsersTable().Values(user2)); |
| 101 | + batch.Add(context.InsertIntoUsersTable().Values(user3)); |
| 102 | + |
| 103 | + var act = async () => await batch.ExecuteAsync(CancellationToken.None); |
| 104 | + thrown = (await act.Should().ThrowAsync<SqlException>()).Which; |
| 105 | + } |
| 106 | + |
| 107 | + thrown.Should().NotBeNull(); |
| 108 | + thrown.Class.Should().BeGreaterThanOrEqualTo(16); |
| 109 | + |
| 110 | + await using var verifyContext = new TestStormContext(_fixture.ConnectionString); |
| 111 | + |
| 112 | + var inserted1 = await verifyContext.SelectFromUsersTable(user1.UserId, user1.BranchId).GetAsync(); |
| 113 | + var inserted2 = await verifyContext.SelectFromUsersTable(user2.UserId, user2.BranchId).GetAsync(); |
| 114 | + var inserted3 = await verifyContext.SelectFromUsersTable(user3.UserId, user3.BranchId).GetAsync(); |
| 115 | + |
| 116 | + inserted1.Should().BeNull(); |
| 117 | + inserted2.Should().BeNull(); |
| 118 | + inserted3.Should().BeNull(); |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public async Task Batch_WithCommandTimeout_ShouldThrowAndRollbackAllCommands() |
| 124 | + { |
| 125 | + const int lockedUserId = 1; |
| 126 | + const short branchId = 7; |
| 127 | + |
| 128 | + await using var lockerConnection = new SqlConnection(_fixture.ConnectionString); |
| 129 | + await lockerConnection.OpenAsync(); |
| 130 | + await using var lockerTransaction = (SqlTransaction)await lockerConnection.BeginTransactionAsync(); |
| 131 | + |
| 132 | + await using (var lockCmd = lockerConnection.CreateCommand()) |
| 133 | + { |
| 134 | + lockCmd.Transaction = lockerTransaction; |
| 135 | + lockCmd.CommandText = "UPDATE dbo.Users SET FullName = FullName WHERE Id = @id AND BranchId = @branchId"; |
| 136 | + lockCmd.Parameters.AddWithValue("@id", lockedUserId); |
| 137 | + lockCmd.Parameters.AddWithValue("@branchId", branchId); |
| 138 | + await lockCmd.ExecuteNonQueryAsync(); |
| 139 | + } |
| 140 | + |
| 141 | + var account = NewAccount(100_300, "US12345678901234560300"); |
| 142 | + |
| 143 | + SqlException? thrown; |
| 144 | + |
| 145 | + using (var _ = new StormTransactionScope()) |
| 146 | + { |
| 147 | + await using var context = new TestStormContext(_fixture.ConnectionString); |
| 148 | + await using var batch = context.CreateBatch(); |
| 149 | + |
| 150 | + var insertCmd = context.InsertIntoAccount().Values(account); |
| 151 | + var blockedUpdateCmd = context.UpdateUsersTable(lockedUserId, branchId) |
| 152 | + .WithCommandTimeOut(1) |
| 153 | + .Set(x => x.FullName, "WillTimeout"); |
| 154 | + |
| 155 | + batch.Add(insertCmd); |
| 156 | + batch.Add(blockedUpdateCmd); |
| 157 | + |
| 158 | + var act = async () => await batch.ExecuteAsync(CancellationToken.None); |
| 159 | + thrown = (await act.Should().ThrowAsync<SqlException>()).Which; |
| 160 | + } |
| 161 | + |
| 162 | + thrown.Should().NotBeNull(); |
| 163 | + thrown.Number.Should().Be(-2); |
| 164 | + |
| 165 | + await lockerTransaction.RollbackAsync(); |
| 166 | + |
| 167 | + await using var verifyContext = new TestStormContext(_fixture.ConnectionString); |
| 168 | + var inserted = await verifyContext.SelectFromAccount(account.IbanAccount, account.Ccy).GetAsync(); |
| 169 | + inserted.Should().BeNull(); |
| 170 | + } |
| 171 | + |
| 172 | + private static Account NewAccount(int id, string iban) |
| 173 | + { |
| 174 | + return new Account |
| 175 | + { |
| 176 | + Id = id, |
| 177 | + RelatedCustomerId = new CustomerId(1), |
| 178 | + Ccy = "USD", |
| 179 | + IbanAccount = iban, |
| 180 | + BbanAccount = 1234567890123456, |
| 181 | + BranchId = 1, |
| 182 | + Type = 1, |
| 183 | + Name = $"Test Account {id}", |
| 184 | + CanDebit = true, |
| 185 | + CanCredit = true |
| 186 | + }; |
| 187 | + } |
| 188 | +} |
0 commit comments