Test: Internal exchange coverage (#1525)

* Test: Internal exchange coverage

* Tests: Rename exchange.TestInstance to Setup

This package function is either going to be imported and used as just exchange.Setup,
or more likely testexch.Setup.
This removes the Stutter of having
internal/testing/exchange.TestInstance which is implicit given the
package path
This commit is contained in:
Gareth Kirwan
2024-05-07 07:14:02 +02:00
committed by GitHub
parent ece58ebf6d
commit 93c2d0122b
9 changed files with 127 additions and 9 deletions

View File

@@ -7,6 +7,7 @@ import (
"log"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
@@ -19,12 +20,19 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/mock"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
testutils "github.com/thrasher-corp/gocryptotrader/internal/testing/utils"
)
// TestInstance takes an empty exchange instance and loads config for it from testdata/configtest and connects a NewTestWebsocket
func TestInstance(e exchange.IBotExchange) error {
// Setup takes an empty exchange instance and loads config for it from testdata/configtest and connects a NewTestWebsocket
func Setup(e exchange.IBotExchange) error {
cfg := &config.Config{}
err := cfg.LoadConfig("../../testdata/configtest.json", true)
root, err := testutils.RootPathFromCWD()
if err != nil {
return err
}
err = cfg.LoadConfig(filepath.Join(root, "testdata", "configtest.json"), true)
if err != nil {
return fmt.Errorf("LoadConfig() error: %w", err)
}
@@ -92,7 +100,7 @@ func MockWsInstance[T any, PT interface {
tb.Helper()
e := PT(new(T))
require.NoError(tb, TestInstance(e), "TestInstance setup should not error")
require.NoError(tb, Setup(e), "Test exchange Setup must not error")
s := httptest.NewServer(h)

View File

@@ -0,0 +1,35 @@
package exchange
import (
"testing"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/exchanges/binance"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
)
// TestSetup exercises Setup
func TestSetup(t *testing.T) {
b := new(binance.Binance)
require.NoError(t, Setup(b), "Setup must not error")
assert.NotNil(t, b.Websocket, "Websocket should not be nil after Setup")
e := new(sharedtestvalues.CustomEx)
assert.ErrorIs(t, Setup(e), config.ErrExchangeNotFound, "Setup should error correctly on a missing exchange")
}
// TestMockHTTPInstance exercises MockHTTPInstance
func TestMockHTTPInstance(t *testing.T) {
b := new(binance.Binance)
require.NoError(t, Setup(b), "Test exchange Setup must not error")
require.NoError(t, MockHTTPInstance(b), "MockHTTPInstance must not error")
}
// TestMockWsInstance exercises MockWsInstance
func TestMockWsInstance(t *testing.T) {
b := MockWsInstance[binance.Binance](t, CurryWsMockUpgrader(t, func(_ []byte, _ *websocket.Conn) error { return nil }))
require.NotNil(t, b, "MockWsInstance must not be nil")
}

View File

@@ -0,0 +1,3 @@
{
"routes": null
}