Skip to content

Commit

Permalink
Added test for rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil.jedrzejuk committed Oct 3, 2024
1 parent 5c42f19 commit a7b5429
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/test/kotlin/camilyed/github/io/common/MoneyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,34 @@ class MoneyTest {
.isA<IllegalArgumentException>()
.message.isEqualTo("Insufficient funds")
}

@Test
fun `should round result to 2 decimal places when subtracting`() {
val money1 = Money(BigDecimal("100.555"), "USD")
val money2 = Money(BigDecimal("50.123"), "USD")

val result = money1 - money2

expectThat(result.amount).isEqualTo(BigDecimal("50.43"))
}

@Test
fun `should round using HALF_EVEN rounding mode when adding`() {
val money1 = Money(BigDecimal("100.505"), "USD")
val money2 = Money(BigDecimal("50.505"), "USD")

val result = money1 + money2

expectThat(result.amount).isEqualTo(BigDecimal("151.01"))
}

@Test
fun `should round using HALF_EVEN rounding mode when subtracting`() {
val money1 = Money(BigDecimal("100.505"), "USD")
val money2 = Money(BigDecimal("50.505"), "USD")

val result = money1 - money2

expectThat(result.amount).isEqualTo(BigDecimal("50.00"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import strikt.assertions.isA
import strikt.assertions.isEqualTo
import strikt.assertions.isFailure
import strikt.assertions.message
import java.math.BigDecimal

class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {

Expand Down Expand Up @@ -271,6 +272,82 @@ class AccountServiceTest : SetNextAccountIdAbility, CreateAccountAbility {
.message.isEqualTo("Exchange rate must be greater than 0")
}

@Test
fun `should round exchange PLN to USD to 2 decimal places`() {
// given
val account = thereIsAnAccount(anAccount().withBalancePln("1000.00"))

// when
val updatedAccount = exchange(
anExchangeToUsd()
.withAccountId(account.id)
.withAmount("123.456")
.withExchangeRate("4.0")
)

// then
expectThat(updatedAccount)
.hasBalanceInPln("876.54")
.hasBalanceInUsd("30.86") // 123.456 / 4 = 30.864 -> rounded to 30.86
}

@Test
fun `should round exchange USD to PLN to 2 decimal places`() {
// given
val account = thereIsAnAccount(anAccount().withBalanceUsd("100.00").withBalancePln("0.00"))

// when
val updatedAccount = exchange(
anExchangeToPln()
.withAccountId(account.id)
.withAmount("33.335")
.withExchangeRate("4.0")
)

// then
expectThat(updatedAccount)
.hasBalanceInPln("133.36")
.hasBalanceInUsd("66.66") // 100 - 33.335 -> rounded to 66.66
}

@Test
fun `should exchange full PLN to USD with rounding`() {
// given
val account = thereIsAnAccount(anAccount().withBalancePln("1000.00"))

// when
val updatedAccount = exchange(
anExchangeToUsd()
.withAccountId(account.id)
.withAmount("1000.00")
.withExchangeRate("3.33")
)

// then
expectThat(updatedAccount)
.hasBalanceInPln("0.00")
.hasBalanceInUsd("300.30") // 1000 / 3.33 = 300.3003 -> rounded to 300.30
}

@Test
fun `should exchange full USD to PLN with rounding`() {
// given
val account = thereIsAnAccount(anAccount().withBalanceUsd("100.00").withBalancePln("0.00"))

// when
val updatedAccount = exchange(
anExchangeToPln()
.withAccountId(account.id)
.withAmount("100.00")
.withExchangeRate("4.5")
)

// then
expectThat(updatedAccount)
.hasBalanceInPln("450.00") // 100 * 4.5 = 450
.hasBalanceInUsd("0.00")
}

private fun create(command: CreateAccountCommandBuilder): AccountSnapshot {
return accountService.create(command.build())
}
Expand Down

0 comments on commit a7b5429

Please sign in to comment.