6
6
7
7
import static org .assertj .core .api .Assertions .assertThat ;
8
8
import static org .assertj .core .api .Assertions .entry ;
9
+ import static org .assertj .core .data .Offset .offset ;
9
10
import static org .hibernate .search .util .impl .integrationtest .mapper .orm .OrmUtils .with ;
10
11
11
12
import java .sql .Date ;
@@ -284,9 +285,14 @@ void terms_value() {
284
285
Map <Genre , Double > sumByPrice = result .aggregation ( sumByCategoryKey );
285
286
// end::terms-sum[]
286
287
assertThat ( sumByPrice )
287
- .containsExactly (
288
- entry ( Genre .SCIENCE_FICTION , 60.97 ),
289
- entry ( Genre .CRIME_FICTION , 7.99 )
288
+ .hasSize ( 2 )
289
+ .containsOnlyKeys (
290
+ Genre .SCIENCE_FICTION ,
291
+ Genre .CRIME_FICTION
292
+ )
293
+ .satisfies (
294
+ map -> assertThat ( map .get ( Genre .SCIENCE_FICTION ) ).isCloseTo ( 60.97 , offset ( 0.0001 ) ),
295
+ map -> assertThat ( map .get ( Genre .CRIME_FICTION ) ).isCloseTo ( 7.99 , offset ( 0.0001 ) )
290
296
);
291
297
} );
292
298
@@ -354,11 +360,41 @@ record PriceAggregation(Double avg, Double min, Double max) {
354
360
Map <Double , PriceAggregation > countsByPrice = result .aggregation ( countsByPriceKey );
355
361
// end::terms-count-composite[]
356
362
assertThat ( countsByPrice )
357
- .containsExactly (
358
- entry ( 7.99 , new PriceAggregation ( 7.99 , 7.99 , 7.99 ) ),
359
- entry ( 15.99 , new PriceAggregation ( 15.99 , 15.99 , 15.99 ) ),
360
- entry ( 19.99 , new PriceAggregation ( 19.99 , 19.99 , 19.99 ) ),
361
- entry ( 24.99 , new PriceAggregation ( 24.99 , 24.99 , 24.99 ) )
363
+ .hasSize ( 4 )
364
+ .containsOnlyKeys ( 7.99 , 15.99 , 19.99 , 24.99 )
365
+ .satisfies (
366
+ map -> {
367
+ var agg = map .get ( 7.99 );
368
+ assertThat ( agg ).satisfies (
369
+ a -> assertThat ( a .avg () ).isCloseTo ( 7.99 , offset ( 0.0001 ) ),
370
+ a -> assertThat ( a .min () ).isCloseTo ( 7.99 , offset ( 0.0001 ) ),
371
+ a -> assertThat ( a .max () ).isCloseTo ( 7.99 , offset ( 0.0001 ) )
372
+ );
373
+ },
374
+ map -> {
375
+ var agg = map .get ( 15.99 );
376
+ assertThat ( agg ).satisfies (
377
+ a -> assertThat ( a .avg () ).isCloseTo ( 15.99 , offset ( 0.0001 ) ),
378
+ a -> assertThat ( a .min () ).isCloseTo ( 15.99 , offset ( 0.0001 ) ),
379
+ a -> assertThat ( a .max () ).isCloseTo ( 15.99 , offset ( 0.0001 ) )
380
+ );
381
+ },
382
+ map -> {
383
+ var agg = map .get ( 19.99 );
384
+ assertThat ( agg ).satisfies (
385
+ a -> assertThat ( a .avg () ).isCloseTo ( 19.99 , offset ( 0.0001 ) ),
386
+ a -> assertThat ( a .min () ).isCloseTo ( 19.99 , offset ( 0.0001 ) ),
387
+ a -> assertThat ( a .max () ).isCloseTo ( 19.99 , offset ( 0.0001 ) )
388
+ );
389
+ },
390
+ map -> {
391
+ var agg = map .get ( 24.99 );
392
+ assertThat ( agg ).satisfies (
393
+ a -> assertThat ( a .avg () ).isCloseTo ( 24.99 , offset ( 0.0001 ) ),
394
+ a -> assertThat ( a .min () ).isCloseTo ( 24.99 , offset ( 0.0001 ) ),
395
+ a -> assertThat ( a .max () ).isCloseTo ( 24.99 , offset ( 0.0001 ) )
396
+ );
397
+ }
362
398
);
363
399
} );
364
400
}
@@ -382,10 +418,16 @@ void range_value() {
382
418
Map <Range <Double >, Double > countsByPrice = result .aggregation ( avgRatingByPriceKey );
383
419
// end::range-avg[]
384
420
assertThat ( countsByPrice )
385
- .containsExactly (
386
- entry ( Range .canonical ( 0.0 , 10.0 ), 4.0 ),
387
- entry ( Range .canonical ( 10.0 , 20.0 ), 3.6 ),
388
- entry ( Range .canonical ( 20.0 , null ), 3.2 )
421
+ .hasSize ( 3 )
422
+ .containsOnlyKeys (
423
+ Range .canonical ( 0.0 , 10.0 ),
424
+ Range .canonical ( 10.0 , 20.0 ),
425
+ Range .canonical ( 20.0 , null )
426
+ )
427
+ .satisfies (
428
+ map -> assertThat ( map .get ( Range .canonical ( 0.0 , 10.0 ) ) ).isCloseTo ( 4.0 , offset ( 0.0001 ) ),
429
+ map -> assertThat ( map .get ( Range .canonical ( 10.0 , 20.0 ) ) ).isCloseTo ( 3.6 , offset ( 0.0001 ) ),
430
+ map -> assertThat ( map .get ( Range .canonical ( 20.0 , null ) ) ).isCloseTo ( 3.2 , offset ( 0.0001 ) )
389
431
);
390
432
} );
391
433
@@ -437,10 +479,37 @@ record PriceAggregation(Double avg, Double min, Double max) {
437
479
Map <Range <Double >, PriceAggregation > countsByPrice = result .aggregation ( countsByPriceKey );
438
480
// end::range-composite[]
439
481
assertThat ( countsByPrice )
440
- .containsExactly (
441
- entry ( Range .canonical ( 0.0 , 10.0 ), new PriceAggregation ( 7.99 , 7.99 , 7.99 ) ),
442
- entry ( Range .canonical ( 10.0 , 20.0 ), new PriceAggregation ( 17.99 , 15.99 , 19.99 ) ),
443
- entry ( Range .canonical ( 20.0 , null ), new PriceAggregation ( 24.99 , 24.99 , 24.99 ) )
482
+ .hasSize ( 3 )
483
+ .containsOnlyKeys (
484
+ Range .canonical ( 0.0 , 10.0 ),
485
+ Range .canonical ( 10.0 , 20.0 ),
486
+ Range .canonical ( 20.0 , null )
487
+ )
488
+ .satisfies (
489
+ map -> {
490
+ var agg = map .get ( Range .canonical ( 0.0 , 10.0 ) );
491
+ assertThat ( agg ).satisfies (
492
+ a -> assertThat ( a .avg () ).isCloseTo ( 7.99 , offset ( 0.0001 ) ),
493
+ a -> assertThat ( a .min () ).isCloseTo ( 7.99 , offset ( 0.0001 ) ),
494
+ a -> assertThat ( a .max () ).isCloseTo ( 7.99 , offset ( 0.0001 ) )
495
+ );
496
+ },
497
+ map -> {
498
+ var agg = map .get ( Range .canonical ( 10.0 , 20.0 ) );
499
+ assertThat ( agg ).satisfies (
500
+ a -> assertThat ( a .avg () ).isCloseTo ( 17.99 , offset ( 0.0001 ) ),
501
+ a -> assertThat ( a .min () ).isCloseTo ( 15.99 , offset ( 0.0001 ) ),
502
+ a -> assertThat ( a .max () ).isCloseTo ( 19.99 , offset ( 0.0001 ) )
503
+ );
504
+ },
505
+ map -> {
506
+ var agg = map .get ( Range .canonical ( 20.0 , null ) );
507
+ assertThat ( agg ).satisfies (
508
+ a -> assertThat ( a .avg () ).isCloseTo ( 24.99 , offset ( 0.0001 ) ),
509
+ a -> assertThat ( a .min () ).isCloseTo ( 24.99 , offset ( 0.0001 ) ),
510
+ a -> assertThat ( a .max () ).isCloseTo ( 24.99 , offset ( 0.0001 ) )
511
+ );
512
+ }
444
513
);
445
514
} );
446
515
}
@@ -643,7 +712,7 @@ void sum() {
643
712
.fetch ( 20 );
644
713
Double sumPrices = result .aggregation ( sumPricesKey );
645
714
// end::sums[]
646
- assertThat ( sumPrices ).isEqualTo ( 60.97 );
715
+ assertThat ( sumPrices ).isCloseTo ( 60.97 , offset ( 0.0001 ) );
647
716
} );
648
717
}
649
718
@@ -733,7 +802,7 @@ void avg() {
733
802
.fetch ( 20 );
734
803
Double avgPrices = result .aggregation ( avgPricesKey );
735
804
// end::avg[]
736
- assertThat ( avgPrices ).isEqualTo ( 20.323333333333334 );
805
+ assertThat ( avgPrices ).isCloseTo ( 20.323333333333334 , offset ( 0.0001 ) );
737
806
} );
738
807
}
739
808
@@ -757,7 +826,11 @@ record PriceAggregation(Double avg, Double min, Double max) {
757
826
.fetch ( 20 );
758
827
PriceAggregation aggregations = result .aggregation ( avgPricesKey ); // <4>
759
828
// end::composite-customObject[]
760
- assertThat ( aggregations ).isEqualTo ( new PriceAggregation ( 17.24 , 7.99 , 24.99 ) );
829
+ assertThat ( aggregations ).satisfies (
830
+ a -> assertThat ( a .avg () ).isCloseTo ( 17.24 , offset ( 0.0001 ) ),
831
+ a -> assertThat ( a .min () ).isCloseTo ( 7.99 , offset ( 0.0001 ) ),
832
+ a -> assertThat ( a .max () ).isCloseTo ( 24.99 , offset ( 0.0001 ) )
833
+ );
761
834
} );
762
835
763
836
withinSearchSession ( searchSession -> {
@@ -784,7 +857,11 @@ record BookAggregation(Double avg, Double min, Double max, Long ratingCount) {
784
857
.fetch ( 20 );
785
858
BookAggregation aggregations = result .aggregation ( aggKey ); // <4>
786
859
// end::composite-customObject-asList[]
787
- assertThat ( aggregations ).isEqualTo ( new BookAggregation ( 17.24 , 7.99 , 24.99 , 20L ) );
860
+ assertThat ( aggregations ).satisfies (
861
+ a -> assertThat ( a .avg () ).isCloseTo ( 17.24 , offset ( 0.0001 ) ),
862
+ a -> assertThat ( a .min () ).isCloseTo ( 7.99 , offset ( 0.0001 ) ),
863
+ a -> assertThat ( a .max () ).isCloseTo ( 24.99 , offset ( 0.0001 ) )
864
+ );
788
865
} );
789
866
790
867
withinSearchSession ( searchSession -> {
@@ -803,8 +880,13 @@ record BookAggregation(Double avg, Double min, Double max, Long ratingCount) {
803
880
List <?> aggregations = result .aggregation ( aggKey ); // <4>
804
881
// end::composite-list[]
805
882
assertThat ( (List ) aggregations )
806
- .hasSize ( 4 )
807
- .containsExactly ( 17.24 , 7.99 , 24.99 , 20L );
883
+ .hasSize ( 4 );
884
+ assertThat ( aggregations ).satisfies (
885
+ a -> assertThat ( (Double ) a .get ( 0 ) ).isCloseTo ( 17.24 , offset ( 0.0001 ) ),
886
+ a -> assertThat ( (Double ) a .get ( 1 ) ).isCloseTo ( 7.99 , offset ( 0.0001 ) ),
887
+ a -> assertThat ( (Double ) a .get ( 2 ) ).isCloseTo ( 24.99 , offset ( 0.0001 ) ),
888
+ a -> assertThat ( (Long ) a .get ( 3 ) ).isEqualTo ( 20L )
889
+ );
808
890
} );
809
891
810
892
withinSearchSession ( searchSession -> {
@@ -823,8 +905,13 @@ record BookAggregation(Double avg, Double min, Double max, Long ratingCount) {
823
905
Object [] aggregations = result .aggregation ( aggKey ); // <4>
824
906
// end::composite-array[]
825
907
assertThat ( aggregations )
826
- .hasSize ( 4 )
827
- .containsExactly ( 17.24 , 7.99 , 24.99 , 20L );
908
+ .hasSize ( 4 );
909
+ assertThat ( aggregations ).satisfies (
910
+ a -> assertThat ( (Double ) a [0 ] ).isCloseTo ( 17.24 , offset ( 0.0001 ) ),
911
+ a -> assertThat ( (Double ) a [1 ] ).isCloseTo ( 7.99 , offset ( 0.0001 ) ),
912
+ a -> assertThat ( (Double ) a [2 ] ).isCloseTo ( 24.99 , offset ( 0.0001 ) ),
913
+ a -> assertThat ( (Long ) a [3 ] ).isEqualTo ( 20L )
914
+ );
828
915
} );
829
916
830
917
withinSearchSession ( searchSession -> {
@@ -842,8 +929,13 @@ record BookAggregation(Double avg, Double min, Double max, Long ratingCount) {
842
929
List <?> aggregations = result .aggregation ( aggKey ); // <4>
843
930
// end::composite-list-singlestep[]
844
931
assertThat ( (List ) aggregations )
845
- .hasSize ( 4 )
846
- .containsExactly ( 17.24 , 7.99 , 24.99 , 20L );
932
+ .hasSize ( 4 );
933
+ assertThat ( aggregations ).satisfies (
934
+ a -> assertThat ( (Double ) a .get ( 0 ) ).isCloseTo ( 17.24 , offset ( 0.0001 ) ),
935
+ a -> assertThat ( (Double ) a .get ( 1 ) ).isCloseTo ( 7.99 , offset ( 0.0001 ) ),
936
+ a -> assertThat ( (Double ) a .get ( 2 ) ).isCloseTo ( 24.99 , offset ( 0.0001 ) ),
937
+ a -> assertThat ( (Long ) a .get ( 3 ) ).isEqualTo ( 20L )
938
+ );
847
939
} );
848
940
}
849
941
0 commit comments