|
| 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.Linq; |
| 12 | +using NHibernate.Cache; |
| 13 | +using NHibernate.Cfg; |
| 14 | +using NHibernate.Cfg.MappingSchema; |
| 15 | +using NHibernate.Linq; |
| 16 | +using NHibernate.Mapping.ByCode; |
| 17 | +using NUnit.Framework; |
| 18 | + |
| 19 | +namespace NHibernate.Test.NHSpecificTest.GH3176 |
| 20 | +{ |
| 21 | + using System.Threading.Tasks; |
| 22 | + [TestFixture(CacheFactory.ReadOnly, false)] |
| 23 | + [TestFixture(CacheFactory.NonstrictReadWrite, false)] |
| 24 | + [TestFixture(CacheFactory.ReadWrite, false)] |
| 25 | + [TestFixture(CacheFactory.ReadWrite, true)] |
| 26 | + public class ByCodeFixtureAsync : TestCaseMappingByCode |
| 27 | + { |
| 28 | + private readonly bool _versioned; |
| 29 | + private int _id; |
| 30 | + |
| 31 | + protected override string CacheConcurrencyStrategy { get; } |
| 32 | + |
| 33 | + public ByCodeFixtureAsync(string cacheStrategy, bool versioned) |
| 34 | + { |
| 35 | + _versioned = versioned; |
| 36 | + CacheConcurrencyStrategy = cacheStrategy; |
| 37 | + } |
| 38 | + |
| 39 | + protected override HbmMapping GetMappings() |
| 40 | + { |
| 41 | + var mapper = new ModelMapper(); |
| 42 | + mapper.Class<Entity>( |
| 43 | + rc => |
| 44 | + { |
| 45 | + rc.Id(x => x.Id, m => m.Generator(Generators.Identity)); |
| 46 | + rc.Property(x => x.Name); |
| 47 | + rc.Component( |
| 48 | + x => x.Component, |
| 49 | + m => |
| 50 | + { |
| 51 | + m.Property(x => x.Field); |
| 52 | + m.Lazy(true); |
| 53 | + }); |
| 54 | + if (_versioned) |
| 55 | + rc.Version(x => x.Version, m => { }); |
| 56 | + }); |
| 57 | + |
| 58 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 59 | + } |
| 60 | + |
| 61 | + protected override void Configure(Configuration configuration) |
| 62 | + { |
| 63 | + base.Configure(configuration); |
| 64 | + configuration.Properties[Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName; |
| 65 | + configuration.Properties[Environment.UseSecondLevelCache] = "true"; |
| 66 | + configuration.Properties[Environment.GenerateStatistics] = "true"; |
| 67 | + } |
| 68 | + |
| 69 | + protected override void OnSetUp() |
| 70 | + { |
| 71 | + using (var session = OpenSession()) |
| 72 | + using (var transaction = session.BeginTransaction()) |
| 73 | + { |
| 74 | + var e1 = new Entity { Name = "Bob", Component = new Component() { Field = "Jim" } }; |
| 75 | + session.Save(e1); |
| 76 | + _id = e1.Id; |
| 77 | + |
| 78 | + var e2 = new Entity { Name = "Sally" }; |
| 79 | + session.Save(e2); |
| 80 | + |
| 81 | + transaction.Commit(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + protected override void OnTearDown() |
| 86 | + { |
| 87 | + using (var session = OpenSession()) |
| 88 | + using (var transaction = session.BeginTransaction()) |
| 89 | + { |
| 90 | + session.CreateQuery("delete from Entity").ExecuteUpdate(); |
| 91 | + transaction.Commit(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public async Task TestPreLoadedDataAsync() |
| 97 | + { |
| 98 | + using (var session = OpenSession()) |
| 99 | + using (var transaction = session.BeginTransaction()) |
| 100 | + { |
| 101 | + // Load the entities into the second-level cache. |
| 102 | + await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).ToListAsync()); |
| 103 | + |
| 104 | + var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync()); |
| 105 | + |
| 106 | + Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True); |
| 107 | + |
| 108 | + var field = result.Component?.Field; |
| 109 | + |
| 110 | + Assert.That(field, Is.EqualTo("Jim")); |
| 111 | + } |
| 112 | + |
| 113 | + using (var session = OpenSession()) |
| 114 | + using (var transaction = session.BeginTransaction()) |
| 115 | + { |
| 116 | + var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync()); |
| 117 | + |
| 118 | + Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True); |
| 119 | + |
| 120 | + var field = result.Component?.Field; |
| 121 | + |
| 122 | + Assert.That(field, Is.EqualTo("Jim")); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + [Test] |
| 127 | + public async Task InitializedLazyPropertyShouldBeCachedAsync() |
| 128 | + { |
| 129 | + using (var session = OpenSession()) |
| 130 | + using (var transaction = session.BeginTransaction()) |
| 131 | + { |
| 132 | + var e = await (session.GetAsync<Entity>(_id)); |
| 133 | + Assert.That(e.Component?.Field, Is.EqualTo("Jim")); |
| 134 | + |
| 135 | + Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True); |
| 136 | + await (transaction.CommitAsync()); |
| 137 | + } |
| 138 | + |
| 139 | + using (var session = OpenSession()) |
| 140 | + using (var transaction = session.BeginTransaction()) |
| 141 | + { |
| 142 | + var e = await (session.GetAsync<Entity>(_id)); |
| 143 | + |
| 144 | + Assert.That(NHibernateUtil.IsPropertyInitialized(e, "Component"), Is.True, "Lazy property is not cached"); |
| 145 | + var field = e.Component?.Field; |
| 146 | + |
| 147 | + Assert.That(field, Is.EqualTo("Jim")); |
| 148 | + await (transaction.CommitAsync()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + [Test] |
| 153 | + public async Task TestNonPreLoadedDataAsync() |
| 154 | + { |
| 155 | + using (var session = OpenSession()) |
| 156 | + using (var transaction = session.BeginTransaction()) |
| 157 | + { |
| 158 | + var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync()); |
| 159 | + |
| 160 | + Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True); |
| 161 | + |
| 162 | + var field = result.Component?.Field; |
| 163 | + |
| 164 | + Assert.That(field, Is.EqualTo("Jim")); |
| 165 | + } |
| 166 | + |
| 167 | + using (var session = OpenSession()) |
| 168 | + using (var transaction = session.BeginTransaction()) |
| 169 | + { |
| 170 | + var result = await (session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).FirstAsync()); |
| 171 | + |
| 172 | + Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True); |
| 173 | + |
| 174 | + var field = result.Component?.Field; |
| 175 | + |
| 176 | + Assert.That(field, Is.EqualTo("Jim")); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments