use ddd_cqrs_es::testing::AggregateFixture;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_successful_bank_account_lifecycle() {
let account_id = "account-123".to_owned();
// Scenario 1: Opening a new account emits AccountOpened
AggregateFixture::<BankAccount>::new()
.given(vec![]) // Establish no pre-existing events
.when(BankAccountCommand::OpenAccount {
account_id: account_id.clone(),
owner: "Uriah".to_owned(),
})
.then_expect_events(vec![BankAccountEvent::AccountOpened {
account_id: account_id.clone(),
owner: "Uriah".to_owned(),
}])
.then_expect_revision(1); // Aggregate should now be at revision 1
// Scenario 2: Depositing money on an open account succeeds
AggregateFixture::<BankAccount>::new()
.given(vec![
// 1. Establish the history: the account is already open
BankAccountEvent::AccountOpened {
account_id: account_id.clone(),
owner: "Uriah".to_owned(),
}
])
.when(BankAccountCommand::DepositMoney { amount: 500 })
.then_expect_events(vec![BankAccountEvent::MoneyDeposited { amount: 500 }])
.then_expect_revision(2); // History has 1 event + 1 new event = revision 2
}
#[test]
fn test_failed_business_rule_validations() {
let account_id = "account-123".to_owned();
// Scenario 1: Cannot deposit money on an account that has not been opened yet
AggregateFixture::<BankAccount>::new()
.given(vec![]) // No history
.when(BankAccountCommand::DepositMoney { amount: 100 })
.then_expect_error(BankAccountError::AccountNotYetOpen);
// Scenario 2: Cannot withdraw more money than the current available balance
AggregateFixture::<BankAccount>::new()
.given(vec![
// Replay history to establish a balance of $100
BankAccountEvent::AccountOpened {
account_id: account_id.clone(),
owner: "Uriah".to_owned(),
},
BankAccountEvent::MoneyDeposited { amount: 100 },
])
.when(BankAccountCommand::WithdrawMoney { amount: 150 }) // Attempting to withdraw $150
.then_expect_error(BankAccountError::InsufficientFunds {
available: 100,
requested: 150,
});
}
}