From c53bfd56dcead9fb65560707941b5522f3ff0cc4 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan <gbjkirwan@gmail.com> Date: Sat, 11 Nov 2023 19:33:29 +0700 Subject: [PATCH] Currency: Use mock provider for tests --- currency/conversion_test.go | 37 +++++++++++++------------------- currency/mock_provider_test.go | 39 ++++++++++++++++++++++++++++++++++ currency/storage_test.go | 2 ++ 3 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 currency/mock_provider_test.go diff --git a/currency/conversion_test.go b/currency/conversion_test.go index 189b18f2178..ca97648d4f2 100644 --- a/currency/conversion_test.go +++ b/currency/conversion_test.go @@ -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) { diff --git a/currency/mock_provider_test.go b/currency/mock_provider_test.go new file mode 100644 index 00000000000..8d71876965c --- /dev/null +++ b/currency/mock_provider_test.go @@ -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 +} diff --git a/currency/storage_test.go b/currency/storage_test.go index 8481ac4a792..ab1eff4ad37 100644 --- a/currency/storage_test.go +++ b/currency/storage_test.go @@ -17,6 +17,8 @@ func TestMain(m *testing.M) { os.Exit(1) } + storage.fiatExchangeMarkets = newMockProvider() + t := m.Run() err = os.RemoveAll(testhelpers.TempDir)