Skip to content

Commit

Permalink
Refactor test for exchange PLN to USD (pass)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil.jedrzejuk committed Oct 2, 2024
1 parent f0a6ba3 commit b7172c0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Account(
init {
require(balancePln >= BigDecimal.ZERO) { "Initial balance cannot be negative" }
require(balanceUsd == BigDecimal.ZERO) { "USD balance must start at 0" }
balancePln = balancePln.setScale(2)
balanceUsd = balanceUsd.setScale(2)
}

fun exchangePlnToUsd(amountPln: BigDecimal, exchangeRate: BigDecimal) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ class AccountServiceTest : SetNextAccountIdAbility {
val account = create(
anAccount()
.withOwner("Jan Kowalski")
.withInitialBalance(BigDecimal(1000.0))
.withInitialBalance("1000.00")
)

// then
expectThat(account)
.hasId("db59d3ba-5044-4ea9-85e2-aa1e67ec713c")
.hasOwner("Jan Kowalski")
.hasBalanceInPln(1000.0)
.hasBalanceInPln("1000.00")
}

@Test
fun `should throw exception when trying to create account with negative balance`() {
// when
val result = expectCatching {
create(anAccount().withInitialBalance(BigDecimal(-1000.0)))
create(anAccount().withInitialBalance("-1000.00"))
}

// then
Expand All @@ -58,26 +58,26 @@ class AccountServiceTest : SetNextAccountIdAbility {
@Test
fun `should have zero USD balance before exchange`() {
// given
val account = create(anAccount().withInitialBalance(BigDecimal(1000.0)))
val account = create(anAccount().withInitialBalance("1000.00"))

// then
expectThat(account)
.hasBalanceInPln(1000.0)
.hasBalanceInUsd("0")
.hasBalanceInPln("1000.00")
.hasBalanceInUsd("0.00")
}

@Test
fun `should exchange PLN to USD`() {
// given
var account = create(anAccount().withInitialBalance(BigDecimal(1000.00)))
var account = create(anAccount().withInitialBalance("1000.00"))
val exchangeRate = BigDecimal(4.0) // 1 USD = 4 PLN

// when
account = accountService.exchangePlnToUsd(account.id, BigDecimal(400.00), exchangeRate)

// then
expectThat(account)
.hasBalanceInPln(600.00)
.hasBalanceInPln("600.00")
.hasBalanceInUsd("100.00")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package camilyed.github.io.currencyexchangeapi.testing.assertions

import camilyed.github.io.currencyexchangeapi.domain.Account
import camilyed.github.io.currencyexchangeapi.domain.AccountSnapshot
import strikt.api.Assertion
import java.math.BigDecimal
Expand All @@ -19,7 +18,7 @@ fun Assertion.Builder<AccountSnapshot>.hasOwner(expectedOwner: String) =
if (it.owner == expectedOwner) pass() else fail()
}

fun Assertion.Builder<AccountSnapshot>.hasBalanceInPln(expectedBalance: Double) =
fun Assertion.Builder<AccountSnapshot>.hasBalanceInPln(expectedBalance: String) =
assert("has initial balance $expectedBalance") {
if (it.balancePln == BigDecimal(expectedBalance)) pass() else fail(
description = "in fact it is %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CreateAccountCommandBuilder private constructor() {
private var initialBalance: BigDecimal = BigDecimal("100.00")

fun withOwner(owner: String) = apply { this.owner = owner }
fun withInitialBalance(initialBalance: BigDecimal) = apply { this.initialBalance = initialBalance }
fun withInitialBalance(initialBalance: String) = apply { this.initialBalance = BigDecimal(initialBalance) }

fun build(): CreateAccountCommand {
return CreateAccountCommand(
Expand Down

0 comments on commit b7172c0

Please sign in to comment.