From fc384443c3730f4bad69a630970cb3de8edbad1c Mon Sep 17 00:00:00 2001 From: "a.usenko" Date: Wed, 27 Dec 2023 16:24:05 +0200 Subject: [PATCH] Remove generic Add test --- .../dev/evo/elasticmagic/aggs/Metric.kt | 4 +- .../dev/evo/elasticmagic/aggs/MetricTests.kt | 47 ++++++++++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/elasticmagic/src/commonMain/kotlin/dev/evo/elasticmagic/aggs/Metric.kt b/elasticmagic/src/commonMain/kotlin/dev/evo/elasticmagic/aggs/Metric.kt index cfca4fb6c4..9a54b8a285 100644 --- a/elasticmagic/src/commonMain/kotlin/dev/evo/elasticmagic/aggs/Metric.kt +++ b/elasticmagic/src/commonMain/kotlin/dev/evo/elasticmagic/aggs/Metric.kt @@ -130,8 +130,8 @@ data class PercentileAggResult(val values: Map) : AggregationRes } } -data class PercentilesAgg( - val field: FieldOperations, +data class PercentilesAgg( + val field: FieldOperations<*>, val percents: List = listOf(1.0, 5.0, 25.0, 50.0, 75.0, 95.0, 99.0), ) : MetricAggregation() { override fun processResult(obj: Deserializer.ObjectCtx): PercentileAggResult { diff --git a/elasticmagic/src/commonTest/kotlin/dev/evo/elasticmagic/aggs/MetricTests.kt b/elasticmagic/src/commonTest/kotlin/dev/evo/elasticmagic/aggs/MetricTests.kt index 882c077a20..c6559152f2 100644 --- a/elasticmagic/src/commonTest/kotlin/dev/evo/elasticmagic/aggs/MetricTests.kt +++ b/elasticmagic/src/commonTest/kotlin/dev/evo/elasticmagic/aggs/MetricTests.kt @@ -59,25 +59,39 @@ class MetricTests : TestAggregation() { @Test fun percentile() { - val agg = PercentilesAgg( - MovieDoc.rating, - percents = listOf(1.0, 5.0, 25.0, 50.0, 75.0, 95.0, 99.0), + val ratingAgg = PercentilesAgg( + MovieDoc.rating ) - agg.compile() shouldContainExactly mapOf( + ratingAgg.compile() shouldContainExactly mapOf( "percentiles" to mapOf( "field" to "rating", "percents" to listOf(1.0, 5.0, 25.0, 50.0, 75.0, 95.0, 99.0), ) ) + + val numRatingsAgg = PercentilesAgg( + MovieDoc.numRatings, + percents = listOf(25.0, 50.0, 75.0, 95.0, 99.0), + ) + + numRatingsAgg.compile() shouldContainExactly mapOf( + "percentiles" to mapOf( + "field" to "num_ratings", + "percents" to listOf(25.0, 50.0, 75.0, 95.0, 99.0), + ) + ) + + + shouldThrow { process( - agg, + ratingAgg, mapOf("values" to null) ) } process( - agg, + ratingAgg, mapOf( "values" to mapOf( "1.0" to 0.9, @@ -100,6 +114,27 @@ class MetricTests : TestAggregation() { 99.0 to 6.6, ) } + + process( + numRatingsAgg, + mapOf( + "values" to mapOf( + "25.0" to 1.0, + "50.0" to 2.0, + "75.0" to 3.0, + "95.0" to 4.0, + "99.0" to 5.0, + ), + ) + ).let { res -> + res.values shouldBe mapOf( + 25.0 to 1.0, + 50.0 to 2.0, + 75.0 to 3.0, + 95.0 to 4.0, + 99.0 to 5.0, + ) + } } @Test