Skip to content

Commit 9d1b76e

Browse files
committed
New test cases
1 parent fc58cbd commit 9d1b76e

File tree

1 file changed

+141
-7
lines changed

1 file changed

+141
-7
lines changed

Orm/Xtensive.Orm.Tests/Issues/IssueGithub0048_CoalesceReturnsWrongResults.cs

Lines changed: 141 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
15
using System;
26
using System.Collections.Generic;
37
using System.Diagnostics.CodeAnalysis;
@@ -231,6 +235,52 @@ public void CoalesceWithTwoEntityFieldsTest()
231235
}
232236
}
233237

238+
[Test]
239+
public void CoalesceComplexEntityTest1()
240+
{
241+
using (var session = Domain.OpenSession())
242+
using (var tx = session.OpenTransaction()) {
243+
var nullAuthorInstance = session.Query.All<Author>().FirstOrDefault(a => a.Id == nullAuthorId);
244+
245+
var localResult = session.Query.All<Book>()
246+
.AsEnumerable()
247+
.Select(a => a.Author ?? a.NoAuthor ?? nullAuthorInstance)
248+
.Where(a => a.FullName.FirstName != null)
249+
.OrderBy(a => a.FullName.FirstName)
250+
.ToList(7);
251+
252+
var ex = Assert.Throws<QueryTranslationException>(() => session.Query.All<Book>()
253+
.Select(a => a.Author ?? a.NoAuthor ?? nullAuthorInstance)
254+
.Where(a => a.FullName.FirstName != null)
255+
.OrderBy(a => a.FullName.FirstName).Run());
256+
Assert.That(ex.InnerException, Is.InstanceOf<NotSupportedException>());
257+
Assert.That(ex.InnerException.Message.Contains("Coalesce expressions", StringComparison.Ordinal));
258+
}
259+
}
260+
261+
[Test]
262+
public void CoalesceComplexEntityTest2()
263+
{
264+
using (var session = Domain.OpenSession())
265+
using (var tx = session.OpenTransaction()) {
266+
var nullAuthorInstance = session.Query.All<Author>().FirstOrDefault(a => a.Id == nullAuthorId);
267+
268+
var localResult = session.Query.All<Book>()
269+
.AsEnumerable()
270+
.Select(a => a.Author ?? nullAuthorInstance ?? a.NoAuthor)
271+
.Where(a => a.FullName.FirstName != null)
272+
.OrderBy(a => a.FullName.FirstName)
273+
.ToList(7);
274+
275+
var ex = Assert.Throws<QueryTranslationException>(() => session.Query.All<Book>()
276+
.Select(a => a.Author ?? nullAuthorInstance ?? a.NoAuthor)
277+
.Where(a => a.FullName.FirstName != null)
278+
.OrderBy(a => a.FullName.FirstName).Run());
279+
Assert.That(ex.InnerException, Is.InstanceOf<NotSupportedException>());
280+
Assert.That(ex.InnerException.Message.Contains("Coalesce expressions", StringComparison.Ordinal));
281+
}
282+
}
283+
234284
[Test]
235285
public void CoalesceWithNullConstantTest01()
236286
{
@@ -319,6 +369,48 @@ public void CoalesceWithNullObjectStructureTest02()
319369
}
320370
}
321371

372+
[Test]
373+
public void CoalesceComplexStructureTest1()
374+
{
375+
using (var session = Domain.OpenSession())
376+
using (var tx = session.OpenTransaction()) {
377+
var nullFullNameInstance = new AuthorName();
378+
379+
var localResult = session.Query.All<Author>()
380+
.AsEnumerable()
381+
.Select(a => a.FullName ?? a.NoFullName ?? nullFullNameInstance)
382+
.Where(a => a.FirstName.Length > 0)
383+
.ToList(7);
384+
385+
var ex = Assert.Throws<QueryTranslationException>(() => session.Query.All<Author>()
386+
.Select(a => a.FullName ?? a.NoFullName ?? nullFullNameInstance)
387+
.Where(a => a.FirstName.Length > 0).Run());
388+
Assert.That(ex.InnerException, Is.InstanceOf<NotSupportedException>());
389+
Assert.That(ex.InnerException.Message.Contains("Coalesce expressions", StringComparison.Ordinal));
390+
}
391+
}
392+
393+
[Test]
394+
public void CoalesceComplexStructureTest2()
395+
{
396+
using (var session = Domain.OpenSession())
397+
using (var tx = session.OpenTransaction()) {
398+
var nullFullNameInstance = new AuthorName();
399+
400+
var localResult = session.Query.All<Author>()
401+
.AsEnumerable()
402+
.Select(a => a.FullName ?? nullFullNameInstance ?? a.NoFullName)
403+
.Where(a => a.FirstName.Length > 0)
404+
.ToList(7);
405+
406+
var ex = Assert.Throws<QueryTranslationException>(() => session.Query.All<Author>()
407+
.Select(a => a.FullName ?? nullFullNameInstance ?? a.NoFullName)
408+
.Where(a => a.FirstName.Length > 0).Run());
409+
Assert.That(ex.InnerException, Is.InstanceOf<NotSupportedException>());
410+
Assert.That(ex.InnerException.Message.Contains("Coalesce expressions", StringComparison.Ordinal));
411+
}
412+
}
413+
322414
[Test]
323415
public void CoalesceWithNumericFieldTest01()
324416
{
@@ -468,6 +560,31 @@ public void TernaryWithNullObjectEntityTest01()
468560
}
469561
}
470562

563+
[Test]
564+
[SuppressMessage("Style", "IDE0029:Use coalesce expression", Justification = "Test suppose to have ternary operator, not coalesce one")]
565+
public void TernaryWithNullObjectEntityTest02()
566+
{
567+
using (var session = Domain.OpenSession())
568+
using (var tx = session.OpenTransaction()) {
569+
var nullAuthorInstance = session.Query.All<Author>().FirstOrDefault(a => a.Id == nullAuthorId);
570+
571+
var localResult = session.Query.All<Book>()
572+
.AsEnumerable()
573+
.Select(book => book.Author == null ? nullAuthorInstance : book.Author)
574+
.Where(a => a.FullName.FirstName != null)
575+
.OrderBy(a => a.FullName.FirstName)
576+
.ToList(7);
577+
578+
var ex = Assert.Throws<QueryTranslationException>(() => session.Query.All<Book>()
579+
.Select(book => book.Author == null ? nullAuthorInstance : book.Author)
580+
.Where(a => a.FullName.FirstName != null)
581+
.OrderBy(a => a.FullName.FirstName)
582+
.Run());
583+
Assert.That(ex.InnerException, Is.InstanceOf<NotSupportedException>());
584+
Assert.That(ex.InnerException.Message.Contains("Conditional expressions", StringComparison.Ordinal));
585+
}
586+
}
587+
471588
[Test]
472589
public void TernaryWithTwoEntityFieldsTest()
473590
{
@@ -492,22 +609,39 @@ public void TernaryWithTwoEntityFieldsTest()
492609
}
493610

494611
[Test]
495-
[SuppressMessage("Style", "IDE0029:Use coalesce expression", Justification = "Test suppose to have ternary operator, not coalesce one")]
496-
public void TernaryWithNullObjectEntityTest02()
612+
public void TernaryComplexEntityTest1()
497613
{
498614
using (var session = Domain.OpenSession())
499615
using (var tx = session.OpenTransaction()) {
500616
var nullAuthorInstance = session.Query.All<Author>().FirstOrDefault(a => a.Id == nullAuthorId);
501617

502-
var localResult = session.Query.All<Book>()
503-
.AsEnumerable()
504-
.Select(book => book.Author == null ? nullAuthorInstance : book.Author)
618+
var ex = Assert.Throws<QueryTranslationException>(() => session.Query.All<Book>()
619+
.Select(book => book.Author.Id > 0
620+
? (book.Author.Id > 10)
621+
? book.Author
622+
: book.NoAuthor
623+
: nullAuthorInstance)
505624
.Where(a => a.FullName.FirstName != null)
506625
.OrderBy(a => a.FullName.FirstName)
507-
.ToList(7);
626+
.Run());
627+
Assert.That(ex.InnerException, Is.InstanceOf<NotSupportedException>());
628+
Assert.That(ex.InnerException.Message.Contains("Conditional expressions", StringComparison.Ordinal));
629+
}
630+
}
631+
632+
[Test]
633+
public void TernaryComplexEntityTest2()
634+
{
635+
using (var session = Domain.OpenSession())
636+
using (var tx = session.OpenTransaction()) {
637+
var nullAuthorInstance = session.Query.All<Author>().FirstOrDefault(a => a.Id == nullAuthorId);
508638

509639
var ex = Assert.Throws<QueryTranslationException>(() => session.Query.All<Book>()
510-
.Select(book => book.Author == null ? nullAuthorInstance : book.Author)
640+
.Select(book => book.Author.Id > 0
641+
? (book.Author.Id > 10)
642+
? book.Author
643+
: nullAuthorInstance
644+
: book.NoAuthor)
511645
.Where(a => a.FullName.FirstName != null)
512646
.OrderBy(a => a.FullName.FirstName)
513647
.Run());

0 commit comments

Comments
 (0)