package kline import ( "math/rand" "testing" "time" "github.com/thrasher-corp/gocryptotrader/common/crypto" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" "github.com/thrasher-corp/gocryptotrader/exchanges/order" ) func TestValidateData(t *testing.T) { err := validateData(nil) if err == nil { t.Error("error cannot be nil") } var empty []order.TradeHistory err = validateData(empty) if err == nil { t.Error("error cannot be nil") } tn := time.Now() trade1 := []order.TradeHistory{ {Timestamp: tn.Add(2 * time.Minute), TID: "2"}, {Timestamp: tn.Add(time.Minute), TID: "1"}, {Timestamp: tn.Add(3 * time.Minute), TID: "3"}, } err = validateData(trade1) if err == nil { t.Error("error cannot be nil") } trade2 := []order.TradeHistory{ {Timestamp: tn.Add(2 * time.Minute), TID: "2", Amount: 1, Price: 0}, } err = validateData(trade2) if err == nil { t.Error("error cannot be nil") } trade3 := []order.TradeHistory{ {TID: "2", Amount: 1, Price: 0}, } err = validateData(trade3) if err == nil { t.Error("error cannot be nil") } trade4 := []order.TradeHistory{ {Timestamp: tn.Add(2 * time.Minute), TID: "2", Amount: 1, Price: 1000}, {Timestamp: tn.Add(time.Minute), TID: "1", Amount: 1, Price: 1001}, {Timestamp: tn.Add(3 * time.Minute), TID: "3", Amount: 1, Price: 1001.5}, } err = validateData(trade4) if err != nil { t.Error(err) } if trade4[0].TID != "1" || trade4[1].TID != "2" || trade4[2].TID != "3" { t.Error("trade history sorted incorrectly") } } func TestCreateKline(t *testing.T) { c, err := CreateKline(nil, time.Minute, currency.NewPair(currency.BTC, currency.USD), asset.Spot, "Binance") if err == nil { t.Fatal("error cannot be nil") } var trades []order.TradeHistory rand.Seed(time.Now().Unix()) for i := 0; i < 24000; i++ { trades = append(trades, order.TradeHistory{ Timestamp: time.Now().Add((time.Duration(rand.Intn(10)) * time.Minute) + (time.Duration(rand.Intn(10)) * time.Second)), TID: crypto.HexEncodeToString([]byte(string(i))), Amount: float64(rand.Intn(20)) + 1, Price: 1000 + float64(rand.Intn(1000)), }) } c, err = CreateKline(trades, 0, currency.NewPair(currency.BTC, currency.USD), asset.Spot, "Binance") if err == nil { t.Fatal("error cannot be nil") } c, err = CreateKline(trades, OneMin, currency.NewPair(currency.BTC, currency.USD), asset.Spot, "Binance") if err != nil { t.Fatal(err) } if len(c.Candles) == 0 { t.Fatal("no data returned, expecting a lot.") } }