|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// <auto-generated> |
| 3 | +// This code was generated by AsyncGenerator. |
| 4 | +// |
| 5 | +// Changes to this file may cause incorrect behavior and will be lost if |
| 6 | +// the code is regenerated. |
| 7 | +// </auto-generated> |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | + |
| 10 | + |
| 11 | +using System; |
| 12 | +using NHibernate.Cfg.MappingSchema; |
| 13 | +using NHibernate.Mapping.ByCode; |
| 14 | +using NUnit.Framework; |
| 15 | + |
| 16 | +namespace NHibernate.Test.NHSpecificTest.GH3204 |
| 17 | +{ |
| 18 | + using System.Threading.Tasks; |
| 19 | + [TestFixture(true)] |
| 20 | + [TestFixture(false)] |
| 21 | + [TestFixture(null)] |
| 22 | + public class OneToOneOptimisticLockFixtureAsync : TestCaseMappingByCode |
| 23 | + { |
| 24 | + private readonly bool? _optimisticLock; |
| 25 | + private Guid _id; |
| 26 | + |
| 27 | + public OneToOneOptimisticLockFixtureAsync(bool? optimisticLock) |
| 28 | + { |
| 29 | + _optimisticLock = optimisticLock; |
| 30 | + } |
| 31 | + |
| 32 | + protected override HbmMapping GetMappings() |
| 33 | + { |
| 34 | + var mapper = new ModelMapper(); |
| 35 | + mapper.Class<Entity>( |
| 36 | + rc => |
| 37 | + { |
| 38 | + rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); |
| 39 | + rc.Property(x => x.Name); |
| 40 | + rc.Version(x => x.Version, m => { }); |
| 41 | + rc.OneToOne( |
| 42 | + x => x.OneToOne, |
| 43 | + m => |
| 44 | + { |
| 45 | + m.PropertyReference(x => x.Parent); |
| 46 | + m.ForeignKey("none"); |
| 47 | + m.Cascade(Mapping.ByCode.Cascade.All); |
| 48 | + if (_optimisticLock.HasValue) |
| 49 | + m.OptimisticLock(_optimisticLock.Value); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + mapper.Class<Child>( |
| 54 | + rc => |
| 55 | + { |
| 56 | + rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb)); |
| 57 | + rc.Property(x => x.Name); |
| 58 | + rc.ManyToOne( |
| 59 | + x => x.Parent, |
| 60 | + m => |
| 61 | + { |
| 62 | + m.Unique(true); |
| 63 | + m.ForeignKey("none"); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 68 | + } |
| 69 | + |
| 70 | + protected override void OnSetUp() |
| 71 | + { |
| 72 | + using (var session = OpenSession()) |
| 73 | + using (var transaction = session.BeginTransaction()) |
| 74 | + { |
| 75 | + var e1 = new Entity { Name = "Bob" }; |
| 76 | + session.Save(e1); |
| 77 | + transaction.Commit(); |
| 78 | + _id = e1.Id; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + protected override void OnTearDown() |
| 83 | + { |
| 84 | + using (var session = OpenSession()) |
| 85 | + using (var transaction = session.BeginTransaction()) |
| 86 | + { |
| 87 | + session.CreateQuery("delete from System.Object").ExecuteUpdate(); |
| 88 | + |
| 89 | + transaction.Commit(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public async Task GetAsync() |
| 95 | + { |
| 96 | + using (var session = OpenSession()) |
| 97 | + using (var transaction = session.BeginTransaction()) |
| 98 | + { |
| 99 | + var result = await (session.GetAsync<Entity>(_id)); |
| 100 | + |
| 101 | + var child = new Child() { Parent = result }; |
| 102 | + result.OneToOne = child; |
| 103 | + var oldVersion = result.Version; |
| 104 | + await (transaction.CommitAsync()); |
| 105 | + session.Clear(); |
| 106 | + |
| 107 | + Assert.That( |
| 108 | + (await (session.GetAsync<Entity>(_id))).Version, |
| 109 | + Is.EqualTo(_optimisticLock.GetValueOrDefault(true) ? oldVersion + 1 : oldVersion)); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments