Skip to content

Commit 273aa90

Browse files
committed
HSEARCH-3661 Follow up: Make aggregation tests less fragile when comparing double results
sometimes the elasticsearch backend would add some calculation error delta in the results
1 parent 827aa2a commit 273aa90

File tree

1 file changed

+118
-26
lines changed

1 file changed

+118
-26
lines changed

documentation/src/test/java/org/hibernate/search/documentation/search/aggregation/AggregationDslIT.java

Lines changed: 118 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import static org.assertj.core.api.Assertions.assertThat;
88
import static org.assertj.core.api.Assertions.entry;
9+
import static org.assertj.core.data.Offset.offset;
910
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with;
1011

1112
import java.sql.Date;
@@ -284,9 +285,14 @@ void terms_value() {
284285
Map<Genre, Double> sumByPrice = result.aggregation( sumByCategoryKey );
285286
// end::terms-sum[]
286287
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 ) )
290296
);
291297
} );
292298

@@ -354,11 +360,41 @@ record PriceAggregation(Double avg, Double min, Double max) {
354360
Map<Double, PriceAggregation> countsByPrice = result.aggregation( countsByPriceKey );
355361
// end::terms-count-composite[]
356362
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+
}
362398
);
363399
} );
364400
}
@@ -382,10 +418,16 @@ void range_value() {
382418
Map<Range<Double>, Double> countsByPrice = result.aggregation( avgRatingByPriceKey );
383419
// end::range-avg[]
384420
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 ) )
389431
);
390432
} );
391433

@@ -437,10 +479,37 @@ record PriceAggregation(Double avg, Double min, Double max) {
437479
Map<Range<Double>, PriceAggregation> countsByPrice = result.aggregation( countsByPriceKey );
438480
// end::range-composite[]
439481
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+
}
444513
);
445514
} );
446515
}
@@ -643,7 +712,7 @@ void sum() {
643712
.fetch( 20 );
644713
Double sumPrices = result.aggregation( sumPricesKey );
645714
// end::sums[]
646-
assertThat( sumPrices ).isEqualTo( 60.97 );
715+
assertThat( sumPrices ).isCloseTo( 60.97, offset( 0.0001 ) );
647716
} );
648717
}
649718

@@ -733,7 +802,7 @@ void avg() {
733802
.fetch( 20 );
734803
Double avgPrices = result.aggregation( avgPricesKey );
735804
// end::avg[]
736-
assertThat( avgPrices ).isEqualTo( 20.323333333333334 );
805+
assertThat( avgPrices ).isCloseTo( 20.323333333333334, offset( 0.0001 ) );
737806
} );
738807
}
739808

@@ -757,7 +826,11 @@ record PriceAggregation(Double avg, Double min, Double max) {
757826
.fetch( 20 );
758827
PriceAggregation aggregations = result.aggregation( avgPricesKey ); // <4>
759828
// 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+
);
761834
} );
762835

763836
withinSearchSession( searchSession -> {
@@ -784,7 +857,11 @@ record BookAggregation(Double avg, Double min, Double max, Long ratingCount) {
784857
.fetch( 20 );
785858
BookAggregation aggregations = result.aggregation( aggKey ); // <4>
786859
// 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+
);
788865
} );
789866

790867
withinSearchSession( searchSession -> {
@@ -803,8 +880,13 @@ record BookAggregation(Double avg, Double min, Double max, Long ratingCount) {
803880
List<?> aggregations = result.aggregation( aggKey ); // <4>
804881
// end::composite-list[]
805882
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+
);
808890
} );
809891

810892
withinSearchSession( searchSession -> {
@@ -823,8 +905,13 @@ record BookAggregation(Double avg, Double min, Double max, Long ratingCount) {
823905
Object[] aggregations = result.aggregation( aggKey ); // <4>
824906
// end::composite-array[]
825907
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+
);
828915
} );
829916

830917
withinSearchSession( searchSession -> {
@@ -842,8 +929,13 @@ record BookAggregation(Double avg, Double min, Double max, Long ratingCount) {
842929
List<?> aggregations = result.aggregation( aggKey ); // <4>
843930
// end::composite-list-singlestep[]
844931
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+
);
847939
} );
848940
}
849941

0 commit comments

Comments
 (0)