Files
gocryptotrader/exchanges/asset/asset_test.go
cranktakular fd9aaf00a2 Coinbase: Update exchange implementation (#1480)
* Slight enhance of Coinbase tests

Continual enhance of Coinbase tests

The revamp continues

Oh jeez the Orderbook part's unfinished don't look

Coinbase revamp, Orderbook still unfinished

* Coinbase revamp; CreateReport is still WIP

* More coinbase improvements; onto sandbox testing

* Coinbase revamp continues

* Coinbase revamp continues

* Coinbasepro revamp is ceaseless

* Coinbase revamp, starting on advanced trade API

* Coinbase Advanced Trade Starts in Ernest

V3 done, onto V2

Coinbase revamp nears completion

Coinbase revamp nears completion

Test commit should fail

Coinbase revamp nears completion

* Coinbase revamp stage wrapper

* Coinbase wrapper coherence continues

* Coinbase wrapper continues writhing

* Coinbase wrapper & codebase cleanup

* Coinbase updates & wrap progress

* More Coinbase wrapper progress

* Wrapper is wrapped, kinda

* Test & type checking

* Coinbase REST revamp finished

* Post-merge fix

* WS revamp begins

* WS Main Revamp Done?

* CB websocket tidying up

* Coinbase WS wrapperupperer

* Coinbase revamp done??

* Linter progress

* Continued lint cleanup

* Further lint cleanup

* Increased lint coverage

* Does this fix all sloppy reassigns & shadowing?

* Undoing retry policy change

* Documentation regeneration

* Coinbase code improvements

* Providing warning about known issue

* Updating an error to new format

* Making gocritic happy

* Review adherence

* Endpoints moved to V3 & nil pointer fixes

* Removing seemingly superfluous constant

* Glorious improvements

* Removing unused error

* Partial public endpoint addition

* Slight improvements

* Wrapper improvements; still a few errors left in other packages

* A lil Coinbase progress

* Json cleaning

* Lint appeasement

* Config repair

* Config fix (real)

* Little fix

* New public endpoint incorporation

* Additional fixes

* Improvements & Appeasements

* LineSaver

* Additional fixes

* Another fix

* Fixing picked nits

* Quick fixies

* Lil fixes

* Subscriptions: Add List.Enabled

* CoinbasePro: Add subscription templating

* fixup! CoinbasePro: Add subscription templating

* fixup! CoinbasePro: Add subscription templating

* Comment fix

* Subsequent fixes

* Issues hopefully fixed

* Lint fix

* Glorious fixes

* Json formatting

* ShazNits

* (L/N)i(n/)t

* Adding a test

* Tiny test improvement

* Template patch testing

* Fixes

* Further shaznits

* Lint nit

* JWT move and other fixes

* Small nits

* Shaznit, singular

* Post-merge fix

* Post-merge fixes

* Typo fix

* Some glorious nits

* Required changes

* Stop going

* Alias attempt

* Alias fix & test cleanup

* Test fix

* GetDepositAddress logic improvement

* Status update: Fixed

* Lint fix

* Happy birthday to PR 1480

* Cleanups

* Necessary nit corrections

* Fixing sillybug

* As per request

* Programming progress

* Order fixes

* Further fixies

* Test fix

* Pre-merge fixes

* More shaznits

* Context

* Sonic error handling

* Import fix

* Better Sonic error handling

* Perfect Sonic error handling?

* F purge

* Coinbase improvements

* API Update Conformity

* Coinbase continuation

* Coinbase order improvements

* Coinbase order improvements

* CreateOrderConfig improvements

* Managing API updates

* Coinbase API update progression

* jwt rename

* Comment link fix

* Coinbase v2 cleanup

* Post-merge fixes

* Review fixes

* GK's suggestions

* Linter fix

* Minor gbjk fixes

* Nit fixes

* Merge fix

* Lint fixes

* Coinbase rename stage 1

* Coinbase rename stage 2

* Coinbase rename stage 3

* Coinbase rename stage 4

* Coinbase rename final fix

* Coinbase: PoC on converting to request structs

* Applying requested changes

* Many review fixes, handled

* Thrashed by nits

* More minor modifications

* The last nit!?

---------

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
2025-09-16 13:37:00 +10:00

198 lines
5.0 KiB
Go

package asset
import (
"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 TestUpper(t *testing.T) {
t.Parallel()
a := Spot
require.Equal(t, "SPOT", a.Upper())
a = 0
require.Empty(t, a.Upper())
}
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)
require.ErrorIs(t, 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))
require.NoError(t, err)
if string(data) != `""` {
t.Fatal("unexpected value")
}
data, err = json.Marshal(Spot)
require.NoError(t, err)
if string(data) != `"spot"` {
t.Fatal("unexpected value")
}
var spot Item
err = json.Unmarshal(data, &spot)
require.NoError(t, err)
if spot != Spot {
t.Fatal("unexpected value")
}
err = json.Unmarshal([]byte(`"confused"`), &spot)
require.ErrorIs(t, err, ErrNotSupported)
err = json.Unmarshal([]byte(`""`), &spot)
require.NoError(t, err)
err = json.Unmarshal([]byte(`123`), &spot)
assert.Error(t, err, "Unmarshal should error correctly")
}
func TestUseDefault(t *testing.T) {
t.Parallel()
if UseDefault() != Spot {
t.Fatalf("received: '%v' but expected: '%v'", UseDefault(), Spot)
}
}