Skip to content

Commit 5c8da97

Browse files
committed
improvements [3]
1 parent a18be94 commit 5c8da97

32 files changed

+236
-216
lines changed

cmd/utils/register_pegin/register_pegin_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package main
33
import (
44
"encoding/hex"
55
"flag"
6+
"math/big"
7+
"os"
8+
"testing"
9+
"time"
10+
611
"github.com/rsksmart/liquidity-provider-server/cmd/utils/scripts"
712
"github.com/rsksmart/liquidity-provider-server/internal/entities"
813
"github.com/rsksmart/liquidity-provider-server/internal/entities/blockchain"
@@ -13,10 +18,6 @@ import (
1318
"github.com/stretchr/testify/mock"
1419
"github.com/stretchr/testify/require"
1520
"golang.org/x/term"
16-
"math/big"
17-
"os"
18-
"testing"
19-
"time"
2021
)
2122

2223
// nolint:funlen
@@ -173,7 +174,7 @@ func TestRegisterPegInScriptInput_ToEnv(t *testing.T) {
173174
})
174175
}
175176

176-
func TestParseRegisterPegInScriptInput(t *testing.T) {
177+
func TestParseRegisterPegInScriptInput(t *testing.T) { //nolint:funlen
177178
parse := func() { /* mock function to prevent calling flag.Parse inside a test */ }
178179
input := RegisterPegInScriptInput{
179180
BaseInput: scripts.BaseInput{

internal/adapters/dataproviders/database/mongo/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (c *Connection) CheckConnection(ctx context.Context) bool {
8080
return err == nil
8181
}
8282

83-
func ListQuotesByDateRange[S any, Q any, R any](
83+
func ListQuotesByDateRange[S any, Q any, R any]( //nolint:funlen,cyclop
8484
ctx context.Context,
8585
conn *Connection,
8686
startDate, endDate time.Time,
@@ -158,7 +158,7 @@ func ListQuotesByDateRange[S any, Q any, R any](
158158
}
159159
}
160160
}
161-
if len(additionalHashes) > 0 {
161+
if len(additionalHashes) > 0 { //nolint:nestif
162162
additionalHashesList := make([]string, 0, len(additionalHashes))
163163
for hash := range additionalHashes {
164164
additionalHashesList = append(additionalHashesList, hash)

internal/adapters/dataproviders/database/mongo/common_test.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/rsksmart/liquidity-provider-server/test/mocks"
1212
"github.com/stretchr/testify/assert"
1313
"github.com/stretchr/testify/mock"
14+
"github.com/stretchr/testify/require"
1415
"go.mongodb.org/mongo-driver/bson"
1516
mongodriver "go.mongodb.org/mongo-driver/mongo"
1617
"go.mongodb.org/mongo-driver/mongo/readpref"
@@ -141,13 +142,13 @@ type TestRetainedQuote struct {
141142
State string
142143
}
143144

144-
func TestListQuotesByDateRange(t *testing.T) {
145+
func TestListQuotesByDateRange(t *testing.T) { //nolint:funlen,maintidx
145146
startDate := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
146147
endDate := time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)
147148
startTimestamp := startDate.Unix()
148149
endTimestamp := endDate.Unix()
149150

150-
setupBasicMocks := func() (*mocks.DbClientBindingMock, *mocks.DbBindingMock, *mocks.CollectionBindingMock, *mocks.CollectionBindingMock) {
151+
setupBasicMocks := func() (*mocks.DbClientBindingMock, *mocks.DbBindingMock, *mocks.CollectionBindingMock, *mocks.CollectionBindingMock) { //nolint:unparam
151152
client, db := getClientAndDatabaseMocks()
152153
quoteCollection := &mocks.CollectionBindingMock{}
153154
retainedCollection := &mocks.CollectionBindingMock{}
@@ -211,9 +212,9 @@ func TestListQuotesByDateRange(t *testing.T) {
211212
},
212213
)
213214

214-
assert.NoError(t, err)
215-
assert.Equal(t, 2, len(quotes))
216-
assert.Equal(t, 2, len(retained))
215+
require.NoError(t, err)
216+
assert.Len(t, quotes, 2)
217+
assert.Len(t, retained, 2)
217218
assert.Equal(t, TestQuote{Value: 1}, quotes[0])
218219
assert.Equal(t, TestQuote{Value: 2}, quotes[1])
219220
assert.Equal(t, TestRetainedQuote{QuoteHash: "hash1", State: "state1"}, retained[0])
@@ -256,7 +257,7 @@ func TestListQuotesByDateRange(t *testing.T) {
256257
},
257258
)
258259

259-
assert.NoError(t, err)
260+
require.NoError(t, err)
260261
assert.Empty(t, quotes)
261262
assert.Empty(t, retained)
262263
})
@@ -279,9 +280,9 @@ func TestListQuotesByDateRange(t *testing.T) {
279280
},
280281
)
281282

282-
assert.Error(t, err)
283-
assert.Nil(t, quotes)
284-
assert.Nil(t, retained)
283+
require.Error(t, err)
284+
assert.Empty(t, quotes)
285+
assert.Empty(t, retained)
285286
})
286287

287288
t.Run("database error on retained collection", func(t *testing.T) {
@@ -324,9 +325,9 @@ func TestListQuotesByDateRange(t *testing.T) {
324325
},
325326
)
326327

327-
assert.Error(t, err)
328-
assert.Nil(t, quotes)
329-
assert.Nil(t, retained)
328+
require.Error(t, err)
329+
assert.Empty(t, quotes)
330+
assert.Empty(t, retained)
330331
})
331332

332333
t.Run("error in quote cursor All", func(t *testing.T) {
@@ -347,9 +348,9 @@ func TestListQuotesByDateRange(t *testing.T) {
347348
},
348349
)
349350

350-
assert.Error(t, err)
351-
assert.Nil(t, quotes)
352-
assert.Nil(t, retained)
351+
require.Error(t, err)
352+
assert.Empty(t, quotes)
353+
assert.Empty(t, retained)
353354
assert.Equal(t, assert.AnError, err)
354355
})
355356

@@ -407,9 +408,9 @@ func TestListQuotesByDateRange(t *testing.T) {
407408
},
408409
)
409410

410-
assert.NoError(t, err)
411-
assert.Equal(t, 2, len(quotes))
412-
assert.Equal(t, 2, len(retained))
411+
require.NoError(t, err)
412+
assert.Len(t, quotes, 2)
413+
assert.Len(t, retained, 2)
413414
assert.Contains(t, []int{1, 2}, quotes[0].Value)
414415
assert.Contains(t, []int{1, 2}, quotes[1].Value)
415416
assert.Contains(t, []string{"state1", "state2"}, retained[0].State)
@@ -463,9 +464,9 @@ func TestListQuotesByDateRange(t *testing.T) {
463464
},
464465
)
465466

466-
assert.NoError(t, err)
467-
assert.Equal(t, 1, len(quotes))
468-
assert.Equal(t, 2, len(retained))
467+
require.NoError(t, err)
468+
assert.Len(t, quotes, 1)
469+
assert.Len(t, retained, 2)
469470
assert.Equal(t, TestQuote{Value: 1}, quotes[0])
470471
assert.Equal(t, "state1", retained[0].State)
471472
assert.Equal(t, "state2", retained[1].State)

internal/adapters/entrypoints/rest/handlers/get_report_summaries_test.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,20 @@ type MockPeginQuoteRepository struct {
2424

2525
func (m *MockPeginQuoteRepository) ListQuotesByDateRange(ctx context.Context, startDate, endDate time.Time) ([]quote.PeginQuote, []quote.RetainedPeginQuote, error) {
2626
args := m.Called(ctx, startDate, endDate)
27-
return args.Get(0).([]quote.PeginQuote), args.Get(1).([]quote.RetainedPeginQuote), args.Error(2)
27+
err := args.Error(2)
28+
if err != nil {
29+
return nil, nil, err
30+
}
31+
return args.Get(0).([]quote.PeginQuote), args.Get(1).([]quote.RetainedPeginQuote), nil
2832
}
2933

3034
func (m *MockPeginQuoteRepository) GetQuote(ctx context.Context, quoteHash string) (*quote.PeginQuote, error) {
3135
args := m.Called(ctx, quoteHash)
32-
return args.Get(0).(*quote.PeginQuote), args.Error(1)
36+
err := args.Error(1)
37+
if err != nil {
38+
return nil, err
39+
}
40+
return args.Get(0).(*quote.PeginQuote), nil
3341
}
3442

3543
type MockPegoutQuoteRepository struct {
@@ -38,12 +46,20 @@ type MockPegoutQuoteRepository struct {
3846

3947
func (m *MockPegoutQuoteRepository) ListQuotesByDateRange(ctx context.Context, startDate, endDate time.Time) ([]quote.PegoutQuote, []quote.RetainedPegoutQuote, error) {
4048
args := m.Called(ctx, startDate, endDate)
41-
return args.Get(0).([]quote.PegoutQuote), args.Get(1).([]quote.RetainedPegoutQuote), args.Error(2)
49+
err := args.Error(2)
50+
if err != nil {
51+
return nil, nil, err
52+
}
53+
return args.Get(0).([]quote.PegoutQuote), args.Get(1).([]quote.RetainedPegoutQuote), nil
4254
}
4355

4456
func (m *MockPegoutQuoteRepository) GetQuote(ctx context.Context, quoteHash string) (*quote.PegoutQuote, error) {
4557
args := m.Called(ctx, quoteHash)
46-
return args.Get(0).(*quote.PegoutQuote), args.Error(1)
58+
err := args.Error(1)
59+
if err != nil {
60+
return nil, err
61+
}
62+
return args.Get(0).(*quote.PegoutQuote), nil
4763
}
4864

4965
type MockSummariesUseCase struct {
@@ -124,7 +140,7 @@ func getReportSummariesHandlerForTest(useCase *MockSummariesUseCase) http.Handle
124140
}
125141
}
126142

127-
func TestGetReportSummariesHandler(t *testing.T) {
143+
func TestGetReportSummariesHandler(t *testing.T) { //nolint:funlen
128144
tests := []struct {
129145
name string
130146
url string
@@ -207,7 +223,7 @@ func TestGetReportSummariesHandler(t *testing.T) {
207223
for _, tt := range tests {
208224
t.Run(tt.name, func(t *testing.T) {
209225
mockUseCase := new(MockSummariesUseCase)
210-
req, err := http.NewRequest("GET", tt.url, nil)
226+
req, err := http.NewRequestWithContext(context.Background(), "GET", tt.url, nil)
211227
require.NoError(t, err)
212228

213229
if tt.expectedStatus == http.StatusOK || tt.expectedStatus == http.StatusInternalServerError {

internal/usecases/liquidity_provider/summaries.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package liquidity_provider
22

33
import (
44
"context"
5-
"fmt"
65
"log"
6+
"strconv"
77
"time"
88

99
"github.com/rsksmart/liquidity-provider-server/internal/entities"
@@ -62,7 +62,7 @@ func (u *SummariesUseCase) Run(ctx context.Context, startDate, endDate time.Time
6262
}, nil
6363
}
6464

65-
func (u *SummariesUseCase) aggregatePeginData(ctx context.Context, startDate, endDate time.Time) (SummaryData, error) {
65+
func (u *SummariesUseCase) aggregatePeginData(ctx context.Context, startDate, endDate time.Time) (SummaryData, error) { //nolint:funlen,cyclop
6666
var data SummaryData
6767
quotes, retainedQuotes, err := u.peginRepo.ListQuotesByDateRange(ctx, startDate, endDate)
6868
if err != nil {
@@ -79,14 +79,14 @@ func (u *SummariesUseCase) aggregatePeginData(ctx context.Context, startDate, en
7979
uniqueQuoteHashes := make(map[string]bool)
8080

8181
for i := range quotes {
82-
quoteHash := fmt.Sprintf("%d", quotes[i].Nonce)
82+
quoteHash := strconv.FormatInt(quotes[i].Nonce, 10)
8383
uniqueQuoteHashes[quoteHash] = true
8484
totalAmount.Add(totalAmount, quotes[i].Total())
8585
quoteCopy := quotes[i]
8686
processedQuotes[quoteHash] = &quoteCopy
8787
}
8888

89-
if len(retainedQuotes) > 0 {
89+
if len(retainedQuotes) > 0 { //nolint:nestif
9090
data.TotalQuotesCount = int64(len(retainedQuotes))
9191
var hashesToFetch []string
9292
for _, retainedQuote := range retainedQuotes {
@@ -145,7 +145,7 @@ func (u *SummariesUseCase) aggregatePeginData(ctx context.Context, startDate, en
145145
return data, nil
146146
}
147147

148-
func (u *SummariesUseCase) aggregatePegoutData(ctx context.Context, startDate, endDate time.Time) (SummaryData, error) {
148+
func (u *SummariesUseCase) aggregatePegoutData(ctx context.Context, startDate, endDate time.Time) (SummaryData, error) { //nolint:funlen,cyclop
149149
var data SummaryData
150150
quotes, retainedQuotes, err := u.pegoutRepo.ListQuotesByDateRange(ctx, startDate, endDate)
151151
if err != nil {
@@ -163,14 +163,14 @@ func (u *SummariesUseCase) aggregatePegoutData(ctx context.Context, startDate, e
163163
uniqueQuoteHashes := make(map[string]bool)
164164

165165
for i := range quotes {
166-
quoteHash := fmt.Sprintf("%d", quotes[i].Nonce)
166+
quoteHash := strconv.FormatInt(quotes[i].Nonce, 10)
167167
uniqueQuoteHashes[quoteHash] = true
168168
totalAmount.Add(totalAmount, quotes[i].Total())
169169
quoteCopy := quotes[i]
170170
processedQuotes[quoteHash] = &quoteCopy
171171
}
172172

173-
if len(retainedQuotes) > 0 {
173+
if len(retainedQuotes) > 0 { //nolint:nestif
174174
data.TotalQuotesCount = int64(len(retainedQuotes))
175175
var hashesToFetch []string
176176
for _, retainedQuote := range retainedQuotes {

internal/usecases/liquidity_provider/summaries_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (m *mockPegoutQuoteRepo) UpsertPegoutDeposits(ctx context.Context, deposits
157157
return args.Error(0)
158158
}
159159

160-
func TestSummariesUseCase_Run(t *testing.T) {
160+
func TestSummariesUseCase_Run(t *testing.T) { //nolint:funlen,maintidx
161161
startDate := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
162162
endDate := time.Date(2023, 1, 31, 23, 59, 59, 0, time.UTC)
163163

test/mocks/abstract_factory_mock.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/mocks/bitcoin_wallet_mock.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/mocks/client_adapter_mock.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/mocks/collection_binding_mock.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)