-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpegout_test.go
More file actions
722 lines (696 loc) · 40.7 KB
/
pegout_test.go
File metadata and controls
722 lines (696 loc) · 40.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
package mongo_test
import (
"context"
"github.com/btcsuite/btcd/btcjson"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/dataproviders/database/mongo"
"github.com/rsksmart/liquidity-provider-server/internal/entities"
"github.com/rsksmart/liquidity-provider-server/internal/entities/quote"
"github.com/rsksmart/liquidity-provider-server/internal/entities/utils"
"github.com/rsksmart/liquidity-provider-server/internal/usecases"
"github.com/rsksmart/liquidity-provider-server/test"
"github.com/rsksmart/liquidity-provider-server/test/mocks"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
mongoDb "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"reflect"
"testing"
"time"
)
var testPegoutQuote = quote.PegoutQuote{
LbcAddress: "0xc2A630c053D12D63d32b025082f6Ba268db18300",
LpRskAddress: "0x7c4890a0f1d4bbf2c669ac2d1effa185c505359b",
BtcRefundAddress: "n2Ge4xMVQKp5Hzzf8xTBJBLppRgjRZYYyq",
RskRefundAddress: "0x79568C2989232dcA1840087d73d403602364c0D4",
LpBtcAddress: "mvL2bVzGUeC9oqVyQWJ4PxQspFzKgjzAqe",
CallFee: entities.NewWei(100000000000000),
PenaltyFee: 10000000000000,
Nonce: 6410832321595034747,
DepositAddress: "n2Ge4xMVQKp5Hzzf8xTBJBLppRgjRZYYyq",
Value: entities.NewWei(5000000000000000),
AgreementTimestamp: 1721944367,
DepositDateLimit: 1721951567,
DepositConfirmations: 4,
TransferConfirmations: 2,
TransferTime: 7200,
ExpireDate: 1721958767,
ExpireBlock: 5366409,
GasFee: entities.NewWei(4170000000000),
ProductFeeAmount: 13,
}
var testRetainedPegoutQuote = quote.RetainedPegoutQuote{
QuoteHash: "27d70ec2bc2c3154dc9a5b53b118a755441b22bc1c8ccde967ed33609970c25f",
DepositAddress: "mkE1WWdiu5VgjfugomDk8GxV6JdEEEJR9s",
Signature: "5c9eab91c753355f87c19d09ea88b2fd02773981e513bc2821fed5ceba0d452a0a3d21e2252cb35348ce5c6803117e3abb62837beb8f5866a375ce66587d004b1c",
RequiredLiquidity: entities.NewWei(55),
State: quote.PegoutStateWaitingForDepositConfirmations,
UserRskTxHash: "0x6b2e1e4daf8cf00c5c3534b72cdeec3526e8a38f70c11e44888b6e4ae1ee7d38",
LpBtcTxHash: "6ac3779dc33ad52f3409cbb909bcd458745995496a2a3954406206f6e5d4cb0e",
RefundPegoutTxHash: "0x8e773a2826e73f8e5792304379a7e46dff38f17089c6d344335e03537b31c2bc",
BridgeRefundTxHash: "0x4f3f6f0664a732e4c907971e75c1e3fd8671461dcb53f566660432fc47255d8b",
}
var testPegoutDeposit = quote.PegoutDeposit{
TxHash: test.AnyString,
QuoteHash: test.AnyString,
Amount: entities.NewWei(999),
Timestamp: time.Unix(1715001146288, 0).UTC(),
BlockNumber: 789,
From: test.AnyAddress,
}
var testPegoutCreationData = quote.PegoutCreationData{
FeeRate: utils.NewBigFloat64(1.55),
FeePercentage: utils.NewBigFloat64(2.41),
GasPrice: entities.NewWei(123),
FixedFee: entities.NewWei(456),
}
func TestPegoutMongoRepository_InsertQuote(t *testing.T) {
t.Run("Insert pegout quote successfully", func(t *testing.T) {
const expectedLog = "INSERT interaction with db: {PegoutQuote:{LbcAddress:0xc2A630c053D12D63d32b025082f6Ba268db18300 LpRskAddress:0x7c4890a0f1d4bbf2c669ac2d1effa185c505359b BtcRefundAddress:n2Ge4xMVQKp5Hzzf8xTBJBLppRgjRZYYyq RskRefundAddress:0x79568C2989232dcA1840087d73d403602364c0D4 LpBtcAddress:mvL2bVzGUeC9oqVyQWJ4PxQspFzKgjzAqe CallFee:100000000000000 PenaltyFee:10000000000000 Nonce:6410832321595034747 DepositAddress:n2Ge4xMVQKp5Hzzf8xTBJBLppRgjRZYYyq Value:5000000000000000 AgreementTimestamp:1721944367 DepositDateLimit:1721951567 DepositConfirmations:4 TransferConfirmations:2 TransferTime:7200 ExpireDate:1721958767 ExpireBlock:5366409 GasFee:4170000000000 ProductFeeAmount:13} Hash:any value}"
client, db := getClientAndDatabaseMocks()
quoteCollection := &mocks.CollectionBindingMock{}
creationDataCollection := &mocks.CollectionBindingMock{}
db.EXPECT().Collection(mongo.PegoutQuoteCollection).Return(quoteCollection)
db.EXPECT().Collection(mongo.PegoutCreationDataCollection).Return(creationDataCollection)
quoteCollection.On("InsertOne", mock.Anything, mock.MatchedBy(func(q mongo.StoredPegoutQuote) bool {
return q.Hash == test.AnyString && reflect.TypeOf(quote.PegoutQuote{}).NumField() == test.CountNonZeroValues(q.PegoutQuote)
})).Return(nil, nil).Once()
creationDataCollection.EXPECT().InsertOne(mock.Anything, mock.MatchedBy(func(q mongo.StoredPegoutCreationData) bool {
return q.Hash == test.AnyString && reflect.TypeOf(quote.PegoutCreationData{}).NumField() == test.CountNonZeroValues(q.PegoutCreationData)
})).Return(nil, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer assertDbInteractionLog(t, expectedLog)()
createdQuote := quote.CreatedPegoutQuote{Hash: test.AnyString, Quote: testPegoutQuote, CreationData: testPegoutCreationData}
err := repo.InsertQuote(context.Background(), createdQuote)
quoteCollection.AssertExpectations(t)
creationDataCollection.AssertExpectations(t)
require.NoError(t, err)
})
t.Run("Db error when inserting pegout quote", func(t *testing.T) {
client, db := getClientAndDatabaseMocks()
quoteCollection := &mocks.CollectionBindingMock{}
creationDataCollection := &mocks.CollectionBindingMock{}
db.EXPECT().Collection(mongo.PegoutQuoteCollection).Return(quoteCollection)
db.EXPECT().Collection(mongo.PegoutCreationDataCollection).Return(creationDataCollection)
quoteCollection.On("InsertOne", mock.Anything, mock.Anything).Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
createdQuote := quote.CreatedPegoutQuote{Hash: test.AnyString, Quote: testPegoutQuote, CreationData: testPegoutCreationData}
err := repo.InsertQuote(context.Background(), createdQuote)
quoteCollection.AssertExpectations(t)
creationDataCollection.AssertNotCalled(t, "InsertOne")
require.Error(t, err)
})
t.Run("Db error when inserting pegout creation data", func(t *testing.T) {
client, db := getClientAndDatabaseMocks()
quoteCollection := &mocks.CollectionBindingMock{}
creationDataCollection := &mocks.CollectionBindingMock{}
db.EXPECT().Collection(mongo.PegoutQuoteCollection).Return(quoteCollection)
db.EXPECT().Collection(mongo.PegoutCreationDataCollection).Return(creationDataCollection)
quoteCollection.On("InsertOne", mock.Anything, mock.Anything).Return(nil, nil).Once()
creationDataCollection.On("InsertOne", mock.Anything, mock.Anything).Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
createdQuote := quote.CreatedPegoutQuote{
Hash: test.AnyString,
Quote: testPegoutQuote,
CreationData: testPegoutCreationData,
}
err := repo.InsertQuote(context.Background(), createdQuote)
quoteCollection.AssertExpectations(t)
creationDataCollection.AssertExpectations(t)
require.Error(t, err)
})
}
func TestPegoutMongoRepository_GetQuote(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.PegoutQuoteCollection)
log.SetLevel(log.DebugLevel)
t.Run("Get pegout quote successfully", func(t *testing.T) {
const expectedLog = "READ interaction with db: {LbcAddress:0xc2A630c053D12D63d32b025082f6Ba268db18300 LpRskAddress:0x7c4890a0f1d4bbf2c669ac2d1effa185c505359b BtcRefundAddress:n2Ge4xMVQKp5Hzzf8xTBJBLppRgjRZYYyq RskRefundAddress:0x79568C2989232dcA1840087d73d403602364c0D4 LpBtcAddress:mvL2bVzGUeC9oqVyQWJ4PxQspFzKgjzAqe CallFee:100000000000000 PenaltyFee:10000000000000 Nonce:6410832321595034747 DepositAddress:n2Ge4xMVQKp5Hzzf8xTBJBLppRgjRZYYyq Value:5000000000000000 AgreementTimestamp:1721944367 DepositDateLimit:1721951567 DepositConfirmations:4 TransferConfirmations:2 TransferTime:7200 ExpireDate:1721958767 ExpireBlock:5366409 GasFee:4170000000000 ProductFeeAmount:13}"
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("FindOne", mock.Anything, bson.D{primitive.E{Key: "hash", Value: test.AnyHash}}).
Return(mongoDb.NewSingleResultFromDocument(mongo.StoredPegoutQuote{
PegoutQuote: testPegoutQuote,
Hash: test.AnyString,
}, nil, nil)).Once()
defer assertDbInteractionLog(t, expectedLog)()
result, err := repo.GetQuote(context.Background(), test.AnyHash)
collection.AssertExpectations(t)
require.NoError(t, err)
assert.Equal(t, testPegoutQuote, *result)
})
t.Run("Db error when getting pegout quote", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("FindOne", mock.Anything, mock.Anything).
Return(mongoDb.NewSingleResultFromDocument(mongo.StoredPegoutQuote{}, assert.AnError, nil)).Once()
result, err := repo.GetQuote(context.Background(), test.AnyHash)
collection.AssertExpectations(t)
require.Error(t, err)
assert.Nil(t, result)
})
t.Run("Pegout quote not present in db", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("FindOne", mock.Anything, mock.Anything).
Return(mongoDb.NewSingleResultFromDocument(mongo.StoredPegoutQuote{}, mongoDb.ErrNoDocuments, nil)).Once()
result, err := repo.GetQuote(context.Background(), test.AnyHash)
collection.AssertExpectations(t)
require.NoError(t, err)
assert.Nil(t, result)
})
t.Run("Fail on invalid pegout quote hash", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
result, err := repo.GetQuote(context.Background(), test.AnyString)
collection.AssertNotCalled(t, "FindOne")
require.Error(t, err)
assert.Nil(t, result)
})
}
func TestPegoutMongoRepository_GetRetainedQuote(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
log.SetLevel(log.DebugLevel)
t.Run("Get retained pegout quote successfully", func(t *testing.T) {
const expectedLog = "READ interaction with db: {QuoteHash:27d70ec2bc2c3154dc9a5b53b118a755441b22bc1c8ccde967ed33609970c25f DepositAddress:mkE1WWdiu5VgjfugomDk8GxV6JdEEEJR9s Signature:5c9eab91c753355f87c19d09ea88b2fd02773981e513bc2821fed5ceba0d452a0a3d21e2252cb35348ce5c6803117e3abb62837beb8f5866a375ce66587d004b1c RequiredLiquidity:55 State:WaitingForDepositConfirmations UserRskTxHash:0x6b2e1e4daf8cf00c5c3534b72cdeec3526e8a38f70c11e44888b6e4ae1ee7d38 LpBtcTxHash:6ac3779dc33ad52f3409cbb909bcd458745995496a2a3954406206f6e5d4cb0e RefundPegoutTxHash:0x8e773a2826e73f8e5792304379a7e46dff38f17089c6d344335e03537b31c2bc BridgeRefundTxHash:0x4f3f6f0664a732e4c907971e75c1e3fd8671461dcb53f566660432fc47255d8b}"
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("FindOne", mock.Anything, bson.D{primitive.E{Key: "quote_hash", Value: test.AnyHash}}).
Return(mongoDb.NewSingleResultFromDocument(testRetainedPegoutQuote, nil, nil)).Once()
defer assertDbInteractionLog(t, expectedLog)()
result, err := repo.GetRetainedQuote(context.Background(), test.AnyHash)
collection.AssertExpectations(t)
require.NoError(t, err)
assert.Equal(t, testRetainedPegoutQuote, *result)
})
t.Run("Db error when getting retained pegout quote", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("FindOne", mock.Anything, mock.Anything).
Return(mongoDb.NewSingleResultFromDocument(quote.RetainedPegoutQuote{}, assert.AnError, nil)).Once()
result, err := repo.GetRetainedQuote(context.Background(), test.AnyHash)
collection.AssertExpectations(t)
require.Error(t, err)
assert.Nil(t, result)
})
t.Run("Retained pegout quote not present in db", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("FindOne", mock.Anything, mock.Anything).
Return(mongoDb.NewSingleResultFromDocument(quote.RetainedPegoutQuote{}, mongoDb.ErrNoDocuments, nil)).Once()
result, err := repo.GetRetainedQuote(context.Background(), test.AnyHash)
collection.AssertExpectations(t)
require.NoError(t, err)
assert.Nil(t, result)
})
t.Run("Fail on invalid pegout quote hash", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
result, err := repo.GetRetainedQuote(context.Background(), test.AnyString)
collection.AssertNotCalled(t, "FindOne")
require.Error(t, err)
assert.Nil(t, result)
})
}
func TestPegoutMongoRepository_InsertRetainedQuote(t *testing.T) {
t.Run("Insert retained pegout quote successfully", func(t *testing.T) {
const expectedLog = "INSERT interaction with db: {QuoteHash:27d70ec2bc2c3154dc9a5b53b118a755441b22bc1c8ccde967ed33609970c25f DepositAddress:mkE1WWdiu5VgjfugomDk8GxV6JdEEEJR9s Signature:5c9eab91c753355f87c19d09ea88b2fd02773981e513bc2821fed5ceba0d452a0a3d21e2252cb35348ce5c6803117e3abb62837beb8f5866a375ce66587d004b1c RequiredLiquidity:55 State:WaitingForDepositConfirmations UserRskTxHash:0x6b2e1e4daf8cf00c5c3534b72cdeec3526e8a38f70c11e44888b6e4ae1ee7d38 LpBtcTxHash:6ac3779dc33ad52f3409cbb909bcd458745995496a2a3954406206f6e5d4cb0e RefundPegoutTxHash:0x8e773a2826e73f8e5792304379a7e46dff38f17089c6d344335e03537b31c2bc BridgeRefundTxHash:0x4f3f6f0664a732e4c907971e75c1e3fd8671461dcb53f566660432fc47255d8b}"
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
collection.On("InsertOne", mock.Anything, mock.MatchedBy(func(q quote.RetainedPegoutQuote) bool {
return q.QuoteHash == testRetainedPegoutQuote.QuoteHash && reflect.TypeOf(quote.RetainedPegoutQuote{}).NumField() == test.CountNonZeroValues(q)
})).Return(nil, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer assertDbInteractionLog(t, expectedLog)()
err := repo.InsertRetainedQuote(context.Background(), testRetainedPegoutQuote)
collection.AssertExpectations(t)
require.NoError(t, err)
})
t.Run("Db error when inserting retained pegout quote", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
collection.On("InsertOne", mock.Anything, mock.Anything).Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
err := repo.InsertRetainedQuote(context.Background(), testRetainedPegoutQuote)
collection.AssertExpectations(t)
require.Error(t, err)
})
}
func TestPegoutMongoRepository_UpdateRetainedQuote(t *testing.T) {
const updated = "updated value"
t.Run("Update retained pegout quote successfully", func(t *testing.T) {
const expectedLog = "UPDATE interaction with db: {QuoteHash:27d70ec2bc2c3154dc9a5b53b118a755441b22bc1c8ccde967ed33609970c25f DepositAddress:updated value Signature:updated value RequiredLiquidity:200 State:SendPegoutFailed UserRskTxHash:updated value LpBtcTxHash:updated value RefundPegoutTxHash:updated value BridgeRefundTxHash:updated value}"
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
updatedQuote := testRetainedPegoutQuote
updatedQuote.State = quote.PegoutStateSendPegoutFailed
updatedQuote.Signature = updated
updatedQuote.RefundPegoutTxHash = updated
updatedQuote.LpBtcTxHash = updated
updatedQuote.DepositAddress = updated
updatedQuote.UserRskTxHash = updated
updatedQuote.BridgeRefundTxHash = updated
updatedQuote.RequiredLiquidity = entities.NewWei(200)
collection.On("UpdateOne", mock.Anything,
bson.D{primitive.E{Key: "quote_hash", Value: testRetainedPegoutQuote.QuoteHash}},
bson.D{primitive.E{Key: "$set", Value: updatedQuote}},
).Return(&mongoDb.UpdateResult{ModifiedCount: 1}, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer assertDbInteractionLog(t, expectedLog)()
err := repo.UpdateRetainedQuote(context.Background(), updatedQuote)
collection.AssertExpectations(t)
require.NoError(t, err)
})
t.Run("Db error when updating retained pegout quote", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
collection.On("UpdateOne", mock.Anything, mock.Anything, mock.Anything).
Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
err := repo.UpdateRetainedQuote(context.Background(), testRetainedPegoutQuote)
collection.AssertExpectations(t)
require.Error(t, err)
})
t.Run("Retained pegout quote to update not found", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
collection.On("UpdateOne", mock.Anything, mock.Anything, mock.Anything).
Return(&mongoDb.UpdateResult{ModifiedCount: 0}, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
err := repo.UpdateRetainedQuote(context.Background(), testRetainedPegoutQuote)
collection.AssertExpectations(t)
require.ErrorIs(t, err, usecases.QuoteNotFoundError)
})
t.Run("Update more than one retained pegout quote", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
collection.On("UpdateOne", mock.Anything, mock.Anything, mock.Anything).
Return(&mongoDb.UpdateResult{ModifiedCount: 2}, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
err := repo.UpdateRetainedQuote(context.Background(), testRetainedPegoutQuote)
collection.AssertExpectations(t)
require.ErrorContains(t, err, "multiple documents updated")
})
}
// nolint:funlen
func TestPegoutMongoRepository_DeleteQuotes(t *testing.T) {
var hashes = []string{"pegout1", "pegout2", "pegout3"}
log.SetLevel(log.DebugLevel)
t.Run("Delete quotes successfully", func(t *testing.T) {
client, quoteCollection := getClientAndCollectionMocks(mongo.PegoutQuoteCollection)
retainedCollection := &mocks.CollectionBindingMock{}
creationDataCollection := &mocks.CollectionBindingMock{}
parsedClientMock, ok := client.Database(mongo.DbName).(*mocks.DbBindingMock)
require.True(t, ok)
parsedClientMock.On("Collection", mongo.RetainedPegoutQuoteCollection).Return(retainedCollection)
parsedClientMock.On("Collection", mongo.PegoutCreationDataCollection).Return(creationDataCollection)
quoteCollection.On("DeleteMany", mock.Anything,
bson.D{primitive.E{Key: "hash", Value: bson.D{primitive.E{Key: "$in", Value: hashes}}}},
).Return(&mongoDb.DeleteResult{DeletedCount: 3}, nil).Once()
retainedCollection.On("DeleteMany", mock.Anything,
bson.D{primitive.E{Key: "quote_hash", Value: bson.D{primitive.E{Key: "$in", Value: hashes}}}},
).Return(&mongoDb.DeleteResult{DeletedCount: 3}, nil).Once()
creationDataCollection.On("DeleteMany", mock.Anything,
bson.D{primitive.E{Key: "hash", Value: bson.D{primitive.E{Key: "$in", Value: hashes}}}},
).Return(&mongoDb.DeleteResult{DeletedCount: 3}, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer assertDbInteractionLog(t, mongo.Delete)()
count, err := repo.DeleteQuotes(context.Background(), hashes)
quoteCollection.AssertExpectations(t)
retainedCollection.AssertExpectations(t)
creationDataCollection.AssertExpectations(t)
require.NoError(t, err)
assert.Equal(t, uint(9), count)
})
t.Run("Db error when deleting pegout quotes", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.PegoutQuoteCollection)
collection.On("DeleteMany", mock.Anything, mock.Anything).Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
count, err := repo.DeleteQuotes(context.Background(), []string{test.AnyString})
collection.AssertExpectations(t)
require.Error(t, err)
assert.Zero(t, count)
})
t.Run("Db error when deleting retained pegout quotes", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.PegoutQuoteCollection)
retainedCollection := &mocks.CollectionBindingMock{}
parsedClientMock, ok := client.Database(mongo.DbName).(*mocks.DbBindingMock)
require.True(t, ok)
parsedClientMock.On("Collection", mongo.RetainedPegoutQuoteCollection).Return(retainedCollection)
collection.On("DeleteMany", mock.Anything, mock.Anything).Return(&mongoDb.DeleteResult{DeletedCount: 3}, nil).Once()
retainedCollection.On("DeleteMany", mock.Anything, mock.Anything).Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
count, err := repo.DeleteQuotes(context.Background(), []string{test.AnyString})
collection.AssertExpectations(t)
retainedCollection.AssertExpectations(t)
require.Error(t, err)
assert.Zero(t, count)
})
t.Run("Db error when deleting pegout creation data", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.PegoutQuoteCollection)
retainedCollection := &mocks.CollectionBindingMock{}
creationDataCollection := &mocks.CollectionBindingMock{}
parsedClientMock, ok := client.Database(mongo.DbName).(*mocks.DbBindingMock)
require.True(t, ok)
parsedClientMock.On("Collection", mongo.RetainedPegoutQuoteCollection).Return(retainedCollection)
parsedClientMock.On("Collection", mongo.PegoutCreationDataCollection).Return(creationDataCollection)
collection.On("DeleteMany", mock.Anything, mock.Anything).Return(&mongoDb.DeleteResult{DeletedCount: 3}, nil).Once()
retainedCollection.On("DeleteMany", mock.Anything, mock.Anything).Return(&mongoDb.DeleteResult{DeletedCount: 3}, nil).Once()
creationDataCollection.On("DeleteMany", mock.Anything, mock.Anything).Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
count, err := repo.DeleteQuotes(context.Background(), []string{test.AnyString})
collection.AssertExpectations(t)
retainedCollection.AssertExpectations(t)
creationDataCollection.AssertExpectations(t)
require.Error(t, err)
assert.Zero(t, count)
})
t.Run("Error when deletion count missmatch", func(t *testing.T) {
client, quoteCollection := getClientAndCollectionMocks(mongo.PegoutQuoteCollection)
retainedCollection := &mocks.CollectionBindingMock{}
creationDataCollection := &mocks.CollectionBindingMock{}
parsedClientMock, ok := client.Database(mongo.DbName).(*mocks.DbBindingMock)
require.True(t, ok)
parsedClientMock.On("Collection", mongo.RetainedPegoutQuoteCollection).Return(retainedCollection)
parsedClientMock.On("Collection", mongo.PegoutCreationDataCollection).Return(creationDataCollection)
quoteCollection.On("DeleteMany", mock.Anything, mock.Anything).Return(&mongoDb.DeleteResult{DeletedCount: 3}, nil).Once()
retainedCollection.On("DeleteMany", mock.Anything, mock.Anything).Return(&mongoDb.DeleteResult{DeletedCount: 4}, nil).Once()
creationDataCollection.On("DeleteMany", mock.Anything, mock.Anything).Return(&mongoDb.DeleteResult{DeletedCount: 3}, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
count, err := repo.DeleteQuotes(context.Background(), hashes)
quoteCollection.AssertExpectations(t)
retainedCollection.AssertExpectations(t)
creationDataCollection.AssertExpectations(t)
require.ErrorContains(t, err, "pegout quote collections didn't match")
assert.Zero(t, count)
})
}
func TestPegoutMongoRepository_GetRetainedQuoteByState(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
log.SetLevel(log.DebugLevel)
states := []quote.PegoutState{quote.PegoutStateSendPegoutSucceeded, quote.PegoutStateSendPegoutFailed}
t.Run("Get retained pegout quotes by state successfully", func(t *testing.T) {
const expectedLog = "READ interaction with db: [{QuoteHash:27d70ec2bc2c3154dc9a5b53b118a755441b22bc1c8ccde967ed33609970c25f DepositAddress:mkE1WWdiu5VgjfugomDk8GxV6JdEEEJR9s Signature:5c9eab91c753355f87c19d09ea88b2fd02773981e513bc2821fed5ceba0d452a0a3d21e2252cb35348ce5c6803117e3abb62837beb8f5866a375ce66587d004b1c RequiredLiquidity:55 State:WaitingForDepositConfirmations UserRskTxHash:0x6b2e1e4daf8cf00c5c3534b72cdeec3526e8a38f70c11e44888b6e4ae1ee7d38 LpBtcTxHash:6ac3779dc33ad52f3409cbb909bcd458745995496a2a3954406206f6e5d4cb0e RefundPegoutTxHash:0x8e773a2826e73f8e5792304379a7e46dff38f17089c6d344335e03537b31c2bc BridgeRefundTxHash:0x4f3f6f0664a732e4c907971e75c1e3fd8671461dcb53f566660432fc47255d8b} {QuoteHash:other hash DepositAddress:mkE1WWdiu5VgjfugomDk8GxV6JdEEEJR9s Signature:456 RequiredLiquidity:777 State:WaitingForDepositConfirmations UserRskTxHash:0x6b2e1e4daf8cf00c5c3534b72cdeec3526e8a38f70c11e44888b6e4ae1ee7d38 LpBtcTxHash:6ac3779dc33ad52f3409cbb909bcd458745995496a2a3954406206f6e5d4cb0e RefundPegoutTxHash:0x8e773a2826e73f8e5792304379a7e46dff38f17089c6d344335e03537b31c2bc BridgeRefundTxHash:0x4f3f6f0664a732e4c907971e75c1e3fd8671461dcb53f566660432fc47255d8b}]"
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
secondQuote := testRetainedPegoutQuote
secondQuote.QuoteHash = "other hash"
secondQuote.Signature = "456"
secondQuote.RequiredLiquidity = entities.NewWei(777)
collection.On("Find", mock.Anything,
bson.D{primitive.E{Key: "state", Value: bson.D{primitive.E{Key: "$in", Value: states}}}},
).Return(mongoDb.NewCursorFromDocuments([]any{testRetainedPegoutQuote, secondQuote}, nil, nil)).Once()
defer assertDbInteractionLog(t, expectedLog)()
result, err := repo.GetRetainedQuoteByState(context.Background(), states...)
collection.AssertExpectations(t)
require.NoError(t, err)
assert.Equal(t, []quote.RetainedPegoutQuote{testRetainedPegoutQuote, secondQuote}, result)
})
t.Run("Db error when getting retained pegout quotes by state", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("Find", mock.Anything, mock.Anything).
Return(nil, assert.AnError).Once()
result, err := repo.GetRetainedQuoteByState(context.Background(), states...)
collection.AssertExpectations(t)
require.Error(t, err)
assert.Nil(t, result)
})
}
func TestPegoutMongoRepository_ListPegoutDepositsByAddress(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.DepositEventsCollection)
log.SetLevel(log.DebugLevel)
t.Run("List pegout deposits by address successfully", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("Find", mock.Anything,
bson.M{"from": bson.M{"$regex": test.AnyAddress, "$options": "i"}},
options.Find().SetSort(bson.M{"timestamp": -1}),
).Return(mongoDb.NewCursorFromDocuments([]any{testPegoutDeposit}, nil, nil)).Once()
defer assertDbInteractionLog(t, mongo.Read)()
result, err := repo.ListPegoutDepositsByAddress(context.Background(), test.AnyAddress)
collection.AssertExpectations(t)
require.NoError(t, err)
assert.Equal(t, []quote.PegoutDeposit{testPegoutDeposit}[0], result[0])
})
t.Run("Db error when listing pegout deposits by address", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("Find", mock.Anything, mock.Anything, mock.Anything).
Return(nil, assert.AnError).Once()
result, err := repo.ListPegoutDepositsByAddress(context.Background(), test.AnyAddress)
collection.AssertExpectations(t)
require.Error(t, err)
assert.Empty(t, result)
})
t.Run("Should sanitize address properly", func(t *testing.T) {
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("Find", mock.Anything,
bson.M{"from": bson.M{"$regex": "0x1234567890abcdef1234567890abcdef12345678\\(a\\+\\)\\+", "$options": "i"}},
options.Find().SetSort(bson.M{"timestamp": -1}),
).Return(mongoDb.NewCursorFromDocuments([]any{testPegoutDeposit}, nil, nil)).Once()
result, err := repo.ListPegoutDepositsByAddress(context.Background(), "0x1234567890abcdef1234567890abcdef12345678(a+)+")
collection.AssertExpectations(t)
require.NoError(t, err)
assert.Equal(t, []quote.PegoutDeposit{testPegoutDeposit}[0], result[0])
})
}
func TestPegoutMongoRepository_UpsertPegoutDeposit(t *testing.T) {
t.Run("Upsert pegout deposit successfully", func(t *testing.T) {
now := time.Now().UTC()
expectedLog := "UPSERT interaction with db: {TxHash:any value QuoteHash:any value Amount:1000 Timestamp:" + now.String() + " BlockNumber:790 From:any address}"
client, collection := getClientAndCollectionMocks(mongo.DepositEventsCollection)
newDeposit := testPegoutDeposit
newDeposit.Amount = entities.NewWei(1000)
newDeposit.Timestamp = now
newDeposit.BlockNumber = 790
collection.On("ReplaceOne", mock.Anything,
bson.M{"tx_hash": testPegoutDeposit.TxHash},
newDeposit,
options.Replace().SetUpsert(true),
).Return(&mongoDb.UpdateResult{ModifiedCount: 1}, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer test.AssertLogContains(t, expectedLog)()
err := repo.UpsertPegoutDeposit(context.Background(), newDeposit)
collection.AssertExpectations(t)
require.NoError(t, err)
})
t.Run("Db error when upserting pegout deposit", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.DepositEventsCollection)
collection.On("ReplaceOne", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
err := repo.UpsertPegoutDeposit(context.Background(), testPegoutDeposit)
collection.AssertExpectations(t)
require.Error(t, err)
})
t.Run("Error when upserting more than one pegout deposit", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.DepositEventsCollection)
collection.On("ReplaceOne", mock.Anything, mock.Anything, mock.Anything, mock.Anything).
Return(&mongoDb.UpdateResult{ModifiedCount: 2}, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
err := repo.UpsertPegoutDeposit(context.Background(), testPegoutDeposit)
collection.AssertExpectations(t)
require.ErrorContains(t, err, "multiple deposits updated")
})
}
func TestPegoutMongoRepository_UpsertPegoutDeposits(t *testing.T) {
t.Run("Upsert pegout deposits successfully", func(t *testing.T) {
now := time.Now().UTC()
expectedLog := "UPSERT interaction with db: [{TxHash:tx1 QuoteHash:quote1 Amount:1000 Timestamp:" + now.String() + " BlockNumber:790 From:any address} {TxHash:tx2 QuoteHash:quote2 Amount:2000 Timestamp:" + now.String() + " BlockNumber:791 From:any address}]"
client, collection := getClientAndCollectionMocks(mongo.DepositEventsCollection)
deposits := []quote.PegoutDeposit{
{TxHash: "tx1", QuoteHash: "quote1", Amount: entities.NewWei(1000), Timestamp: now, BlockNumber: 790, From: test.AnyAddress},
{TxHash: "tx2", QuoteHash: "quote2", Amount: entities.NewWei(2000), Timestamp: now, BlockNumber: 791, From: test.AnyAddress},
}
collection.On("BulkWrite", mock.Anything,
[]mongoDb.WriteModel{
&mongoDb.ReplaceOneModel{
Upsert: btcjson.Bool(true),
Filter: bson.M{"tx_hash": deposits[0].TxHash},
Replacement: deposits[0],
},
&mongoDb.ReplaceOneModel{
Upsert: btcjson.Bool(true),
Filter: bson.M{"tx_hash": deposits[1].TxHash},
Replacement: deposits[1],
},
}).
Return(nil, nil).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer test.AssertLogContains(t, expectedLog)()
err := repo.UpsertPegoutDeposits(context.Background(), deposits)
collection.AssertExpectations(t)
require.NoError(t, err)
})
t.Run("Db error when upserting pegout deposits", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.DepositEventsCollection)
collection.On("BulkWrite", mock.Anything, mock.Anything).
Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
err := repo.UpsertPegoutDeposits(context.Background(), []quote.PegoutDeposit{testPegoutDeposit})
collection.AssertExpectations(t)
require.Error(t, err)
})
}
// nolint:funlen
func TestPegoutMongoRepository_UpdateRetainedQuotes(t *testing.T) {
retainedQuotes := []quote.RetainedPegoutQuote{
{QuoteHash: "quote1", DepositAddress: test.AnyAddress, Signature: test.AnyString, RequiredLiquidity: entities.NewWei(1000), State: quote.PegoutStateSendPegoutSucceeded},
{QuoteHash: "quote2", DepositAddress: test.AnyAddress, Signature: test.AnyString, RequiredLiquidity: entities.NewWei(2000), State: quote.PegoutStateSendPegoutFailed},
}
t.Run("Update retained quotes successfully", func(t *testing.T) {
const expectedLog = "UPDATE interaction with db: [{QuoteHash:quote1 DepositAddress:any address Signature:any value RequiredLiquidity:1000 State:SendPegoutSucceeded UserRskTxHash: LpBtcTxHash: RefundPegoutTxHash: BridgeRefundTxHash:} {QuoteHash:quote2 DepositAddress:any address Signature:any value RequiredLiquidity:2000 State:SendPegoutFailed UserRskTxHash: LpBtcTxHash: RefundPegoutTxHash: BridgeRefundTxHash:}]"
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
session := &mocks.SessionBindingMock{}
client.On("StartSession").Return(session, nil).Once()
session.On("EndSession", mock.Anything).Return().Once()
session.On("WithTransaction", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
fn, ok := args.Get(1).(func(mongoDb.SessionContext) (any, error))
require.True(t, ok)
count, err := fn(mongoDb.NewSessionContext(context.Background(), mongoDb.SessionFromContext(context.Background())))
require.NoError(t, err)
assert.Equal(t, int64(len(retainedQuotes)), count)
}).
Return(any(int64(len(retainedQuotes))), nil)
for _, q := range retainedQuotes {
collection.On("UpdateOne", mock.Anything,
bson.D{primitive.E{Key: "quote_hash", Value: q.QuoteHash}},
bson.D{primitive.E{Key: "$set", Value: q}},
).Return(&mongoDb.UpdateResult{ModifiedCount: 1}, nil).Once()
}
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer assertDbInteractionLog(t, expectedLog)()
err := repo.UpdateRetainedQuotes(context.Background(), retainedQuotes)
collection.AssertExpectations(t)
client.AssertExpectations(t)
session.AssertExpectations(t)
require.NoError(t, err)
})
t.Run("Error creating session", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
session := &mocks.SessionBindingMock{}
client.On("StartSession").Return(session, assert.AnError).Once()
session.On("EndSession", mock.Anything).Return().Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer test.AssertNoLog(t)()
err := repo.UpdateRetainedQuotes(context.Background(), retainedQuotes)
collection.AssertExpectations(t)
client.AssertExpectations(t)
session.AssertExpectations(t)
require.Error(t, err)
})
t.Run("Error updating one quote", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
session := &mocks.SessionBindingMock{}
client.On("StartSession").Return(session, nil).Once()
session.On("EndSession", mock.Anything).Return().Once()
session.On("WithTransaction", mock.Anything, mock.Anything).
Run(func(args mock.Arguments) {
fn, ok := args.Get(1).(func(mongoDb.SessionContext) (any, error))
require.True(t, ok)
count, err := fn(mongoDb.NewSessionContext(context.Background(), mongoDb.SessionFromContext(context.Background())))
require.Error(t, err)
assert.Equal(t, int64(0), count)
}).
Return(int64(0), assert.AnError)
collection.On("UpdateOne", mock.Anything, mock.Anything, mock.Anything).Return(&mongoDb.UpdateResult{ModifiedCount: 1}, nil).Once()
collection.On("UpdateOne", mock.Anything, mock.Anything, mock.Anything).Return(nil, assert.AnError).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer test.AssertNoLog(t)()
err := repo.UpdateRetainedQuotes(context.Background(), retainedQuotes)
collection.AssertExpectations(t)
client.AssertExpectations(t)
session.AssertExpectations(t)
require.Error(t, err)
})
t.Run("Update count mismatch", func(t *testing.T) {
client, _ := getClientAndCollectionMocks(mongo.RetainedPegoutQuoteCollection)
session := &mocks.SessionBindingMock{}
client.On("StartSession").Return(session, nil).Once()
session.On("EndSession", mock.Anything).Return().Once()
session.On("WithTransaction", mock.Anything, mock.Anything).Return(any(int64(len(retainedQuotes)-1)), nil)
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
defer test.AssertNoLog(t)()
err := repo.UpdateRetainedQuotes(context.Background(), retainedQuotes)
client.AssertExpectations(t)
session.AssertExpectations(t)
require.ErrorContains(t, err, "mismatch on updated documents. Expected 2, updated 1")
})
}
func TestPegoutMongoRepository_GetPegoutCreationData(t *testing.T) {
log.SetLevel(log.DebugLevel)
t.Run("read pegout creation data properly", func(t *testing.T) {
const (
expectedLog = "READ interaction with db: {FeeRate:1.55 FeePercentage:2.41 GasPrice:123 FixedFee:456}"
hash = "27d70ec2bc2c3154dc9a5b53b118a755441b22bc1c8ccde967ed33609970c25f"
)
client, collection := getClientAndCollectionMocks(mongo.PegoutCreationDataCollection)
collection.EXPECT().FindOne(mock.Anything, bson.D{primitive.E{Key: "hash", Value: hash}}).
Return(mongoDb.NewSingleResultFromDocument(testPegoutCreationData, nil, nil)).Once()
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
defer assertDbInteractionLog(t, expectedLog)()
result := repo.GetPegoutCreationData(context.Background(), hash)
collection.AssertExpectations(t)
assert.Equal(t, testPegoutCreationData, result)
})
t.Run("return zero value on invalid hash", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.PegoutCreationDataCollection)
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
result := repo.GetPegoutCreationData(context.Background(), test.AnyString)
collection.AssertNotCalled(t, "FindOne")
assert.Equal(t, quote.PegoutCreationDataZeroValue(), result)
})
t.Run("return zero value on db error", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.PegoutCreationDataCollection)
collection.EXPECT().FindOne(mock.Anything, mock.Anything).
Return(mongoDb.NewSingleResultFromDocument(nil, nil, nil)).Once()
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
result := repo.GetPegoutCreationData(context.Background(), test.AnyHash)
collection.AssertExpectations(t)
assert.Equal(t, quote.PegoutCreationDataZeroValue(), result)
})
}
func TestPegoutMongoRepository_GetQuotes(t *testing.T) {
t.Run("Successfully retrieves quotes", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.PegoutQuoteCollection)
log.SetLevel(log.DebugLevel)
hashes := []string{testRetainedPegoutQuote.QuoteHash}
repo := mongo.NewPegoutMongoRepository(mongo.NewConnection(client, time.Duration(1)))
collection.On("Find", mock.Anything,
bson.M{"hash": bson.M{"$in": hashes}},
).Return(mongoDb.NewCursorFromDocuments([]any{testPegoutQuote}, nil, nil)).Once()
result, err := repo.GetQuotes(context.Background(), hashes)
collection.AssertExpectations(t)
require.NoError(t, err)
assert.Equal(t, []quote.PegoutQuote{testPegoutQuote}, result)
})
t.Run("Fails validation for hashes", func(t *testing.T) {
client, _ := getClientAndCollectionMocks(mongo.PegoutQuoteCollection)
invalidHashes := []string{"invalidHash"}
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
_, err := repo.GetQuotes(context.Background(), invalidHashes)
require.Error(t, err)
assert.Equal(t, "invalid quote hash length: expected 64 characters, got 11", err.Error())
})
t.Run("error reading quotes from DB", func(t *testing.T) {
client, collection := getClientAndCollectionMocks(mongo.PegoutQuoteCollection)
expectedHashes := []string{testRetainedPegoutQuote.QuoteHash}
collection.On("Find", mock.Anything, bson.M{"hash": bson.M{"$in": expectedHashes}}).Return(nil, mongoDb.ErrNoDocuments).Once()
conn := mongo.NewConnection(client, time.Duration(1))
repo := mongo.NewPegoutMongoRepository(conn)
quotes, err := repo.GetQuotes(context.Background(), expectedHashes)
require.Error(t, err)
assert.Equal(t, "mongo: no documents in result", err.Error())
assert.Nil(t, quotes)
})
}