mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-23 23:16:49 +00:00
* drop common uuid v4 func and imported package as needed * removed common functions regarding json marshal and unmarshal and used the json package directly. WRT unmarshal it was calling reflect and converted to string which is also checked in the JSON package so it was doing a double up, this will be a tiny gain as it was directly used in the requester package for all our outbound requests. * add in string * explicitly throw away return error value * atleast return the error that websocket initialise returns * return error when not connected * fix comment * Adds comments * move package declarations * drop append whenever we call supported * remove unused import * Change incorrect spelling * fix tests * fix go import issue
189 lines
4.5 KiB
Go
189 lines
4.5 KiB
Go
package currency
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestPairsUpper(t *testing.T) {
|
|
pairs := NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
|
|
expected := "BTC_USD,BTC_AUD,BTC_LTC"
|
|
|
|
if pairs.Upper().Join() != expected {
|
|
t.Errorf("Pairs Join() error expected %s but received %s",
|
|
expected, pairs.Join())
|
|
}
|
|
}
|
|
|
|
func TestPairsString(t *testing.T) {
|
|
pairs := NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
|
|
expected := []string{"btc_usd", "btc_aud", "btc_ltc"}
|
|
|
|
for i, p := range pairs {
|
|
if p.String() != expected[i] {
|
|
t.Errorf("Pairs String() error expected %s but received %s",
|
|
expected, p.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPairsJoin(t *testing.T) {
|
|
pairs := NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
|
|
expected := "btc_usd,btc_aud,btc_ltc"
|
|
|
|
if pairs.Join() != expected {
|
|
t.Errorf("Pairs Join() error expected %s but received %s",
|
|
expected, pairs.Join())
|
|
}
|
|
}
|
|
|
|
func TestPairsFormat(t *testing.T) {
|
|
pairs := NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"})
|
|
|
|
expected := "BTC-USD,BTC-AUD,BTC-LTC"
|
|
if pairs.Format("-", "", true).Join() != expected {
|
|
t.Errorf("Pairs Join() error expected %s but received %s",
|
|
expected, pairs.Format("-", "", true).Join())
|
|
}
|
|
|
|
expected = "btc:usd,btc:aud,btc:ltc"
|
|
if pairs.Format(":", "", false).Join() != expected {
|
|
t.Errorf("Pairs Join() error expected %s but received %s",
|
|
expected, pairs.Format(":", "", false).Join())
|
|
}
|
|
|
|
if pairs.Format(":", "KRW", false).Join() != "" {
|
|
t.Errorf("Pairs Join() error expected %s but received %s",
|
|
expected, pairs.Format(":", "KRW", true).Join())
|
|
}
|
|
|
|
pairs = NewPairsFromStrings([]string{"DASHKRW", "BTCKRW"})
|
|
expected = "dash-krw,btc-krw"
|
|
if pairs.Format("-", "KRW", false).Join() != expected {
|
|
t.Errorf("Pairs Join() error expected %s but received %s",
|
|
expected, pairs.Format("-", "KRW", false).Join())
|
|
}
|
|
}
|
|
|
|
func TestPairsUnmarshalJSON(t *testing.T) {
|
|
var unmarshalHere Pairs
|
|
configPairs := ""
|
|
encoded, err := json.Marshal(configPairs)
|
|
if err != nil {
|
|
t.Fatal("Pairs UnmarshalJSON() error", err)
|
|
}
|
|
|
|
err = json.Unmarshal([]byte{1, 3, 3, 7}, &unmarshalHere)
|
|
if err == nil {
|
|
t.Fatal("error cannot be nil")
|
|
}
|
|
|
|
err = json.Unmarshal(encoded, &unmarshalHere)
|
|
if err != nil {
|
|
t.Fatal("Pairs UnmarshalJSON() error", err)
|
|
}
|
|
|
|
configPairs = "btc_usd,btc_aud,btc_ltc"
|
|
encoded, err = json.Marshal(configPairs)
|
|
if err != nil {
|
|
t.Fatal("Pairs UnmarshalJSON() error", err)
|
|
}
|
|
|
|
err = json.Unmarshal(encoded, &unmarshalHere)
|
|
if err != nil {
|
|
t.Fatal("Pairs UnmarshalJSON() error", err)
|
|
}
|
|
|
|
err = json.Unmarshal(encoded, &unmarshalHere)
|
|
if err != nil {
|
|
t.Fatal("Pairs UnmarshalJSON() error", err)
|
|
}
|
|
|
|
if unmarshalHere.Join() != configPairs {
|
|
t.Errorf("Pairs UnmarshalJSON() error expected %s but received %s",
|
|
configPairs, unmarshalHere.Join())
|
|
}
|
|
}
|
|
|
|
func TestPairsMarshalJSON(t *testing.T) {
|
|
quickstruct := struct {
|
|
Pairs Pairs `json:"soManyPairs"`
|
|
}{
|
|
Pairs: NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"}),
|
|
}
|
|
|
|
encoded, err := json.Marshal(quickstruct)
|
|
if err != nil {
|
|
t.Fatal("Pairs MarshalJSON() error", err)
|
|
}
|
|
|
|
expected := `{"soManyPairs":"btc_usd,btc_aud,btc_ltc"}`
|
|
if string(encoded) != expected {
|
|
t.Errorf("Pairs MarshalJSON() error expected %s but received %s",
|
|
expected, string(encoded))
|
|
}
|
|
}
|
|
|
|
func TestRemovePairsByFilter(t *testing.T) {
|
|
var pairs = Pairs{
|
|
NewPair(BTC, USD),
|
|
NewPair(LTC, USD),
|
|
NewPair(LTC, USDT),
|
|
}
|
|
|
|
pairs = pairs.RemovePairsByFilter(USDT)
|
|
if pairs.Contains(NewPair(LTC, USDT), true) {
|
|
t.Error("TestRemovePairsByFilter unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestRemove(t *testing.T) {
|
|
var pairs = Pairs{
|
|
NewPair(BTC, USD),
|
|
NewPair(LTC, USD),
|
|
NewPair(LTC, USDT),
|
|
}
|
|
|
|
p := NewPair(BTC, USD)
|
|
pairs = pairs.Remove(p)
|
|
if pairs.Contains(p, true) || len(pairs) != 2 {
|
|
t.Error("TestRemove unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestAdd(t *testing.T) {
|
|
var pairs = Pairs{
|
|
NewPair(BTC, USD),
|
|
NewPair(LTC, USD),
|
|
NewPair(LTC, USDT),
|
|
}
|
|
|
|
// Test adding a new pair to the list of pairs
|
|
p := NewPair(BTC, USDT)
|
|
pairs = pairs.Add(p)
|
|
if !pairs.Contains(p, true) || len(pairs) != 4 {
|
|
t.Error("TestAdd unexpected result")
|
|
}
|
|
|
|
// Now test adding a pair which already exists
|
|
pairs = pairs.Add(p)
|
|
if len(pairs) != 4 {
|
|
t.Error("TestAdd unexpected result")
|
|
}
|
|
}
|
|
|
|
func TestContains(t *testing.T) {
|
|
var pairs = Pairs{
|
|
NewPair(BTC, USD),
|
|
NewPair(LTC, USD),
|
|
}
|
|
|
|
if !pairs.Contains(NewPair(BTC, USD), true) {
|
|
t.Errorf("TestContains: Expected pair was not found")
|
|
}
|
|
|
|
if pairs.Contains(NewPair(ETH, USD), false) {
|
|
t.Errorf("TestContains: Non-existent pair was found")
|
|
}
|
|
}
|