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
+
1
5
using System ;
2
6
using System . Collections . Generic ;
3
7
using System . Diagnostics . CodeAnalysis ;
@@ -231,6 +235,52 @@ public void CoalesceWithTwoEntityFieldsTest()
231
235
}
232
236
}
233
237
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
+
234
284
[ Test ]
235
285
public void CoalesceWithNullConstantTest01 ( )
236
286
{
@@ -319,6 +369,48 @@ public void CoalesceWithNullObjectStructureTest02()
319
369
}
320
370
}
321
371
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
+
322
414
[ Test ]
323
415
public void CoalesceWithNumericFieldTest01 ( )
324
416
{
@@ -468,6 +560,31 @@ public void TernaryWithNullObjectEntityTest01()
468
560
}
469
561
}
470
562
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
+
471
588
[ Test ]
472
589
public void TernaryWithTwoEntityFieldsTest ( )
473
590
{
@@ -492,22 +609,39 @@ public void TernaryWithTwoEntityFieldsTest()
492
609
}
493
610
494
611
[ 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 ( )
497
613
{
498
614
using ( var session = Domain . OpenSession ( ) )
499
615
using ( var tx = session . OpenTransaction ( ) ) {
500
616
var nullAuthorInstance = session . Query . All < Author > ( ) . FirstOrDefault ( a => a . Id == nullAuthorId ) ;
501
617
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 )
505
624
. Where ( a => a . FullName . FirstName != null )
506
625
. 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 ) ;
508
638
509
639
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 )
511
645
. Where ( a => a . FullName . FirstName != null )
512
646
. OrderBy ( a => a . FullName . FirstName )
513
647
. Run ( ) ) ;
0 commit comments