Skip to content

Commit

Permalink
added unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Ronnak Saxena <[email protected]>
  • Loading branch information
ronnaksaxena committed Sep 7, 2023
1 parent 4ef546b commit bf04e32
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ fun parseRollup(response: GetResponse, xContentRegistry: NamedXContentRegistry =

return xcp.parseWithType(response.id, response.seqNo, response.primaryTerm, Rollup.Companion::parse)
}
// Changes aggregations in search source builder to new original aggregation (Change query too?)
// Returns a SearchSourceBuilder with different aggregations but the rest of the properties are the same
@Suppress("ComplexMethod")
fun SearchSourceBuilder.changeAggregations(aggregationBuilderCollection: Collection<AggregationBuilder>): SearchSourceBuilder {
val ssb = SearchSourceBuilder()
Expand Down Expand Up @@ -501,12 +501,14 @@ fun SearchSourceBuilder.changeAggregations(aggregationBuilderCollection: Collect
if (this.collapse() != null) ssb.collapse(this.collapse())
return ssb
}

@Suppress("MagicNumber")
fun convertDateStringToEpochMillis(dateString: String): Long {
val parts = dateString.split(" ")
require(parts.size == 2) { "Date in was not correct format" }
val dateParts = parts[0].split("-")
val timeParts = parts[1].split(":")

require((dateParts.size == 3 && timeParts.size == 3)) { "Date in was not correct format" }
val year = dateParts[0].toInt()
val month = dateParts[1].toInt()
val day = dateParts[2].toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ import org.opensearch.indexmanagement.rollup.randomValueCount
import org.opensearch.indexmanagement.transform.randomAggregationBuilder
import org.opensearch.search.aggregations.metrics.AvgAggregationBuilder
import org.opensearch.search.aggregations.metrics.ValueCountAggregationBuilder
import org.opensearch.search.builder.SearchSourceBuilder
import org.opensearch.test.OpenSearchTestCase
import org.opensearch.test.rest.OpenSearchRestTestCase
import java.time.ZoneId
import java.time.ZonedDateTime
import kotlin.test.assertFails
import kotlin.test.assertFailsWith

class RollupUtilsTests : OpenSearchTestCase() {

Expand Down Expand Up @@ -231,6 +236,59 @@ class RollupUtilsTests : OpenSearchTestCase() {
}
}
fun `test changeAggregations`() {
/* add later */
val ssb = SearchSourceBuilder()
val oldAggBuilder = randomAggregationBuilder()
ssb.aggregation(oldAggBuilder)
var newAgg = randomAggregationBuilder()
while (newAgg == oldAggBuilder) newAgg = randomAggregationBuilder()
val newSsb = ssb.changeAggregations(listOf(newAgg))
assertNotEquals("Did not change search source builders aggregations :(", newSsb, ssb)
}
fun `test convertDateStringToEpochMillis`() {
// Check correct time format
val dateString = "2023-07-18 12:30:00"
val expectedMillis = 1689683400000L
val actualMillis = convertDateStringToEpochMillis(dateString)
assertEquals(expectedMillis, actualMillis)

// Testing an invalid date format throws error
assertFails { convertDateStringToEpochMillis("invalid format") }
}
fun `test convertFixedIntervalStringToMs`() {
// Test ms
val msString = "5ms"
val expectedMs = 5L
assertEquals("ms conversion is wrong", convertFixedIntervalStringToMs(msString), expectedMs)
// Test s
val sString = "5s"
val expectedS = 5000L
assertEquals("ms conversion is wrong", convertFixedIntervalStringToMs(sString), expectedS)
// Test m
val mString = "3m"
val expectedM = 180000L
assertEquals("m conversion is wrong", convertFixedIntervalStringToMs(mString), expectedM)
// Test h
val hString = "2h"
val expectedH = 7200000L
assertEquals("h conversion is wrong", convertFixedIntervalStringToMs(hString), expectedH)
// Test d
val dString = "1d"
val expectedD = 86400000L
assertEquals("d conversion is wrong", convertFixedIntervalStringToMs(dString), expectedD)
// Test w
val wString = "1w"
val expectedW = 604800000L
assertEquals("w conversion is wrong", convertFixedIntervalStringToMs(wString), expectedW)
// Test error
val invalid = ";)"
assertFailsWith<IllegalArgumentException> {
convertFixedIntervalStringToMs(invalid)
}
}
fun `test zonedDateTimeToMillis`() {
val zonedDateTime = ZonedDateTime.of(2023, 7, 18, 12, 30, 0, 0, ZoneId.of("UTC"))
val expectedMillis = 1689683400000L // ms since epoch of the zonedDateTime
val actualMillis = zonedDateTimeToMillis(zonedDateTime)
assertEquals(expectedMillis, actualMillis)
}
}

0 comments on commit bf04e32

Please sign in to comment.