Files
gocryptotrader/exchanges/asset/asset_test.go
Samuael A aeb4a87913 exchanges: Add Deribit exchange support (#1082)
* deribit implementation

* add ws impll

* cleanup

* Update deribit_wrapper.go

* Add missing endpoints

* Fix config file

* asset type update

* Update code structure

* Update authenticated private endpoints unit tests

* Updating websocket

* Updating websocket connection and subscription handling

* Finishing up adding subscription push data

* Adding websocket public endpoint

* Adding WS endpoints

* Adding websocket unit tests

* Minor clean-up

* Integrating websocket endpoints into the wrapper funcs

* Updating exchange documentations

* Fixing test issues

* Code cleaning-up

* fix test issues

* Updating validations and logic errors

* Updating wrapper issues

* fix test issues

* Slight test update

* Unit test and code structure update

* Update websocket tempos

* Slight update on code structure

* Minor update on unit tests

* Update depending on review comments

* Minor code fix and doc re-generating

* Update on Candlestick wrapper functions

* Minor updates

* minor unit test updates

* Minor updates on weboscket and unit tests

* minor linter fix

* codespell and rate limiter issues

* single linter issue fix

* adding rate limiter

* Add ratelimiter to websocket conn and overall code update

* fix websocket push data issue

* Implementing missing wrapper function

* Websocket fix

* Minor update on missing endpoint and other

* fixing websocket issues and cleaningup

* Minor tempo fix

* Minor linter issues

* unit test update

* Indexing error fix

* Websocket connection fix

* string formatting fix

* Small fix on unit tests

* fix minor json conversion issue

* websocket and documentation update

* websocket, wrapper and unit test updates

* Documentation and unit tests update

* Fix unit tests

* wrapper fix for new change

* Unit test fix

* timestamp conversion and unit tests update

* Minor instrument ID conversion fix

* instrument formats and unit test update

* formatting and unit test fix

* config update

* Updating websocket and adding the Spot support

* Add small unit test fix

* unit test and websocket handlers update

* Linter issues fix

* minor documentation and code update

* Minor fix

* added a wrapper func GetLatestFundingRates

* Types, wrapper update, and unit tests

* Minor config update

* fix wrapper unit tests

* Resolve all panic and wrapper test issues

* minor unit test fix

* fix issues and adding newly added endpoints

* updates and added remaining endpoints with unit tests

* Update unit tests using assert

* Added missing endpoints and unit tests

* Minor updates and clean-ups

* Resolve tradable pair fetching  panic

* Mutex fix

* Added Options assets test and minor fixes

* subscription mothod updated

* Remove misadded code lines

* resolve websocket

* Updating tests, types, endpoint methods and others

* Added GetFuturesContractDetails and minor fix

* fix linter issue

* revert change on candlestic time

* Added filters to candles

* minor unit test and wrapper fix

* Minor unit tests update

* cahnge param key for GetOrderMarginByID

* updating unit tests and resolve issues

* Update websocket unit tests

* Minor fix based on review

* Revert unit test change

* fix pair config issue

* Added missing wrapper functions

* Fix missing review changes

* Fix options request pair formatting

* fix AllExchangeWrappers test issue

* Changes with unit test and wrapper based on the review

* Fix to options reg-exp

* wrapper functions fix

* Update MaximumFundingRateHistory filter and minor fixes

* Fix besed on review comment

* Fix issues on review comment

* linter fix

* fix minor unit test issue

* Fix unit test issues

* Update trade order cancellation responses

* fix config files issue

* lint update config files

* Update unit tests

* Update return values and response handling

* added missing endpoint and fixes based on review comment

* toggle useTestNet back

* Update cancel by label and other fix

* fix forgotten cancel all response type

* update CancelResp type

* Fix unmarshaling error

* updated websocket orderbook load issue

* fix websocket lock and groups

* Change Items to Tranche and fix linter issues

* Fix orderbook issue

* Update unit tests offline error handling, and endpoints argument and  error handling

* Contributors documentation update and change error return type

* Updated unit tests based on review comment

* Update unit tests and removed password change endpoint

* Fix race condition

* Update on tests, test pairs, and wrapper config

* Update test tradable pairs loading

* Update unit tests, fix linter issues, and update wrapper functions

* remove credentials

* Update test and fix authentication method and few authenticated endpoints

* fix codespell issue

* group the repeated currency code check to a func

* added unit test for repeated pair check func

* Added a base coin and related updates

---------

Co-authored-by: E Sequeira <earncef@earncef.com>
2024-05-29 10:28:03 +10:00

292 lines
6.2 KiB
Go

package asset
import (
"encoding/json"
"errors"
"testing"
"github.com/thrasher-corp/gocryptotrader/common"
)
func TestString(t *testing.T) {
t.Parallel()
a := Spot
if a.String() != "spot" {
t.Fatal("TestString returned an unexpected result")
}
a = 0
if a.String() != "" {
t.Fatal("TestString returned an unexpected result")
}
}
func TestToStringArray(t *testing.T) {
t.Parallel()
a := Items{Spot, Futures}
result := a.Strings()
for x := range a {
if !common.StringDataCompare(result, a[x].String()) {
t.Fatal("TestToStringArray returned an unexpected result")
}
}
}
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()
if Item(0).IsValid() {
t.Fatal("TestIsValid returned an unexpected result")
}
if !Spot.IsValid() {
t.Fatal("TestIsValid returned an unexpected result")
}
}
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: "future_combo", Expected: FutureCombo},
{Input: "option_combo", Expected: OptionCombo},
}
for x := range cases {
tt := cases[x]
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 := 0; i < len(supportedList); i++ {
if s[i] != supportedList[i] {
t.Fatal("TestSupported returned an unexpected result")
}
}
}
func TestIsFutures(t *testing.T) {
t.Parallel()
type scenario struct {
item Item
isFutures bool
}
scenarios := []scenario{
{
item: Spot,
isFutures: false,
},
{
item: Margin,
isFutures: false,
},
{
item: MarginFunding,
isFutures: false,
},
{
item: Index,
isFutures: false,
},
{
item: Binary,
isFutures: false,
},
{
item: PerpetualContract,
isFutures: true,
},
{
item: PerpetualSwap,
isFutures: true,
},
{
item: Futures,
isFutures: true,
},
{
item: UpsideProfitContract,
isFutures: true,
},
{
item: DownsideProfitContract,
isFutures: true,
},
{
item: CoinMarginedFutures,
isFutures: true,
},
{
item: USDTMarginedFutures,
isFutures: true,
},
{
item: USDCMarginedFutures,
isFutures: true,
}, {
item: FutureCombo,
isFutures: true,
},
}
for _, s := range scenarios {
testScenario := s
t.Run(testScenario.item.String(), func(t *testing.T) {
t.Parallel()
if testScenario.item.IsFutures() != testScenario.isFutures {
t.Errorf("expected %v isFutures to be %v", testScenario.item, testScenario.isFutures)
}
})
}
}
func TestIsOptions(t *testing.T) {
t.Parallel()
type scenario struct {
item Item
isOptions bool
}
scenarios := []scenario{
{
item: Options,
isOptions: true,
}, {
item: OptionCombo,
isOptions: true,
},
{
item: Futures,
isOptions: false,
},
{
item: Empty,
isOptions: false,
},
}
for _, s := range scenarios {
testScenario := s
t.Run(testScenario.item.String(), func(t *testing.T) {
t.Parallel()
if testScenario.item.IsOptions() != testScenario.isOptions {
t.Errorf("expected %v isOptions to be %v", testScenario.item, testScenario.isOptions)
}
})
}
}
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)
}
}