Skip to content

Commit

Permalink
Cleaned up new linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed May 3, 2024
1 parent bf3a40c commit f9d92c6
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 177 deletions.
44 changes: 22 additions & 22 deletions best_quote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"

Expand All @@ -29,32 +29,32 @@ func (m *mockHTTPValidBestQuote) Do(req *http.Request) (*http.Response, error) {
// Valid response
if req.URL.String() == feeQuoteURLTaal {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T21:26:17.410Z\",\"expiryTime\":\"2020-10-09T21:36:17.410Z\",\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"currentHighestBlockHash\":\"0000000000000000035c5f8c0294802a01e500fa7b95337963bb3640da3bd565\",\"currentHighestBlockHeight\":656169,\"minerReputation\":null,\"fees\":[{\"id\":1,\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":400,\"bytes\":1000},\"relayFee\":{\"satoshis\":250,\"bytes\":1000}},{\"id\":2,\"feeType\":\"data\",\"miningFee\":{\"satoshis\":500,\"bytes\":1000},\"relayFee\":{\"satoshis\":225,\"bytes\":1000}}]}",
"signature": "3045022100eed49f6bf75d8f975f581271e3df658fbe8ec67e6301ea8fc25a72d18c92e30e022056af253f0d24db6a8fde4e2c1ee95e7a5ecf2c7cdc93246f8328c9e0ca582fc4",
"publicKey": "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`)))
"publicKey": "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}

if req.URL.String() == feeQuoteURLMatterPool {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T22:08:26.236Z\",\"expiryTime\":\"2020-10-09T22:18:26.236Z\",\"minerId\":\"0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087\",\"currentHighestBlockHash\":\"0000000000000000028285a9168c95457521a743765f499de389c094e883f42a\",\"currentHighestBlockHeight\":656171,\"minerReputation\":null,\"fees\":[{\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":400,\"bytes\":1000},\"relayFee\":{\"satoshis\":100,\"bytes\":1000}},{\"feeType\":\"data\",\"miningFee\":{\"satoshis\":430,\"bytes\":1000},\"relayFee\":{\"satoshis\":110,\"bytes\":1000}}]}",
"signature": "3044022011f90db2661726eb2659c3447ccaa9fd3368194f87d5d86a23e673c45d5d714502200c51eb600e3370b49d759aa4d441000286937b0803037a1d6de4c5a5c559d74c",
"publicKey": "0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`)))
"publicKey": "0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}

if req.URL.String() == feeQuoteURLMempool {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T22:09:04.433Z\",\"expiryTime\":\"2020-10-09T22:19:04.433Z\",\"minerId\":null,\"currentHighestBlockHash\":\"0000000000000000028285a9168c95457521a743765f499de389c094e883f42a\",\"currentHighestBlockHeight\":656171,\"minerReputation\":null,\"fees\":[{\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":500,\"bytes\":1000},\"relayFee\":{\"satoshis\":250,\"bytes\":1000}},{\"feeType\":\"data\",\"miningFee\":{\"satoshis\":420,\"bytes\":1000},\"relayFee\":{\"satoshis\":150,\"bytes\":1000}}]}",
"signature": null,"publicKey": null,"encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`)))
"signature": null,"publicKey": null,"encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}

if req.URL.String() == feeQuoteURLGorillaPool {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T22:09:04.433Z\",\"expiryTime\":\"2020-10-09T22:19:04.433Z\",\"minerId\":null,\"currentHighestBlockHash\":\"0000000000000000101c34c7cabadbff321f125fac9ba3c2b1294c4d81085f4a\",\"currentHighestBlockHeight\":713780,\"minerReputation\":null,\"fees\":[{\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":500,\"bytes\":1000},\"relayFee\":{\"satoshis\":250,\"bytes\":1000}},{\"feeType\":\"data\",\"miningFee\":{\"satoshis\":500,\"bytes\":1000},\"relayFee\":{\"satoshis\":250,\"bytes\":1000}}]}",
"signature": null,"publicKey": null,"encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`)))
"signature": null,"publicKey": null,"encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}

// Default is valid
Expand All @@ -77,28 +77,28 @@ func (m *mockHTTPBadRate) Do(req *http.Request) (*http.Response, error) {
// Valid response
if req.URL.String() == feeQuoteURLTaal {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T21:26:17.410Z\",\"expiryTime\":\"2020-10-09T21:36:17.410Z\",\"minerId\":\"03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270\",\"currentHighestBlockHash\":\"0000000000000000035c5f8c0294802a01e500fa7b95337963bb3640da3bd565\",\"currentHighestBlockHeight\":656169,\"minerReputation\":null,\"fees\":[{\"id\":1,\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":0,\"bytes\":1000},\"relayFee\":{\"satoshis\":0,\"bytes\":1000}},{\"id\":2,\"feeType\":\"data\",\"miningFee\":{\"satoshis\":0,\"bytes\":1000},\"relayFee\":{\"satoshis\":0,\"bytes\":1000}}]}",
"signature": "3045022100eed49f6bf75d8f975f581271e3df658fbe8ec67e6301ea8fc25a72d18c92e30e022056af253f0d24db6a8fde4e2c1ee95e7a5ecf2c7cdc93246f8328c9e0ca582fc4",
"publicKey": "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`)))
"publicKey": "03e92d3e5c3f7bd945dfbf48e7a99393b1bfb3f11f380ae30d286e7ff2aec5a270","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}

if req.URL.String() == feeQuoteURLMatterPool {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{
resp.Body = io.NopCloser(bytes.NewBufferString(`{
"payload": "{\"apiVersion\":\"` + testAPIVersion + `\",\"timestamp\":\"2020-10-09T22:08:26.236Z\",\"expiryTime\":\"2020-10-09T22:18:26.236Z\",\"minerId\":\"0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087\",\"currentHighestBlockHash\":\"0000000000000000028285a9168c95457521a743765f499de389c094e883f42a\",\"currentHighestBlockHeight\":656171,\"minerReputation\":null,\"fees\":[{\"feeType\":\"standard\",\"miningFee\":{\"satoshis\":0,\"bytes\":1000},\"relayFee\":{\"satoshis\":0,\"bytes\":1000}},{\"feeType\":\"data\",\"miningFee\":{\"satoshis\":0,\"bytes\":1000},\"relayFee\":{\"satoshis\":0,\"bytes\":1000}}]}",
"signature": "3044022011f90db2661726eb2659c3447ccaa9fd3368194f87d5d86a23e673c45d5d714502200c51eb600e3370b49d759aa4d441000286937b0803037a1d6de4c5a5c559d74c",
"publicKey": "0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`)))
"publicKey": "0211ccfc29e3058b770f3cf3eb34b0b2fd2293057a994d4d275121be4151cdf087","encoding": "` + testEncoding + `","mimetype": "` + testMimeType + `"}`))
}

if req.URL.String() == feeQuoteURLMempool {
resp.StatusCode = http.StatusBadRequest
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}

if req.URL.String() == feeQuoteURLGorillaPool {
resp.StatusCode = http.StatusBadRequest
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}

// Default is valid
Expand All @@ -121,22 +121,22 @@ func (m *mockHTTPBestQuoteAllFailed) Do(req *http.Request) (*http.Response, erro
// Valid response
if req.URL.String() == feeQuoteURLTaal {
resp.StatusCode = http.StatusBadRequest
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}

if req.URL.String() == feeQuoteURLMatterPool {
resp.StatusCode = http.StatusBadRequest
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}

if req.URL.String() == feeQuoteURLMempool {
resp.StatusCode = http.StatusBadRequest
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}

if req.URL.String() == feeQuoteURLGorillaPool {
resp.StatusCode = http.StatusBadRequest
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
resp.Body = io.NopCloser(bytes.NewBufferString(``))
}

// Default is valid
Expand All @@ -161,7 +161,7 @@ func TestClient_BestQuote(t *testing.T) {
assert.Equal(t, testMimeType, response.MimeType)

// Check that we got fees
assert.Equal(t, 2, len(response.Quote.Fees))
assert.Len(t, response.Quote.Fees, 2)
})

t.Run("http error", func(t *testing.T) {
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestClient_BestQuote(t *testing.T) {
require.NotNil(t, response)

// Check that we got fees
assert.Equal(t, 2, len(response.Quote.Fees))
assert.Len(t, response.Quote.Fees, 2)

var fee uint64
fee, err = response.Quote.CalculateFee(mapi.FeeCategoryRelay, mapi.FeeTypeData, 1000)
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestClient_BestQuote(t *testing.T) {

// Create a req
response, err := client.BestQuote(context.Background(), mapi.FeeCategoryMining, mapi.FeeTypeData)
assert.Error(t, err)
require.Error(t, err)
assert.Nil(t, response)
})
}
Expand Down
61 changes: 32 additions & 29 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package minercraft
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"testing"
"time"
Expand Down Expand Up @@ -39,7 +39,7 @@ func (m *mockHTTPDefaultClient) Do(req *http.Request) (*http.Response, error) {

if req.URL.String() == "/test" {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"message":"test"}`)))
resp.Body = io.NopCloser(bytes.NewBufferString(`{"message":"test"}`))
}

// Default is valid
Expand All @@ -60,22 +60,22 @@ func TestNewClient(t *testing.T) {
t.Run("valid new client", func(t *testing.T) {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
assert.NotNil(t, client)
assert.NoError(t, err)
require.NoError(t, err)

// Test default miners
assert.Equal(t, 2, len(client.Miners()))
assert.Len(t, client.Miners(), 2)
})

t.Run("custom http client", func(t *testing.T) {
client, err := NewClient(nil, http.DefaultClient, testAPIType, nil, nil)
assert.NotNil(t, client)
assert.NoError(t, err)
require.NoError(t, err)
})

t.Run("default miners", func(t *testing.T) {
client, err := NewClient(nil, nil, testAPIType, nil, nil)
assert.NotNil(t, client)
assert.NoError(t, err)
require.NotNil(t, client)
require.NoError(t, err)

// Get Taal
miner := client.MinerByName(MinerTaal)
Expand Down Expand Up @@ -104,13 +104,13 @@ func TestNewClient(t *testing.T) {

client, err := NewClient(nil, nil, testAPIType, miners, minerAPIs)
assert.NotNil(t, client)
assert.NoError(t, err)
require.NoError(t, err)

// Get test miner
miner := client.MinerByName(testMinerName)
assert.Equal(t, testMinerName, miner.Name)

assert.Equal(t, 1, len(client.Miners()))
assert.Len(t, client.Miners(), 1)
})
}

Expand Down Expand Up @@ -141,7 +141,7 @@ func TestDefaultClientOptions(t *testing.T) {
options := DefaultClientOptions()

assert.Equal(t, defaultUserAgent, options.UserAgent)
assert.Equal(t, 2.0, options.BackOffExponentFactor)
assert.InDelta(t, 2.0, options.BackOffExponentFactor, 0.001)
assert.Equal(t, 2*time.Millisecond, options.BackOffInitialTimeout)
assert.Equal(t, 2*time.Millisecond, options.BackOffMaximumJitterInterval)
assert.Equal(t, 10*time.Millisecond, options.BackOffMaxTimeout)
Expand All @@ -160,7 +160,7 @@ func TestDefaultClientOptions(t *testing.T) {
options.RequestRetryCount = 0
client, err := NewClient(options, nil, testAPIType, nil, nil)
assert.NotNil(t, client)
assert.NoError(t, err)
require.NoError(t, err)
})
}

Expand Down Expand Up @@ -206,7 +206,7 @@ func TestClient_AddMiner(t *testing.T) {
Name: "Test",
},
[]API{
API{
{
Token: testMinerToken,
URL: "https://testminer.com",
Type: testAPIType,
Expand All @@ -222,7 +222,7 @@ func TestClient_AddMiner(t *testing.T) {
for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
err := client.AddMiner(test.inputMiner, test.inputAPIs)
assert.NoError(t, err)
require.NoError(t, err)

// Get the miner
miner := client.MinerByName(test.inputMiner.Name)
Expand Down Expand Up @@ -330,8 +330,8 @@ func TestClient_AddMiner(t *testing.T) {
client := newTestClient(&mockHTTPDefaultClient{})

// Add a miner to start
err := client.AddMiner(Miner{MinerID: testMinerID, Name: "Test"}, []API{API{URL: testMinerURL, Type: testAPIType}})
assert.NoError(t, err)
err := client.AddMiner(Miner{MinerID: testMinerID, Name: "Test"}, []API{{URL: testMinerURL, Type: testAPIType}})
require.NoError(t, err)

for _, test := range tests {
t.Run(test.testCase, func(t *testing.T) {
Expand Down Expand Up @@ -380,7 +380,7 @@ func TestClient_MinerByName(t *testing.T) {
err := client.AddMiner(Miner{
Name: testMinerName,
}, []API{{URL: testMinerURL, Type: testAPIType}})
assert.NoError(t, err)
require.NoError(t, err)

// Get valid miner
miner := client.MinerByName(testMinerName)
Expand All @@ -395,7 +395,7 @@ func TestClient_MinerByName(t *testing.T) {
Name: testMinerName,
}, []API{{URL: testMinerURL, Type: testAPIType}},
)
assert.NoError(t, err)
require.NoError(t, err)

// Get invalid miner
miner := client.MinerByName("Unknown")
Expand Down Expand Up @@ -443,7 +443,7 @@ func TestClient_MinerByID(t *testing.T) {
Name: testMinerName,
MinerID: testMinerID,
}, []API{{URL: testMinerURL, Type: testAPIType}})
assert.NoError(t, err)
require.NoError(t, err)

// Get valid miner
miner := client.MinerByID(testMinerID)
Expand All @@ -458,7 +458,7 @@ func TestClient_MinerByID(t *testing.T) {
Name: testMinerName,
MinerID: testMinerID,
}, []API{{URL: testMinerURL, Type: testAPIType}})
assert.NoError(t, err)
require.NoError(t, err)

// Get invalid miner
miner := client.MinerByID("00000")
Expand Down Expand Up @@ -488,7 +488,9 @@ func ExampleClient_MinerByID() {
// BenchmarkClient_MinerByID benchmarks the method MinerByID()
func BenchmarkClient_MinerByID(b *testing.B) {
client, _ := NewClient(nil, nil, testAPIType, nil, nil)
_ = client.AddMiner(Miner{Name: testMinerName, MinerID: testMinerID}, []API{API{URL: testMinerURL, Type: testAPIType}})
_ = client.AddMiner(
Miner{Name: testMinerName, MinerID: testMinerID},
[]API{{URL: testMinerURL, Type: testAPIType}})
for i := 0; i < b.N; i++ {
_ = client.MinerByID(testMinerID)
}
Expand All @@ -506,15 +508,16 @@ func TestClient_MinerUpdateToken(t *testing.T) {
Name: testMinerName,
MinerID: testMinerID,
}, []API{{URL: testMinerURL, Type: testAPIType, Token: testMinerToken}})
assert.NoError(t, err)
require.NoError(t, err)

// Update a valid miner token
client.MinerUpdateToken(testMinerName, "99999", testAPIType)

// Get valid miner
miner := client.MinerByID(testMinerID)
api, err := client.MinerAPIByMinerID(testMinerID, testAPIType)
assert.NoError(t, err)
var api *API
api, err = client.MinerAPIByMinerID(testMinerID, testAPIType)
require.NoError(t, err)
assert.NotNil(t, miner)
assert.Equal(t, "99999", api.Token)
})
Expand All @@ -526,8 +529,8 @@ func TestClient_MinerUpdateToken(t *testing.T) {
err := client.AddMiner(Miner{
Name: testMinerName,
MinerID: testMinerID,
}, []API{API{URL: testMinerURL, Type: testAPIType, Token: testMinerToken}})
assert.NoError(t, err)
}, []API{{URL: testMinerURL, Type: testAPIType, Token: testMinerToken}})
require.NoError(t, err)

// Update a invalid miner token
client.MinerUpdateToken("Unknown", "99999", testAPIType)
Expand Down Expand Up @@ -574,7 +577,7 @@ func TestClient_RemoveMiner(t *testing.T) {

// Remove miner
removed := client.RemoveMiner(client.MinerByName(MinerTaal))
assert.Equal(t, true, removed)
assert.True(t, removed)

// Try to get the miner
miner := client.MinerByName(MinerTaal)
Expand All @@ -592,7 +595,7 @@ func TestClient_RemoveMiner(t *testing.T) {

// Remove miner
removed := client.RemoveMiner(dummyMiner)
assert.Equal(t, false, removed)
assert.False(t, removed)
})

t.Run("remove a nil miner", func(t *testing.T) {
Expand All @@ -601,7 +604,7 @@ func TestClient_RemoveMiner(t *testing.T) {
// Remove miner
assert.Panics(t, func() {
removed := client.RemoveMiner(nil)
assert.Equal(t, false, removed)
assert.False(t, removed)
})
})
}
Expand Down Expand Up @@ -639,7 +642,7 @@ func TestDefaultMiners(t *testing.T) {
require.NotNil(t, miners)
// assert.Equal(t, MinerMempool, miners[1].Name)
//assert.Equal(t, MinerMatterpool, miners[1].Name)
assert.Equal(t, 2, len(miners))
assert.Len(t, miners, 2)
assert.Equal(t, MinerTaal, miners[0].Name)
assert.Equal(t, MinerGorillaPool, miners[1].Name)
})
Expand Down

0 comments on commit f9d92c6

Please sign in to comment.