|
| 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.Criterion; |
| 13 | +using NHibernate.Transform; |
| 14 | +using NUnit.Framework; |
| 15 | +using NHibernate.Linq; |
| 16 | + |
| 17 | +namespace NHibernate.Test.NHSpecificTest.NH3787 |
| 18 | +{ |
| 19 | + using System.Threading.Tasks; |
| 20 | + [TestFixture] |
| 21 | + public class TestFixtureAsync : BugTestCase |
| 22 | + { |
| 23 | + private const decimal _testRate = 12345.123456789M; |
| 24 | + |
| 25 | + protected override void OnSetUp() |
| 26 | + { |
| 27 | + base.OnSetUp(); |
| 28 | + |
| 29 | + using (var s = OpenSession()) |
| 30 | + using (var t = s.BeginTransaction()) |
| 31 | + { |
| 32 | + var testEntity = new TestEntity |
| 33 | + { |
| 34 | + UsePreviousRate = true, |
| 35 | + PreviousRate = _testRate, |
| 36 | + Rate = 54321.123456789M |
| 37 | + }; |
| 38 | + s.Save(testEntity); |
| 39 | + t.Commit(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + protected override void OnTearDown() |
| 44 | + { |
| 45 | + using (var s = OpenSession()) |
| 46 | + using (var t = s.BeginTransaction()) |
| 47 | + { |
| 48 | + s.CreateQuery("delete from TestEntity").ExecuteUpdate(); |
| 49 | + t.Commit(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public async Task TestLinqQueryAsync() |
| 55 | + { |
| 56 | + using (var s = OpenSession()) |
| 57 | + using (var t = s.BeginTransaction()) |
| 58 | + { |
| 59 | + var queryResult = await (s |
| 60 | + .Query<TestEntity>() |
| 61 | + .Where(e => e.PreviousRate == _testRate) |
| 62 | + .ToListAsync()); |
| 63 | + |
| 64 | + Assert.That(queryResult.Count, Is.EqualTo(1)); |
| 65 | + Assert.That(queryResult[0].PreviousRate, Is.EqualTo(_testRate)); |
| 66 | + await (t.CommitAsync()); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public async Task TestLinqProjectionAsync() |
| 72 | + { |
| 73 | + using (var s = OpenSession()) |
| 74 | + using (var t = s.BeginTransaction()) |
| 75 | + { |
| 76 | + var queryResult = await ((from test in s.Query<TestEntity>() |
| 77 | + select new RateDto { Rate = test.UsePreviousRate ? test.PreviousRate : test.Rate }).ToListAsync()); |
| 78 | + |
| 79 | + // Check it has not been truncated to the default 5 positions of NHibernate. |
| 80 | + Assert.That(queryResult[0].Rate, Is.EqualTo(_testRate)); |
| 81 | + await (t.CommitAsync()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + [Test] |
| 86 | + public async Task TestLinqQueryOnExpressionAsync() |
| 87 | + { |
| 88 | + using (var s = OpenSession()) |
| 89 | + using (var t = s.BeginTransaction()) |
| 90 | + { |
| 91 | + var queryResult = await (s |
| 92 | + .Query<TestEntity>() |
| 93 | + .Where(e => (e.UsePreviousRate ? e.PreviousRate : e.Rate) == _testRate) |
| 94 | + .ToListAsync()); |
| 95 | + |
| 96 | + Assert.That(queryResult.Count, Is.EqualTo(1)); |
| 97 | + Assert.That(queryResult[0].PreviousRate, Is.EqualTo(_testRate)); |
| 98 | + await (t.CommitAsync()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + [Test] |
| 103 | + public async Task TestQueryOverProjectionAsync() |
| 104 | + { |
| 105 | + using (var s = OpenSession()) |
| 106 | + using (var t = s.BeginTransaction()) |
| 107 | + { |
| 108 | + TestEntity testEntity = null; |
| 109 | + |
| 110 | + var rateDto = new RateDto(); |
| 111 | + //Generated sql |
| 112 | + //exec sp_executesql N'SELECT (case when this_.UsePreviousRate = @p0 then this_.PreviousRate else this_.Rate end) as y0_ FROM [TestEntity] this_',N'@p0 bit',@p0=1 |
| 113 | + var query = s |
| 114 | + .QueryOver(() => testEntity) |
| 115 | + .Select( |
| 116 | + Projections.Alias( |
| 117 | + Projections.Conditional( |
| 118 | + Restrictions.Eq(Projections.Property(() => testEntity.UsePreviousRate), true), |
| 119 | + Projections.Property(() => testEntity.PreviousRate), |
| 120 | + Projections.Property(() => testEntity.Rate)), |
| 121 | + "Rate") |
| 122 | + .WithAlias(() => rateDto.Rate)); |
| 123 | + |
| 124 | + var queryResult = await (query.TransformUsing(Transformers.AliasToBean<RateDto>()).ListAsync<RateDto>()); |
| 125 | + |
| 126 | + Assert.That(queryResult[0].Rate, Is.EqualTo(_testRate)); |
| 127 | + await (t.CommitAsync()); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments