Skip to content

Commit cdf2a7b

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

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;
@@ -395,14 +398,14 @@ public void AnyObject()
395398
}
396399

397400
// Failing case till NH-3850 is fixed
398-
[Test]
401+
[Test, Ignore("Won't fix: requires reshaping the query")]
399402
public void AverageBBase()
400403
{
401404
Average<DomainClassBExtendedByA>(1.5m);
402405
}
403406

404-
// Non-reg case
405-
[Test]
407+
// Failing case till NH-3850 is fixed
408+
[Test, Ignore("Won't fix: requires reshaping the query")]
406409
public void AverageCBase()
407410
{
408411
Average<DomainClassCExtendedByD>(1.5m);
@@ -423,7 +426,7 @@ public void AverageF()
423426
}
424427

425428
// Failing case till NH-3850 is fixed
426-
[Test]
429+
[Test, Ignore("Won't fix: requires reshaping the query")]
427430
public void AverageGBase()
428431
{
429432
Average<DomainClassGExtendedByH>(2.5m);
@@ -452,14 +455,17 @@ private void Average<DC>(decimal? expectedResult) where DC : DomainClassBase
452455
else
453456
{
454457
Assert.That(() => { dcQuery.Average(dc => dc.NonNullableDecimal); },
455-
Throws.InnerException.InstanceOf<ArgumentNullException>(),
458+
// After fix
459+
Throws.InstanceOf<InvalidOperationException>()
460+
// Before fix
461+
.Or.InnerException.InstanceOf<ArgumentNullException>(),
456462
"Non nullable decimal average has failed");
457463
}
458464
}
459465
}
460466

461467
// Failing case till NH-3850 is fixed
462-
[Test]
468+
[Test, Ignore("Won't fix: requires reshaping the query")]
463469
public void AverageObject()
464470
{
465471
using (var session = OpenSession())
@@ -945,7 +951,10 @@ private void Max<DC>(int? expectedResult) where DC : DomainClassBase
945951
else
946952
{
947953
Assert.That(() => { dcQuery.Max(dc => dc.NonNullableDecimal); },
948-
Throws.InnerException.InstanceOf<ArgumentNullException>(),
954+
// After fix
955+
Throws.InstanceOf<InvalidOperationException>()
956+
// Before fix
957+
.Or.InnerException.InstanceOf<ArgumentNullException>(),
949958
"Non nullable decimal max has failed");
950959
}
951960
}
@@ -1026,7 +1035,10 @@ private void Min<DC>(int? expectedResult) where DC : DomainClassBase
10261035
else
10271036
{
10281037
Assert.That(() => { dcQuery.Min(dc => dc.NonNullableDecimal); },
1029-
Throws.InnerException.InstanceOf<ArgumentNullException>(),
1038+
// After fix
1039+
Throws.InstanceOf<InvalidOperationException>()
1040+
// Before fix
1041+
.Or.InnerException.InstanceOf<ArgumentNullException>(),
10301042
"Non nullable decimal min has failed");
10311043
}
10321044
}
@@ -1246,10 +1258,77 @@ private void Sum<DC>(int? expectedResult) where DC : DomainClassBase
12461258
else
12471259
{
12481260
Assert.That(() => { dcQuery.Sum(dc => dc.NonNullableDecimal); },
1249-
Throws.InnerException.InstanceOf<ArgumentNullException>(),
1261+
// After fix
1262+
Throws.InstanceOf<InvalidOperationException>()
1263+
// Before fix
1264+
.Or.InnerException.InstanceOf<ArgumentNullException>(),
12501265
"Non nullable decimal sum has failed");
12511266
}
12521267
}
12531268
}
1269+
1270+
[Test]
1271+
public void BadOverload()
1272+
{
1273+
var sumMethodTemplate = ReflectHelper.GetMethod(() => Queryable.Sum((IQueryable<int>)null));
1274+
Assert.Throws<InvalidOperationException>(() =>
1275+
{
1276+
ReflectHelper.GetMethodOverload(sumMethodTemplate, typeof(object));
1277+
});
1278+
}
1279+
1280+
[Test, Explicit("Just a blunt perf comparison among some candidate overload reflection patterns, one being required for NH-3850")]
1281+
public void OverloadReflectionBluntPerfCompare()
1282+
{
1283+
var sumMethodTemplate = ReflectHelper.GetMethod(() => Queryable.Sum((IQueryable<int>)null));
1284+
1285+
var swNoSameParamsCheck = new Stopwatch();
1286+
swNoSameParamsCheck.Start();
1287+
for (var i = 0; i < 1000; i++)
1288+
{
1289+
var sumMethod = sumMethodTemplate.DeclaringType.GetMethod(sumMethodTemplate.Name,
1290+
(sumMethodTemplate.IsStatic ? BindingFlags.Static : BindingFlags.Instance) | BindingFlags.Public,
1291+
null, new[] { typeof(IQueryable<decimal>) }, null);
1292+
Trace.TraceInformation(sumMethod.ToString());
1293+
}
1294+
swNoSameParamsCheck.Stop();
1295+
1296+
var swCurrentChoiceSameType = new Stopwatch();
1297+
swCurrentChoiceSameType.Start();
1298+
for (var i = 0; i < 1000; i++)
1299+
{
1300+
var sumMethod = ReflectHelper.GetMethodOverload(sumMethodTemplate, typeof(IQueryable<int>));
1301+
Trace.TraceInformation(sumMethod.ToString());
1302+
}
1303+
swCurrentChoiceSameType.Stop();
1304+
1305+
var swCurrentChoice = new Stopwatch();
1306+
swCurrentChoice.Start();
1307+
for (var i = 0; i < 1000; i++)
1308+
{
1309+
var sumMethod = ReflectHelper.GetMethodOverload(sumMethodTemplate, typeof(IQueryable<long>));
1310+
Trace.TraceInformation(sumMethod.ToString());
1311+
}
1312+
swCurrentChoice.Stop();
1313+
1314+
var swEnHlp = new Stopwatch();
1315+
swEnHlp.Start();
1316+
for (var i = 0; i < 1000; i++)
1317+
{
1318+
// Testing the obsolete helper perf. Disable obsolete warning. Remove this swEnHlp part of the test if this helper is to be removed.
1319+
#pragma warning disable 0618
1320+
var sumMethod = EnumerableHelper.GetMethod("Sum", new[] { typeof(IEnumerable<int>) });
1321+
#pragma warning restore 0618
1322+
Trace.TraceInformation(sumMethod.ToString());
1323+
}
1324+
swEnHlp.Stop();
1325+
1326+
Assert.Pass(@"Blunt perf timings:
1327+
Direct reflection: {0}
1328+
Current impl, same overload: {1}
1329+
Current impl, other overload: {2}
1330+
EnumerableHelper.GetMethod(non generic overload): {3}",
1331+
swNoSameParamsCheck.Elapsed, swCurrentChoiceSameType.Elapsed, swCurrentChoice.Elapsed, swEnHlp.Elapsed);
1332+
}
12541333
}
12551334
}

0 commit comments

Comments
 (0)