Created
May 8, 2022 23:51
-
-
Save xrrocha/728e0bc3fffc670eb24bc978bf1b8b27 to your computer and use it in GitHub Desktop.
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
val bank1 = Bank() | |
val memimg1 = MemImg(bank1, eventSourcing) | |
| |
memimg1.execute(CreateAccount("janet", "Janet Doe")) | |
assertEquals(Amount.ZERO, bank1.accounts["janet"]!!.balance) | |
| |
memimg1.execute(Deposit("janet", Amount(100))) | |
assertEquals(Amount(100), bank1.accounts["janet"]!!.balance) | |
| |
memimg1.execute(Withdrawal("janet", Amount(10))) | |
assertEquals(Amount(90), bank1.accounts["janet"]!!.balance) | |
| |
memimg1.execute(CreateAccount("john", "John Doe")) | |
assertEquals(Amount.ZERO, bank1.accounts["john"]!!.balance) | |
| |
memimg1.execute(Deposit("john", Amount(50))) | |
assertEquals(Amount(50), bank1.accounts["john"]!!.balance) | |
| |
memimg1.execute(Transfer("janet", "john", Amount(20))) | |
assertEquals(Amount(70), bank1.accounts["janet"]!!.balance) | |
assertEquals(Amount(70), bank1.accounts["john"]!!.balance) | |
| |
memimg1.close() | |
| |
val bank2 = Bank() | |
val memimg2 = MemImg(bank2, eventSourcing) | |
// System state restored from empty state via event sourcing! | |
assertEquals(Amount(70), bank2.accounts["janet"]!!.balance) | |
assertEquals(Amount(70), bank2.accounts["john"]!!.balance) | |
| |
// Some random query; executes at in-memory speeds | |
val accountsWith70 = memimg2.execute(object : BankQuery { | |
override fun extractFrom(bank: Bank) = | |
bank.accounts.values | |
.filter { it.balance == Amount(70) } | |
.map { it.name } | |
.toSet() | |
}) | |
assertEquals(setOf("Janet Doe", "John Doe"), accountsWith70) | |
| |
// Attempt to transfer beyond means... | |
val insufficientFunds = assertThrows<CommandApplicationException> { | |
memimg2.execute(Transfer("janet", "john", Amount(1000))) | |
} | |
assertContains(insufficientFunds.message!!, "Invalid value for Account.balance") | |
// Look ma: system state restored on failure after partial mutation | |
assertEquals(Amount(70), bank2.accounts["janet"]!!.balance) | |
assertEquals(Amount(70), bank2.accounts["john"]!!.balance) | |
| |
memimg2.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment