Skip to content

Commit

Permalink
fix(transaction):#195 fixed unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Aug 16, 2021
1 parent f46a8d7 commit 15f2dc5
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
17 changes: 11 additions & 6 deletions core/resty/resty.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@ func New(transport *http.Transport, handle Handle, opts ...Option) *Resty {
r.transport = &http.Transport{}
}

r.client = CreateClient(r.transport, r.timeout)

return r
}

// CreateClient a function that create a client instance
var CreateClient = func(t *http.Transport, timeout time.Duration) Client {
client := &http.Client{
Transport: r.transport,
Transport: t,
}
if r.timeout > 0 {
client.Timeout = r.timeout
if timeout > 0 {
client.Timeout = timeout
}

r.client = client

return r
return client
}

// Client http client
Expand Down
2 changes: 1 addition & 1 deletion zboxcore/sdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

var (
noBLOBBERS = errors.New("no_blobber", "No Blobbers set in this allocation")
noBLOBBERS = errors.New("", "No Blobbers set in this allocation")
notInitialized = errors.New("sdk_not_initialized", "Please call InitStorageSDK Init and use GetAllocation to get the allocation object")
)

Expand Down
17 changes: 12 additions & 5 deletions zboxcore/sdk/allocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
"strings"
"sync"
"testing"
"time"

"github.com/0chain/gosdk/zboxcore/encryption"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/resty"

"github.com/0chain/gosdk/core/transaction"
"github.com/0chain/gosdk/core/util"
Expand Down Expand Up @@ -2673,6 +2675,13 @@ func TestAllocation_CommitFolderChange(t *testing.T) {

var mockClient = mocks.HttpClient{}
util.Client = &mockClient
resty.CreateClient = func(t *http.Transport, timeout time.Duration) resty.Client {
return &mockClient
}

transaction.SetConfig(&conf.Config{
MinConfirmation: 50,
})

client := zclient.GetClient()
client.Wallet = &zcncrypto.Wallet{
Expand All @@ -2683,7 +2692,7 @@ func TestAllocation_CommitFolderChange(t *testing.T) {
setupHttpResponse := func(t *testing.T, name string, httpMethod string, statusCode int, body []byte) {
mockClient.On("Do", mock.MatchedBy(func(req *http.Request) bool {
return req.Method == httpMethod &&
strings.HasPrefix(req.URL.Path, name)
strings.Index(req.URL.String(), name) > -1
})).Return(&http.Response{
StatusCode: statusCode,
Body: ioutil.NopCloser(bytes.NewReader([]byte(body))),
Expand Down Expand Up @@ -2719,8 +2728,6 @@ func TestAllocation_CommitFolderChange(t *testing.T) {
name: "Test_Sharder_Verify_Txn_Failed",
setup: func(t *testing.T, testCaseName string, a *Allocation) (teardown func(t *testing.T)) {

transaction.SetConfig(&conf.Config{})

body, err := json.Marshal(&transaction.Transaction{
Hash: mockHash,
})
Expand Down Expand Up @@ -2780,8 +2787,8 @@ func TestAllocation_CommitFolderChange(t *testing.T) {
}
a.InitAllocation()
sdkInitialized = true
blockchain.SetMiners([]string{tt.name + "mockMiners"})
blockchain.SetSharders([]string{tt.name + "mockSharders"})
blockchain.SetMiners([]string{"http://" + tt.name + "mockMiners"})
blockchain.SetSharders([]string{"http://" + tt.name + "mockSharders"})
if tt.setup != nil {
if teardown := tt.setup(t, tt.name, a); teardown != nil {
defer teardown(t)
Expand Down
3 changes: 2 additions & 1 deletion zboxcore/sdk/filemetaworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"

"github.com/0chain/errors"

"github.com/0chain/gosdk/core/zcncrypto"
"github.com/0chain/gosdk/zboxcore/blockchain"
zclient "github.com/0chain/gosdk/zboxcore/client"
Expand Down Expand Up @@ -70,7 +71,7 @@ func TestListRequest_getFileMetaInfoFromBlobber(t *testing.T) {
})).Return(&http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
StatusCode: p.respStatusCode,
}, errors.New(mockErrorMessage))
}, errors.New("", mockErrorMessage))
},
wantErr: true,
errMsg: mockErrorMessage,
Expand Down
2 changes: 1 addition & 1 deletion zboxcore/sdk/filestatsworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestListRequest_getFileStatsInfoFromBlobber(t *testing.T) {
})).Return(&http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
StatusCode: p.respStatusCode,
}, errors.New(mockErrorMessage))
}, errors.New("", mockErrorMessage))
},
wantErr: true,
errMsg: mockErrorMessage,
Expand Down
2 changes: 1 addition & 1 deletion zboxcore/sdk/listworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestListRequest_getListInfoFromBlobber(t *testing.T) {
})).Return(&http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
StatusCode: p.respStatusCode,
}, errors.New(mockErrorMessage))
}, errors.New("", mockErrorMessage))
},
wantErr: true,
errMsg: mockErrorMessage,
Expand Down

0 comments on commit 15f2dc5

Please sign in to comment.