From 3a26ff9cf92eafbcc3c761fa35b85cd1cab0c3c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= Date: Wed, 17 Jan 2018 14:29:42 +0100 Subject: [PATCH 1/5] Fix some HQL functions registration * ceiling was not registered for Oracle, while it needs to be transcripted to ceil * ceil was registered as ceil for SQL-Server, which only supports ceiling * chr was wrongly registered as chr for SQL-Server, it needs to be registered as char --- src/NHibernate.Test/Async/Hql/HQLFunctions.cs | 45 +++++++++++++++++++ src/NHibernate.Test/Hql/HQLFunctions.cs | 45 +++++++++++++++++++ src/NHibernate/Dialect/MsSql2000Dialect.cs | 4 +- src/NHibernate/Dialect/Oracle8iDialect.cs | 1 + src/NHibernate/Dialect/OracleLiteDialect.cs | 1 + 5 files changed, 94 insertions(+), 2 deletions(-) diff --git a/src/NHibernate.Test/Async/Hql/HQLFunctions.cs b/src/NHibernate.Test/Async/Hql/HQLFunctions.cs index fb72118278d..4d7b5f494da 100644 --- a/src/NHibernate.Test/Async/Hql/HQLFunctions.cs +++ b/src/NHibernate.Test/Async/Hql/HQLFunctions.cs @@ -75,6 +75,7 @@ protected override void OnTearDown() { s.Delete("from Human"); s.Delete("from Animal"); + s.Delete("from MaterialResource"); s.Flush(); } } @@ -531,6 +532,28 @@ public async Task AbsAsync() } } + [Test] + public async Task CeilingAsync() + { + using (var s = OpenSession()) + { + var a1 = new Animal("a1", 1.3f); + await (s.SaveAsync(a1)); + await (s.FlushAsync()); + } + using (var s = OpenSession()) + { + var ceiling = await (s.CreateQuery("select ceiling(a.BodyWeight) from Animal a").UniqueResultAsync()); + Assert.That(ceiling, Is.EqualTo(2)); + var count = + await (s + .CreateQuery("select count(*) from Animal a where ceiling(a.BodyWeight) = :c") + .SetInt32("c", 2) + .UniqueResultAsync()); + Assert.That(count, Is.EqualTo(1)); + } + } + [Test] public async Task ModAsync() { @@ -637,6 +660,28 @@ public async Task LowerAsync() } } + [Test] + public async Task ChrAsync() + { + using (var s = OpenSession()) + { + var m = new MaterialResource("Blah", "000", (MaterialResource.MaterialState)32); + await (s.SaveAsync(m)); + await (s.FlushAsync()); + } + using (var s = OpenSession()) + { + var space = await (s.CreateQuery("select chr(m.State) from MaterialResource m").UniqueResultAsync()); + Assert.That(space, Is.EqualTo(' ')); + var count = + await (s + .CreateQuery("select count(*) from MaterialResource m where chr(m.State) = :c") + .SetCharacter("c", ' ') + .UniqueResultAsync()); + Assert.That(count, Is.EqualTo(1)); + } + } + [Test] public async Task CastAsync() { diff --git a/src/NHibernate.Test/Hql/HQLFunctions.cs b/src/NHibernate.Test/Hql/HQLFunctions.cs index 6588930bcc1..b71f9da103c 100644 --- a/src/NHibernate.Test/Hql/HQLFunctions.cs +++ b/src/NHibernate.Test/Hql/HQLFunctions.cs @@ -64,6 +64,7 @@ protected override void OnTearDown() { s.Delete("from Human"); s.Delete("from Animal"); + s.Delete("from MaterialResource"); s.Flush(); } } @@ -520,6 +521,28 @@ public void Abs() } } + [Test] + public void Ceiling() + { + using (var s = OpenSession()) + { + var a1 = new Animal("a1", 1.3f); + s.Save(a1); + s.Flush(); + } + using (var s = OpenSession()) + { + var ceiling = s.CreateQuery("select ceiling(a.BodyWeight) from Animal a").UniqueResult(); + Assert.That(ceiling, Is.EqualTo(2)); + var count = + s + .CreateQuery("select count(*) from Animal a where ceiling(a.BodyWeight) = :c") + .SetInt32("c", 2) + .UniqueResult(); + Assert.That(count, Is.EqualTo(1)); + } + } + [Test] public void Mod() { @@ -626,6 +649,28 @@ public void Lower() } } + [Test] + public void Chr() + { + using (var s = OpenSession()) + { + var m = new MaterialResource("Blah", "000", (MaterialResource.MaterialState)32); + s.Save(m); + s.Flush(); + } + using (var s = OpenSession()) + { + var space = s.CreateQuery("select chr(m.State) from MaterialResource m").UniqueResult(); + Assert.That(space, Is.EqualTo(' ')); + var count = + s + .CreateQuery("select count(*) from MaterialResource m where chr(m.State) = :c") + .SetCharacter("c", ' ') + .UniqueResult(); + Assert.That(count, Is.EqualTo(1)); + } + } + [Test] public void Cast() { diff --git a/src/NHibernate/Dialect/MsSql2000Dialect.cs b/src/NHibernate/Dialect/MsSql2000Dialect.cs index 213f7152e63..3f84b3c47f3 100644 --- a/src/NHibernate/Dialect/MsSql2000Dialect.cs +++ b/src/NHibernate/Dialect/MsSql2000Dialect.cs @@ -284,7 +284,7 @@ protected virtual void RegisterFunctions() RegisterFunction("sign", new StandardSQLFunction("sign", NHibernateUtil.Int32)); RegisterFunction("ceiling", new StandardSQLFunction("ceiling")); - RegisterFunction("ceil", new StandardSQLFunction("ceil")); + RegisterFunction("ceil", new StandardSQLFunction("ceiling")); RegisterFunction("floor", new StandardSQLFunction("floor")); RegisterFunction("round", new StandardSQLFunction("round")); @@ -326,7 +326,7 @@ protected virtual void RegisterFunctions() RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "dateadd(dd, 0, datediff(dd, 0, ?1))")); RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "+", ")")); RegisterFunction("digits", new StandardSQLFunction("digits", NHibernateUtil.String)); - RegisterFunction("chr", new StandardSQLFunction("chr", NHibernateUtil.Character)); + RegisterFunction("chr", new StandardSQLFunction("char", NHibernateUtil.Character)); RegisterFunction("upper", new StandardSQLFunction("upper")); RegisterFunction("ucase", new StandardSQLFunction("ucase")); RegisterFunction("lcase", new StandardSQLFunction("lcase")); diff --git a/src/NHibernate/Dialect/Oracle8iDialect.cs b/src/NHibernate/Dialect/Oracle8iDialect.cs index 45e15891cc9..321237c3a05 100644 --- a/src/NHibernate/Dialect/Oracle8iDialect.cs +++ b/src/NHibernate/Dialect/Oracle8iDialect.cs @@ -230,6 +230,7 @@ protected virtual void RegisterFunctions() RegisterFunction("round", new StandardSQLFunction("round")); RegisterFunction("trunc", new StandardSQLFunction("trunc")); RegisterFunction("ceil", new StandardSQLFunction("ceil")); + RegisterFunction("ceiling", new StandardSQLFunction("ceil")); RegisterFunction("floor", new StandardSQLFunction("floor")); RegisterFunction("chr", new StandardSQLFunction("chr", NHibernateUtil.Character)); diff --git a/src/NHibernate/Dialect/OracleLiteDialect.cs b/src/NHibernate/Dialect/OracleLiteDialect.cs index d0945678fe9..9b6259f36dc 100644 --- a/src/NHibernate/Dialect/OracleLiteDialect.cs +++ b/src/NHibernate/Dialect/OracleLiteDialect.cs @@ -69,6 +69,7 @@ public OracleLiteDialect() RegisterFunction("round", new StandardSQLFunction("round")); RegisterFunction("trunc", new StandardSQLFunction("trunc")); RegisterFunction("ceil", new StandardSQLFunction("ceil")); + RegisterFunction("ceiling", new StandardSQLFunction("ceil")); RegisterFunction("floor", new StandardSQLFunction("floor")); RegisterFunction("chr", new StandardSQLFunction("chr", NHibernateUtil.Character)); From fce95fe2f08b24f5f47c81427f3a323b59ed5d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= Date: Wed, 17 Jan 2018 19:03:28 +0100 Subject: [PATCH 2/5] More fixes. Fixes #837 * More registrations of ascii, chr, floor, ceiling, ceil --- src/NHibernate.Test/Async/Hql/HQLFunctions.cs | 30 ++++++++++++++++++- src/NHibernate.Test/Hql/HQLFunctions.cs | 30 ++++++++++++++++++- src/NHibernate/Dialect/DB2Dialect.cs | 1 + src/NHibernate/Dialect/FirebirdDialect.cs | 3 ++ src/NHibernate/Dialect/MsSql2000Dialect.cs | 1 + src/NHibernate/Dialect/PostgreSQLDialect.cs | 6 ++++ src/NHibernate/Dialect/SQLiteDialect.cs | 1 + 7 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/NHibernate.Test/Async/Hql/HQLFunctions.cs b/src/NHibernate.Test/Async/Hql/HQLFunctions.cs index 4d7b5f494da..d85c942d21b 100644 --- a/src/NHibernate.Test/Async/Hql/HQLFunctions.cs +++ b/src/NHibernate.Test/Async/Hql/HQLFunctions.cs @@ -535,6 +535,8 @@ public async Task AbsAsync() [Test] public async Task CeilingAsync() { + Assume.That(Dialect.Functions.ContainsKey("ceiling"), Is.True, Dialect + " doesn't support ceiling function."); + using (var s = OpenSession()) { var a1 = new Animal("a1", 1.3f); @@ -660,9 +662,35 @@ public async Task LowerAsync() } } + [Test] + public async Task AsciiAsync() + { + Assume.That(Dialect.Functions.ContainsKey("ascii"), Is.True, Dialect + " doesn't support ascii function."); + + using (var s = OpenSession()) + { + var m = new MaterialResource(" ", "000", MaterialResource.MaterialState.Available); + await (s.SaveAsync(m)); + await (s.FlushAsync()); + } + using (var s = OpenSession()) + { + var space = await (s.CreateQuery("select ascii(m.Description) from MaterialResource m").UniqueResultAsync()); + Assert.That(space, Is.EqualTo(32)); + var count = + await (s + .CreateQuery("select count(*) from MaterialResource m where ascii(m.Description) = :c") + .SetInt32("c", 32) + .UniqueResultAsync()); + Assert.That(count, Is.EqualTo(1)); + } + } + [Test] public async Task ChrAsync() { + Assume.That(Dialect.Functions.ContainsKey("chr"), Is.True, Dialect + " doesn't support chr function."); + using (var s = OpenSession()) { var m = new MaterialResource("Blah", "000", (MaterialResource.MaterialState)32); @@ -1037,7 +1065,7 @@ public async Task StrAsync() public async Task IifAsync() { if (!Dialect.Functions.ContainsKey("iif")) - Assert.Ignore(Dialect + "doesn't support iif function."); + Assert.Ignore(Dialect + " doesn't support iif function."); using (ISession s = OpenSession()) { await (s.SaveAsync(new MaterialResource("Flash card 512MB", "A001/07", MaterialResource.MaterialState.Available))); diff --git a/src/NHibernate.Test/Hql/HQLFunctions.cs b/src/NHibernate.Test/Hql/HQLFunctions.cs index b71f9da103c..365b7baf96e 100644 --- a/src/NHibernate.Test/Hql/HQLFunctions.cs +++ b/src/NHibernate.Test/Hql/HQLFunctions.cs @@ -524,6 +524,8 @@ public void Abs() [Test] public void Ceiling() { + Assume.That(Dialect.Functions.ContainsKey("ceiling"), Is.True, Dialect + " doesn't support ceiling function."); + using (var s = OpenSession()) { var a1 = new Animal("a1", 1.3f); @@ -649,9 +651,35 @@ public void Lower() } } + [Test] + public void Ascii() + { + Assume.That(Dialect.Functions.ContainsKey("ascii"), Is.True, Dialect + " doesn't support ascii function."); + + using (var s = OpenSession()) + { + var m = new MaterialResource(" ", "000", MaterialResource.MaterialState.Available); + s.Save(m); + s.Flush(); + } + using (var s = OpenSession()) + { + var space = s.CreateQuery("select ascii(m.Description) from MaterialResource m").UniqueResult(); + Assert.That(space, Is.EqualTo(32)); + var count = + s + .CreateQuery("select count(*) from MaterialResource m where ascii(m.Description) = :c") + .SetInt32("c", 32) + .UniqueResult(); + Assert.That(count, Is.EqualTo(1)); + } + } + [Test] public void Chr() { + Assume.That(Dialect.Functions.ContainsKey("chr"), Is.True, Dialect + " doesn't support chr function."); + using (var s = OpenSession()) { var m = new MaterialResource("Blah", "000", (MaterialResource.MaterialState)32); @@ -1026,7 +1054,7 @@ public void Str() public void Iif() { if (!Dialect.Functions.ContainsKey("iif")) - Assert.Ignore(Dialect + "doesn't support iif function."); + Assert.Ignore(Dialect + " doesn't support iif function."); using (ISession s = OpenSession()) { s.Save(new MaterialResource("Flash card 512MB", "A001/07", MaterialResource.MaterialState.Available)); diff --git a/src/NHibernate/Dialect/DB2Dialect.cs b/src/NHibernate/Dialect/DB2Dialect.cs index b9b785a2025..3bcd3f01da2 100644 --- a/src/NHibernate/Dialect/DB2Dialect.cs +++ b/src/NHibernate/Dialect/DB2Dialect.cs @@ -115,6 +115,7 @@ public DB2Dialect() RegisterFunction("smallint", new StandardSQLFunction("smallint", NHibernateUtil.Int16)); RegisterFunction("digits", new StandardSQLFunction("digits", NHibernateUtil.String)); + RegisterFunction("ascii", new StandardSQLFunction("ascii", NHibernateUtil.Int32)); RegisterFunction("chr", new StandardSQLFunction("chr", NHibernateUtil.Character)); RegisterFunction("upper", new StandardSQLFunction("upper")); RegisterFunction("ucase", new StandardSQLFunction("ucase")); diff --git a/src/NHibernate/Dialect/FirebirdDialect.cs b/src/NHibernate/Dialect/FirebirdDialect.cs index e84436b5a08..4e7888b155f 100644 --- a/src/NHibernate/Dialect/FirebirdDialect.cs +++ b/src/NHibernate/Dialect/FirebirdDialect.cs @@ -454,6 +454,7 @@ private void RegisterMathematicalFunctions() { RegisterFunction("abs", new StandardSQLFunction("abs", NHibernateUtil.Double)); RegisterFunction("ceiling", new StandardSQLFunction("ceiling", NHibernateUtil.Double)); + RegisterFunction("ceil", new StandardSQLFunction("ceil", NHibernateUtil.Double)); RegisterFunction("div", new StandardSQLFunction("div", NHibernateUtil.Double)); RegisterFunction("dpower", new StandardSQLFunction("dpower", NHibernateUtil.Double)); RegisterFunction("ln", new StandardSQLFunction("ln", NHibernateUtil.Double)); @@ -486,7 +487,9 @@ private void RegisterDateTimeFunctions() private void RegisterStringAndCharFunctions() { RegisterFunction("ascii_char", new StandardSQLFunction("ascii_char")); + RegisterFunction("chr", new StandardSQLFunction("ascii_char")); RegisterFunction("ascii_val", new StandardSQLFunction("ascii_val", NHibernateUtil.Int16)); + RegisterFunction("ascii", new StandardSQLFunction("ascii_val", NHibernateUtil.Int16)); RegisterFunction("lpad", new StandardSQLFunction("lpad")); RegisterFunction("ltrim", new StandardSQLFunction("ltrim")); RegisterFunction("sright", new StandardSQLFunction("sright")); diff --git a/src/NHibernate/Dialect/MsSql2000Dialect.cs b/src/NHibernate/Dialect/MsSql2000Dialect.cs index 3f84b3c47f3..7681e988d39 100644 --- a/src/NHibernate/Dialect/MsSql2000Dialect.cs +++ b/src/NHibernate/Dialect/MsSql2000Dialect.cs @@ -326,6 +326,7 @@ protected virtual void RegisterFunctions() RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "dateadd(dd, 0, datediff(dd, 0, ?1))")); RegisterFunction("concat", new VarArgsSQLFunction(NHibernateUtil.String, "(", "+", ")")); RegisterFunction("digits", new StandardSQLFunction("digits", NHibernateUtil.String)); + RegisterFunction("ascii", new StandardSQLFunction("ascii", NHibernateUtil.Int32)); RegisterFunction("chr", new StandardSQLFunction("char", NHibernateUtil.Character)); RegisterFunction("upper", new StandardSQLFunction("upper")); RegisterFunction("ucase", new StandardSQLFunction("ucase")); diff --git a/src/NHibernate/Dialect/PostgreSQLDialect.cs b/src/NHibernate/Dialect/PostgreSQLDialect.cs index cb1f5086ab2..3ddc692c767 100644 --- a/src/NHibernate/Dialect/PostgreSQLDialect.cs +++ b/src/NHibernate/Dialect/PostgreSQLDialect.cs @@ -79,6 +79,12 @@ public PostgreSQLDialect() RegisterFunction("power", new StandardSQLFunction("power", NHibernateUtil.Double)); + RegisterFunction("floor", new StandardSQLFunction("floor")); + RegisterFunction("ceiling", new StandardSQLFunction("ceiling")); + RegisterFunction("ceil", new StandardSQLFunction("ceil")); + RegisterFunction("chr", new StandardSQLFunction("chr", NHibernateUtil.Character)); + RegisterFunction("ascii", new StandardSQLFunction("ascii", NHibernateUtil.Int32)); + // Register the date function, since when used in LINQ select clauses, NH must know the data type. RegisterFunction("date", new SQLFunctionTemplate(NHibernateUtil.Date, "cast(?1 as date)")); diff --git a/src/NHibernate/Dialect/SQLiteDialect.cs b/src/NHibernate/Dialect/SQLiteDialect.cs index 48236f82949..87904958221 100644 --- a/src/NHibernate/Dialect/SQLiteDialect.cs +++ b/src/NHibernate/Dialect/SQLiteDialect.cs @@ -78,6 +78,7 @@ protected virtual void RegisterFunctions() RegisterFunction("left", new SQLFunctionTemplate(NHibernateUtil.String, "substr(?1,1,?2)")); RegisterFunction("trim", new AnsiTrimEmulationFunction()); RegisterFunction("replace", new StandardSafeSQLFunction("replace", NHibernateUtil.String, 3)); + RegisterFunction("chr", new StandardSQLFunction("char", NHibernateUtil.Character)); RegisterFunction("mod", new SQLFunctionTemplate(NHibernateUtil.Int32, "((?1) % (?2))")); From a176f7c4a1756fa84e2242ce10d608b22fd04373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= Date: Thu, 18 Jan 2018 14:23:48 +0100 Subject: [PATCH 3/5] Fix registrations in MySQL and Firebird --- src/NHibernate/Dialect/FirebirdDialect.cs | 14 +++++++------- src/NHibernate/Dialect/MySQLDialect.cs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/NHibernate/Dialect/FirebirdDialect.cs b/src/NHibernate/Dialect/FirebirdDialect.cs index 4e7888b155f..5fa709e15c9 100644 --- a/src/NHibernate/Dialect/FirebirdDialect.cs +++ b/src/NHibernate/Dialect/FirebirdDialect.cs @@ -453,8 +453,8 @@ private void RegisterExternalFbAndIbStandardUDFs() private void RegisterMathematicalFunctions() { RegisterFunction("abs", new StandardSQLFunction("abs", NHibernateUtil.Double)); - RegisterFunction("ceiling", new StandardSQLFunction("ceiling", NHibernateUtil.Double)); - RegisterFunction("ceil", new StandardSQLFunction("ceil", NHibernateUtil.Double)); + RegisterFunction("ceiling", new StandardSQLFunction("ceiling")); + RegisterFunction("ceil", new StandardSQLFunction("ceil")); RegisterFunction("div", new StandardSQLFunction("div", NHibernateUtil.Double)); RegisterFunction("dpower", new StandardSQLFunction("dpower", NHibernateUtil.Double)); RegisterFunction("ln", new StandardSQLFunction("ln", NHibernateUtil.Double)); @@ -465,7 +465,7 @@ private void RegisterMathematicalFunctions() RegisterFunction("sign", new StandardSQLFunction("sign", NHibernateUtil.Int32)); RegisterFunction("sqtr", new StandardSQLFunction("sqtr", NHibernateUtil.Double)); RegisterFunction("truncate", new StandardSQLFunction("truncate")); - RegisterFunction("floor", new StandardSafeSQLFunction("floor", NHibernateUtil.Double, 1)); + RegisterFunction("floor", new StandardSQLFunction("floor")); RegisterFunction("round", new StandardSQLFunction("round")); } @@ -486,10 +486,10 @@ private void RegisterDateTimeFunctions() private void RegisterStringAndCharFunctions() { - RegisterFunction("ascii_char", new StandardSQLFunction("ascii_char")); - RegisterFunction("chr", new StandardSQLFunction("ascii_char")); - RegisterFunction("ascii_val", new StandardSQLFunction("ascii_val", NHibernateUtil.Int16)); - RegisterFunction("ascii", new StandardSQLFunction("ascii_val", NHibernateUtil.Int16)); + RegisterFunction("ascii_char", new StandardSQLFunction("ascii_char", NHibernateUtil.Character)); + RegisterFunction("chr", new StandardSQLFunction("ascii_char", NHibernateUtil.Character)); + RegisterFunction("ascii_val", new StandardSQLFunction("ascii_val", NHibernateUtil.Int32)); + RegisterFunction("ascii", new StandardSQLFunction("ascii_val", NHibernateUtil.Int32)); RegisterFunction("lpad", new StandardSQLFunction("lpad")); RegisterFunction("ltrim", new StandardSQLFunction("ltrim")); RegisterFunction("sright", new StandardSQLFunction("sright")); diff --git a/src/NHibernate/Dialect/MySQLDialect.cs b/src/NHibernate/Dialect/MySQLDialect.cs index fb913a92320..f38759e7964 100644 --- a/src/NHibernate/Dialect/MySQLDialect.cs +++ b/src/NHibernate/Dialect/MySQLDialect.cs @@ -284,7 +284,7 @@ protected virtual void RegisterFunctions() RegisterFunction("ucase", new StandardSQLFunction("ucase")); RegisterFunction("lcase", new StandardSQLFunction("lcase")); - RegisterFunction("chr", new StandardSQLFunction("char", NHibernateUtil.Character)); + RegisterFunction("chr", new SQLFunctionTemplate(NHibernateUtil.Character, "cast(char(?1) as char)")); RegisterFunction("ascii", new StandardSQLFunction("ascii", NHibernateUtil.Int32)); RegisterFunction("instr", new StandardSQLFunction("instr", NHibernateUtil.Int32)); RegisterFunction("lpad", new StandardSQLFunction("lpad", NHibernateUtil.String)); From d8d86d604cb806e0fe7297a30f36980bcb010abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= Date: Thu, 18 Jan 2018 14:24:31 +0100 Subject: [PATCH 4/5] Consolidate function support test logic in HQLFunctions --- src/NHibernate.Test/Async/Hql/HQLFunctions.cs | 128 +++++++++--------- src/NHibernate.Test/Hql/HQLFunctions.cs | 128 +++++++++--------- 2 files changed, 126 insertions(+), 130 deletions(-) diff --git a/src/NHibernate.Test/Async/Hql/HQLFunctions.cs b/src/NHibernate.Test/Async/Hql/HQLFunctions.cs index d85c942d21b..b1110f70260 100644 --- a/src/NHibernate.Test/Async/Hql/HQLFunctions.cs +++ b/src/NHibernate.Test/Async/Hql/HQLFunctions.cs @@ -10,6 +10,7 @@ using System; using System.Collections; +using System.Collections.Generic; using NHibernate.Dialect; using NUnit.Framework; @@ -23,40 +24,40 @@ namespace NHibernate.Test.Hql [TestFixture] public class HQLFunctionsAsync : TestCase { - static readonly Hashtable notSupportedStandardFunction; + private static readonly Dictionary> DialectsNotSupportingStandardFunction; + static HQLFunctionsAsync() { - notSupportedStandardFunction = - new Hashtable + DialectsNotSupportingStandardFunction = + new Dictionary> + { + {"locate", new HashSet {typeof (SQLiteDialect)}}, + {"bit_length", new HashSet {typeof (SQLiteDialect)}}, + {"extract", new HashSet {typeof (SQLiteDialect)}}, { - {"locate", new[] {typeof (SQLiteDialect)}}, - {"bit_length", new[] {typeof (SQLiteDialect)}}, - {"extract", new[] {typeof (SQLiteDialect)}}, + "nullif", + new HashSet { - "nullif", - new[] - { - typeof (Oracle8iDialect), - // Actually not supported by the db engine. (Well, could likely still be done with a case when override.) - typeof (MsSqlCeDialect), - typeof (MsSqlCe40Dialect) - }} - }; - } - - private bool IsOracleDialect() - { - return Dialect is Oracle8iDialect; + // Actually not supported by the db engine. (Well, could likely still be done with a case when override.) + typeof (MsSqlCeDialect), + typeof (MsSqlCe40Dialect) + }} + }; } - private void IgnoreIfNotSupported(string functionName) + private void AssumeSupported(string functionName) { - if (notSupportedStandardFunction.ContainsKey(functionName)) - { - IList dialects = (IList)notSupportedStandardFunction[functionName]; - if(dialects.Contains(Dialect.GetType())) - Assert.Ignore(Dialect + " doesn't support "+functionName+" function."); - } + Assume.That( + Sfi.SQLFunctionRegistry.HasFunction(functionName), + Is.True, + $"{Dialect} doesn't support {functionName} function."); + + if (!DialectsNotSupportingStandardFunction.TryGetValue(functionName, out var dialects)) + return; + Assume.That( + dialects, + Does.Not.Contain(Dialect.GetType()), + $"{Dialect} doesn't support {functionName} standard function."); } protected override string MappingsAssembly @@ -256,7 +257,7 @@ public async Task SubStringTwoParametersAsync() // the two-parameter overload - emulating it by generating the // third parameter (length) if the database requires three parameters. - IgnoreIfNotSupported("substring"); + AssumeSupported("substring"); using (ISession s = OpenSession()) { @@ -294,7 +295,7 @@ public async Task SubStringTwoParametersAsync() [Test] public async Task SubStringAsync() { - IgnoreIfNotSupported("substring"); + AssumeSupported("substring"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -346,7 +347,7 @@ public async Task SubStringAsync() [Test] public async Task LocateAsync() { - IgnoreIfNotSupported("locate"); + AssumeSupported("locate"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -368,7 +369,7 @@ public async Task LocateAsync() [Test] public async Task TrimAsync() { - IgnoreIfNotSupported("trim"); + AssumeSupported("trim"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abc ", 1); @@ -426,7 +427,7 @@ public async Task TrimAsync() [Test] public async Task LengthAsync() { - IgnoreIfNotSupported("length"); + AssumeSupported("length"); using (ISession s = OpenSession()) { @@ -451,7 +452,7 @@ public async Task LengthAsync() [Test] public async Task Bit_lengthAsync() { - IgnoreIfNotSupported("bit_length"); + AssumeSupported("bit_length"); // test only the parser using (ISession s = OpenSession()) @@ -467,7 +468,7 @@ public async Task Bit_lengthAsync() [Test] public async Task CoalesceAsync() { - IgnoreIfNotSupported("coalesce"); + AssumeSupported("coalesce"); // test only the parser and render using (ISession s = OpenSession()) { @@ -482,9 +483,9 @@ public async Task CoalesceAsync() [Test] public async Task NullifAsync() { - IgnoreIfNotSupported("nullif"); + AssumeSupported("nullif"); string hql1, hql2; - if(!IsOracleDialect()) + if(!(Dialect is Oracle8iDialect)) { hql1 = "select nullif(h.NickName, '1e1') from Human h"; hql2 = "from Human h where not(nullif(h.NickName, '1e1') is null)"; @@ -506,7 +507,7 @@ public async Task NullifAsync() [Test] public async Task AbsAsync() { - IgnoreIfNotSupported("abs"); + AssumeSupported("abs"); using (ISession s = OpenSession()) { Animal a1 = new Animal("Dog", 9); @@ -535,7 +536,7 @@ public async Task AbsAsync() [Test] public async Task CeilingAsync() { - Assume.That(Dialect.Functions.ContainsKey("ceiling"), Is.True, Dialect + " doesn't support ceiling function."); + AssumeSupported("ceiling"); using (var s = OpenSession()) { @@ -559,7 +560,7 @@ public async Task CeilingAsync() [Test] public async Task ModAsync() { - IgnoreIfNotSupported("mod"); + AssumeSupported("mod"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -585,7 +586,7 @@ public async Task ModAsync() [Test] public async Task SqrtAsync() { - IgnoreIfNotSupported("sqrt"); + AssumeSupported("sqrt"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 65536f); @@ -607,7 +608,7 @@ public async Task SqrtAsync() [Test] public async Task UpperAsync() { - IgnoreIfNotSupported("upper"); + AssumeSupported("upper"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1f); @@ -636,7 +637,7 @@ public async Task UpperAsync() [Test] public async Task LowerAsync() { - IgnoreIfNotSupported("lower"); + AssumeSupported("lower"); using (ISession s = OpenSession()) { Animal a1 = new Animal("ABCDEF", 1f); @@ -665,7 +666,7 @@ public async Task LowerAsync() [Test] public async Task AsciiAsync() { - Assume.That(Dialect.Functions.ContainsKey("ascii"), Is.True, Dialect + " doesn't support ascii function."); + AssumeSupported("ascii"); using (var s = OpenSession()) { @@ -689,7 +690,7 @@ public async Task AsciiAsync() [Test] public async Task ChrAsync() { - Assume.That(Dialect.Functions.ContainsKey("chr"), Is.True, Dialect + " doesn't support chr function."); + AssumeSupported("chr"); using (var s = OpenSession()) { @@ -715,7 +716,7 @@ public async Task CastAsync() { const double magicResult = 7 + 123 - 5*1.3d; - IgnoreIfNotSupported("cast"); + AssumeSupported("cast"); // The cast is used to test various cases of a function render // Cast was selected because represent a special case for: // 1) Has more then 1 argument @@ -893,7 +894,7 @@ public async Task CastAsync() [Test] public async Task CastNH1446Async() { - IgnoreIfNotSupported("cast"); + AssumeSupported("cast"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -913,7 +914,7 @@ public async Task CastNH1446Async() [Test] public async Task CastNH1979Async() { - IgnoreIfNotSupported("cast"); + AssumeSupported("cast"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -931,7 +932,7 @@ public async Task CastNH1979Async() [Test] public async Task Current_TimeStampAsync() { - IgnoreIfNotSupported("current_timestamp"); + AssumeSupported("current_timestamp"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -951,9 +952,7 @@ public async Task Current_TimeStampAsync() [Test] public async Task Current_TimeStamp_OffsetAsync() { - if (!Dialect.Functions.ContainsKey("current_timestamp_offset")) - Assert.Ignore(Dialect + " doesn't support current_timestamp_offset function"); - IgnoreIfNotSupported("current_timestamp_offset"); + AssumeSupported("current_timestamp_offset"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -970,8 +969,8 @@ public async Task Current_TimeStamp_OffsetAsync() [Test] public async Task ExtractAsync() { - IgnoreIfNotSupported("extract"); - IgnoreIfNotSupported("current_timestamp"); + AssumeSupported("extract"); + AssumeSupported("current_timestamp"); // test only the parser and render using (ISession s = OpenSession()) @@ -987,7 +986,7 @@ public async Task ExtractAsync() [Test] public async Task ConcatAsync() { - IgnoreIfNotSupported("concat"); + AssumeSupported("concat"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1f); @@ -1013,10 +1012,10 @@ public async Task ConcatAsync() [Test] public async Task HourMinuteSecondAsync() { - IgnoreIfNotSupported("second"); - IgnoreIfNotSupported("minute"); - IgnoreIfNotSupported("hour"); - IgnoreIfNotSupported("current_timestamp"); + AssumeSupported("second"); + AssumeSupported("minute"); + AssumeSupported("hour"); + AssumeSupported("current_timestamp"); // test only the parser and render using (ISession s = OpenSession()) { @@ -1028,9 +1027,9 @@ public async Task HourMinuteSecondAsync() [Test] public async Task DayMonthYearAsync() { - IgnoreIfNotSupported("day"); - IgnoreIfNotSupported("month"); - IgnoreIfNotSupported("year"); + AssumeSupported("day"); + AssumeSupported("month"); + AssumeSupported("year"); // test only the parser and render using (ISession s = OpenSession()) { @@ -1042,7 +1041,7 @@ public async Task DayMonthYearAsync() [Test] public async Task StrAsync() { - IgnoreIfNotSupported("str"); + AssumeSupported("str"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -1064,8 +1063,7 @@ public async Task StrAsync() [Test] public async Task IifAsync() { - if (!Dialect.Functions.ContainsKey("iif")) - Assert.Ignore(Dialect + " doesn't support iif function."); + AssumeSupported("Iif"); using (ISession s = OpenSession()) { await (s.SaveAsync(new MaterialResource("Flash card 512MB", "A001/07", MaterialResource.MaterialState.Available))); @@ -1108,7 +1106,7 @@ from MaterialResource mr [Test] public async Task NH1725Async() { - IgnoreIfNotSupported("iif"); + AssumeSupported("iif"); // Only to test the parser using (ISession s = OpenSession()) { diff --git a/src/NHibernate.Test/Hql/HQLFunctions.cs b/src/NHibernate.Test/Hql/HQLFunctions.cs index 365b7baf96e..2ae5ff2b0b6 100644 --- a/src/NHibernate.Test/Hql/HQLFunctions.cs +++ b/src/NHibernate.Test/Hql/HQLFunctions.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; using NHibernate.Dialect; using NUnit.Framework; @@ -12,40 +13,40 @@ namespace NHibernate.Test.Hql [TestFixture] public class HQLFunctions : TestCase { - static readonly Hashtable notSupportedStandardFunction; + private static readonly Dictionary> DialectsNotSupportingStandardFunction; + static HQLFunctions() { - notSupportedStandardFunction = - new Hashtable + DialectsNotSupportingStandardFunction = + new Dictionary> + { + {"locate", new HashSet {typeof (SQLiteDialect)}}, + {"bit_length", new HashSet {typeof (SQLiteDialect)}}, + {"extract", new HashSet {typeof (SQLiteDialect)}}, { - {"locate", new[] {typeof (SQLiteDialect)}}, - {"bit_length", new[] {typeof (SQLiteDialect)}}, - {"extract", new[] {typeof (SQLiteDialect)}}, + "nullif", + new HashSet { - "nullif", - new[] - { - typeof (Oracle8iDialect), - // Actually not supported by the db engine. (Well, could likely still be done with a case when override.) - typeof (MsSqlCeDialect), - typeof (MsSqlCe40Dialect) - }} - }; - } - - private bool IsOracleDialect() - { - return Dialect is Oracle8iDialect; + // Actually not supported by the db engine. (Well, could likely still be done with a case when override.) + typeof (MsSqlCeDialect), + typeof (MsSqlCe40Dialect) + }} + }; } - private void IgnoreIfNotSupported(string functionName) + private void AssumeSupported(string functionName) { - if (notSupportedStandardFunction.ContainsKey(functionName)) - { - IList dialects = (IList)notSupportedStandardFunction[functionName]; - if(dialects.Contains(Dialect.GetType())) - Assert.Ignore(Dialect + " doesn't support "+functionName+" function."); - } + Assume.That( + Sfi.SQLFunctionRegistry.HasFunction(functionName), + Is.True, + $"{Dialect} doesn't support {functionName} function."); + + if (!DialectsNotSupportingStandardFunction.TryGetValue(functionName, out var dialects)) + return; + Assume.That( + dialects, + Does.Not.Contain(Dialect.GetType()), + $"{Dialect} doesn't support {functionName} standard function."); } protected override string MappingsAssembly @@ -245,7 +246,7 @@ public void SubStringTwoParameters() // the two-parameter overload - emulating it by generating the // third parameter (length) if the database requires three parameters. - IgnoreIfNotSupported("substring"); + AssumeSupported("substring"); using (ISession s = OpenSession()) { @@ -283,7 +284,7 @@ public void SubStringTwoParameters() [Test] public void SubString() { - IgnoreIfNotSupported("substring"); + AssumeSupported("substring"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -335,7 +336,7 @@ public void SubString() [Test] public void Locate() { - IgnoreIfNotSupported("locate"); + AssumeSupported("locate"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -357,7 +358,7 @@ public void Locate() [Test] public void Trim() { - IgnoreIfNotSupported("trim"); + AssumeSupported("trim"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abc ", 1); @@ -415,7 +416,7 @@ public void Trim() [Test] public void Length() { - IgnoreIfNotSupported("length"); + AssumeSupported("length"); using (ISession s = OpenSession()) { @@ -440,7 +441,7 @@ public void Length() [Test] public void Bit_length() { - IgnoreIfNotSupported("bit_length"); + AssumeSupported("bit_length"); // test only the parser using (ISession s = OpenSession()) @@ -456,7 +457,7 @@ public void Bit_length() [Test] public void Coalesce() { - IgnoreIfNotSupported("coalesce"); + AssumeSupported("coalesce"); // test only the parser and render using (ISession s = OpenSession()) { @@ -471,9 +472,9 @@ public void Coalesce() [Test] public void Nullif() { - IgnoreIfNotSupported("nullif"); + AssumeSupported("nullif"); string hql1, hql2; - if(!IsOracleDialect()) + if(!(Dialect is Oracle8iDialect)) { hql1 = "select nullif(h.NickName, '1e1') from Human h"; hql2 = "from Human h where not(nullif(h.NickName, '1e1') is null)"; @@ -495,7 +496,7 @@ public void Nullif() [Test] public void Abs() { - IgnoreIfNotSupported("abs"); + AssumeSupported("abs"); using (ISession s = OpenSession()) { Animal a1 = new Animal("Dog", 9); @@ -524,7 +525,7 @@ public void Abs() [Test] public void Ceiling() { - Assume.That(Dialect.Functions.ContainsKey("ceiling"), Is.True, Dialect + " doesn't support ceiling function."); + AssumeSupported("ceiling"); using (var s = OpenSession()) { @@ -548,7 +549,7 @@ public void Ceiling() [Test] public void Mod() { - IgnoreIfNotSupported("mod"); + AssumeSupported("mod"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -574,7 +575,7 @@ public void Mod() [Test] public void Sqrt() { - IgnoreIfNotSupported("sqrt"); + AssumeSupported("sqrt"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 65536f); @@ -596,7 +597,7 @@ public void Sqrt() [Test] public void Upper() { - IgnoreIfNotSupported("upper"); + AssumeSupported("upper"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1f); @@ -625,7 +626,7 @@ public void Upper() [Test] public void Lower() { - IgnoreIfNotSupported("lower"); + AssumeSupported("lower"); using (ISession s = OpenSession()) { Animal a1 = new Animal("ABCDEF", 1f); @@ -654,7 +655,7 @@ public void Lower() [Test] public void Ascii() { - Assume.That(Dialect.Functions.ContainsKey("ascii"), Is.True, Dialect + " doesn't support ascii function."); + AssumeSupported("ascii"); using (var s = OpenSession()) { @@ -678,7 +679,7 @@ public void Ascii() [Test] public void Chr() { - Assume.That(Dialect.Functions.ContainsKey("chr"), Is.True, Dialect + " doesn't support chr function."); + AssumeSupported("chr"); using (var s = OpenSession()) { @@ -704,7 +705,7 @@ public void Cast() { const double magicResult = 7 + 123 - 5*1.3d; - IgnoreIfNotSupported("cast"); + AssumeSupported("cast"); // The cast is used to test various cases of a function render // Cast was selected because represent a special case for: // 1) Has more then 1 argument @@ -882,7 +883,7 @@ public void Cast() [Test] public void CastNH1446() { - IgnoreIfNotSupported("cast"); + AssumeSupported("cast"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -902,7 +903,7 @@ public void CastNH1446() [Test] public void CastNH1979() { - IgnoreIfNotSupported("cast"); + AssumeSupported("cast"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -920,7 +921,7 @@ public void CastNH1979() [Test] public void Current_TimeStamp() { - IgnoreIfNotSupported("current_timestamp"); + AssumeSupported("current_timestamp"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -940,9 +941,7 @@ public void Current_TimeStamp() [Test] public void Current_TimeStamp_Offset() { - if (!Dialect.Functions.ContainsKey("current_timestamp_offset")) - Assert.Ignore(Dialect + " doesn't support current_timestamp_offset function"); - IgnoreIfNotSupported("current_timestamp_offset"); + AssumeSupported("current_timestamp_offset"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -959,8 +958,8 @@ public void Current_TimeStamp_Offset() [Test] public void Extract() { - IgnoreIfNotSupported("extract"); - IgnoreIfNotSupported("current_timestamp"); + AssumeSupported("extract"); + AssumeSupported("current_timestamp"); // test only the parser and render using (ISession s = OpenSession()) @@ -976,7 +975,7 @@ public void Extract() [Test] public void Concat() { - IgnoreIfNotSupported("concat"); + AssumeSupported("concat"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1f); @@ -1002,10 +1001,10 @@ public void Concat() [Test] public void HourMinuteSecond() { - IgnoreIfNotSupported("second"); - IgnoreIfNotSupported("minute"); - IgnoreIfNotSupported("hour"); - IgnoreIfNotSupported("current_timestamp"); + AssumeSupported("second"); + AssumeSupported("minute"); + AssumeSupported("hour"); + AssumeSupported("current_timestamp"); // test only the parser and render using (ISession s = OpenSession()) { @@ -1017,9 +1016,9 @@ public void HourMinuteSecond() [Test] public void DayMonthYear() { - IgnoreIfNotSupported("day"); - IgnoreIfNotSupported("month"); - IgnoreIfNotSupported("year"); + AssumeSupported("day"); + AssumeSupported("month"); + AssumeSupported("year"); // test only the parser and render using (ISession s = OpenSession()) { @@ -1031,7 +1030,7 @@ public void DayMonthYear() [Test] public void Str() { - IgnoreIfNotSupported("str"); + AssumeSupported("str"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -1053,8 +1052,7 @@ public void Str() [Test] public void Iif() { - if (!Dialect.Functions.ContainsKey("iif")) - Assert.Ignore(Dialect + " doesn't support iif function."); + AssumeSupported("Iif"); using (ISession s = OpenSession()) { s.Save(new MaterialResource("Flash card 512MB", "A001/07", MaterialResource.MaterialState.Available)); @@ -1097,7 +1095,7 @@ from MaterialResource mr [Test] public void NH1725() { - IgnoreIfNotSupported("iif"); + AssumeSupported("iif"); // Only to test the parser using (ISession s = OpenSession()) { From 97478c1461c4b4ce9bbf3aa07480693ce706f91b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delaporte?= Date: Thu, 18 Jan 2018 14:46:03 +0100 Subject: [PATCH 5/5] Consolidate function support test logic in Test project --- src/NHibernate.Test/Async/Hql/HQLFunctions.cs | 105 ++++++------------ src/NHibernate.Test/Hql/HQLFunctions.cs | 105 ++++++------------ src/NHibernate.Test/Linq/MathTests.cs | 33 +++--- src/NHibernate.Test/TestCase.cs | 40 ++++++- 4 files changed, 120 insertions(+), 163 deletions(-) diff --git a/src/NHibernate.Test/Async/Hql/HQLFunctions.cs b/src/NHibernate.Test/Async/Hql/HQLFunctions.cs index b1110f70260..2009abcf704 100644 --- a/src/NHibernate.Test/Async/Hql/HQLFunctions.cs +++ b/src/NHibernate.Test/Async/Hql/HQLFunctions.cs @@ -10,7 +10,6 @@ using System; using System.Collections; -using System.Collections.Generic; using NHibernate.Dialect; using NUnit.Framework; @@ -24,42 +23,6 @@ namespace NHibernate.Test.Hql [TestFixture] public class HQLFunctionsAsync : TestCase { - private static readonly Dictionary> DialectsNotSupportingStandardFunction; - - static HQLFunctionsAsync() - { - DialectsNotSupportingStandardFunction = - new Dictionary> - { - {"locate", new HashSet {typeof (SQLiteDialect)}}, - {"bit_length", new HashSet {typeof (SQLiteDialect)}}, - {"extract", new HashSet {typeof (SQLiteDialect)}}, - { - "nullif", - new HashSet - { - // Actually not supported by the db engine. (Well, could likely still be done with a case when override.) - typeof (MsSqlCeDialect), - typeof (MsSqlCe40Dialect) - }} - }; - } - - private void AssumeSupported(string functionName) - { - Assume.That( - Sfi.SQLFunctionRegistry.HasFunction(functionName), - Is.True, - $"{Dialect} doesn't support {functionName} function."); - - if (!DialectsNotSupportingStandardFunction.TryGetValue(functionName, out var dialects)) - return; - Assume.That( - dialects, - Does.Not.Contain(Dialect.GetType()), - $"{Dialect} doesn't support {functionName} standard function."); - } - protected override string MappingsAssembly { get { return "NHibernate.Test"; } @@ -257,7 +220,7 @@ public async Task SubStringTwoParametersAsync() // the two-parameter overload - emulating it by generating the // third parameter (length) if the database requires three parameters. - AssumeSupported("substring"); + AssumeFunctionSupported("substring"); using (ISession s = OpenSession()) { @@ -295,7 +258,7 @@ public async Task SubStringTwoParametersAsync() [Test] public async Task SubStringAsync() { - AssumeSupported("substring"); + AssumeFunctionSupported("substring"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -347,7 +310,7 @@ public async Task SubStringAsync() [Test] public async Task LocateAsync() { - AssumeSupported("locate"); + AssumeFunctionSupported("locate"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -369,7 +332,7 @@ public async Task LocateAsync() [Test] public async Task TrimAsync() { - AssumeSupported("trim"); + AssumeFunctionSupported("trim"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abc ", 1); @@ -427,7 +390,7 @@ public async Task TrimAsync() [Test] public async Task LengthAsync() { - AssumeSupported("length"); + AssumeFunctionSupported("length"); using (ISession s = OpenSession()) { @@ -452,7 +415,7 @@ public async Task LengthAsync() [Test] public async Task Bit_lengthAsync() { - AssumeSupported("bit_length"); + AssumeFunctionSupported("bit_length"); // test only the parser using (ISession s = OpenSession()) @@ -468,7 +431,7 @@ public async Task Bit_lengthAsync() [Test] public async Task CoalesceAsync() { - AssumeSupported("coalesce"); + AssumeFunctionSupported("coalesce"); // test only the parser and render using (ISession s = OpenSession()) { @@ -483,7 +446,7 @@ public async Task CoalesceAsync() [Test] public async Task NullifAsync() { - AssumeSupported("nullif"); + AssumeFunctionSupported("nullif"); string hql1, hql2; if(!(Dialect is Oracle8iDialect)) { @@ -507,7 +470,7 @@ public async Task NullifAsync() [Test] public async Task AbsAsync() { - AssumeSupported("abs"); + AssumeFunctionSupported("abs"); using (ISession s = OpenSession()) { Animal a1 = new Animal("Dog", 9); @@ -536,7 +499,7 @@ public async Task AbsAsync() [Test] public async Task CeilingAsync() { - AssumeSupported("ceiling"); + AssumeFunctionSupported("ceiling"); using (var s = OpenSession()) { @@ -560,7 +523,7 @@ public async Task CeilingAsync() [Test] public async Task ModAsync() { - AssumeSupported("mod"); + AssumeFunctionSupported("mod"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -586,7 +549,7 @@ public async Task ModAsync() [Test] public async Task SqrtAsync() { - AssumeSupported("sqrt"); + AssumeFunctionSupported("sqrt"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 65536f); @@ -608,7 +571,7 @@ public async Task SqrtAsync() [Test] public async Task UpperAsync() { - AssumeSupported("upper"); + AssumeFunctionSupported("upper"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1f); @@ -637,7 +600,7 @@ public async Task UpperAsync() [Test] public async Task LowerAsync() { - AssumeSupported("lower"); + AssumeFunctionSupported("lower"); using (ISession s = OpenSession()) { Animal a1 = new Animal("ABCDEF", 1f); @@ -666,7 +629,7 @@ public async Task LowerAsync() [Test] public async Task AsciiAsync() { - AssumeSupported("ascii"); + AssumeFunctionSupported("ascii"); using (var s = OpenSession()) { @@ -690,7 +653,7 @@ public async Task AsciiAsync() [Test] public async Task ChrAsync() { - AssumeSupported("chr"); + AssumeFunctionSupported("chr"); using (var s = OpenSession()) { @@ -716,7 +679,7 @@ public async Task CastAsync() { const double magicResult = 7 + 123 - 5*1.3d; - AssumeSupported("cast"); + AssumeFunctionSupported("cast"); // The cast is used to test various cases of a function render // Cast was selected because represent a special case for: // 1) Has more then 1 argument @@ -894,7 +857,7 @@ public async Task CastAsync() [Test] public async Task CastNH1446Async() { - AssumeSupported("cast"); + AssumeFunctionSupported("cast"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -914,7 +877,7 @@ public async Task CastNH1446Async() [Test] public async Task CastNH1979Async() { - AssumeSupported("cast"); + AssumeFunctionSupported("cast"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -932,7 +895,7 @@ public async Task CastNH1979Async() [Test] public async Task Current_TimeStampAsync() { - AssumeSupported("current_timestamp"); + AssumeFunctionSupported("current_timestamp"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -952,7 +915,7 @@ public async Task Current_TimeStampAsync() [Test] public async Task Current_TimeStamp_OffsetAsync() { - AssumeSupported("current_timestamp_offset"); + AssumeFunctionSupported("current_timestamp_offset"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -969,8 +932,8 @@ public async Task Current_TimeStamp_OffsetAsync() [Test] public async Task ExtractAsync() { - AssumeSupported("extract"); - AssumeSupported("current_timestamp"); + AssumeFunctionSupported("extract"); + AssumeFunctionSupported("current_timestamp"); // test only the parser and render using (ISession s = OpenSession()) @@ -986,7 +949,7 @@ public async Task ExtractAsync() [Test] public async Task ConcatAsync() { - AssumeSupported("concat"); + AssumeFunctionSupported("concat"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1f); @@ -1012,10 +975,10 @@ public async Task ConcatAsync() [Test] public async Task HourMinuteSecondAsync() { - AssumeSupported("second"); - AssumeSupported("minute"); - AssumeSupported("hour"); - AssumeSupported("current_timestamp"); + AssumeFunctionSupported("second"); + AssumeFunctionSupported("minute"); + AssumeFunctionSupported("hour"); + AssumeFunctionSupported("current_timestamp"); // test only the parser and render using (ISession s = OpenSession()) { @@ -1027,9 +990,9 @@ public async Task HourMinuteSecondAsync() [Test] public async Task DayMonthYearAsync() { - AssumeSupported("day"); - AssumeSupported("month"); - AssumeSupported("year"); + AssumeFunctionSupported("day"); + AssumeFunctionSupported("month"); + AssumeFunctionSupported("year"); // test only the parser and render using (ISession s = OpenSession()) { @@ -1041,7 +1004,7 @@ public async Task DayMonthYearAsync() [Test] public async Task StrAsync() { - AssumeSupported("str"); + AssumeFunctionSupported("str"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -1063,7 +1026,7 @@ public async Task StrAsync() [Test] public async Task IifAsync() { - AssumeSupported("Iif"); + AssumeFunctionSupported("Iif"); using (ISession s = OpenSession()) { await (s.SaveAsync(new MaterialResource("Flash card 512MB", "A001/07", MaterialResource.MaterialState.Available))); @@ -1106,7 +1069,7 @@ from MaterialResource mr [Test] public async Task NH1725Async() { - AssumeSupported("iif"); + AssumeFunctionSupported("iif"); // Only to test the parser using (ISession s = OpenSession()) { diff --git a/src/NHibernate.Test/Hql/HQLFunctions.cs b/src/NHibernate.Test/Hql/HQLFunctions.cs index 2ae5ff2b0b6..cc2f5fbeb74 100644 --- a/src/NHibernate.Test/Hql/HQLFunctions.cs +++ b/src/NHibernate.Test/Hql/HQLFunctions.cs @@ -1,6 +1,5 @@ using System; using System.Collections; -using System.Collections.Generic; using NHibernate.Dialect; using NUnit.Framework; @@ -13,42 +12,6 @@ namespace NHibernate.Test.Hql [TestFixture] public class HQLFunctions : TestCase { - private static readonly Dictionary> DialectsNotSupportingStandardFunction; - - static HQLFunctions() - { - DialectsNotSupportingStandardFunction = - new Dictionary> - { - {"locate", new HashSet {typeof (SQLiteDialect)}}, - {"bit_length", new HashSet {typeof (SQLiteDialect)}}, - {"extract", new HashSet {typeof (SQLiteDialect)}}, - { - "nullif", - new HashSet - { - // Actually not supported by the db engine. (Well, could likely still be done with a case when override.) - typeof (MsSqlCeDialect), - typeof (MsSqlCe40Dialect) - }} - }; - } - - private void AssumeSupported(string functionName) - { - Assume.That( - Sfi.SQLFunctionRegistry.HasFunction(functionName), - Is.True, - $"{Dialect} doesn't support {functionName} function."); - - if (!DialectsNotSupportingStandardFunction.TryGetValue(functionName, out var dialects)) - return; - Assume.That( - dialects, - Does.Not.Contain(Dialect.GetType()), - $"{Dialect} doesn't support {functionName} standard function."); - } - protected override string MappingsAssembly { get { return "NHibernate.Test"; } @@ -246,7 +209,7 @@ public void SubStringTwoParameters() // the two-parameter overload - emulating it by generating the // third parameter (length) if the database requires three parameters. - AssumeSupported("substring"); + AssumeFunctionSupported("substring"); using (ISession s = OpenSession()) { @@ -284,7 +247,7 @@ public void SubStringTwoParameters() [Test] public void SubString() { - AssumeSupported("substring"); + AssumeFunctionSupported("substring"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -336,7 +299,7 @@ public void SubString() [Test] public void Locate() { - AssumeSupported("locate"); + AssumeFunctionSupported("locate"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -358,7 +321,7 @@ public void Locate() [Test] public void Trim() { - AssumeSupported("trim"); + AssumeFunctionSupported("trim"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abc ", 1); @@ -416,7 +379,7 @@ public void Trim() [Test] public void Length() { - AssumeSupported("length"); + AssumeFunctionSupported("length"); using (ISession s = OpenSession()) { @@ -441,7 +404,7 @@ public void Length() [Test] public void Bit_length() { - AssumeSupported("bit_length"); + AssumeFunctionSupported("bit_length"); // test only the parser using (ISession s = OpenSession()) @@ -457,7 +420,7 @@ public void Bit_length() [Test] public void Coalesce() { - AssumeSupported("coalesce"); + AssumeFunctionSupported("coalesce"); // test only the parser and render using (ISession s = OpenSession()) { @@ -472,7 +435,7 @@ public void Coalesce() [Test] public void Nullif() { - AssumeSupported("nullif"); + AssumeFunctionSupported("nullif"); string hql1, hql2; if(!(Dialect is Oracle8iDialect)) { @@ -496,7 +459,7 @@ public void Nullif() [Test] public void Abs() { - AssumeSupported("abs"); + AssumeFunctionSupported("abs"); using (ISession s = OpenSession()) { Animal a1 = new Animal("Dog", 9); @@ -525,7 +488,7 @@ public void Abs() [Test] public void Ceiling() { - AssumeSupported("ceiling"); + AssumeFunctionSupported("ceiling"); using (var s = OpenSession()) { @@ -549,7 +512,7 @@ public void Ceiling() [Test] public void Mod() { - AssumeSupported("mod"); + AssumeFunctionSupported("mod"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -575,7 +538,7 @@ public void Mod() [Test] public void Sqrt() { - AssumeSupported("sqrt"); + AssumeFunctionSupported("sqrt"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 65536f); @@ -597,7 +560,7 @@ public void Sqrt() [Test] public void Upper() { - AssumeSupported("upper"); + AssumeFunctionSupported("upper"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1f); @@ -626,7 +589,7 @@ public void Upper() [Test] public void Lower() { - AssumeSupported("lower"); + AssumeFunctionSupported("lower"); using (ISession s = OpenSession()) { Animal a1 = new Animal("ABCDEF", 1f); @@ -655,7 +618,7 @@ public void Lower() [Test] public void Ascii() { - AssumeSupported("ascii"); + AssumeFunctionSupported("ascii"); using (var s = OpenSession()) { @@ -679,7 +642,7 @@ public void Ascii() [Test] public void Chr() { - AssumeSupported("chr"); + AssumeFunctionSupported("chr"); using (var s = OpenSession()) { @@ -705,7 +668,7 @@ public void Cast() { const double magicResult = 7 + 123 - 5*1.3d; - AssumeSupported("cast"); + AssumeFunctionSupported("cast"); // The cast is used to test various cases of a function render // Cast was selected because represent a special case for: // 1) Has more then 1 argument @@ -883,7 +846,7 @@ public void Cast() [Test] public void CastNH1446() { - AssumeSupported("cast"); + AssumeFunctionSupported("cast"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -903,7 +866,7 @@ public void CastNH1446() [Test] public void CastNH1979() { - AssumeSupported("cast"); + AssumeFunctionSupported("cast"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -921,7 +884,7 @@ public void CastNH1979() [Test] public void Current_TimeStamp() { - AssumeSupported("current_timestamp"); + AssumeFunctionSupported("current_timestamp"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -941,7 +904,7 @@ public void Current_TimeStamp() [Test] public void Current_TimeStamp_Offset() { - AssumeSupported("current_timestamp_offset"); + AssumeFunctionSupported("current_timestamp_offset"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1.3f); @@ -958,8 +921,8 @@ public void Current_TimeStamp_Offset() [Test] public void Extract() { - AssumeSupported("extract"); - AssumeSupported("current_timestamp"); + AssumeFunctionSupported("extract"); + AssumeFunctionSupported("current_timestamp"); // test only the parser and render using (ISession s = OpenSession()) @@ -975,7 +938,7 @@ public void Extract() [Test] public void Concat() { - AssumeSupported("concat"); + AssumeFunctionSupported("concat"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 1f); @@ -1001,10 +964,10 @@ public void Concat() [Test] public void HourMinuteSecond() { - AssumeSupported("second"); - AssumeSupported("minute"); - AssumeSupported("hour"); - AssumeSupported("current_timestamp"); + AssumeFunctionSupported("second"); + AssumeFunctionSupported("minute"); + AssumeFunctionSupported("hour"); + AssumeFunctionSupported("current_timestamp"); // test only the parser and render using (ISession s = OpenSession()) { @@ -1016,9 +979,9 @@ public void HourMinuteSecond() [Test] public void DayMonthYear() { - AssumeSupported("day"); - AssumeSupported("month"); - AssumeSupported("year"); + AssumeFunctionSupported("day"); + AssumeFunctionSupported("month"); + AssumeFunctionSupported("year"); // test only the parser and render using (ISession s = OpenSession()) { @@ -1030,7 +993,7 @@ public void DayMonthYear() [Test] public void Str() { - AssumeSupported("str"); + AssumeFunctionSupported("str"); using (ISession s = OpenSession()) { Animal a1 = new Animal("abcdef", 20); @@ -1052,7 +1015,7 @@ public void Str() [Test] public void Iif() { - AssumeSupported("Iif"); + AssumeFunctionSupported("Iif"); using (ISession s = OpenSession()) { s.Save(new MaterialResource("Flash card 512MB", "A001/07", MaterialResource.MaterialState.Available)); @@ -1095,7 +1058,7 @@ from MaterialResource mr [Test] public void NH1725() { - AssumeSupported("iif"); + AssumeFunctionSupported("iif"); // Only to test the parser using (ISession s = OpenSession()) { diff --git a/src/NHibernate.Test/Linq/MathTests.cs b/src/NHibernate.Test/Linq/MathTests.cs index b078c7f0f5f..4c492379738 100644 --- a/src/NHibernate.Test/Linq/MathTests.cs +++ b/src/NHibernate.Test/Linq/MathTests.cs @@ -1,7 +1,6 @@ using System; using System.Linq; using System.Linq.Expressions; -using NHibernate.Dialect; using NHibernate.DomainModel.Northwind.Entities; using NUnit.Framework; @@ -12,12 +11,6 @@ public class MathTests : LinqTestCase { private IQueryable _orderLines; - private void IgnoreIfNotSupported(string function) - { - if (!Dialect.Functions.ContainsKey(function)) - Assert.Ignore("Dialect {0} does not support '{1}' function", Dialect.GetType(), function); - } - protected override void OnSetUp() { base.OnSetUp(); @@ -29,7 +22,7 @@ protected override void OnSetUp() [Test] public void SignAllPositiveTest() { - IgnoreIfNotSupported("sign"); + AssumeFunctionSupported("sign"); var signs = (from o in db.OrderLines select Math.Sign(o.UnitPrice)).ToList(); @@ -39,7 +32,7 @@ public void SignAllPositiveTest() [Test] public void SignAllNegativeTest() { - IgnoreIfNotSupported("sign"); + AssumeFunctionSupported("sign"); var signs = (from o in db.OrderLines select Math.Sign(0m - o.UnitPrice)).ToList(); @@ -49,77 +42,77 @@ public void SignAllNegativeTest() [Test] public void SinTest() { - IgnoreIfNotSupported("sin"); + AssumeFunctionSupported("sin"); Test(o => Math.Round(Math.Sin((double) o.UnitPrice), 5)); } [Test] public void CosTest() { - IgnoreIfNotSupported("cos"); + AssumeFunctionSupported("cos"); Test(o => Math.Round(Math.Cos((double)o.UnitPrice), 5)); } [Test] public void TanTest() { - IgnoreIfNotSupported("tan"); + AssumeFunctionSupported("tan"); Test(o => Math.Round(Math.Tan((double)o.Discount), 5)); } [Test] public void SinhTest() { - IgnoreIfNotSupported("sinh"); + AssumeFunctionSupported("sinh"); Test(o => Math.Round(Math.Sinh((double)o.Discount), 5)); } [Test] public void CoshTest() { - IgnoreIfNotSupported("cosh"); + AssumeFunctionSupported("cosh"); Test(o => Math.Round(Math.Cosh((double)o.Discount), 5)); } [Test] public void TanhTest() { - IgnoreIfNotSupported("tanh"); + AssumeFunctionSupported("tanh"); Test(o => Math.Round(Math.Tanh((double)o.Discount), 5)); } [Test] public void AsinTest() { - IgnoreIfNotSupported("asin"); + AssumeFunctionSupported("asin"); Test(o => Math.Round(Math.Asin((double)o.Discount), 5)); } [Test] public void AcosTest() { - IgnoreIfNotSupported("acos"); + AssumeFunctionSupported("acos"); Test(o => Math.Round(Math.Acos((double)o.Discount), 5)); } [Test] public void AtanTest() { - IgnoreIfNotSupported("atan"); + AssumeFunctionSupported("atan"); Test(o => Math.Round(Math.Atan((double)o.UnitPrice), 5)); } [Test] public void Atan2Test() { - IgnoreIfNotSupported("atan2"); + AssumeFunctionSupported("atan2"); Test(o => Math.Round(Math.Atan2((double)o.Discount, 0.5d), 5)); } [Test] public void PowTest() { - IgnoreIfNotSupported("power"); + AssumeFunctionSupported("power"); Test(o => Math.Round(Math.Pow((double)o.Discount, 0.5d), 5)); } diff --git a/src/NHibernate.Test/TestCase.cs b/src/NHibernate.Test/TestCase.cs index 24c685ad7e4..13f82daf61e 100644 --- a/src/NHibernate.Test/TestCase.cs +++ b/src/NHibernate.Test/TestCase.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Data; using System.Reflection; using log4net; @@ -13,6 +14,7 @@ using NUnit.Framework; using NUnit.Framework.Interfaces; using System.Text; +using NHibernate.Dialect; using NHibernate.Driver; namespace NHibernate.Test @@ -438,7 +440,43 @@ protected DateTime RoundForDialect(DateTime value) { return AbstractDateTimeType.Round(value, Dialect.TimestampResolutionInTicks); } - + + private static readonly Dictionary> DialectsNotSupportingStandardFunction = + new Dictionary> + { + {"locate", new HashSet {typeof (SQLiteDialect)}}, + {"bit_length", new HashSet {typeof (SQLiteDialect)}}, + {"extract", new HashSet {typeof (SQLiteDialect)}}, + { + "nullif", + new HashSet + { + // Actually not supported by the db engine. (Well, could likely still be done with a case when override.) + typeof (MsSqlCeDialect), + typeof (MsSqlCe40Dialect) + }} + }; + + protected void AssumeFunctionSupported(string functionName) + { + // We could test Sfi.SQLFunctionRegistry.HasFunction(functionName) which has the advantage of + // accounting for additionnal functions added in configuration. But Dialect is normally never + // null, while Sfi could be not yet initialized, depending from where this function is called. + // Furtermore there are currently no additionnal functions added in configuration for NHibernate + // tests. + Assume.That( + Dialect.Functions, + Does.ContainKey(functionName), + $"{Dialect} doesn't support {functionName} function."); + + if (!DialectsNotSupportingStandardFunction.TryGetValue(functionName, out var dialects)) + return; + Assume.That( + dialects, + Does.Not.Contain(Dialect.GetType()), + $"{Dialect} doesn't support {functionName} standard function."); + } + #endregion } }