|
| 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 System.Collections.Generic; |
| 13 | +using System.Linq; |
| 14 | +using NHibernate.Cfg; |
| 15 | +using NHibernate.SqlTypes; |
| 16 | +using NUnit.Framework; |
| 17 | +using Environment = NHibernate.Cfg.Environment; |
| 18 | +using NHibernate.Linq; |
| 19 | + |
| 20 | +namespace NHibernate.Test.Linq |
| 21 | +{ |
| 22 | + using System.Threading.Tasks; |
| 23 | + [TestFixture(false, false)] |
| 24 | + [TestFixture(true, false)] |
| 25 | + [TestFixture(false, true)] |
| 26 | + public class PreEvaluationTestsAsync : LinqTestCase |
| 27 | + { |
| 28 | + private readonly bool LegacyPreEvaluation; |
| 29 | + private readonly bool FallbackOnPreEvaluation; |
| 30 | + |
| 31 | + public PreEvaluationTestsAsync(bool legacy, bool fallback) |
| 32 | + { |
| 33 | + LegacyPreEvaluation = legacy; |
| 34 | + FallbackOnPreEvaluation = fallback; |
| 35 | + } |
| 36 | + |
| 37 | + protected override void Configure(Configuration configuration) |
| 38 | + { |
| 39 | + base.Configure(configuration); |
| 40 | + |
| 41 | + configuration.SetProperty(Environment.FormatSql, "false"); |
| 42 | + configuration.SetProperty(Environment.LinqToHqlLegacyPreEvaluation, LegacyPreEvaluation.ToString()); |
| 43 | + configuration.SetProperty(Environment.LinqToHqlFallbackOnPreEvaluation, FallbackOnPreEvaluation.ToString()); |
| 44 | + } |
| 45 | + |
| 46 | + private void RunTest(bool isSupported, Action<SqlLogSpy> test) |
| 47 | + { |
| 48 | + using (var spy = new SqlLogSpy()) |
| 49 | + { |
| 50 | + try |
| 51 | + { |
| 52 | + test(spy); |
| 53 | + } |
| 54 | + catch (QueryException) |
| 55 | + { |
| 56 | + if (!isSupported && !FallbackOnPreEvaluation) |
| 57 | + // Expected failure |
| 58 | + return; |
| 59 | + throw; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (!isSupported && !FallbackOnPreEvaluation) |
| 64 | + Assert.Fail("The test should have thrown a QueryException, but has not thrown anything"); |
| 65 | + } |
| 66 | + |
| 67 | + [Test] |
| 68 | + public async Task CanQueryByRandomIntAsync() |
| 69 | + { |
| 70 | + var isSupported = IsFunctionSupported("random") && IsFunctionSupported("floor"); |
| 71 | + var idMin = await (db.Orders.MinAsync(o => o.OrderId)); |
| 72 | + RunTest( |
| 73 | + isSupported, |
| 74 | + spy => |
| 75 | + { |
| 76 | + var random = new Random(); |
| 77 | + // Dodge a Firebird driver limitation by putting the constants before the order id. |
| 78 | + // This driver cast parameters to their types in some cases for avoiding Firebird complaining of not |
| 79 | + // knowing the type of the condition. For some reasons the driver considers the casting should not be |
| 80 | + // done next to the conditional operator. Having the cast only on one side is enough for avoiding |
| 81 | + // Firebird complain, so moving the constants on the left side have been put before the order id, in |
| 82 | + // order for these constants to be casted by the driver. |
| 83 | + var x = db.Orders.Count(o => -idMin - 1 + o.OrderId < random.Next()); |
| 84 | + |
| 85 | + Assert.That(x, Is.GreaterThan(0)); |
| 86 | + // Next requires support of both floor and rand |
| 87 | + AssertFunctionInSql(IsFunctionSupported("floor") ? "random" : "floor", spy); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + [Test] |
| 92 | + public async Task CanQueryByRandomIntWithMaxAsync() |
| 93 | + { |
| 94 | + var isSupported = IsFunctionSupported("random") && IsFunctionSupported("floor"); |
| 95 | + var idMin = await (db.Orders.MinAsync(o => o.OrderId)); |
| 96 | + RunTest( |
| 97 | + isSupported, |
| 98 | + spy => |
| 99 | + { |
| 100 | + var random = new Random(); |
| 101 | + // Dodge a Firebird driver limitation by putting the constants before the order id. |
| 102 | + // This driver cast parameters to their types in some cases for avoiding Firebird complaining of not |
| 103 | + // knowing the type of the condition. For some reasons the driver considers the casting should not be |
| 104 | + // done next to the conditional operator. Having the cast only on one side is enough for avoiding |
| 105 | + // Firebird complain, so moving the constants on the left side have been put before the order id, in |
| 106 | + // order for these constants to be casted by the driver. |
| 107 | + var x = db.Orders.Count(o => -idMin + o.OrderId <= random.Next(10)); |
| 108 | + |
| 109 | + Assert.That(x, Is.GreaterThan(0).And.LessThan(11)); |
| 110 | + // Next requires support of both floor and rand |
| 111 | + AssertFunctionInSql(IsFunctionSupported("floor") ? "random" : "floor", spy); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + [Test] |
| 116 | + public async Task CanQueryByRandomIntWithMinMaxAsync() |
| 117 | + { |
| 118 | + var isSupported = IsFunctionSupported("random") && IsFunctionSupported("floor"); |
| 119 | + var idMin = await (db.Orders.MinAsync(o => o.OrderId)); |
| 120 | + RunTest( |
| 121 | + isSupported, |
| 122 | + spy => |
| 123 | + { |
| 124 | + var random = new Random(); |
| 125 | + // Dodge a Firebird driver limitation by putting the constants before the order id. |
| 126 | + // This driver cast parameters to their types in some cases for avoiding Firebird complaining of not |
| 127 | + // knowing the type of the condition. For some reasons the driver considers the casting should not be |
| 128 | + // done next to the conditional operator. Having the cast only on one side is enough for avoiding |
| 129 | + // Firebird complain, so moving the constants on the left side have been put before the order id, in |
| 130 | + // order for these constants to be casted by the driver. |
| 131 | + var x = db.Orders.Count(o => -idMin + o.OrderId < random.Next(1, 10)); |
| 132 | + |
| 133 | + Assert.That(x, Is.GreaterThan(0).And.LessThan(10)); |
| 134 | + // Next requires support of both floor and rand |
| 135 | + AssertFunctionInSql(IsFunctionSupported("floor") ? "random" : "floor", spy); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + private void AssertFunctionInSql(string functionName, SqlLogSpy spy) |
| 140 | + { |
| 141 | + if (!IsFunctionSupported(functionName)) |
| 142 | + Assert.Inconclusive($"{functionName} is not supported by the dialect"); |
| 143 | + |
| 144 | + var function = Dialect.Functions[functionName].Render(new List<object>(), Sfi).ToString(); |
| 145 | + |
| 146 | + if (LegacyPreEvaluation) |
| 147 | + Assert.That(spy.GetWholeLog(), Does.Not.Contain(function)); |
| 148 | + else |
| 149 | + Assert.That(spy.GetWholeLog(), Does.Contain(function)); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments