|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Xunit; |
| 4 | +using Dapper.Contrib.Extensions; |
| 5 | +using System.Linq; |
| 6 | +using System.Data; |
| 7 | +using System.Collections; |
| 8 | +using Simpleverse.Dapper.SqlServer; |
| 9 | +using Dapper; |
| 10 | + |
| 11 | +namespace Simpleverse.Dapper.Test.SqlServer |
| 12 | +{ |
| 13 | + [Collection("SqlServerCollection")] |
| 14 | + public class UpdateTests : IClassFixture<DatabaseFixture> |
| 15 | + { |
| 16 | + DatabaseFixture fixture; |
| 17 | + |
| 18 | + public UpdateTests(DatabaseFixture fixture) |
| 19 | + { |
| 20 | + this.fixture = fixture; |
| 21 | + } |
| 22 | + |
| 23 | + [Theory] |
| 24 | + [ClassData(typeof(UpdateTestData))] |
| 25 | + public void UpdateTest<T>(string testName, IEnumerable<T> records, Action<T, T> check, int expected) where T : Identity |
| 26 | + { |
| 27 | + using (var connection = fixture.GetConnection()) |
| 28 | + { |
| 29 | + Arange<T>(connection); |
| 30 | + |
| 31 | + var inserted = connection.Insert(records); |
| 32 | + records = records.Skip(1); |
| 33 | + foreach (var record in records) |
| 34 | + record.Name = (record.Id + 2).ToString(); |
| 35 | + |
| 36 | + // act |
| 37 | + var updated = connection.UpdateBulkAsync(records).Result; |
| 38 | + |
| 39 | + // assert |
| 40 | + Assert(connection, records, check, records.Count(), updated); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + [Theory] |
| 45 | + [ClassData(typeof(UpdateTestData))] |
| 46 | + public void UpdateTransactionAsyncTest<T>(string testName, IEnumerable<T> records, Action<T, T> check, int expected) where T : Identity |
| 47 | + { |
| 48 | + using (var connection = fixture.GetConnection()) |
| 49 | + { |
| 50 | + Arange<T>(connection); |
| 51 | + |
| 52 | + var inserted = connection.Insert(records); |
| 53 | + records = records.Skip(1); |
| 54 | + foreach (var record in records) |
| 55 | + record.Name = (record.Id + 2).ToString(); |
| 56 | + |
| 57 | + // act |
| 58 | + int updated = 0; |
| 59 | + using (var transaction = connection.BeginTransaction()) |
| 60 | + { |
| 61 | + updated = connection.UpdateBulkAsync(records, transaction: transaction).Result; |
| 62 | + transaction.Commit(); |
| 63 | + } |
| 64 | + |
| 65 | + Assert<T>(connection, records, check, records.Count(), updated); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private void Arange<T>(IDbConnection connection) where T : class |
| 70 | + { |
| 71 | + connection.Open(); |
| 72 | + fixture.TearDownDb(); |
| 73 | + fixture.SetupDb(); |
| 74 | + } |
| 75 | + |
| 76 | + private void Assert<T>(IDbConnection connection, IEnumerable<T> records, Action<T, T> check, int expected, int updated) where T : Identity |
| 77 | + { |
| 78 | + IEnumerable<T> updatedRecords = null; |
| 79 | + |
| 80 | + // Workaround for Dapper.Contrib not supporting composite keys |
| 81 | + if (typeof(T).Equals(typeof(IdentityAndExplict))) |
| 82 | + updatedRecords = connection.Query<T>("SELECT * FROM [IdentityAndExplict]"); |
| 83 | + else |
| 84 | + updatedRecords = connection.GetAll<T>(); |
| 85 | + |
| 86 | + Xunit.Assert.Equal(expected, updated); |
| 87 | + Xunit.Assert.Equal("1", updatedRecords.First(x => x.Id == 1).Name); |
| 88 | + for (int i = 0; i < records.Count(); i++) |
| 89 | + { |
| 90 | + var record = records.ElementAt(i); |
| 91 | + var updatedRecord = updatedRecords.FirstOrDefault(x => x.Id == record.Id); |
| 92 | + |
| 93 | + check(record, updatedRecord); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public class UpdateTestData : IEnumerable<object[]> |
| 99 | + { |
| 100 | + public IEnumerator<object[]> GetEnumerator() |
| 101 | + { |
| 102 | + yield return InsertTestData.IdentityTest(2); |
| 103 | + yield return InsertTestData.IdentityTest(10); |
| 104 | + yield return InsertTestData.IdentityAndExplictTest(2); |
| 105 | + yield return InsertTestData.IdentityAndExplictTest(10); |
| 106 | + yield return InsertTestData.ComputedTest(2); |
| 107 | + yield return InsertTestData.ComputedTest(10); |
| 108 | + yield return InsertTestData.WriteTest(2); |
| 109 | + yield return InsertTestData.WriteTest(10); |
| 110 | + yield return InsertTestData.DataTypeTest(2); |
| 111 | + yield return InsertTestData.DataTypeTest(10); |
| 112 | + yield return InsertTestData.DataTypeNullableTest(2); |
| 113 | + yield return InsertTestData.DataTypeNullableTest(10); |
| 114 | + } |
| 115 | + |
| 116 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 117 | + } |
| 118 | +} |
0 commit comments