Skip to content

Commit

Permalink
add test, fix query to limit 1
Browse files Browse the repository at this point in the history
  • Loading branch information
nullpointer0x00 committed Oct 3, 2024
1 parent 5c0d4a2 commit 4a5773b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,13 @@ class TokenHistoricalDailyRecord(id: EntityID<DateTime>) : Entity<DateTime>(id)
TokenHistoricalDailyRecord.wrapRows(query).map { it.data }.toList()
}

fun lastKnownPriceForDate(date: DateTime) = transaction {
fun lastKnownPriceForDate(date: DateTime): BigDecimal = transaction {
TokenHistoricalDailyRecord
.find { TokenHistoricalDailyTable.timestamp lessEq date }
.orderBy(Pair(TokenHistoricalDailyTable.timestamp, SortOrder.DESC))
.firstOrNull()?.data?.quote?.get(USD_UPPER)?.close ?: BigDecimal.ZERO
.limit(1)
.map { it.data.quote[USD_UPPER]?.close ?: BigDecimal.ZERO }
.singleOrNull() ?: BigDecimal.ZERO
}

fun getLatestDateEntry(): TokenHistoricalDailyRecord? = transaction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class TokenService(
return@runBlocking allPrices
}

protected fun processHistoricalData(startDate: DateTime, today: DateTime, historicalPrices: List<HistoricalPrice>): List<CmcHistoricalQuote> {
fun processHistoricalData(startDate: DateTime, today: DateTime, historicalPrices: List<HistoricalPrice>): List<CmcHistoricalQuote> {
val baseMap = Interval(startDate, today)
.let { int -> generateSequence(int.start) { dt -> dt.plusDays(1) }.takeWhile { dt -> dt < int.end } }
.map { it to emptyList<HistoricalPrice>() }.toMap().toMutableMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ package io.provenance.explorer.service
import io.mockk.every
import io.mockk.spyk
import io.provenance.explorer.config.ExplorerProperties
import io.provenance.explorer.domain.extensions.startOfDay
import io.provenance.explorer.domain.models.HistoricalPrice
import io.provenance.explorer.grpc.flow.FlowApiGrpcClient
import io.provenance.explorer.grpc.v1.AccountGrpcClient
import io.provenance.explorer.model.base.USD_UPPER
import io.provenance.explorer.service.pricing.fetchers.HistoricalPriceFetcherFactory
import kotlinx.coroutines.runBlocking
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.transactions.transaction
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test
import java.math.BigDecimal
import java.net.URI

class TokenServiceTest {
Expand Down Expand Up @@ -143,4 +148,86 @@ class TokenServiceTest {
assertEquals("450", latestData?.quote?.get(USD_UPPER)?.volume_24h?.toPlainString(), "Volume sum is incorrect")
assertEquals("1600.000", latestData?.quote?.get(USD_UPPER)?.market_cap_by_total_supply?.toPlainString(), "Market cap is incorrect")
}

@Test
fun `test processHistoricalData with valid prices`() {
Database.connect("jdbc:h2:mem:test;MODE=PostgreSQL;DB_CLOSE_DELAY=-1;", driver = "org.h2.Driver")

transaction {
exec(
"""
CREATE TABLE IF NOT EXISTS token_historical_daily (
historical_timestamp TIMESTAMP NOT NULL,
data TEXT NOT NULL,
source TEXT NOT NULL
)
"""
)
exec(
"""
INSERT INTO token_historical_daily (historical_timestamp, data, source)
VALUES (
'${DateTime.now(DateTimeZone.UTC).minusDays(7).toString("yyyy-MM-dd HH:mm:ss")}',
'{"quote": {"USD": {"close": 1.2}}}',
'test-source'
)
"""
)
}

val startDate = DateTime.now(DateTimeZone.UTC).minusDays(7).startOfDay()
val today = DateTime.now(DateTimeZone.UTC)

val historicalPrices = listOf(
HistoricalPrice(
time = today.minusDays(6).startOfDay().millis / 1000,
high = BigDecimal("1.6"),
low = BigDecimal("1.1"),
close = BigDecimal("1.5"),
open = BigDecimal("1.3"),
volume = BigDecimal("200"),
source = "source1"
),
HistoricalPrice(
time = today.minusDays(5).startOfDay().millis / 1000,
high = BigDecimal("1.7"),
low = BigDecimal("1.2"),
close = BigDecimal("1.6"),
open = BigDecimal("1.4"),
volume = BigDecimal("300"),
source = "source2"
),
HistoricalPrice(
time = today.minusDays(3).startOfDay().millis / 1000,
high = BigDecimal("1.8"),
low = BigDecimal("1.3"),
close = BigDecimal("1.7"),
open = BigDecimal("1.5"),
volume = BigDecimal("400"),
source = "source3"
)
)

val tokenServiceSpy = spyk(tokenService) {
every { totalSupply() } returns BigDecimal("1000000").multiply(ExplorerProperties.UTILITY_TOKEN_BASE_MULTIPLIER)
}

val result = tokenServiceSpy.processHistoricalData(startDate, today, historicalPrices)

println("Number of records returned: ${result.size}")
result.forEach { println(it) }

assertNotNull(result)
assertEquals(8, result.size)

val firstDayData = result.first()
assertEquals(BigDecimal("0"), firstDayData.quote[USD_UPPER]?.open)
assertEquals(BigDecimal("0"), firstDayData.quote[USD_UPPER]?.close)
assertEquals(BigDecimal("0"), firstDayData.quote[USD_UPPER]?.volume)

val sixDaysAgo = result[1]
assertEquals(BigDecimal("1.3"), sixDaysAgo.quote[USD_UPPER]?.open)
assertEquals(BigDecimal("1.5"), sixDaysAgo.quote[USD_UPPER]?.close)
assertEquals(BigDecimal("2E+2"), sixDaysAgo.quote[USD_UPPER]?.volume)
}
}

0 comments on commit 4a5773b

Please sign in to comment.