Skip to content

Commit 838bdd9

Browse files
bodymindartsthevaibhav-dixit
authored andcommitted
test: refactor payout unit test
1 parent ee87068 commit 838bdd9

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

src/entity/event.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,9 @@ impl<T: DeserializeOwned + Serialize + 'static> EntityEvents<T> {
109109
query.execute(&mut **tx).await?;
110110
Ok(())
111111
}
112+
113+
#[cfg(test)]
114+
pub fn last(&self, n: usize) -> &[T] {
115+
&self.events[self.events.len() - n..]
116+
}
112117
}

src/payout/entity.rs

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -173,51 +173,63 @@ mod tests {
173173

174174
use super::*;
175175

176-
fn mock_payout() -> Payout {
177-
Payout {
178-
id: uuid::Uuid::new_v4().into(),
179-
wallet_id: uuid::Uuid::new_v4().into(),
180-
profile_id: uuid::Uuid::new_v4().into(),
181-
payout_queue_id: uuid::Uuid::new_v4().into(),
182-
batch_id: None,
183-
outpoint: None,
184-
satoshis: Satoshis::from(Decimal::from(21)),
185-
destination: PayoutDestination::OnchainAddress {
186-
value: "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej"
187-
.parse()
188-
.unwrap(),
176+
fn init_events() -> EntityEvents<PayoutEvent> {
177+
EntityEvents::init([
178+
PayoutEvent::Initialized {
179+
id: PayoutId::new(),
180+
wallet_id: WalletId::new(),
181+
profile_id: ProfileId::new(),
182+
payout_queue_id: PayoutQueueId::new(),
183+
destination: PayoutDestination::OnchainAddress {
184+
value: "bc1qwqdg6squsna38e46795at95yu9atm8azzmyvckulcc7kytlcckxswvvzej"
185+
.parse()
186+
.unwrap(),
187+
},
188+
satoshis: Satoshis::from(Decimal::from(21)),
189189
},
190-
external_id: String::from("test_external_id"),
191-
metadata: None,
192-
events: EntityEvents::new(),
193-
}
190+
PayoutEvent::ExternalIdUpdated {
191+
external_id: "external_id".to_string(),
192+
},
193+
])
194194
}
195195

196196
#[test]
197-
fn errors_when_payout_already_cancelled() {
198-
let mut payout = mock_payout();
199-
payout.events.push(PayoutEvent::Cancelled {
200-
executed_by: payout.profile_id,
201-
});
197+
fn cancel_payout() {
198+
let mut payout = Payout::try_from(init_events()).unwrap();
199+
assert!(payout.cancel_payout(payout.profile_id).is_ok());
200+
assert!(matches!(
201+
payout.events.last(1)[0],
202+
PayoutEvent::Cancelled { .. }
203+
));
204+
}
202205

206+
#[test]
207+
fn can_only_cancel_payout_one_time() {
208+
let mut events = init_events();
209+
events.push(PayoutEvent::Cancelled {
210+
executed_by: ProfileId::new(),
211+
});
212+
let mut payout = Payout::try_from(events).unwrap();
203213
let result = payout.cancel_payout(payout.profile_id);
204214
assert!(matches!(result, Err(PayoutError::PayoutAlreadyCancelled)));
205215
}
206216

207217
#[test]
208218
fn errors_when_payout_already_committed() {
209-
let mut payout = mock_payout();
210-
payout.batch_id = Some(uuid::Uuid::new_v4().into());
211-
212-
let result = payout.cancel_payout(payout.profile_id);
213-
assert!(matches!(result, Err(PayoutError::PayoutAlreadyCommitted)));
214-
}
219+
let mut events = init_events();
220+
events.push(PayoutEvent::CommittedToBatch {
221+
batch_id: BatchId::new(),
222+
outpoint: bitcoin::OutPoint {
223+
txid: "4010e27ff7dc6d9c66a5657e6b3d94b4c4e394d968398d16fefe4637463d194d"
224+
.parse()
225+
.unwrap(),
226+
vout: 0,
227+
},
228+
});
215229

216-
#[test]
217-
fn cancel_payout_success() {
218-
let mut payout = mock_payout();
230+
let mut payout = Payout::try_from(events).unwrap();
219231

220232
let result = payout.cancel_payout(payout.profile_id);
221-
assert!(result.is_ok());
233+
assert!(matches!(result, Err(PayoutError::PayoutAlreadyCommitted)));
222234
}
223235
}

0 commit comments

Comments
 (0)