@@ -173,51 +173,63 @@ mod tests {
173
173
174
174
use super :: * ;
175
175
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 ) ) ,
189
189
} ,
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
+ ] )
194
194
}
195
195
196
196
#[ 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
+ }
202
205
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 ( ) ;
203
213
let result = payout. cancel_payout ( payout. profile_id ) ;
204
214
assert ! ( matches!( result, Err ( PayoutError :: PayoutAlreadyCancelled ) ) ) ;
205
215
}
206
216
207
217
#[ test]
208
218
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
+ } ) ;
215
229
216
- #[ test]
217
- fn cancel_payout_success ( ) {
218
- let mut payout = mock_payout ( ) ;
230
+ let mut payout = Payout :: try_from ( events) . unwrap ( ) ;
219
231
220
232
let result = payout. cancel_payout ( payout. profile_id ) ;
221
- assert ! ( result. is_ok ( ) ) ;
233
+ assert ! ( matches! ( result, Err ( PayoutError :: PayoutAlreadyCommitted ) ) ) ;
222
234
}
223
235
}
0 commit comments