Skip to content

Commit 466ee0d

Browse files
NH-4057 - fix tests for MySQL
1 parent aff16f0 commit 466ee0d

File tree

18 files changed

+176
-16136
lines changed

18 files changed

+176
-16136
lines changed

lib/teamcity/mysql/NHibernate.Test.last-results.xml

Lines changed: 0 additions & 16068 deletions
This file was deleted.

src/NHibernate.Test/Criteria/CriteriaQueryTest.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ public void DetachedCriteriaTest()
493493
[Test]
494494
public void SubqueryPaginationOnlyWithFirst()
495495
{
496+
if (!Dialect.SupportsSubSelectsWithPagingAsInPredicateRhs)
497+
Assert.Ignore("Dialect does not support sub-select as in predicate.");
498+
496499
using (ISession session = OpenSession())
497500
using (ITransaction t = session.BeginTransaction())
498501
{
@@ -533,6 +536,9 @@ public void SubqueryPaginationOnlyWithFirst()
533536
[Test]
534537
public void SubqueryPagination()
535538
{
539+
if (!Dialect.SupportsSubSelectsWithPagingAsInPredicateRhs)
540+
Assert.Ignore("Dialect does not support sub-select as in predicate.");
541+
536542
using (ISession session = OpenSession())
537543
using (ITransaction t = session.BeginTransaction())
538544
{

src/NHibernate.Test/Hql/HQLFunctions.cs

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,12 @@ public void Abs()
499499
lresult = s.CreateQuery(hql).List();
500500
Assert.AreEqual(1, lresult.Count);
501501

502-
hql = "select abs(a.BodyWeight*-1) from Animal a group by abs(a.BodyWeight*-1) having abs(a.BodyWeight*-1)>0";
503-
lresult = s.CreateQuery(hql).List();
504-
Assert.AreEqual(1, lresult.Count);
502+
if (Dialect.SupportsHavingOnGroupedByComputation)
503+
{
504+
hql = "select abs(a.BodyWeight*-1) from Animal a group by abs(a.BodyWeight*-1) having abs(a.BodyWeight*-1)>0";
505+
lresult = s.CreateQuery(hql).List();
506+
Assert.AreEqual(1, lresult.Count);
507+
}
505508
}
506509
}
507510

@@ -572,10 +575,13 @@ public void Upper()
572575
hql = "from Animal an where upper(an.Description)='ABCDEF'";
573576
Animal result = (Animal) s.CreateQuery(hql).UniqueResult();
574577
Assert.AreEqual("abcdef", result.Description);
575-
576-
//test only parser
577-
hql = "select upper(an.Description) from Animal an group by upper(an.Description) having upper(an.Description)='ABCDEF'";
578-
lresult = s.CreateQuery(hql).List();
578+
579+
if (Dialect.SupportsHavingOnGroupedByComputation)
580+
{
581+
//test only parser
582+
hql = "select upper(an.Description) from Animal an group by upper(an.Description) having upper(an.Description)='ABCDEF'";
583+
lresult = s.CreateQuery(hql).List();
584+
}
579585
}
580586
}
581587

@@ -599,9 +605,12 @@ public void Lower()
599605
Animal result = (Animal)s.CreateQuery(hql).UniqueResult();
600606
Assert.AreEqual("ABCDEF", result.Description);
601607

602-
//test only parser
603-
hql = "select lower(an.Description) from Animal an group by lower(an.Description) having lower(an.Description)='abcdef'";
604-
lresult = s.CreateQuery(hql).List();
608+
if (Dialect.SupportsHavingOnGroupedByComputation)
609+
{
610+
//test only parser
611+
hql = "select lower(an.Description) from Animal an group by lower(an.Description) having lower(an.Description)='abcdef'";
612+
lresult = s.CreateQuery(hql).List();
613+
}
605614
}
606615
}
607616

@@ -701,66 +710,72 @@ public void Cast()
701710
Assert.That(l[0], Is.TypeOf(typeof(double)));
702711
}
703712

704-
// Rendered in HAVING using a property
705-
hql = "select cast(a.BodyWeight as Double) from Animal a group by cast(a.BodyWeight as Double) having cast(a.BodyWeight as Double)>0";
706-
l = s.CreateQuery(hql).List();
707-
Assert.AreEqual(1, l.Count);
708-
Assert.That(l[0], Is.TypeOf(typeof(double)));
709-
710-
// Rendered in HAVING using a property in an operation with costants
711-
hql = "select cast(7+123.3-1*a.BodyWeight as int) from Animal a group by cast(7+123.3-1*a.BodyWeight as int) having cast(7+123.3-1*a.BodyWeight as int)>0";
712-
l = s.CreateQuery(hql).List();
713-
Assert.AreEqual(1, l.Count);
714-
Assert.AreEqual((int)(7 + 123.3 - 1 * 1.3d), l[0]);
715-
716-
// Rendered in HAVING using a property and named param (NOT SUPPORTED)
717-
try
713+
if (Dialect.SupportsHavingOnGroupedByComputation)
718714
{
719-
hql = "select cast(:aParam+a.BodyWeight as int) from Animal a group by cast(:aParam+a.BodyWeight as int) having cast(:aParam+a.BodyWeight as int)>0";
720-
l = s.CreateQuery(hql).SetInt32("aParam", 10).List();
715+
// Rendered in HAVING using a property
716+
hql =
717+
"select cast(a.BodyWeight as Double) from Animal a group by cast(a.BodyWeight as Double) having cast(a.BodyWeight as Double)>0";
718+
l = s.CreateQuery(hql).List();
721719
Assert.AreEqual(1, l.Count);
722-
Assert.AreEqual(11, l[0]);
723-
}
724-
catch (QueryException ex)
725-
{
726-
if (!(ex.InnerException is NotSupportedException))
727-
throw;
728-
}
729-
catch (ADOException ex)
730-
{
731-
if (Dialect is Oracle8iDialect)
720+
Assert.That(l[0], Is.TypeOf(typeof(double)));
721+
722+
// Rendered in HAVING using a property in an operation with costants
723+
hql =
724+
"select cast(7+123.3-1*a.BodyWeight as int) from Animal a group by cast(7+123.3-1*a.BodyWeight as int) having cast(7+123.3-1*a.BodyWeight as int)>0";
725+
l = s.CreateQuery(hql).List();
726+
Assert.AreEqual(1, l.Count);
727+
Assert.AreEqual((int)(7 + 123.3 - 1 * 1.3d), l[0]);
728+
729+
// Rendered in HAVING using a property and named param (NOT SUPPORTED)
730+
try
732731
{
733-
if (!ex.InnerException.Message.StartsWith("ORA-00979"))
734-
throw;
732+
hql =
733+
"select cast(:aParam+a.BodyWeight as int) from Animal a group by cast(:aParam+a.BodyWeight as int) having cast(:aParam+a.BodyWeight as int)>0";
734+
l = s.CreateQuery(hql).SetInt32("aParam", 10).List();
735+
Assert.AreEqual(1, l.Count);
736+
Assert.AreEqual(11, l[0]);
735737
}
736-
else if (Dialect is FirebirdDialect)
738+
catch (QueryException ex)
737739
{
738-
string msgToCheck =
739-
"not contained in either an aggregate function or the GROUP BY clause";
740-
// This test raises an exception in Firebird for an unknown reason.
741-
if (!ex.InnerException.Message.Contains(msgToCheck))
740+
if (!(ex.InnerException is NotSupportedException))
742741
throw;
743742
}
744-
else
743+
catch (ADOException ex)
745744
{
746-
string msgToCheck =
747-
"Column 'Animal.BodyWeight' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.";
748-
// This test raises an exception in SQL Server because named
749-
// parameters internally are always positional (@p0, @p1, etc.)
750-
// and named differently hence they mismatch between GROUP BY and HAVING clauses.
751-
if (!ex.InnerException.Message.Equals(msgToCheck))
752-
throw;
745+
if (Dialect is Oracle8iDialect)
746+
{
747+
if (!ex.InnerException.Message.StartsWith("ORA-00979"))
748+
throw;
749+
}
750+
else if (Dialect is FirebirdDialect)
751+
{
752+
string msgToCheck =
753+
"not contained in either an aggregate function or the GROUP BY clause";
754+
// This test raises an exception in Firebird for an unknown reason.
755+
if (!ex.InnerException.Message.Contains(msgToCheck))
756+
throw;
757+
}
758+
else
759+
{
760+
string msgToCheck =
761+
"Column 'Animal.BodyWeight' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.";
762+
// This test raises an exception in SQL Server because named
763+
// parameters internally are always positional (@p0, @p1, etc.)
764+
// and named differently hence they mismatch between GROUP BY and HAVING clauses.
765+
if (!ex.InnerException.Message.Equals(msgToCheck))
766+
throw;
767+
}
753768
}
754-
}
755769

756-
// Rendered in HAVING using a property and nested functions
757-
if (!(Dialect is Oracle8iDialect))
758-
{
759-
string castExpr = "cast(cast(cast(a.BodyWeight as string) as double) as int)";
760-
hql = string.Format("select {0} from Animal a group by {0} having {0} = 1", castExpr);
761-
l = s.CreateQuery(hql).List();
762-
Assert.AreEqual(1, l.Count);
763-
Assert.AreEqual(1, l[0]);
770+
// Rendered in HAVING using a property and nested functions
771+
if (!(Dialect is Oracle8iDialect))
772+
{
773+
string castExpr = "cast(cast(cast(a.BodyWeight as string) as double) as int)";
774+
hql = string.Format("select {0} from Animal a group by {0} having {0} = 1", castExpr);
775+
l = s.CreateQuery(hql).List();
776+
Assert.AreEqual(1, l.Count);
777+
Assert.AreEqual(1, l[0]);
778+
}
764779
}
765780
}
766781
}

src/NHibernate.Test/Linq/MiscellaneousTextFixture.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ public void ReferenceToOuter()
7171
c.Orders.Any(o => o.ShippedTo == c.CompanyName)
7272
select c;
7373

74-
Assert.AreEqual(85, results.Count());
74+
var count = results.Count();
75+
76+
Assert.That(count,
77+
// Accent sensitive case
78+
Is.EqualTo(85).
79+
// Accent insensitive case (MySql has most of its case insensitive collations accent insensitive too)
80+
// https://bugs.mysql.com/bug.php?id=19567
81+
Or.EqualTo(87));
7582
}
7683

7784
[Category("Paging")]

src/NHibernate.Test/Linq/WhereTests.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,14 @@ public void BitwiseQuery2()
699699
public void BitwiseQuery3()
700700
{
701701
var featureSet = FeatureSet.HasThat;
702-
var query = (from o in session.Query<User>()
703-
where ((o.Features | featureSet) & featureSet) == featureSet
704-
select o).ToList();
702+
var query = (
703+
from o in session.Query<User>()
704+
// When converted to SQL, "undue" parenthesis are stripped out. For most DB, binary operators have same precedence,
705+
// causing "((o.Features | featureSet) & featureSet)" to be equivalent to "o.Features | featureSet & featureSet"
706+
// But for MySql, & take precedence on |, wrecking the test for it. So it is needed to write the test in a way
707+
// such as the parenthesis will be preserved.
708+
where (featureSet & (o.Features | featureSet)) == featureSet
709+
select o).ToList();
705710

706711
Assert.That(query.Count, Is.EqualTo(3));
707712
}
@@ -829,8 +834,8 @@ private static List<object[]> CanUseCompareInQueryDataSource()
829834
TestRow(p => p.UnitsInStock.CompareTo(13) < 0, 15, false),
830835
TestRow(p => p.UnitsInStock.CompareTo(13) >= 0, 62, false),
831836

832-
// Over floats.
833-
TestRow(p => p.ShippingWeight.CompareTo((float) 4.98) <= 0, 17, false),
837+
// Over floats. (Always include a tolerance with them)
838+
TestRow(p => p.ShippingWeight.CompareTo(4.980001f) <= 0, 17, false),
834839

835840
// Over nullable decimals.
836841
TestRow(p => p.UnitPrice.Value.CompareTo((decimal) 14.00) <= 0, 24, false),

src/NHibernate.Test/NHSpecificTest/NH1632/Fixture.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ public void When_using_two_sessions_with_explicit_flush()
176176
{
177177
if (!TestDialect.SupportsConcurrentTransactions)
178178
Assert.Ignore(Dialect.GetType().Name + " does not support concurrent transactions.");
179+
if (!TestDialect.SupportsDistributedTransactions)
180+
Assert.Ignore(Dialect.GetType().Name + " does not support distributed transactions.");
179181

180182
object id1, id2;
181183
using (var tx = new TransactionScope())
@@ -214,6 +216,8 @@ public void When_using_two_sessions()
214216
{
215217
if (!TestDialect.SupportsConcurrentTransactions)
216218
Assert.Ignore(Dialect.GetType().Name + " does not support concurrent transactions.");
219+
if (!TestDialect.SupportsDistributedTransactions)
220+
Assert.Ignore(Dialect.GetType().Name + " does not support distributed transactions.");
217221

218222
object id1, id2;
219223
using (var tx = new TransactionScope())

src/NHibernate.Test/NHSpecificTest/NH1792/Fixture.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ protected override void OnSetUp()
5656
[Test]
5757
public void PageWithDetachedCriteriaSubqueryWithOrderBy()
5858
{
59+
if (!Dialect.SupportsSubSelectsWithPagingAsInPredicateRhs)
60+
Assert.Ignore("Current dialect does not support paging within IN sub-queries");
5961
//create the subquery
6062
DetachedCriteria subQuery =
6163
DetachedCriteria.For<Product>().SetProjection(Projections.Id()).AddOrder(Order.Desc("Name")).SetMaxResults(5);

src/NHibernate.Test/NHSpecificTest/NH2251/Fixture.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public void FuturePagedHql()
110110
[Test]
111111
public void MultiplePagingParametersInSingleQuery()
112112
{
113+
if (!Dialect.SupportsSubSelectsWithPagingAsInPredicateRhs)
114+
Assert.Ignore("Current dialect does not support paging within IN sub-queries");
115+
113116
using (var session = OpenSession())
114117
using (var transaction = session.BeginTransaction())
115118
{

src/NHibernate.Test/NHSpecificTest/NH2296/Fixture.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ protected override void OnTearDown()
5252
[Test]
5353
public void Test()
5454
{
55+
// This test causes lazy loading of products to use the first query, restricted to id, as a "in (sub-query)" clause.
56+
if (!Dialect.SupportsSubSelectsWithPagingAsInPredicateRhs)
57+
Assert.Ignore("Current dialect does not support paging within IN sub-queries");
58+
5559
using (var s = OpenSession())
5660
using (var tx = s.BeginTransaction())
5761
{

src/NHibernate.Test/NHSpecificTest/NH2302/Fixture.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Data;
2+
using NHibernate.Dialect;
23
using NHibernate.Mapping;
34
using NUnit.Framework;
45

@@ -152,6 +153,8 @@ public void BlobWithLength()
152153
[Test]
153154
public void BlobWithoutLength()
154155
{
156+
if (Dialect is MySQLDialect)
157+
Assert.Ignore("Not fixed for MySQLDialect, still generating a varchar(255) column for StringClob without length");
155158
int id;
156159
// buildup a string the exceed the mapping
157160
string str = GetFixedLengthString12000();

src/NHibernate.Test/NHSpecificTest/NH2746/Fixture.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public class Fixture: BugTestCase
99
[Test]
1010
public void TestQuery()
1111
{
12+
if (!Dialect.SupportsSubSelectsWithPagingAsInPredicateRhs)
13+
Assert.Ignore("Current dialect does not support paging within IN sub-queries");
14+
1215
using (ISession session = OpenSession())
1316
{
1417
DetachedCriteria page = DetachedCriteria.For<T1>()

src/NHibernate.Test/NHSpecificTest/NH3332/TestJoinsWithSameTable.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Linq;
2+
using NHibernate.Dialect;
23
using NHibernate.Linq;
34
using NUnit.Framework;
45

@@ -7,6 +8,13 @@ namespace NHibernate.Test.NHSpecificTest.NH3332
78
[TestFixture]
89
public class TestJoinsWithSameTable : BugTestCase
910
{
11+
protected override bool AppliesTo(Dialect.Dialect dialect)
12+
{
13+
// This test uses a version mapping corresponding to SLQ Server timestamp, where the type is not
14+
// a datetime but an incremented binary int.
15+
return Dialect is MsSql2000Dialect;
16+
}
17+
1018
protected override void OnSetUp()
1119
{
1220
using (var session = OpenSession())

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,7 @@
14641464
<Compile Include="TestDialect.cs" />
14651465
<Compile Include="TestDialects\MsSql2008TestDialect.cs" />
14661466
<Compile Include="TestDialects\PostgreSQL83TestDialect.cs" />
1467+
<Compile Include="TestDialects\MySQL5TestDialect.cs" />
14671468
<Compile Include="TestDialects\SQLiteTestDialect.cs" />
14681469
<Compile Include="Tools\hbm2ddl\SchemaExportTests\ExportToFileFixture.cs" />
14691470
<Compile Include="TransactionTest\Person.cs" />

src/NHibernate.Test/SqlTest/Query/SelfReferencingCollectionLoadTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using NHibernate.Dialect;
23
using NUnit.Framework;
34

45
namespace NHibernate.Test.SqlTest.Query
@@ -16,6 +17,13 @@ protected override string MappingsAssembly
1617
get { return "NHibernate.Test"; }
1718
}
1819

20+
protected override bool AppliesTo(Dialect.Dialect dialect)
21+
{
22+
// Hacky mapping causing the primary key to reference itself as a foreign key, which is not supported by MySQL. It
23+
// fails when trying to insert data by considering the foreign key violated.
24+
return !(Dialect is MySQLDialect);
25+
}
26+
1927
[Test]
2028
public void LoadCollection()
2129
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace NHibernate.Test.TestDialects
2+
{
3+
public class MySQL5TestDialect : TestDialect
4+
{
5+
public MySQL5TestDialect(Dialect.Dialect dialect)
6+
: base(dialect)
7+
{
8+
}
9+
10+
public override bool SupportsDistributedTransactions => false;
11+
}
12+
}

0 commit comments

Comments
 (0)