chainfee: use proper mock for WebAPIFeeSource in tests
This commit is contained in:
2 files changed
+24
-14
No files matched your search
@@ -9,15 +9,6 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockSparseConfFeeSource struct {
|
|
||||||
url string
|
|
||||||
fees map[uint32]uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e mockSparseConfFeeSource) GetFeeMap() (map[uint32]uint32, error) {
|
|
||||||
return e.fees, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestFeeRateTypes checks that converting fee rates between the
|
// TestFeeRateTypes checks that converting fee rates between the
|
||||||
// different types that represent fee rates and calculating fees
|
// different types that represent fee rates and calculating fees
|
||||||
// work as expected.
|
// work as expected.
|
||||||
@@ -204,10 +195,9 @@ func TestWebAPIFeeEstimator(t *testing.T) {
|
|||||||
maxTarget: minFeeRate,
|
maxTarget: minFeeRate,
|
||||||
}
|
}
|
||||||
|
|
||||||
feeSource := mockSparseConfFeeSource{
|
// Create a mock fee source and mock its returned map.
|
||||||
url: "https://www.github.com",
|
feeSource := &mockFeeSource{}
|
||||||
fees: feeRateResp,
|
feeSource.On("GetFeeMap").Return(feeRateResp, nil)
|
||||||
}
|
|
||||||
|
|
||||||
estimator := NewWebAPIEstimator(feeSource, false)
|
estimator := NewWebAPIEstimator(feeSource, false)
|
||||||
|
|
||||||
@@ -238,12 +228,15 @@ func TestWebAPIFeeEstimator(t *testing.T) {
|
|||||||
|
|
||||||
exp := SatPerKVByte(tc.expectedFeeRate).FeePerKWeight()
|
exp := SatPerKVByte(tc.expectedFeeRate).FeePerKWeight()
|
||||||
require.Equalf(t, exp, est, "target %v failed, fee "+
|
require.Equalf(t, exp, est, "target %v failed, fee "+
|
||||||
"map is %v", tc.target, feeSource.fees)
|
"map is %v", tc.target, feeRateResp)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop the estimator when test ends.
|
// Stop the estimator when test ends.
|
||||||
require.NoError(t, estimator.Stop(), "unable to stop fee estimator")
|
require.NoError(t, estimator.Stop(), "unable to stop fee estimator")
|
||||||
|
|
||||||
|
// Assert the mocked fee source is called as expected.
|
||||||
|
feeSource.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestGetCachedFee checks that the fee caching logic works as expected.
|
// TestGetCachedFee checks that the fee caching logic works as expected.
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package chainfee
|
||||||
|
|
||||||
|
import "github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
|
type mockFeeSource struct {
|
||||||
|
mock.Mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// A compile-time assertion to ensure that mockFeeSource implements the
|
||||||
|
// WebAPIFeeSource interface.
|
||||||
|
var _ WebAPIFeeSource = (*mockFeeSource)(nil)
|
||||||
|
|
||||||
|
func (m *mockFeeSource) GetFeeMap() (map[uint32]uint32, error) {
|
||||||
|
args := m.Called()
|
||||||
|
|
||||||
|
return args.Get(0).(map[uint32]uint32), args.Error(1)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user