From 211147db2c9113189181bf583ef6fa5188d6ac55 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 23 Aug 2022 15:35:43 +0200 Subject: [PATCH] ledger: additional error-cases testing --- src/ledger.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/ledger.rs b/src/ledger.rs index 82feb23..565e81e 100644 --- a/src/ledger.rs +++ b/src/ledger.rs @@ -502,4 +502,97 @@ mod test { "#]], ); } + + #[test] + fn unknown_client_and_transaction() { + let error = process_transactions(inline_csv!( + "type, client, tx, amount", + "deposit, 1, 1, 1.0", + "dispute, 2, 2", + )) + .unwrap_err(); + assert_eq!(error, LedgerError::UnknownTx(ClientId(2), TxId(2))); + } + + #[test] + fn unknown_client() { + let error = process_transactions(inline_csv!( + "type, client, tx, amount", + "deposit, 1, 1, 1.0", + "dispute, 2, 1", + )) + .unwrap_err(); + assert_eq!(error, LedgerError::UnknownTx(ClientId(2), TxId(1))); + } + + #[test] + fn unknown_transaction() { + let error = process_transactions(inline_csv!( + "type, client, tx, amount", + "deposit, 1, 1, 1.0", + "dispute, 1, 2", + )) + .unwrap_err(); + assert_eq!(error, LedgerError::UnknownTx(ClientId(1), TxId(2))); + } + + #[test] + fn double_dispute() { + let error = process_transactions(inline_csv!( + "type, client, tx, amount", + "deposit, 1, 1, 1.0", + "dispute, 1, 1", + "dispute, 1, 1", + )) + .unwrap_err(); + assert_eq!(error, LedgerError::AlreadyDisputed); + } + + #[test] + fn dispute_after_resolution() { + let error = process_transactions(inline_csv!( + "type, client, tx, amount", + "deposit, 1, 1, 1.0", + "dispute, 1, 1", + "resolve, 1, 1", + "dispute, 1, 1", + )) + .unwrap_err(); + assert_eq!(error, LedgerError::AlreadyDisputed); + } + + #[test] + fn dispute_after_chargeback() { + let error = process_transactions(inline_csv!( + "type, client, tx, amount", + "deposit, 1, 1, 1.0", + "dispute, 1, 1", + "chargeback, 1, 1", + "dispute, 1, 1", + )) + .unwrap_err(); + assert_eq!(error, LedgerError::AlreadyDisputed); + } + + #[test] + fn resolution_no_dispute() { + let error = process_transactions(inline_csv!( + "type, client, tx, amount", + "deposit, 1, 1, 1.0", + "resolve, 1, 1", + )) + .unwrap_err(); + assert_eq!(error, LedgerError::NotDisputed); + } + + #[test] + fn chargeback_no_dispute() { + let error = process_transactions(inline_csv!( + "type, client, tx, amount", + "deposit, 1, 1, 1.0", + "chargeback, 1, 1", + )) + .unwrap_err(); + assert_eq!(error, LedgerError::NotDisputed); + } }