Bitmex: Use mock server for leaderboard and historical stats tests (#1939)

* Bitmex: Enhance leaderboard and stats history tests with mock server responses

* Bitmex: Validate HTTP method in leaderboard and stats historical tests

* Bitmex: Improve assertions in leaderboard and stats historical tests for clarity
This commit is contained in:
Adrian Gallagher
2025-06-25 15:48:25 +10:00
committed by GitHub
parent ebcbfab358
commit 48a66c9faa
2 changed files with 82 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package bitmex
import (
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
@@ -199,8 +200,42 @@ func TestGetInsuranceFundHistory(t *testing.T) {
func TestGetLeaderboard(t *testing.T) {
t.Parallel()
_, err := b.GetLeaderboard(t.Context(), LeaderboardGetParams{})
require.NoError(t, err)
b := new(Bitmex) //nolint:govet // Intentional shadow
err := testexch.Setup(b)
require.NoError(t, err, "Setup must not error")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Request method should be correct")
assert.Equal(t, "/api/v1/leaderboard", r.URL.Path, "Request path should be correct")
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(`[
{
"isRealName": true,
"name": "Trader Joe",
"profit": 12345.67
},
{
"isRealName": false,
"name": "CryptoKing",
"profit": 9876.54
}
]`))
assert.NoError(t, err, "Writing response to handler should not error")
}))
defer server.Close()
err = b.API.Endpoints.SetRunningURL(exchange.RestSpot.String(), server.URL+"/api/v1")
require.NoError(t, err, "SetRunningURL must not error")
expectedLeaderboard := []Leaderboard{
{IsRealName: true, Name: "Trader Joe", Profit: 12345.67},
{Name: "CryptoKing", Profit: 9876.54},
}
result, err := b.GetLeaderboard(t.Context(), LeaderboardGetParams{})
require.NoError(t, err, "GetLeaderboard must not error")
assert.Equal(t, expectedLeaderboard, result, "GetLeaderboard result should be correct")
}
func TestGetAliasOnLeaderboard(t *testing.T) {
@@ -349,8 +384,46 @@ func TestGetStats(t *testing.T) {
func TestGetStatsHistorical(t *testing.T) {
t.Parallel()
_, err := b.GetStatsHistorical(t.Context())
require.NoError(t, err)
b := new(Bitmex) //nolint:govet // Intentional shadow
err := testexch.Setup(b)
require.NoError(t, err, "Setup must not error")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Request method should be correct")
assert.Equal(t, "/api/v1/stats/history", r.URL.Path, "Request path should be correct")
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(`[
{
"currency": "XBt",
"date": "2023-10-26T00:00:00.000Z",
"rootSymbol": "XBT",
"turnover": 5000000000,
"volume": 100000
},
{
"currency": "XBt",
"date": "2023-10-25T10:35:42.123Z",
"rootSymbol": "XBT",
"turnover": 4500000000,
"volume": 90000
}
]`)) // Bitmex sends XBt as the currency for BTC, so we mimic it exactly
assert.NoError(t, err, "Writing response to handler should not error")
}))
defer server.Close()
err = b.API.Endpoints.SetRunningURL(exchange.RestSpot.String(), server.URL+"/api/v1")
require.NoError(t, err, "SetRunningURL must not error")
expectedStats := []StatsHistory{
{Currency: "XBt", Date: time.Date(2023, 10, 26, 0, 0, 0, 0, time.UTC), RootSymbol: "XBT", Turnover: 5000000000, Volume: 100000},
{Currency: "XBt", Date: time.Date(2023, 10, 25, 10, 35, 42, 123000000, time.UTC), RootSymbol: "XBT", Turnover: 4500000000, Volume: 90000},
}
result, err := b.GetStatsHistorical(t.Context())
require.NoError(t, err, "GetStatsHistorical must not error")
assert.Equal(t, expectedStats, result, "GetStatsHistorical result should be correct")
}
func TestGetStatSummary(t *testing.T) {

View File

@@ -460,11 +460,11 @@ type Stats struct {
// StatsHistory stats history
type StatsHistory struct {
Currency string `json:"currency"`
Date string `json:"date"`
RootSymbol string `json:"rootSymbol"`
Turnover int64 `json:"turnover"`
Volume int64 `json:"volume"`
Currency string `json:"currency"`
Date time.Time `json:"date"`
RootSymbol string `json:"rootSymbol"`
Turnover int64 `json:"turnover"`
Volume int64 `json:"volume"`
}
// StatsUSD contains summary of exchange stats