Files
gocryptotrader/common/convert/convert_test.go
Gareth Kirwan 33e82c170f Kraken: Subscription improvements (#1587)
* Convert: Fix TimeFromUnixTimestampDecimal using local

All parsed times should be in UTC

* Subscriptions: Add IgnoringAssetsKey

* Tests: Pass tb to curried WS handlers

* Websocket: Make ErrNoMessageListener a public error

* Kraken: Fix URLMap ignored for websocket URLs

* Kraken: Move SeedAssets from Setup to Bootstrap

Having SeedAssets in Setup is cruel and unusual because it calls the
API. Most other interactive data seeding happens in Bootstrap.

This made it so that fixing and creating unit tests for Kraken was
painfully slow, particularly on flaky internet.

* Kraken: Remove convert test

Duplicate of convert_test.go TestTimeFromUnixTimestampDecimal

* Kraken: Test config upgrades

* Kraken: Sub Channel improvements

* Use Websocket subscriptionChannels instead of local slice
* Remove ChannelID - Deprecated in docs
* Simplify ping handlers and hardcodes message
* Add Depth as configurable orderbook channel param
* Simplify auth/non-auth channel updates
* Add configurable Book depth
* Add configurable Candle timeframes

Kraken: Simplify all WS handlers with reqId

* Kraken: Subscription templating

* Generate N+ subs for pairs
If we generate one sub for all pairs, but then fan it out in the
responses, we end up with a mis-match between the sub store and
GenerateSubs, and when we do FlushChannels it will try to resub
everything again.

* Kraken: Rename channelName var throughout

Avoid shadowing func of same name

* Kraken: Add TestEnforceStandardChannelNames

* Websocket: Fix Resubscribe erroring Duplicate
2024-10-08 10:34:10 +11:00

318 lines
8.3 KiB
Go

package convert
import (
"strings"
"testing"
"time"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
)
func TestFloatFromString(t *testing.T) {
t.Parallel()
testString := "1.41421356237"
expectedOutput := float64(1.41421356237)
actualOutput, err := FloatFromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Common FloatFromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = FloatFromString(testByte)
if err == nil {
t.Error("Common FloatFromString. Converted non-string.")
}
testString = " something unconvertible "
_, err = FloatFromString(testString)
if err == nil {
t.Error("Common FloatFromString. Converted invalid syntax.")
}
}
func TestIntFromString(t *testing.T) {
t.Parallel()
testString := "1337"
actualOutput, err := IntFromString(testString)
if expectedOutput := 1337; actualOutput != expectedOutput || err != nil {
t.Errorf("Common IntFromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = IntFromString(testByte)
if err == nil {
t.Error("Common IntFromString. Converted non-string.")
}
testString = "1.41421356237"
_, err = IntFromString(testString)
if err == nil {
t.Error("Common IntFromString. Converted invalid syntax.")
}
}
func TestInt64FromString(t *testing.T) {
t.Parallel()
testString := "4398046511104"
expectedOutput := int64(1 << 42)
actualOutput, err := Int64FromString(testString)
if actualOutput != expectedOutput || err != nil {
t.Errorf("Common Int64FromString. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
var testByte []byte
_, err = Int64FromString(testByte)
if err == nil {
t.Error("Common Int64FromString. Converted non-string.")
}
testString = "1.41421356237"
_, err = Int64FromString(testString)
if err == nil {
t.Error("Common Int64FromString. Converted invalid syntax.")
}
}
func TestTimeFromUnixTimestampFloat(t *testing.T) {
t.Parallel()
testTimestamp := float64(1414456320000)
expectedOutput := time.Date(2014, time.October, 28, 0, 32, 0, 0, time.UTC)
actualOutput, err := TimeFromUnixTimestampFloat(testTimestamp)
if actualOutput.UTC().String() != expectedOutput.UTC().String() || err != nil {
t.Errorf("Common TimeFromUnixTimestampFloat. Expected '%v'. Actual '%v'. Error: %s",
expectedOutput, actualOutput, err)
}
testString := "Time"
_, err = TimeFromUnixTimestampFloat(testString)
if err == nil {
t.Error("Common TimeFromUnixTimestampFloat. Converted invalid syntax.")
}
}
func TestTimeFromUnixTimestampDecimal(t *testing.T) {
for in, exp := range map[float64]time.Time{
1590633982.5714: time.Date(2020, 5, 28, 2, 46, 22, 571400000, time.UTC),
1560516023.070651: time.Date(2019, 6, 14, 12, 40, 23, 70651000, time.UTC),
// Examples from Kraken
1373750306.9819: time.Date(2013, 7, 13, 21, 18, 26, 981900000, time.UTC),
1534614098.345543: time.Date(2018, 8, 18, 17, 41, 38, 345543000, time.UTC),
} {
got := TimeFromUnixTimestampDecimal(in)
z, _ := got.Zone()
assert.Equal(t, "UTC", z, "TimeFromUnixTimestampDecimal should return a UTC time")
assert.WithinRangef(t, got, exp.Add(-time.Microsecond), exp.Add(time.Microsecond), "TimeFromUnixTimestampDecimal(%f) should parse a unix timestamp correctly", in)
}
}
func TestUnixTimestampToTime(t *testing.T) {
t.Parallel()
testTime := int64(1489439831)
tm := time.Unix(testTime, 0)
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
actualResult := UnixTimestampToTime(testTime)
if tm.String() != actualResult.String() {
t.Errorf(
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
}
}
func TestUnixTimestampStrToTime(t *testing.T) {
t.Parallel()
testTime := "1489439831"
incorrectTime := "DINGDONG"
expectedOutput := "2017-03-13 21:17:11 +0000 UTC"
actualResult, err := UnixTimestampStrToTime(testTime)
if err != nil {
t.Error(err)
}
if actualResult.UTC().String() != expectedOutput {
t.Errorf(
"Expected '%s'. Actual '%s'.", expectedOutput, actualResult)
}
_, err = UnixTimestampStrToTime(incorrectTime)
if err == nil {
t.Error("should throw an error")
}
}
func TestBoolPtr(t *testing.T) {
y := BoolPtr(true)
if !*y {
t.Fatal("true expected received false")
}
z := BoolPtr(false)
if *z {
t.Fatal("false expected received true")
}
}
func TestFloatToHumanFriendlyString(t *testing.T) {
t.Parallel()
test := FloatToHumanFriendlyString(0, 3, ".", ",")
if strings.Contains(test, ",") {
t.Error("unexpected ','")
}
test = FloatToHumanFriendlyString(100, 3, ".", ",")
if strings.Contains(test, ",") {
t.Error("unexpected ','")
}
test = FloatToHumanFriendlyString(1000, 3, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = FloatToHumanFriendlyString(-1000, 3, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = FloatToHumanFriendlyString(-1000, 10, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = FloatToHumanFriendlyString(1000.1337, 1, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec := strings.Split(test, ".")
if len(dec) == 1 {
t.Error("expected decimal place")
}
if dec[1] != "1" {
t.Error("expected decimal place")
}
}
func TestDecimalToHumanFriendlyString(t *testing.T) {
t.Parallel()
test := DecimalToHumanFriendlyString(decimal.Zero, 0, ".", ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromInt(100), 0, ".", ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromInt(1000), 0, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(1000.1337), 1, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec := strings.Split(test, ".")
if len(dec) == 1 {
t.Error("expected decimal place")
}
if dec[1] != "1" {
t.Error("expected decimal place")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(-1000.1337), 1, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(-1000.1337), 100000, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(1000.1), 10, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec = strings.Split(test, ".")
if len(dec) == 1 {
t.Error("expected decimal place")
}
if dec[1] != "1" {
t.Error("expected decimal place")
}
}
func TestIntToHumanFriendlyString(t *testing.T) {
t.Parallel()
test := IntToHumanFriendlyString(0, ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = IntToHumanFriendlyString(100, ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = IntToHumanFriendlyString(1000, ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = IntToHumanFriendlyString(-1000, ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = IntToHumanFriendlyString(1000000, ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec := strings.Split(test, ",")
if len(dec) <= 2 {
t.Error("expected two commas place")
}
}
func TestNumberToHumanFriendlyString(t *testing.T) {
resp := numberToHumanFriendlyString("1", 1337, ".", ",", false)
if strings.Contains(resp, ".") {
t.Error("expected no comma")
}
}
func TestInterfaceToFloat64OrZeroValue(t *testing.T) {
var x interface{}
if r := InterfaceToFloat64OrZeroValue(x); r != 0 {
t.Errorf("expected 0, got: %v", r)
}
x = float64(420)
if r := InterfaceToFloat64OrZeroValue(x); r != 420 {
t.Errorf("expected 420, got: %v", x)
}
}
func TestInterfaceToIntOrZeroValue(t *testing.T) {
var x interface{}
if r := InterfaceToIntOrZeroValue(x); r != 0 {
t.Errorf("expected 0, got: %v", r)
}
x = int(420)
if r := InterfaceToIntOrZeroValue(x); r != 420 {
t.Errorf("expected 420, got: %v", x)
}
}
func TestInterfaceToStringOrZeroValue(t *testing.T) {
var x interface{}
if r := InterfaceToStringOrZeroValue(x); r != "" {
t.Errorf("expected empty string, got: %v", r)
}
x = string("meow")
if r := InterfaceToStringOrZeroValue(x); r != "meow" {
t.Errorf("expected meow, got: %v", x)
}
}