-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
.../market-repository/src/test/java/ir/composenews/data/mapper/MarketChartDtoMapperKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters