Files
gocryptotrader/exchanges/asset/asset_test.go
Ryan O'Hara-Reid e99adca86f encoding/json: Add custom JSON package with build tag support for Sonic (#1623)
* tag optional sonic and allow full library conversion

* Add workflow and disallow arm and darwin usage

* Add basic hotswap benchmark

* linter: fix

* use bash

* linter: fix?

* Fix whoopsie, add to make file, also add mention in features list.

* test enforcement

* actually read documentation see if this works

* linter: fix

* linter: fix

* sonic: bump tagged version

* encoding/json: drop build tag arch and os filters

* encoding/json: consolidate tests

* encoding/json: log build tag usage

* rm superfluous builds

* glorious/nits: add template change and regen docs

* glorious/nits: update commentary on nolint directive

* glorious/nits: rm init func and log results in main.go

* Test to actually pull flag in

* linter: fix

* thrasher: nits

* gk: nits 4 goflags goooooooooo!

* gk: nits rn

* make sonic default json implementation

* screen 386

* linter: fix

* Add commentary

* glorious: nits Makefile not working

* gk: nits

* gk: nits whoops

* whoopsirino

* mention 32bit systems won't be sonic

* gk: super-duper nit of extremes

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
2025-02-20 16:05:55 +11:00

204 lines
5.3 KiB
Go

package asset
import (
"errors"
"slices"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
func TestString(t *testing.T) {
t.Parallel()
for a := range All {
if a == 0 {
assert.Empty(t, a.String(), "Empty.String should return empty")
} else {
assert.NotEmptyf(t, a.String(), "%s.String should return empty", a)
}
}
}
func TestStrings(t *testing.T) {
t.Parallel()
assert.ElementsMatch(t, Items{Spot, Futures}.Strings(), []string{"spot", "futures"})
}
func TestContains(t *testing.T) {
t.Parallel()
a := Items{Spot, Futures}
if a.Contains(666) {
t.Fatal("TestContains returned an unexpected result")
}
if !a.Contains(Spot) {
t.Fatal("TestContains returned an unexpected result")
}
if a.Contains(Binary) {
t.Fatal("TestContains returned an unexpected result")
}
// Every asset should be created and matched with func New so this should
// not be matched against list
if a.Contains(0) {
t.Error("TestContains returned an unexpected result")
}
}
func TestJoinToString(t *testing.T) {
t.Parallel()
a := Items{Spot, Futures}
if a.JoinToString(",") != "spot,futures" {
t.Fatal("TestJoinToString returned an unexpected result")
}
}
func TestIsValid(t *testing.T) {
t.Parallel()
for a := range All {
if a.String() == "" {
require.Falsef(t, a.IsValid(), "IsValid must return false with non-asset value %d", a)
} else {
require.Truef(t, a.IsValid(), "IsValid must return true for %s", a)
}
}
require.False(t, All.IsValid(), "IsValid must return false for All")
}
func TestIsFutures(t *testing.T) {
t.Parallel()
valid := []Item{PerpetualContract, PerpetualSwap, Futures, DeliveryFutures, UpsideProfitContract, DownsideProfitContract, CoinMarginedFutures, USDTMarginedFutures, USDCMarginedFutures, FutureCombo, LinearContract, Spread}
for a := range All {
if slices.Contains(valid, a) {
require.Truef(t, a.IsFutures(), "IsFutures must return true for %s", a)
} else {
require.Falsef(t, a.IsFutures(), "IsFutures must return false for non-asset value %d (%s)", a, a)
}
}
}
func TestIsOptions(t *testing.T) {
t.Parallel()
valid := []Item{Options, OptionCombo}
for a := range All {
if slices.Contains(valid, a) {
require.Truef(t, a.IsOptions(), "IsOptions must return true for %s", a)
} else {
require.Falsef(t, a.IsOptions(), "IsOptions must return false for non-asset value %d (%s)", a, a)
}
}
}
func TestNew(t *testing.T) {
t.Parallel()
cases := []struct {
Input string
Expected Item
Error error
}{
{Input: "Spota", Error: ErrNotSupported},
{Input: "MARGIN", Expected: Margin},
{Input: "MARGINFUNDING", Expected: MarginFunding},
{Input: "INDEX", Expected: Index},
{Input: "BINARY", Expected: Binary},
{Input: "PERPETUALCONTRACT", Expected: PerpetualContract},
{Input: "PERPETUALSWAP", Expected: PerpetualSwap},
{Input: "FUTURES", Expected: Futures},
{Input: "UpsideProfitContract", Expected: UpsideProfitContract},
{Input: "DownsideProfitContract", Expected: DownsideProfitContract},
{Input: "CoinMarginedFutures", Expected: CoinMarginedFutures},
{Input: "USDTMarginedFutures", Expected: USDTMarginedFutures},
{Input: "USDCMarginedFutures", Expected: USDCMarginedFutures},
{Input: "Options", Expected: Options},
{Input: "Option", Expected: Options},
{Input: "Future", Error: ErrNotSupported},
{Input: "option_combo", Expected: OptionCombo},
{Input: "future_combo", Expected: FutureCombo},
{Input: "spread", Expected: Spread},
{Input: "linearContract", Expected: LinearContract},
}
for _, tt := range cases {
t.Run("", func(t *testing.T) {
t.Parallel()
returned, err := New(tt.Input)
if !errors.Is(err, tt.Error) {
t.Fatalf("received: '%v' but expected: '%v'", err, tt.Error)
}
if returned != tt.Expected {
t.Fatalf("received: '%v' but expected: '%v'", returned, tt.Expected)
}
})
}
}
func TestSupported(t *testing.T) {
t.Parallel()
s := Supported()
if len(supportedList) != len(s) {
t.Fatal("TestSupported mismatched lengths")
}
for i := range supportedList {
if s[i] != supportedList[i] {
t.Fatal("TestSupported returned an unexpected result")
}
}
}
func TestUnmarshalMarshal(t *testing.T) {
t.Parallel()
data, err := json.Marshal(Item(0))
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
if string(data) != `""` {
t.Fatal("unexpected value")
}
data, err = json.Marshal(Spot)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
if string(data) != `"spot"` {
t.Fatal("unexpected value")
}
var spot Item
err = json.Unmarshal(data, &spot)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
if spot != Spot {
t.Fatal("unexpected value")
}
err = json.Unmarshal([]byte(`"confused"`), &spot)
if !errors.Is(err, ErrNotSupported) {
t.Fatalf("received: '%v' but expected: '%v'", err, ErrNotSupported)
}
err = json.Unmarshal([]byte(`""`), &spot)
if !errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", err, nil)
}
err = json.Unmarshal([]byte(`123`), &spot)
if errors.Is(err, nil) {
t.Fatalf("received: '%v' but expected: '%v'", nil, "an error")
}
}
func TestUseDefault(t *testing.T) {
t.Parallel()
if UseDefault() != Spot {
t.Fatalf("received: '%v' but expected: '%v'", UseDefault(), Spot)
}
}