Skip to content

Commit

Permalink
Additional tests for Timeframe
Browse files Browse the repository at this point in the history
Peter Dekkers committed Nov 14, 2022
1 parent d40b4f8 commit eb161f4
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 0 additions & 4 deletions roboquant/src/main/kotlin/org/roboquant/common/Timeframe.kt
Original file line number Diff line number Diff line change
@@ -237,10 +237,6 @@ data class Timeframe(val start: Instant, val end: Instant, val inclusive: Boolea
return start.atZone(zoneId).toLocalDate() == realEnd.atZone(zoneId).toLocalDate()
}





/**
* Convert this timeframe to a [Timeline] where each time seperated by a [step] amount.
* If the temporalAmount is defined as a [Period], the [Config.defaultZoneId] will be used as the ZoneId.
13 changes: 10 additions & 3 deletions roboquant/src/test/kotlin/org/roboquant/common/TimeframeTest.kt
Original file line number Diff line number Diff line change
@@ -18,7 +18,9 @@ package org.roboquant.common

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.time.Instant
import java.time.ZoneId
import kotlin.math.absoluteValue
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
@@ -86,15 +88,18 @@ internal class TimeframeTest {
}
}


private fun Double.roughly(other: Double) = (this - other).absoluteValue < (0.01 + this.absoluteValue / 100.0)

@Test
fun annualize() {
val tf = Timeframe.fromYears(2019, 2020)
val x = tf.annualize(0.1)
assertTrue(x - 0.1 < 0.01)
assertTrue(x.roughly(0.1))

val tf2 = Timeframe.fromYears(2019, 2020)
val tf2 = Timeframe.fromYears(2019, 2021)
val y = tf2.annualize(0.1)
assertTrue(0.05 - y < 0.1)
assertTrue(y.roughly(0.05))
}

@Test
@@ -115,6 +120,8 @@ internal class TimeframeTest {
fun toTimeline() {
val tf = Timeframe.parse("2020-01-01T18:00:00Z", "2021-12-31T18:00:00Z")
val timeline = tf.toTimeline(1.days)
assertEquals(Instant.parse("2020-01-01T18:00:00Z"), timeline.first())
assertTrue(Instant.parse("2021-12-31T18:00:00Z") > timeline.last())
assertTrue(timeline.size > 200)
}
}

0 comments on commit eb161f4

Please sign in to comment.