Skip to content

Commit

Permalink
Convert to the next unit when the date pattern value is bigger than 1M (
Browse files Browse the repository at this point in the history
#273)

Co-authored-by: Francisco Sanmartin <[email protected]>
  • Loading branch information
2 people authored and azzmiks committed Jan 12, 2025
1 parent 9d3548c commit aaf5c40
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### New features & improvements

### Bug fixes
- Convert to the next unit when the date pattern value is bigger than 1M ([#274](https://github.com/personio/datadog-synthetic-test-support/pull/273))

### Dependencies
- Update commons-text dependency from 1.12.0 to 1.13.0 ([#270](https://github.com/personio/datadog-synthetic-test-support/pull/270))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ abstract class SyntheticTestBuilder(
) {
val (scaledValue, unit) =
checkNotNull(getScaledDate(duration)) {
"The passed duration should be less than 10_000_000 days for the date pattern variable $name."
"The passed duration should be between -1_000_000 and 1_000_000 days for the date pattern variable $name."
}
addLocalVariable(name, "$prefix{{ date($scaledValue$unit, $format) }}$suffix")
}
Expand All @@ -335,7 +335,7 @@ abstract class SyntheticTestBuilder(
) {
val (scaledValue, unit) =
checkNotNull(getScaledTimestamp(duration)) {
"The passed duration should be less than 1_000_000_000 seconds for the timestamp pattern variable $name."
"The passed duration should be between -999_999_999 and 999_999_999 seconds for the timestamp pattern variable $name."
}
addLocalVariable(name, "$prefix{{ timestamp($scaledValue, $unit) }}$suffix")
}
Expand Down Expand Up @@ -375,13 +375,13 @@ abstract class SyntheticTestBuilder(
private fun getScaledDate(value: Duration): Pair<Long, String>? =
value.getScaledValue(
sequenceOf(DurationUnit.MILLISECONDS, DurationUnit.SECONDS, DurationUnit.MINUTES, DurationUnit.HOURS, DurationUnit.DAYS),
10_000_000,
1_000_000,
)

private fun getScaledTimestamp(value: Duration): Pair<Long, String>? =
value.getScaledValue(
sequenceOf(DurationUnit.MILLISECONDS, DurationUnit.SECONDS),
1_000_000_000,
999_999_999,
)

private fun Duration.getScaledValue(
Expand Down
10 changes: 5 additions & 5 deletions src/main/kotlin/com/personio/synthetics/config/Variable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fun BrowserTest.datePatternVariable(
) = apply {
val (scaledValue, unit) =
checkNotNull(getScaledDate(duration)) {
"The passed duration should be less than 10_000_000 days for the date pattern variable $name."
"The passed duration should be between -1_000_000 and 1_000_000 days for the date pattern variable $name."
}
addLocalVariable(name, "$prefix{{ date($scaledValue$unit, $format) }}$suffix")
}
Expand All @@ -116,7 +116,7 @@ fun BrowserTest.timestampPatternVariable(
) = apply {
val (scaledValue, unit) =
checkNotNull(getScaledTimestamp(duration)) {
"The passed duration should be less than 1_000_000_000 seconds for the timestamp pattern variable $name."
"The passed duration should be between -999_999_999 and 999_999_999 seconds for the timestamp pattern variable $name."
}
addLocalVariable(name, "$prefix{{ timestamp($scaledValue, $unit) }}$suffix")
}
Expand Down Expand Up @@ -164,13 +164,13 @@ private fun BrowserTest.addLocalVariable(
private fun getScaledDate(value: Duration): Pair<Long, String>? =
value.getScaledValue(
sequenceOf(DurationUnit.MILLISECONDS, DurationUnit.SECONDS, DurationUnit.MINUTES, DurationUnit.HOURS, DurationUnit.DAYS),
10_000_000,
1_000_000,
)

private fun getScaledTimestamp(value: Duration): Pair<Long, String>? =
value.getScaledValue(
sequenceOf(DurationUnit.MILLISECONDS, DurationUnit.SECONDS),
1_000_000_000,
999_999_999,
)

private fun Duration.getScaledValue(
Expand All @@ -179,7 +179,7 @@ private fun Duration.getScaledValue(
): Pair<Long, String>? =
sequence
.map { unit -> this.toLong(unit) to unit.toDatadogDurationUnit() }
.firstOrNull { (scaled, _) -> scaled.absoluteValue < limit }
.firstOrNull { (scaled, _) -> scaled.absoluteValue <= limit }

private fun DurationUnit.toDatadogDurationUnit(): String {
return when (this) {
Expand Down
72 changes: 66 additions & 6 deletions src/test/kotlin/com/personio/synthetics/config/VariableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,46 @@ class VariableTest {
assertEquals(expectedResult, browserTest.config?.variables?.get(0))
}

@Test
fun `datePatternVariable output is more than 1M seconds so it returns minutes`() {
val variableName = "VARIABLE1"
val dateValue = 45.days
val dateFormat = "YYYY-MM-DD"

val expectedResult =
syntheticBrowserVariable()
.name(variableName)
.pattern("{{ date(${dateValue.inWholeMinutes}m, $dateFormat) }}")

browserTest.datePatternVariable(
name = variableName,
duration = dateValue,
format = dateFormat,
)

assertEquals(expectedResult, browserTest.config?.variables?.get(0))
}

@Test
fun `datePatternVariable output is more than 1M minutes so it returns hours`() {
val variableName = "VARIABLE1"
val dateValue = 700.days
val dateFormat = "YYYY-MM-DD"

val expectedResult =
syntheticBrowserVariable()
.name(variableName)
.pattern("{{ date(${dateValue.inWholeHours}h, $dateFormat) }}")

browserTest.datePatternVariable(
name = variableName,
duration = dateValue,
format = dateFormat,
)

assertEquals(expectedResult, browserTest.config?.variables?.get(0))
}

@Test
fun `datePatternVariable allows a value to be appended before the pattern`() {
val variableName = "VARIABLE1"
Expand Down Expand Up @@ -336,9 +376,9 @@ class VariableTest {
}

@Test
fun `datePatternVariable uses the next higher unit if the value is greater than or equal to 10_000_000`() {
fun `datePatternVariable uses the next higher unit if the value is greater than 1_000_000`() {
val variableName = "VARIABLE1"
val dateValue = 10_000_000.minutes
val dateValue = 1_000_001.minutes
val dateFormat = "YYYY-MM-DD"

val expectedResult =
Expand All @@ -356,9 +396,29 @@ class VariableTest {
}

@Test
fun `datePatternVariable allows passing the duration less than 10_000_000 days`() {
fun `datePatternVariable does not use the next higher unit if the value is lower than 1_000_001`() {
val variableName = "VARIABLE1"
val dateValue = 1_000_000.minutes
val dateFormat = "YYYY-MM-DD"

val expectedResult =
syntheticBrowserVariable()
.name(variableName)
.pattern("{{ date(${dateValue.inWholeMinutes}m, $dateFormat) }}")

browserTest.datePatternVariable(
name = variableName,
duration = dateValue,
format = dateFormat,
)

assertEquals(expectedResult, browserTest.config?.variables?.get(0))
}

@Test
fun `datePatternVariable allows passing the duration less than or equal to 1_000_000 days`() {
val variableName = "VARIABLE1"
val dateValue = 9_999_999.days
val dateValue = 1_000_000.days
val dateFormat = "YYYY-MM-DD"

val expectedResult =
Expand Down Expand Up @@ -416,11 +476,11 @@ class VariableTest {
}

@Test
fun `datePatternVariable throws an exception if the duration is greater than or equal to 10_000_000 days`() {
fun `datePatternVariable throws an exception if the duration is greater than 1_000_000 days`() {
assertThrows<IllegalStateException> {
browserTest.datePatternVariable(
name = "VARIABLE1",
duration = 10_000_000.days,
duration = 1_000_001.days,
format = "YYYY-MM-DD",
)
}
Expand Down

0 comments on commit aaf5c40

Please sign in to comment.