Skip to content

Commit

Permalink
[bug] ref #335 - Modified TestRemoteWalletScanUnspentOutputs and Test…
Browse files Browse the repository at this point in the history
…LocalWalletScanUnspentOutputs test to increase the coverage.
  • Loading branch information
hsequeda committed Feb 11, 2020
1 parent 67f310b commit d9e4084
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
47 changes: 33 additions & 14 deletions src/coin/skycoin/models/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ func TestRemoteWalletScanUnspentOutputs(t *testing.T) {
Label: "wallet_no_outputs",
Encrypted: true,
},
Entries: []readable.WalletEntry{
{Address: "2JJ8pgq8EDAnrzf9xxBJapE2qkYLefW4uF8"},
},
},
nil)

Expand All @@ -336,7 +339,7 @@ func TestRemoteWalletScanUnspentOutputs(t *testing.T) {
// addrs
global_mock.On("OutputsForAddresses", []string{"2kvLEyXwAYvHfJuFCkjnYNRTUfHPyWgVwKt"}).Return(response, nil)
// no_output
global_mock.On("OutputsForAddresses", []string{"2JJ8pgq8EDAnrzf9xxBJapE2qkYLefW4uF8"}).Return(&readable.UnspentOutputsSummary{}, nil)
global_mock.On("OutputsForAddresses", []string{"2JJ8pgq8EDAnrzf9xxBJapE2qkYLefW4uF8"}).Return(nil, errors.New("error"))

iter, err = wlt.ScanUnspentOutputs()
require.NoError(t, err)
Expand All @@ -354,14 +357,8 @@ func TestRemoteWalletScanUnspentOutputs(t *testing.T) {
poolSection: PoolSection,
}
iter, err = wlt.ScanUnspentOutputs()
require.NoError(t, err)
items = 0
for iter.Next() {
to := iter.Value()
items++
require.Nil(t, to)
}
require.Equal(t, 0, items)
require.Error(t, err)
require.Nil(t, iter)
}

func TestRemoteWalletListTransactions(t *testing.T) {
Expand Down Expand Up @@ -416,12 +413,12 @@ func TestRemoteWalletListTransactions(t *testing.T) {
func TestLocalWalletScanUnspentOutputs(t *testing.T) {
CleanGlobalMock()

global_mock.On("Wallet", "test.wlt").Return(
global_mock.On("Wallet", "test1.wlt").Return(
&api.WalletResponse{
Meta: readable.WalletMeta{
Coin: "Sky",
Filename: "FiberCrypto",
Label: "test.wlt",
Label: "test1.wlt",
Encrypted: true,
},
Entries: []readable.WalletEntry{
Expand All @@ -437,13 +434,35 @@ func TestLocalWalletScanUnspentOutputs(t *testing.T) {
"6gnBM5gMSSb7XRUEap7q3WxFnuvbN9usTq",
}

mockSkyApiOutputsForAddresses(global_mock, addresses)

wlt := &LocalWallet{WalletDir: "./testdata", Id: "no_wallet.wlt"}
wlt := &LocalWallet{WalletDir: "./testdata", Id: "test.wlt"}
mockSkyApiOutputsForAddresses(global_mock, addresses, true)
iter, err := wlt.ScanUnspentOutputs()
require.Error(t, err)
require.Nil(t, iter)

CleanGlobalMock()

global_mock.On("Wallet", "test1.wlt").Return(
&api.WalletResponse{
Meta: readable.WalletMeta{
Coin: "Sky",
Filename: "FiberCrypto",
Label: "test1.wlt",
Encrypted: true,
},
Entries: []readable.WalletEntry{
{Address: "2JJ8pgq8EDAnrzf9xxBJapE2qkYLefW4uF8"},
},
},
nil)

mockSkyApiOutputsForAddresses(global_mock, addresses, false)

wlt = &LocalWallet{WalletDir: "./testdata", Id: "no_wallet.wlt"}
iter, err = wlt.ScanUnspentOutputs()
require.Error(t, err)
require.Nil(t, iter)

wlt = &LocalWallet{WalletDir: "./testdata", Id: "test.wlt"}
iter, err = wlt.ScanUnspentOutputs()
require.NoError(t, err)
Expand Down
12 changes: 11 additions & 1 deletion src/coin/skycoin/models/skyapi_mock_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package skycoin

import (
"errors"
"github.com/SkycoinProject/skycoin/src/api"
"github.com/SkycoinProject/skycoin/src/cipher"
"github.com/SkycoinProject/skycoin/src/coin"
Expand Down Expand Up @@ -67,7 +68,7 @@ func mockSkyApiCreateTransaction(mock *SkycoinApiMock, req *api.CreateTransactio
nil)
}

func mockSkyApiOutputsForAddresses(mock *SkycoinApiMock, addresses []string) {
func mockSkyApiOutputsForAddresses(mock *SkycoinApiMock, addresses []string, isValid bool) {
usOut := readable.UnspentOutput{
Hash: "hash1",
Coins: "42",
Expand All @@ -80,12 +81,21 @@ func mockSkyApiOutputsForAddresses(mock *SkycoinApiMock, addresses []string) {
}

for _, addr := range addresses {
if isValid {
mock.On(
"OutputsForAddresses",
[]string{
addr,
},
).Return(nil, errors.New("error"))
}
mock.On(
"OutputsForAddresses",
[]string{
addr,
},
).Return(response, nil)

}
}

Expand Down

0 comments on commit d9e4084

Please sign in to comment.