Skip to content

Commit

Permalink
Refactor test for creating a new account with 0 balance (USD) (pass)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil.jedrzejuk committed Oct 2, 2024
1 parent 57e0217 commit 93f65a2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package camilyed.github.io.currencyexchangeapi.application

import camilyed.github.io.currencyexchangeapi.domain.Account
import camilyed.github.io.currencyexchangeapi.domain.AccountRepository
import camilyed.github.io.currencyexchangeapi.domain.AccountSnapshot
import java.math.BigDecimal

class AccountService(
private val repository: AccountRepository
) {

fun create(command: CreateAccountCommand): Account {
fun create(command: CreateAccountCommand): AccountSnapshot {
val id = repository.nextAccountId()
return Account(
id = id,
owner = command.owner,
balancePln = command.initialBalance,
balanceUsd = BigDecimal.ZERO
)
).toSnapshot()
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package camilyed.github.io.currencyexchangeapi.domain

import java.math.BigDecimal
import java.util.UUID
import java.util.*

data class Account(
val id: UUID,
val owner: String,
val balancePln: BigDecimal,
val balanceUsd: BigDecimal
class Account(
private val id: UUID,
private val owner: String,
private var balancePln: BigDecimal,
private var balanceUsd: BigDecimal = BigDecimal.ZERO
) {

init {
require(balancePln >= BigDecimal.ZERO) { "Initial balance cannot be negative" }
require(balanceUsd == BigDecimal.ZERO) { "USD balance must start at 0" }
}

fun toSnapshot(): AccountSnapshot {
return AccountSnapshot(
id = id,
owner = owner,
balancePln = balancePln,
balanceUsd = balanceUsd
)
}
}

data class AccountSnapshot(
val id: UUID,
val owner: String,
val balancePln: BigDecimal,
val balanceUsd: BigDecimal
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package camilyed.github.io.currencyexchangeapi.application

import camilyed.github.io.currencyexchangeapi.domain.Account
import camilyed.github.io.currencyexchangeapi.domain.AccountSnapshot
import camilyed.github.io.currencyexchangeapi.testing.ability.SetNextAccountIdAbility
import camilyed.github.io.currencyexchangeapi.testing.ability.SetNextAccountIdAbility.Companion.accountRepository
import camilyed.github.io.currencyexchangeapi.testing.assertions.hasBalanceInPln
import camilyed.github.io.currencyexchangeapi.testing.assertions.hasBalanceInUsd
import camilyed.github.io.currencyexchangeapi.testing.assertions.hasId
import camilyed.github.io.currencyexchangeapi.testing.assertions.hasBalanceInPln
import camilyed.github.io.currencyexchangeapi.testing.assertions.hasOwner
import camilyed.github.io.currencyexchangeapi.testing.builders.CreateAccountCommandBuilder
import camilyed.github.io.currencyexchangeapi.testing.builders.CreateAccountCommandBuilder.Companion.anAccount
Expand Down Expand Up @@ -66,7 +66,7 @@ class AccountServiceTest : SetNextAccountIdAbility {
.hasBalanceInUsd(0.0)
}

private fun create(command: CreateAccountCommandBuilder): Account {
private fun create(command: CreateAccountCommandBuilder): AccountSnapshot {
return accountService.create(command.build())
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
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
import java.util.*

fun Assertion.Builder<Account>.hasId(expectedId: String) =
fun Assertion.Builder<AccountSnapshot>.hasId(expectedId: String) =
assert("has id $expectedId") {
if (it.id == UUID.fromString(expectedId)) pass() else fail(
description = "in fact it is %s",
actual = it.id
)
}

fun Assertion.Builder<Account>.hasOwner(expectedOwner: String) =
fun Assertion.Builder<AccountSnapshot>.hasOwner(expectedOwner: String) =
assert("has owner $expectedOwner") {
if (it.owner == expectedOwner) pass() else fail()
}

fun Assertion.Builder<Account>.hasBalanceInPln(expectedBalance: Double) =
fun Assertion.Builder<AccountSnapshot>.hasBalanceInPln(expectedBalance: Double) =
assert("has initial balance $expectedBalance") {
if (it.balancePln == BigDecimal(expectedBalance)) pass() else fail(
description = "in fact it is %s",
actual = it.balancePln
)
}

fun Assertion.Builder<Account>.hasBalanceInUsd(expectedBalance: Double) =
fun Assertion.Builder<AccountSnapshot>.hasBalanceInUsd(expectedBalance: Double) =
assert("has balance in usd $expectedBalance") {
if (it.balanceUsd == BigDecimal(expectedBalance)) pass() else fail(
description = "in fact it is %s",
Expand Down

0 comments on commit 93f65a2

Please sign in to comment.