Skip to content

Commit

Permalink
test: refactor payout unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
bodymindarts authored and thevaibhav-dixit committed Oct 31, 2023
1 parent ee87068 commit 838bdd9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
5 changes: 5 additions & 0 deletions src/entity/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,9 @@ impl<T: DeserializeOwned + Serialize + 'static> EntityEvents<T> {
query.execute(&mut **tx).await?;
Ok(())
}

#[cfg(test)]
pub fn last(&self, n: usize) -> &[T] {
&self.events[self.events.len() - n..]
}
}
76 changes: 44 additions & 32 deletions src/payout/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,51 +173,63 @@ mod tests {

use super::*;

fn mock_payout() -> Payout {
Payout {
id: uuid::Uuid::new_v4().into(),
wallet_id: uuid::Uuid::new_v4().into(),
profile_id: uuid::Uuid::new_v4().into(),
payout_queue_id: uuid::Uuid::new_v4().into(),
batch_id: None,
outpoint: None,
satoshis: Satoshis::from(Decimal::from(21)),
destination: PayoutDestination::OnchainAddress {
value: "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej"
.parse()
.unwrap(),
fn init_events() -> EntityEvents<PayoutEvent> {
EntityEvents::init([
PayoutEvent::Initialized {
id: PayoutId::new(),
wallet_id: WalletId::new(),
profile_id: ProfileId::new(),
payout_queue_id: PayoutQueueId::new(),
destination: PayoutDestination::OnchainAddress {
value: "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej"
.parse()
.unwrap(),
},
satoshis: Satoshis::from(Decimal::from(21)),
},
external_id: String::from("test_external_id"),
metadata: None,
events: EntityEvents::new(),
}
PayoutEvent::ExternalIdUpdated {
external_id: "external_id".to_string(),
},
])
}

#[test]
fn errors_when_payout_already_cancelled() {
let mut payout = mock_payout();
payout.events.push(PayoutEvent::Cancelled {
executed_by: payout.profile_id,
});
fn cancel_payout() {
let mut payout = Payout::try_from(init_events()).unwrap();
assert!(payout.cancel_payout(payout.profile_id).is_ok());
assert!(matches!(
payout.events.last(1)[0],
PayoutEvent::Cancelled { .. }
));
}

#[test]
fn can_only_cancel_payout_one_time() {
let mut events = init_events();
events.push(PayoutEvent::Cancelled {
executed_by: ProfileId::new(),
});
let mut payout = Payout::try_from(events).unwrap();
let result = payout.cancel_payout(payout.profile_id);
assert!(matches!(result, Err(PayoutError::PayoutAlreadyCancelled)));
}

#[test]
fn errors_when_payout_already_committed() {
let mut payout = mock_payout();
payout.batch_id = Some(uuid::Uuid::new_v4().into());

let result = payout.cancel_payout(payout.profile_id);
assert!(matches!(result, Err(PayoutError::PayoutAlreadyCommitted)));
}
let mut events = init_events();
events.push(PayoutEvent::CommittedToBatch {
batch_id: BatchId::new(),
outpoint: bitcoin::OutPoint {
txid: "4010e27ff7dc6d9c66a5657e6b3d94b4c4e394d968398d16fefe4637463d194d"
.parse()
.unwrap(),
vout: 0,
},
});

#[test]
fn cancel_payout_success() {
let mut payout = mock_payout();
let mut payout = Payout::try_from(events).unwrap();

let result = payout.cancel_payout(payout.profile_id);
assert!(result.is_ok());
assert!(matches!(result, Err(PayoutError::PayoutAlreadyCommitted)));
}
}

0 comments on commit 838bdd9

Please sign in to comment.