|
| 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.Dialect; |
| 13 | +using NHibernate.Linq; |
| 14 | +using NSubstitute; |
| 15 | +using NSubstitute.Extensions; |
| 16 | +using NUnit.Framework; |
| 17 | + |
| 18 | +namespace NHibernate.Test.Linq |
| 19 | +{ |
| 20 | + using System.Threading.Tasks; |
| 21 | + [TestFixture] |
| 22 | + public class QueryPlanTestsAsync : LinqTestCase |
| 23 | + { |
| 24 | + [Test] |
| 25 | + public async Task SelectConstantShouldBeCachedAsync() |
| 26 | + { |
| 27 | + ClearQueryPlanCache(); |
| 28 | + |
| 29 | + var c1 = await (db.Customers.Select(o => new {o.CustomerId, Constant = "constant"}).FirstAsync()); |
| 30 | + var c2 = await (db.Customers.Select(o => new {o.CustomerId, Constant = "constant2"}).FirstAsync()); |
| 31 | + var constant = "constant3"; |
| 32 | + var c3 = await (db.Customers.Select(o => new {o.CustomerId, Constant = constant}).FirstAsync()); |
| 33 | + constant = "constant4"; |
| 34 | + var c4 = await (db.Customers.Select(o => new {o.CustomerId, Constant = constant}).FirstAsync()); |
| 35 | + |
| 36 | + var queryCache = GetQueryPlanCache(); |
| 37 | + Assert.That(queryCache.Count, Is.EqualTo(1)); |
| 38 | + |
| 39 | + Assert.That(c1.Constant, Is.EqualTo("constant")); |
| 40 | + Assert.That(c2.Constant, Is.EqualTo("constant2")); |
| 41 | + Assert.That(c3.Constant, Is.EqualTo("constant3")); |
| 42 | + Assert.That(c4.Constant, Is.EqualTo("constant4")); |
| 43 | + } |
| 44 | + |
| 45 | + [Test] |
| 46 | + public async Task GroupByConstantShouldBeCachedAsync() |
| 47 | + { |
| 48 | + ClearQueryPlanCache(); |
| 49 | + |
| 50 | + var c1 = await (db.Customers.GroupBy(o => new {o.CustomerId, Constant = "constant"}).Select(o => o.Key).FirstAsync()); |
| 51 | + var c2 = await (db.Customers.GroupBy(o => new {o.CustomerId, Constant = "constant2"}).Select(o => o.Key).FirstAsync()); |
| 52 | + var constant = "constant3"; |
| 53 | + var c3 = await (db.Customers.GroupBy(o => new {o.CustomerId, Constant = constant}).Select(o => o.Key).FirstAsync()); |
| 54 | + constant = "constant4"; |
| 55 | + var c4 = await (db.Customers.GroupBy(o => new {o.CustomerId, Constant = constant}).Select(o => o.Key).FirstAsync()); |
| 56 | + |
| 57 | + var queryCache = GetQueryPlanCache(); |
| 58 | + Assert.That(queryCache.Count, Is.EqualTo(1)); |
| 59 | + |
| 60 | + Assert.That(c1.Constant, Is.EqualTo("constant")); |
| 61 | + Assert.That(c2.Constant, Is.EqualTo("constant2")); |
| 62 | + Assert.That(c3.Constant, Is.EqualTo("constant3")); |
| 63 | + Assert.That(c4.Constant, Is.EqualTo("constant4")); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public async Task WithLockShouldBeCachedAsync() |
| 68 | + { |
| 69 | + ClearQueryPlanCache(); |
| 70 | + // Limit to a few dialects where we know the "nowait" keyword is used to make life easier. |
| 71 | + Assume.That(Dialect is MsSql2000Dialect || Dialect is Oracle8iDialect || Dialect is PostgreSQL81Dialect); |
| 72 | + |
| 73 | + await (db.Customers.WithLock(LockMode.Upgrade).ToListAsync()); |
| 74 | + await (db.Customers.WithLock(LockMode.UpgradeNoWait).ToListAsync()); |
| 75 | + var lockMode = LockMode.None; |
| 76 | + await (db.Customers.WithLock(lockMode).ToListAsync()); |
| 77 | + lockMode = LockMode.Read; |
| 78 | + await (db.Customers.WithLock(lockMode).ToListAsync()); |
| 79 | + |
| 80 | + var queryCache = GetQueryPlanCache(); |
| 81 | + Assert.That(queryCache.Count, Is.EqualTo(4)); |
| 82 | + } |
| 83 | + |
| 84 | + [TestCase(true)] |
| 85 | + [TestCase(false)] |
| 86 | + public async Task SkipShouldBeCachedAsync(bool supportsVariableLimit) |
| 87 | + { |
| 88 | + if (!Dialect.SupportsLimit || (supportsVariableLimit && !Dialect.SupportsVariableLimit)) |
| 89 | + { |
| 90 | + Assert.Ignore(); |
| 91 | + } |
| 92 | + |
| 93 | + ClearQueryPlanCache(); |
| 94 | + using (var substitute = SubstituteDialect()) |
| 95 | + { |
| 96 | + substitute.Value.Configure().SupportsVariableLimit.Returns(supportsVariableLimit); |
| 97 | + |
| 98 | + var c1 = await (db.Customers.Skip(1).ToListAsync()); |
| 99 | + var c2 = await (db.Customers.Skip(2).ToListAsync()); |
| 100 | + var skip = 3; |
| 101 | + var c3 = await (db.Customers.Skip(skip).ToListAsync()); |
| 102 | + skip = 4; |
| 103 | + var c4 = await (db.Customers.Skip(skip).ToListAsync()); |
| 104 | + |
| 105 | + var queryCache = GetQueryPlanCache(); |
| 106 | + Assert.That(c1.Count, Is.Not.EqualTo(c2.Count)); |
| 107 | + Assert.That(c2.Count, Is.Not.EqualTo(c3.Count)); |
| 108 | + Assert.That(c3.Count, Is.Not.EqualTo(c4.Count)); |
| 109 | + Assert.That(queryCache.Count, Is.EqualTo(supportsVariableLimit ? 1 : 4)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + [TestCase(true)] |
| 114 | + [TestCase(false)] |
| 115 | + public async Task TakeShouldBeCachedAsync(bool supportsVariableLimit) |
| 116 | + { |
| 117 | + if (!Dialect.SupportsLimit || (supportsVariableLimit && !Dialect.SupportsVariableLimit)) |
| 118 | + { |
| 119 | + Assert.Ignore(); |
| 120 | + } |
| 121 | + |
| 122 | + ClearQueryPlanCache(); |
| 123 | + using (var substitute = SubstituteDialect()) |
| 124 | + { |
| 125 | + substitute.Value.Configure().SupportsVariableLimit.Returns(supportsVariableLimit); |
| 126 | + |
| 127 | + var c1 = await (db.Customers.Take(1).ToListAsync()); |
| 128 | + var c2 = await (db.Customers.Take(2).ToListAsync()); |
| 129 | + var skip = 3; |
| 130 | + var c3 = await (db.Customers.Take(skip).ToListAsync()); |
| 131 | + skip = 4; |
| 132 | + var c4 = await (db.Customers.Take(skip).ToListAsync()); |
| 133 | + |
| 134 | + var queryCache = GetQueryPlanCache(); |
| 135 | + Assert.That(c1.Count, Is.EqualTo(1)); |
| 136 | + Assert.That(c2.Count, Is.EqualTo(2)); |
| 137 | + Assert.That(c3.Count, Is.EqualTo(3)); |
| 138 | + Assert.That(c4.Count, Is.EqualTo(4)); |
| 139 | + Assert.That(queryCache.Count, Is.EqualTo(supportsVariableLimit ? 1 : 4)); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + [Test] |
| 144 | + public async Task TrimFunctionShouldNotBeCachedAsync() |
| 145 | + { |
| 146 | + ClearQueryPlanCache(); |
| 147 | + |
| 148 | + await (db.Customers.Select(o => new {CustomerId = o.CustomerId.Trim('-')}).FirstAsync()); |
| 149 | + await (db.Customers.Select(o => new {CustomerId = o.CustomerId.Trim('+')}).FirstAsync()); |
| 150 | + |
| 151 | + var queryCache = GetQueryPlanCache(); |
| 152 | + Assert.That(queryCache.Count, Is.EqualTo(0)); |
| 153 | + } |
| 154 | + |
| 155 | + [Test] |
| 156 | + public async Task SubstringFunctionShouldBeCachedAsync() |
| 157 | + { |
| 158 | + ClearQueryPlanCache(); |
| 159 | + |
| 160 | + var queryCache = GetQueryPlanCache(); |
| 161 | + var c1 = await (db.Customers.Select(o => new {Name = o.ContactName.Substring(1)}).FirstAsync()); |
| 162 | + var c2 = await (db.Customers.Select(o => new {Name = o.ContactName.Substring(2)}).FirstAsync()); |
| 163 | + |
| 164 | + Assert.That(c1.Name, Is.Not.EqualTo(c2.Name)); |
| 165 | + Assert.That(queryCache.Count, Is.EqualTo(1)); |
| 166 | + |
| 167 | + ClearQueryPlanCache(); |
| 168 | + c1 = await (db.Customers.Select(o => new { Name = o.ContactName.Substring(1, 2) }).FirstAsync()); |
| 169 | + c2 = await (db.Customers.Select(o => new { Name = o.ContactName.Substring(2, 1) }).FirstAsync()); |
| 170 | + |
| 171 | + Assert.That(c1.Name, Is.Not.EqualTo(c2.Name)); |
| 172 | + Assert.That(queryCache.Count, Is.EqualTo(1)); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments