Files
gocryptotrader/backtester/report/chart_test.go
Adrian Gallagher a5b638bfb7 GHA: Add additional checks for common issues (#1922)
* GHA, tests: Add additional checks for common issues

These checks include:
- Ensuring that all testify funcs use their formatted variants (e.g., `assert.Equalf(t, expected, actual)` instead of `assert.Equal(t, expected, actual)`).
- Replacing `%s` with %q
- Enforcing consistent usage of should/must wording for testify assert/require messages

* Add support for checking backticked string format specifiers and fix issues

* tests: Fix error comparisons

* tests: Replace errors.Is(err, nil) usage with testify and automate check

* refactor: Rename ExtractPort to ExtractPortOrDefault

* tests: Replace assert with require for error handling in multiple test files

* tests: Replace assert with require for error handling and improve assertions in data tests

* tests: Fix typo in assertion message for StreamVol test

* OKX: Fix GetOpenInterestAndVolumeStrike test with instrument selection and improved assertions

* OKX: Revert intentional error check

* Improve error message for expiry time check in GetOpenInterestAndVolumeStrike test
2025-05-28 12:26:51 +10:00

232 lines
6.4 KiB
Go

package report
import (
"errors"
"testing"
"time"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio"
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics"
evkline "github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
"github.com/thrasher-corp/gocryptotrader/backtester/funding"
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/futures"
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
)
func TestCreateUSDTotalsChart(t *testing.T) {
t.Parallel()
_, err := createUSDTotalsChart(nil, nil)
if !errors.Is(err, gctcommon.ErrNilPointer) {
t.Errorf("received '%v' expected '%v'", err, gctcommon.ErrNilPointer)
}
tt := time.Now()
items := []statistics.ValueAtTime{
{
Time: tt,
Value: decimal.NewFromInt(1337),
Set: true,
},
}
_, err = createUSDTotalsChart(items, nil)
if !errors.Is(err, gctcommon.ErrNilPointer) {
t.Errorf("received '%v' expected '%v'", err, gctcommon.ErrNilPointer)
}
stats := []statistics.FundingItemStatistics{
{
ReportItem: &funding.ReportItem{
Snapshots: []funding.ItemSnapshot{
{
Time: tt,
USDValue: decimal.NewFromInt(1337),
},
},
},
},
}
resp, err := createUSDTotalsChart(items, stats)
require.NoError(t, err)
if len(resp.Data) == 0 {
t.Fatal("expected not nil")
}
if resp.Data[0].Name != "Total USD value" {
t.Error("expected not nil")
}
if resp.Data[0].LinePlots[0].Value != 1337 {
t.Error("expected not nil")
}
}
func TestCreateHoldingsOverTimeChart(t *testing.T) {
t.Parallel()
_, err := createHoldingsOverTimeChart(nil)
if !errors.Is(err, gctcommon.ErrNilPointer) {
t.Errorf("received '%v' expected '%v'", err, gctcommon.ErrNilPointer)
}
tt := time.Now()
items := []statistics.FundingItemStatistics{
{
ReportItem: &funding.ReportItem{
Exchange: "hello",
Asset: asset.Spot,
Currency: currency.BTC,
Snapshots: []funding.ItemSnapshot{
{
Time: tt,
Available: decimal.NewFromInt(1337),
},
{
Time: tt,
},
},
},
},
}
resp, err := createHoldingsOverTimeChart(items)
assert.NoError(t, err)
if !resp.ShowZeroDisclaimer {
t.Error("expected ShowZeroDisclaimer")
}
}
func TestCreatePNLCharts(t *testing.T) {
t.Parallel()
_, err := createPNLCharts(nil)
if !errors.Is(err, gctcommon.ErrNilPointer) {
t.Errorf("received '%v' expected '%v'", err, gctcommon.ErrNilPointer)
}
tt := time.Now()
var d Data
d.Statistics = &statistics.Statistic{}
d.Statistics.ExchangeAssetPairStatistics = make(map[key.ExchangePairAsset]*statistics.CurrencyPairStatistic)
d.Statistics.ExchangeAssetPairStatistics[key.ExchangePairAsset{
Exchange: testExchange,
Base: currency.BTC.Item,
Quote: currency.USDT.Item,
Asset: asset.Spot,
}] = &statistics.CurrencyPairStatistic{
Events: []statistics.DataAtOffset{
{
PNL: &portfolio.PNLSummary{
Result: futures.PNLResult{
Time: tt,
UnrealisedPNL: decimal.NewFromInt(1337),
RealisedPNLBeforeFees: decimal.NewFromInt(1337),
RealisedPNL: decimal.NewFromInt(1337),
Price: decimal.NewFromInt(1337),
Exposure: decimal.NewFromInt(1337),
Direction: gctorder.Short,
},
},
},
},
}
err = d.SetKlineData(&gctkline.Item{
Exchange: testExchange,
Pair: currency.NewBTCUSDT(),
Asset: asset.Spot,
Interval: gctkline.OneDay,
Candles: []gctkline.Candle{
{
Time: tt,
Open: 1336,
High: 1338,
Low: 1336,
Close: 1337,
Volume: 1337,
},
},
})
assert.NoError(t, err)
err = d.enhanceCandles()
assert.NoError(t, err)
_, err = createPNLCharts(d.Statistics.ExchangeAssetPairStatistics)
assert.NoError(t, err)
}
func TestCreateFuturesSpotDiffChart(t *testing.T) {
t.Parallel()
_, err := createFuturesSpotDiffChart(nil)
if !errors.Is(err, gctcommon.ErrNilPointer) {
t.Errorf("received '%v' expected '%v'", err, gctcommon.ErrNilPointer)
}
tt := time.Now()
cp := currency.NewBTCUSD()
cp2 := currency.NewPair(currency.BTC, currency.DOGE)
var d Data
d.Statistics = &statistics.Statistic{}
d.Statistics.ExchangeAssetPairStatistics = make(map[key.ExchangePairAsset]*statistics.CurrencyPairStatistic)
d.Statistics.ExchangeAssetPairStatistics[key.ExchangePairAsset{
Exchange: testExchange,
Base: currency.BTC.Item,
Quote: currency.USD.Item,
Asset: asset.Spot,
}] = &statistics.CurrencyPairStatistic{
Currency: cp,
Events: []statistics.DataAtOffset{
{
Time: tt,
DataEvent: &evkline.Kline{Close: decimal.NewFromInt(1337)},
PNL: &portfolio.PNLSummary{
Result: futures.PNLResult{
Time: tt,
UnrealisedPNL: decimal.NewFromInt(1337),
RealisedPNLBeforeFees: decimal.NewFromInt(1337),
RealisedPNL: decimal.NewFromInt(1337),
Price: decimal.NewFromInt(1337),
Exposure: decimal.NewFromInt(1337),
Direction: gctorder.Buy,
},
},
},
},
}
d.Statistics.ExchangeAssetPairStatistics[key.ExchangePairAsset{
Exchange: testExchange,
Base: currency.BTC.Item,
Quote: currency.DOGE.Item,
Asset: asset.Futures,
}] = &statistics.CurrencyPairStatistic{
UnderlyingPair: cp,
Currency: cp2,
Events: []statistics.DataAtOffset{
{
Time: tt,
DataEvent: &evkline.Kline{Close: decimal.NewFromInt(1337)},
PNL: &portfolio.PNLSummary{
Result: futures.PNLResult{
Time: tt,
UnrealisedPNL: decimal.NewFromInt(1337),
RealisedPNLBeforeFees: decimal.NewFromInt(1337),
RealisedPNL: decimal.NewFromInt(1337),
Price: decimal.NewFromInt(1337),
Exposure: decimal.NewFromInt(1337),
Direction: gctorder.Short,
},
},
},
},
}
charty, err := createFuturesSpotDiffChart(d.Statistics.ExchangeAssetPairStatistics)
assert.NoError(t, err)
if len(charty.Data) == 0 {
t.Error("expected data")
}
}