Files
gocryptotrader/exchanges/collateral/collateral_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

185 lines
4.9 KiB
Go

package collateral
import (
"errors"
"strings"
"testing"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
)
func TestValidCollateralType(t *testing.T) {
t.Parallel()
if !SingleMode.Valid() {
t.Fatal("expected 'true', received 'false'")
}
if !MultiMode.Valid() {
t.Fatal("expected 'true', received 'false'")
}
if !PortfolioMode.Valid() {
t.Fatal("expected 'true', received 'false'")
}
if !SpotFuturesMode.Valid() {
t.Fatal("expected 'true', received 'false'")
}
if UnsetMode.Valid() {
t.Fatal("expected 'false', received 'true'")
}
if UnknownMode.Valid() {
t.Fatal("expected 'false', received 'true'")
}
if Mode(137).Valid() {
t.Fatal("expected 'false', received 'true'")
}
}
func TestUnmarshalJSONCollateralType(t *testing.T) {
t.Parallel()
type martian struct {
M Mode `json:"collateral"`
}
var alien martian
jason := []byte(`{"collateral":"single"}`)
err := json.Unmarshal(jason, &alien)
if err != nil {
t.Error(err)
}
if alien.M != SingleMode {
t.Errorf("received '%v' expected 'single'", alien.M)
}
jason = []byte(`{"collateral":"multi"}`)
err = json.Unmarshal(jason, &alien)
if err != nil {
t.Error(err)
}
if alien.M != MultiMode {
t.Errorf("received '%v' expected 'Multi'", alien.M)
}
jason = []byte(`{"collateral":"portfolio"}`)
err = json.Unmarshal(jason, &alien)
if err != nil {
t.Error(err)
}
if alien.M != PortfolioMode {
t.Errorf("received '%v' expected 'Portfolio'", alien.M)
}
jason = []byte(`{"collateral":"hello moto"}`)
err = json.Unmarshal(jason, &alien)
if !errors.Is(err, ErrInvalidCollateralMode) {
t.Error(err)
}
if alien.M != UnknownMode {
t.Errorf("received '%v' expected 'UnknownMode'", alien.M)
}
}
func TestStringCollateralType(t *testing.T) {
t.Parallel()
if UnknownMode.String() != unknownCollateralStr {
t.Errorf("received '%v' expected '%v'", UnknownMode.String(), unknownCollateralStr)
}
if SingleMode.String() != singleCollateralStr {
t.Errorf("received '%v' expected '%v'", SingleMode.String(), singleCollateralStr)
}
if MultiMode.String() != multiCollateralStr {
t.Errorf("received '%v' expected '%v'", MultiMode.String(), multiCollateralStr)
}
if PortfolioMode.String() != portfolioCollateralStr {
t.Errorf("received '%v' expected '%v'", PortfolioMode.String(), portfolioCollateralStr)
}
if UnsetMode.String() != unsetCollateralStr {
t.Errorf("received '%v' expected '%v'", UnsetMode.String(), unsetCollateralStr)
}
}
func TestUpperCollateralType(t *testing.T) {
t.Parallel()
if UnknownMode.Upper() != strings.ToUpper(unknownCollateralStr) {
t.Errorf("received '%v' expected '%v'", UnknownMode.Upper(), strings.ToUpper(unknownCollateralStr))
}
if SingleMode.Upper() != strings.ToUpper(singleCollateralStr) {
t.Errorf("received '%v' expected '%v'", SingleMode.Upper(), strings.ToUpper(singleCollateralStr))
}
if MultiMode.Upper() != strings.ToUpper(multiCollateralStr) {
t.Errorf("received '%v' expected '%v'", MultiMode.Upper(), strings.ToUpper(multiCollateralStr))
}
if PortfolioMode.Upper() != strings.ToUpper(portfolioCollateralStr) {
t.Errorf("received '%v' expected '%v'", PortfolioMode.Upper(), strings.ToUpper(portfolioCollateralStr))
}
if UnsetMode.Upper() != strings.ToUpper(unsetCollateralStr) {
t.Errorf("received '%v' expected '%v'", UnsetMode.Upper(), strings.ToUpper(unsetCollateralStr))
}
}
func TestIsValidCollateralTypeString(t *testing.T) {
t.Parallel()
if IsValidCollateralModeString("lol") {
t.Fatal("expected 'false', received 'true'")
}
if !IsValidCollateralModeString("single") {
t.Fatal("expected 'true', received 'false'")
}
if !IsValidCollateralModeString("multi") {
t.Fatal("expected 'true', received 'false'")
}
if !IsValidCollateralModeString("portfolio") {
t.Fatal("expected 'true', received 'false'")
}
if !IsValidCollateralModeString("unset") {
t.Fatal("expected 'true', received 'false'")
}
if IsValidCollateralModeString("") {
t.Fatal("expected 'false', received 'true'")
}
if IsValidCollateralModeString("unknown") {
t.Fatal("expected 'false', received 'true'")
}
}
func TestStringToCollateralType(t *testing.T) {
t.Parallel()
resp, err := StringToMode("lol")
if !errors.Is(err, ErrInvalidCollateralMode) {
t.Error(err)
}
if resp != UnknownMode {
t.Errorf("received '%v' expected '%v'", resp, UnknownMode)
}
resp, err = StringToMode("")
if err != nil {
t.Error(err)
}
if resp != UnsetMode {
t.Errorf("received '%v' expected '%v'", resp, UnsetMode)
}
resp, err = StringToMode("single")
if err != nil {
t.Error(err)
}
if resp != SingleMode {
t.Errorf("received '%v' expected '%v'", resp, SingleMode)
}
resp, err = StringToMode("multi")
if err != nil {
t.Error(err)
}
if resp != MultiMode {
t.Errorf("received '%v' expected '%v'", resp, MultiMode)
}
resp, err = StringToMode("portfolio")
if err != nil {
t.Error(err)
}
if resp != PortfolioMode {
t.Errorf("received '%v' expected '%v'", resp, PortfolioMode)
}
}