Skip to content

Commit 310fd7f

Browse files
NH-3850 - Amending test cases for polymorphic Linq query results aggregators failures: acknowledging cases which will not be fixed.
1 parent 98ea1c9 commit 310fd7f

File tree

2 files changed

+94
-11
lines changed

2 files changed

+94
-11
lines changed

src/NHibernate.Test/Linq/ByMethod/SumTests.cs

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

45
namespace NHibernate.Test.Linq.ByMethod
@@ -14,7 +15,10 @@ public void EmptySumDecimal()
1415
{
1516
db.OrderLines.Where(ol => false).Sum(ol => ol.Discount);
1617
},
17-
Throws.InstanceOf<HibernateException>());
18+
// Before NH-3850
19+
Throws.InstanceOf<HibernateException>()
20+
// After NH-3850
21+
.Or.InstanceOf<InvalidOperationException>());
1822
}
1923

2024
[Test]

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

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
24
using System.Data;
35
using System.Diagnostics;
46
using System.Linq;
7+
using System.Reflection;
58
using System.Text;
69
using NHibernate.Dialect;
710
using NHibernate.Driver;
@@ -375,14 +378,14 @@ public void AnyObject()
375378
}
376379

377380
// Failing case till NH-3850 is fixed
378-
[Test]
381+
[Test, Ignore("Won't fix: requires reshaping the query")]
379382
public void AverageBBase()
380383
{
381384
Average<DomainClassBExtendedByA>(1.5m);
382385
}
383386

384-
// Non-reg case
385-
[Test]
387+
// Failing case till NH-3850 is fixed
388+
[Test, Ignore("Won't fix: requires reshaping the query")]
386389
public void AverageCBase()
387390
{
388391
Average<DomainClassCExtendedByD>(1.5m);
@@ -403,7 +406,7 @@ public void AverageF()
403406
}
404407

405408
// Failing case till NH-3850 is fixed
406-
[Test]
409+
[Test, Ignore("Won't fix: requires reshaping the query")]
407410
public void AverageGBase()
408411
{
409412
Average<DomainClassGExtendedByH>(2.5m);
@@ -432,14 +435,17 @@ private void Average<DC>(decimal? expectedResult) where DC : DomainClassBase
432435
else
433436
{
434437
Assert.That(() => { dcQuery.Average(dc => dc.NonNullableDecimal); },
435-
Throws.InnerException.InstanceOf<ArgumentNullException>(),
438+
// After fix
439+
Throws.InstanceOf<InvalidOperationException>()
440+
// Before fix
441+
.Or.InnerException.InstanceOf<ArgumentNullException>(),
436442
"Non nullable decimal average has failed");
437443
}
438444
}
439445
}
440446

441447
// Failing case till NH-3850 is fixed
442-
[Test]
448+
[Test, Ignore("Won't fix: requires reshaping the query")]
443449
public void AverageObject()
444450
{
445451
using (var session = OpenSession())
@@ -925,7 +931,10 @@ private void Max<DC>(int? expectedResult) where DC : DomainClassBase
925931
else
926932
{
927933
Assert.That(() => { dcQuery.Max(dc => dc.NonNullableDecimal); },
928-
Throws.InnerException.InstanceOf<ArgumentNullException>(),
934+
// After fix
935+
Throws.InstanceOf<InvalidOperationException>()
936+
// Before fix
937+
.Or.InnerException.InstanceOf<ArgumentNullException>(),
929938
"Non nullable decimal max has failed");
930939
}
931940
}
@@ -1006,7 +1015,10 @@ private void Min<DC>(int? expectedResult) where DC : DomainClassBase
10061015
else
10071016
{
10081017
Assert.That(() => { dcQuery.Min(dc => dc.NonNullableDecimal); },
1009-
Throws.InnerException.InstanceOf<ArgumentNullException>(),
1018+
// After fix
1019+
Throws.InstanceOf<InvalidOperationException>()
1020+
// Before fix
1021+
.Or.InnerException.InstanceOf<ArgumentNullException>(),
10101022
"Non nullable decimal min has failed");
10111023
}
10121024
}
@@ -1226,10 +1238,77 @@ private void Sum<DC>(int? expectedResult) where DC : DomainClassBase
12261238
else
12271239
{
12281240
Assert.That(() => { dcQuery.Sum(dc => dc.NonNullableDecimal); },
1229-
Throws.InnerException.InstanceOf<ArgumentNullException>(),
1241+
// After fix
1242+
Throws.InstanceOf<InvalidOperationException>()
1243+
// Before fix
1244+
.Or.InnerException.InstanceOf<ArgumentNullException>(),
12301245
"Non nullable decimal sum has failed");
12311246
}
12321247
}
12331248
}
1249+
1250+
[Test]
1251+
public void BadOverload()
1252+
{
1253+
var sumMethodTemplate = ReflectionHelper.GetMethod(() => Queryable.Sum((IQueryable<int>)null));
1254+
Assert.Throws<InvalidOperationException>(() =>
1255+
{
1256+
ReflectionHelper.GetMethodOverload(sumMethodTemplate, new[] { typeof(object) });
1257+
});
1258+
}
1259+
1260+
[Test, Explicit("Just a blunt perf comparison among some candidate overload reflection patterns, one being required for NH-3850")]
1261+
public void OverloadReflectionBluntPerfCompare()
1262+
{
1263+
var sumMethodTemplate = ReflectionHelper.GetMethod(() => Queryable.Sum((IQueryable<int>)null));
1264+
1265+
var swNoSameParamsCheck = new Stopwatch();
1266+
swNoSameParamsCheck.Start();
1267+
for (var i = 0; i < 1000; i++)
1268+
{
1269+
var sumMethod = sumMethodTemplate.DeclaringType.GetMethod(sumMethodTemplate.Name,
1270+
(sumMethodTemplate.IsStatic ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.Public,
1271+
null, new[] { typeof(IQueryable<decimal>) }, null);
1272+
Trace.TraceInformation(sumMethod.ToString());
1273+
}
1274+
swNoSameParamsCheck.Stop();
1275+
1276+
var swCurrentChoiceSameType = new Stopwatch();
1277+
swCurrentChoiceSameType.Start();
1278+
for (var i = 0; i < 1000; i++)
1279+
{
1280+
var sumMethod = ReflectionHelper.GetMethodOverload(sumMethodTemplate, new[] { typeof(IQueryable<int>) });
1281+
Trace.TraceInformation(sumMethod.ToString());
1282+
}
1283+
swCurrentChoiceSameType.Stop();
1284+
1285+
var swCurrentChoice = new Stopwatch();
1286+
swCurrentChoice.Start();
1287+
for (var i = 0; i < 1000; i++)
1288+
{
1289+
var sumMethod = ReflectionHelper.GetMethodOverload(sumMethodTemplate, new[] { typeof(IQueryable<long>) });
1290+
Trace.TraceInformation(sumMethod.ToString());
1291+
}
1292+
swCurrentChoice.Stop();
1293+
1294+
var swEnHlp = new Stopwatch();
1295+
swEnHlp.Start();
1296+
for (var i = 0; i < 1000; i++)
1297+
{
1298+
// Testing the obsolete helper perf. Disable obsolete warning. Remove this swEnHlp part of the test if this helper is to be removed.
1299+
#pragma warning disable 0618
1300+
var sumMethod = EnumerableHelper.GetMethod("Sum", new[] { typeof(IEnumerable<int>) });
1301+
#pragma warning restore 0618
1302+
Trace.TraceInformation(sumMethod.ToString());
1303+
}
1304+
swEnHlp.Stop();
1305+
1306+
Assert.Pass(@"Blunt perf timings:
1307+
Direct reflection: {0}
1308+
Current impl, same overload: {1}
1309+
Current impl, other overload: {2}
1310+
EnumerableHelper.GetMethod(non generic overload): {3}",
1311+
swNoSameParamsCheck.Elapsed, swCurrentChoiceSameType.Elapsed, swCurrentChoice.Elapsed, swEnHlp.Elapsed);
1312+
}
12341313
}
12351314
}

0 commit comments

Comments
 (0)