|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using NHibernate.Cfg; |
| 5 | +using NHibernate.SqlTypes; |
| 6 | +using NUnit.Framework; |
| 7 | +using Environment = NHibernate.Cfg.Environment; |
| 8 | + |
| 9 | +namespace NHibernate.Test.Linq |
| 10 | +{ |
| 11 | + [TestFixture(false, false)] |
| 12 | + [TestFixture(true, false)] |
| 13 | + [TestFixture(false, true)] |
| 14 | + public class PreEvaluationTests : LinqTestCase |
| 15 | + { |
| 16 | + private readonly bool LegacyPreEvaluation; |
| 17 | + private readonly bool FallbackOnPreEvaluation; |
| 18 | + |
| 19 | + public PreEvaluationTests(bool legacy, bool fallback) |
| 20 | + { |
| 21 | + LegacyPreEvaluation = legacy; |
| 22 | + FallbackOnPreEvaluation = fallback; |
| 23 | + } |
| 24 | + |
| 25 | + protected override void Configure(Configuration configuration) |
| 26 | + { |
| 27 | + base.Configure(configuration); |
| 28 | + |
| 29 | + configuration.SetProperty(Environment.FormatSql, "false"); |
| 30 | + configuration.SetProperty(Environment.LinqToHqlLegacyPreEvaluation, LegacyPreEvaluation.ToString()); |
| 31 | + configuration.SetProperty(Environment.LinqToHqlFallbackOnPreEvaluation, FallbackOnPreEvaluation.ToString()); |
| 32 | + } |
| 33 | + |
| 34 | + [Test] |
| 35 | + public void CanQueryByDateTimeNow() |
| 36 | + { |
| 37 | + var isSupported = IsFunctionSupported("current_timestamp"); |
| 38 | + RunTest( |
| 39 | + isSupported, |
| 40 | + spy => |
| 41 | + { |
| 42 | + var x = db.Orders.Count(o => o.OrderDate.Value < DateTime.Now); |
| 43 | + |
| 44 | + Assert.That(x, Is.GreaterThan(0)); |
| 45 | + AssertFunctionInSql("current_timestamp", spy); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + [Test] |
| 50 | + public void CanSelectDateTimeNow() |
| 51 | + { |
| 52 | + var isSupported = IsFunctionSupported("current_timestamp"); |
| 53 | + RunTest( |
| 54 | + isSupported, |
| 55 | + spy => |
| 56 | + { |
| 57 | + var x = |
| 58 | + db |
| 59 | + .Orders.Select(o => new { id = o.OrderId, d = DateTime.Now }) |
| 60 | + .OrderBy(o => o.id).Take(1).ToList(); |
| 61 | + |
| 62 | + Assert.That(x, Has.Count.GreaterThan(0)); |
| 63 | + AssertFunctionInSql("current_timestamp", spy); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + [Test] |
| 68 | + public void CanQueryByDateTimeUtcNow() |
| 69 | + { |
| 70 | + var isSupported = IsFunctionSupported("current_utctimestamp"); |
| 71 | + RunTest( |
| 72 | + isSupported, |
| 73 | + spy => |
| 74 | + { |
| 75 | + var x = db.Orders.Count(o => o.OrderDate.Value < DateTime.UtcNow); |
| 76 | + |
| 77 | + Assert.That(x, Is.GreaterThan(0)); |
| 78 | + AssertFunctionInSql("current_utctimestamp", spy); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + [Test] |
| 83 | + public void CanSelectDateTimeUtcNow() |
| 84 | + { |
| 85 | + var isSupported = IsFunctionSupported("current_utctimestamp"); |
| 86 | + RunTest( |
| 87 | + isSupported, |
| 88 | + spy => |
| 89 | + { |
| 90 | + var x = |
| 91 | + db |
| 92 | + .Orders.Select(o => new { id = o.OrderId, d = DateTime.UtcNow }) |
| 93 | + .OrderBy(o => o.id).Take(1).ToList(); |
| 94 | + |
| 95 | + Assert.That(x, Has.Count.GreaterThan(0)); |
| 96 | + AssertFunctionInSql("current_utctimestamp", spy); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + [Test] |
| 101 | + public void CanQueryByDateTimeToday() |
| 102 | + { |
| 103 | + var isSupported = IsFunctionSupported("current_date"); |
| 104 | + RunTest( |
| 105 | + isSupported, |
| 106 | + spy => |
| 107 | + { |
| 108 | + var x = db.Orders.Count(o => o.OrderDate.Value < DateTime.Today); |
| 109 | + |
| 110 | + Assert.That(x, Is.GreaterThan(0)); |
| 111 | + AssertFunctionInSql("current_date", spy); |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + [Test] |
| 116 | + public void CanSelectDateTimeToday() |
| 117 | + { |
| 118 | + var isSupported = IsFunctionSupported("current_date"); |
| 119 | + RunTest( |
| 120 | + isSupported, |
| 121 | + spy => |
| 122 | + { |
| 123 | + var x = |
| 124 | + db |
| 125 | + .Orders.Select(o => new { id = o.OrderId, d = DateTime.Today }) |
| 126 | + .OrderBy(o => o.id).Take(1).ToList(); |
| 127 | + |
| 128 | + Assert.That(x, Has.Count.GreaterThan(0)); |
| 129 | + AssertFunctionInSql("current_date", spy); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + [Test] |
| 134 | + public void CanQueryByDateTimeOffsetTimeNow() |
| 135 | + { |
| 136 | + if (!TestDialect.SupportsSqlType(SqlTypeFactory.DateTimeOffSet)) |
| 137 | + Assert.Ignore("Dialect does not support DateTimeOffSet"); |
| 138 | + |
| 139 | + var isSupported = IsFunctionSupported("current_timestamp_offset"); |
| 140 | + RunTest( |
| 141 | + isSupported, |
| 142 | + spy => |
| 143 | + { |
| 144 | + var testDate = DateTimeOffset.Now.AddDays(-1); |
| 145 | + var x = db.Orders.Count(o => testDate < DateTimeOffset.Now); |
| 146 | + |
| 147 | + Assert.That(x, Is.GreaterThan(0)); |
| 148 | + AssertFunctionInSql("current_timestamp_offset", spy); |
| 149 | + }); |
| 150 | + } |
| 151 | + |
| 152 | + [Test] |
| 153 | + public void CanSelectDateTimeOffsetNow() |
| 154 | + { |
| 155 | + if (!TestDialect.SupportsSqlType(SqlTypeFactory.DateTimeOffSet)) |
| 156 | + Assert.Ignore("Dialect does not support DateTimeOffSet"); |
| 157 | + |
| 158 | + var isSupported = IsFunctionSupported("current_timestamp_offset"); |
| 159 | + RunTest( |
| 160 | + isSupported, |
| 161 | + spy => |
| 162 | + { |
| 163 | + var x = |
| 164 | + db |
| 165 | + .Orders.Select(o => new { id = o.OrderId, d = DateTimeOffset.Now }) |
| 166 | + .OrderBy(o => o.id).Take(1).ToList(); |
| 167 | + |
| 168 | + Assert.That(x, Has.Count.GreaterThan(0)); |
| 169 | + AssertFunctionInSql("current_timestamp_offset", spy); |
| 170 | + }); |
| 171 | + } |
| 172 | + |
| 173 | + [Test] |
| 174 | + public void CanQueryByDateTimeOffsetUtcNow() |
| 175 | + { |
| 176 | + if (!TestDialect.SupportsSqlType(SqlTypeFactory.DateTimeOffSet)) |
| 177 | + Assert.Ignore("Dialect does not support DateTimeOffSet"); |
| 178 | + |
| 179 | + var isSupported = IsFunctionSupported("current_utctimestamp_offset"); |
| 180 | + RunTest( |
| 181 | + isSupported, |
| 182 | + spy => |
| 183 | + { |
| 184 | + var testDate = DateTimeOffset.UtcNow.AddDays(-1); |
| 185 | + var x = db.Orders.Count(o => testDate < DateTimeOffset.UtcNow); |
| 186 | + |
| 187 | + Assert.That(x, Is.GreaterThan(0)); |
| 188 | + AssertFunctionInSql("current_utctimestamp_offset", spy); |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + [Test] |
| 193 | + public void CanSelectDateTimeOffsetUtcNow() |
| 194 | + { |
| 195 | + if (!TestDialect.SupportsSqlType(SqlTypeFactory.DateTimeOffSet)) |
| 196 | + Assert.Ignore("Dialect does not support DateTimeOffSet"); |
| 197 | + |
| 198 | + var isSupported = IsFunctionSupported("current_utctimestamp_offset"); |
| 199 | + RunTest( |
| 200 | + isSupported, |
| 201 | + spy => |
| 202 | + { |
| 203 | + var x = |
| 204 | + db |
| 205 | + .Orders.Select(o => new { id = o.OrderId, d = DateTimeOffset.UtcNow }) |
| 206 | + .OrderBy(o => o.id).Take(1).ToList(); |
| 207 | + |
| 208 | + Assert.That(x, Has.Count.GreaterThan(0)); |
| 209 | + AssertFunctionInSql("current_utctimestamp_offset", spy); |
| 210 | + }); |
| 211 | + } |
| 212 | + |
| 213 | + private void RunTest(bool isSupported, Action<SqlLogSpy> test) |
| 214 | + { |
| 215 | + using (var spy = new SqlLogSpy()) |
| 216 | + { |
| 217 | + try |
| 218 | + { |
| 219 | + test(spy); |
| 220 | + } |
| 221 | + catch (QueryException) |
| 222 | + { |
| 223 | + if (!isSupported && !FallbackOnPreEvaluation) |
| 224 | + // Expected failure |
| 225 | + return; |
| 226 | + throw; |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + if (!isSupported && !FallbackOnPreEvaluation) |
| 231 | + Assert.Fail("The test should have thrown a QueryException, but has not thrown anything"); |
| 232 | + } |
| 233 | + |
| 234 | + private void AssertFunctionInSql(string functionName, SqlLogSpy spy) |
| 235 | + { |
| 236 | + if (!IsFunctionSupported(functionName)) |
| 237 | + Assert.Inconclusive($"{functionName} is not supported by the dialect"); |
| 238 | + |
| 239 | + var function = Dialect.Functions[functionName].Render(new List<object>(), Sfi).ToString(); |
| 240 | + |
| 241 | + if (LegacyPreEvaluation) |
| 242 | + Assert.That(spy.GetWholeLog(), Does.Not.Contain(function)); |
| 243 | + else |
| 244 | + Assert.That(spy.GetWholeLog(), Does.Contain(function)); |
| 245 | + } |
| 246 | + } |
| 247 | +} |
0 commit comments