exchanges: Improve TestMain usage (#1946)

* exchanges: Improve TestMain usage

* exchanges: Further cleanups

* exchanges/kucoin: update TestProcessOrderbook to use test context

* refactor: rename SetRunning to SetRunningURL for clarity across exchanges
This commit is contained in:
Adrian Gallagher
2025-06-25 13:02:47 +10:00
committed by GitHub
parent 61a3d765ee
commit ebcbfab358
38 changed files with 214 additions and 460 deletions

View File

@@ -7,6 +7,7 @@ import (
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
@@ -58,24 +59,32 @@ const httpMockFile = "testdata/http.json"
// MockHTTPInstance takes an existing Exchange instance and attaches it to a new http server
// It is expected to be run once, since http requests do not often tangle with each other
func MockHTTPInstance(e exchange.IBotExchange) error {
serverDetails, newClient, err := mock.NewVCRServer(httpMockFile)
func MockHTTPInstance(e exchange.IBotExchange, optionalPathPostfix ...string) error {
serverPath, newClient, err := mock.NewVCRServer(httpMockFile)
if err != nil {
return fmt.Errorf("mock server error %s", err)
return fmt.Errorf("error starting NewVCRServer: %w", err)
}
b := e.GetBase()
b.SkipAuthCheck = true
err = b.SetHTTPClient(newClient)
if err != nil {
return fmt.Errorf("mock server error %s", err)
if err := b.SetHTTPClient(newClient); err != nil {
return fmt.Errorf("error setting HTTP client: %w", err)
}
endpointMap := b.API.Endpoints.GetURLMap()
for k := range endpointMap {
err = b.API.Endpoints.SetRunning(k, serverDetails)
if len(optionalPathPostfix) > 0 {
newPath, err := url.JoinPath(serverPath, optionalPathPostfix...)
if err != nil {
return fmt.Errorf("mock server error %s", err)
return fmt.Errorf("error joining server URL path: %w", err)
}
serverPath = newPath
}
for k := range b.API.Endpoints.GetURLMap() {
if err := b.API.Endpoints.SetRunningURL(k, serverPath); err != nil {
return fmt.Errorf("error setting running endpoint: %w", err)
}
}
log.Printf(sharedtestvalues.MockTesting, e.GetName())
return nil
@@ -100,8 +109,8 @@ func MockWsInstance[T any, PT interface {
b := e.GetBase()
b.SkipAuthCheck = true
b.API.AuthenticatedWebsocketSupport = true
err := b.API.Endpoints.SetRunning("RestSpotURL", s.URL)
require.NoError(tb, err, "Endpoints.SetRunning must not error for RestSpotURL")
err := b.API.Endpoints.SetRunningURL("RestSpotURL", s.URL)
require.NoError(tb, err, "Endpoints.SetRunningURL must not error for RestSpotURL")
for _, auth := range []bool{true, false} {
err = b.Websocket.SetWebsocketURL("ws"+strings.TrimPrefix(s.URL, "http"), auth, true)
require.NoErrorf(tb, err, "SetWebsocketURL must not error for auth: %v", auth)

View File

@@ -26,7 +26,8 @@ func TestSetup(t *testing.T) {
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")
require.NoError(t, MockHTTPInstance(b), "MockHTTPInstance with no optional path must not error")
require.NoError(t, MockHTTPInstance(b, "api"), "MockHTTPInstance with optional path must not error")
}
// TestMockWsInstance exercises MockWsInstance