Skip to content

Commit

Permalink
[test] MarketChartDtoMapper #254
Browse files Browse the repository at this point in the history
This commit adds comprehensive tests for the `MarketChartDtoMapper` which is responsible for converting `MarketChartResponse` to `Chart`.

The tests cover various scenarios:

-   Conversion of valid market chart responses.
-   Handling of empty prices.
-   Handling of negative timestamps and prices.
-   Handling of zero values.
-   Handling of large double values.
-   Truncation of decimals in non-integer timestamps.
-   Handling of missing elements in price pairs, throwing `IndexOutOfBoundsException`.
  • Loading branch information
Kaaveh committed Feb 7, 2025
1 parent 05ef060 commit 64ba71a
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import ir.composenews.remotedatasource.dto.MarketChartResponse
import kotlinx.collections.immutable.toPersistentList

fun MarketChartResponse.toChart(): Chart = Chart(
prices = prices.map { pair -> Pair(pair[0].toInt(), pair[1]) }.toPersistentList(),
prices = prices.map { element -> Pair(element[0].toLong(), element[1]) }.toPersistentList(),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package ir.composenews.data.mapper

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.collections.shouldContainExactly
import io.kotest.matchers.shouldBe
import ir.composenews.remotedatasource.dto.MarketChartResponse

class MarketChartDtoMapperKtTest : StringSpec({
"Given valid market chart response, When converting to chart, Then returns correct chart" {
val marketChartResponse = MarketChartResponse(
prices = listOf(
listOf(-1700000005000.0, 45000.0),
listOf(1700000005000.0, 46000.5),
),
)

val chart = marketChartResponse.toChart()
val expected = listOf(
-1700000005000 to 45000.0,
1700000005000 to 46000.5,
)

chart.prices shouldContainExactly expected
}

"Given market chart response with empty prices, When converting to chart, Then returns empty chart" {
val marketChartResponse = MarketChartResponse(prices = emptyList())

val chart = marketChartResponse.toChart()

chart.prices.shouldBeEmpty()
}

"Given market chart response with negative timestamps, When converting to chart, Then returns correctly mapped chart" {

val marketChartResponse = MarketChartResponse(
prices = listOf(
listOf(-1000.0, 45000.0),
listOf(-500.0, 46000.5),
),
)

val chart = marketChartResponse.toChart()

chart.prices.zip(marketChartResponse.prices).forEach { (actual, expected) ->
actual.first shouldBe expected[0].toLong()
actual.second shouldBe expected[1]
}
}

"Given market chart response with negative prices, When converting to chart, Then returns correctly mapped chart" {
val marketChartResponse = MarketChartResponse(
prices = listOf(
listOf(1700000000000.0, -45000.0),
listOf(1700000005000.0, -46000.5),
),
)

val chart = marketChartResponse.toChart()
val expected = listOf(
1700000000000 to -45000.0,
1700000005000 to -46000.5,
)

chart.prices shouldContainExactly expected
}

"Given market chart response with zero values, When converting to chart, Then returns correctly mapped chart" {
val marketChartResponse = MarketChartResponse(
prices = listOf(
listOf(0.0, 0.0),
listOf(0.0, 0.0),
),
)

val chart = marketChartResponse.toChart()
val expected = listOf(
0L to 0.0,
0L to 0.0,
)

chart.prices shouldContainExactly expected
}

"Given market chart response with large double values, When converting to chart, Then returns correctly mapped chart" {
val marketChartResponse = MarketChartResponse(
prices = listOf(
listOf(Double.MAX_VALUE, Double.MAX_VALUE),
listOf(Double.MIN_VALUE, Double.MIN_VALUE),
),
)

val chart = marketChartResponse.toChart()
val expected = listOf(
Double.MAX_VALUE.toLong() to Double.MAX_VALUE,
Double.MIN_VALUE.toLong() to Double.MIN_VALUE,
)

chart.prices shouldContainExactly expected
}

"Given market chart response with non-integer timestamps, When converting to chart, Then truncates decimals" {
val marketChartResponse = MarketChartResponse(
prices = listOf(
listOf(1700000000000.99, 45000.55),
listOf(1700000005000.49, 46000.99),
),
)

val chart = marketChartResponse.toChart()
val expected = listOf(
1700000000000 to 45000.55,
1700000005000 to 46000.99,
)

chart.prices shouldContainExactly expected
}

"Given market chart response with missing second element in price pair, When converting to chart, Then throws IndexOutOfBoundsException" {
val marketChartResponse = MarketChartResponse(
prices = listOf(
listOf(1700000000000.0),
),
)

shouldThrow<IndexOutOfBoundsException> {
marketChartResponse.toChart()
}
}

"Given market chart response with missing first element in price pair, When converting to chart, Then throws IndexOutOfBoundsException" {
val marketChartResponse = MarketChartResponse(
prices = listOf(
listOf(),
),
)

shouldThrow<IndexOutOfBoundsException> {
marketChartResponse.toChart()
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package ir.composenews.domain.model
import kotlinx.collections.immutable.PersistentList

data class Chart(
val prices: PersistentList<Pair<Int, Double>>,
val prices: PersistentList<Pair<Long, Double>>,
)

0 comments on commit 64ba71a

Please sign in to comment.