Skip to content

Commit

Permalink
Currency: Use mock provider for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Nov 11, 2023
1 parent a1f316a commit c53bfd5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 22 deletions.
37 changes: 15 additions & 22 deletions currency/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@ package currency

import (
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewConversionFromString(t *testing.T) {
expected := "AUDUSD"
conv, err := NewConversionFromString(expected)
if err != nil {
t.Error(err)
}
if conv.String() != expected {
t.Errorf("NewConversion() error expected %s but received %s",
expected,
conv)
}

newexpected := strings.ToLower(expected)
conv, err = NewConversionFromString(newexpected)
if err != nil {
t.Error(err)
}
if conv.String() != newexpected {
t.Errorf("NewConversion() error expected %s but received %s",
newexpected,
conv)
}
conv, err := NewConversionFromString("AUDUSD")
assert.NoError(t, err, "NewConversionFromString should not error")
assert.Equal(t, "AUDUSD", conv.String(), "Should provide correct conversion currency")
r, err := conv.GetRate()
assert.NoError(t, err, "GetRate should not error")
assert.Positive(t, r, "Should provide correct conversion rate")

conv, err = NewConversionFromString("audusd")
assert.NoError(t, err, "NewConversionFromString should not error")
assert.Equal(t, "audusd", conv.String(), "Should provide correct conversion for lowercase")
r, err = conv.GetRate()
assert.NoError(t, err, "GetRate should not error")
assert.Positive(t, r, "Should provide correct conversion rate")
}

func TestNewConversionFromStrings(t *testing.T) {
Expand Down
39 changes: 39 additions & 0 deletions currency/mock_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package currency

import (
"math/rand"
"strings"

"github.com/thrasher-corp/gocryptotrader/currency/forexprovider"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider/base"
)

type MockProvider struct{}

func newMockProvider() *forexprovider.ForexProviders {
p := &MockProvider{}
c, _ := p.GetSupportedCurrencies()
return &forexprovider.ForexProviders{
base.FXHandler{
Primary: base.Provider{
Provider: p,
SupportedCurrencies: c,
},
},
}
}

func (m *MockProvider) GetName() string { return "MockProvider" }
func (m *MockProvider) Setup(base.Settings) error { return nil }
func (m *MockProvider) IsEnabled() bool { return true }
func (m *MockProvider) IsPrimaryProvider() bool { return true }
func (m *MockProvider) GetSupportedCurrencies() ([]string, error) {
return storage.defaultFiatCurrencies.Strings(), nil
}
func (m *MockProvider) GetRates(baseCurrency, symbols string) (map[string]float64, error) {
c := map[string]float64{}
for _, s := range strings.Split(symbols, ",") {
c[baseCurrency+s] = 1/1 + rand.Float64() // The year is 2027; The USD is nearly worthless. The world reserve currency is eggs.
}
return c, nil
}
2 changes: 2 additions & 0 deletions currency/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func TestMain(m *testing.M) {
os.Exit(1)
}

storage.fiatExchangeMarkets = newMockProvider()

t := m.Run()

err = os.RemoveAll(testhelpers.TempDir)
Expand Down

0 comments on commit c53bfd5

Please sign in to comment.