mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-09 07:26:48 +00:00
New communications package
Support for Slack, SMSGlobal, SMTP and Telegram Supersedes: https://github.com/thrasher-/gocryptotrader/pull/126
This commit is contained in:
committed by
Adrian Gallagher
parent
d34fc9aae8
commit
9d0616d8cf
@@ -1,368 +1,369 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
"github.com/thrasher-/gocryptotrader/smsglobal"
|
||||
)
|
||||
|
||||
var (
|
||||
loaded = false
|
||||
)
|
||||
|
||||
func testSetup(t *testing.T) {
|
||||
if !loaded {
|
||||
cfg := config.GetConfig()
|
||||
err := cfg.LoadConfig("")
|
||||
if err != nil {
|
||||
t.Fatalf("Test failed. Failed to load config %s", err)
|
||||
}
|
||||
smsglobal.New(cfg.SMS.Username, cfg.SMS.Password, cfg.Name, cfg.SMS.Contacts)
|
||||
loaded = true
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddEvent(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
eventID, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
if err != nil && eventID != 0 {
|
||||
t.Errorf("Test Failed. AddEvent: Error, %s", err)
|
||||
}
|
||||
eventID, err = AddEvent("ANXX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
if err == nil && eventID == 0 {
|
||||
t.Error("Test Failed. AddEvent: Error, error not captured in Exchange")
|
||||
}
|
||||
eventID, err = AddEvent("ANX", "prices", ">,==", pair, "SPOT", actionTest)
|
||||
if err == nil && eventID == 0 {
|
||||
t.Error("Test Failed. AddEvent: Error, error not captured in Item")
|
||||
}
|
||||
eventID, err = AddEvent("ANX", "price", "3===D", pair, "SPOT", actionTest)
|
||||
if err == nil && eventID == 0 {
|
||||
t.Error("Test Failed. AddEvent: Error, error not captured in Condition")
|
||||
}
|
||||
eventID, err = AddEvent("ANX", "price", ">,==", pair, "SPOT", "console_prints")
|
||||
if err == nil && eventID == 0 {
|
||||
t.Error("Test Failed. AddEvent: Error, error not captured in Action")
|
||||
}
|
||||
|
||||
if !RemoveEvent(eventID) {
|
||||
t.Error("Test Failed. RemoveEvent: Error, error removing event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveEvent(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
eventID, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
if err != nil && eventID != 0 {
|
||||
t.Errorf("Test Failed. RemoveEvent: Error, %s", err)
|
||||
}
|
||||
if !RemoveEvent(eventID) {
|
||||
t.Error("Test Failed. RemoveEvent: Error, error removing event")
|
||||
}
|
||||
if RemoveEvent(1234) {
|
||||
t.Error("Test Failed. RemoveEvent: Error, error removing event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEventCounter(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
one, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. GetEventCounter: Error, %s", err)
|
||||
}
|
||||
two, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. GetEventCounter: Error, %s", err)
|
||||
}
|
||||
three, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. GetEventCounter: Error, %s", err)
|
||||
}
|
||||
|
||||
Events[three-1].Executed = true
|
||||
|
||||
total, _ := GetEventCounter()
|
||||
if total <= 0 {
|
||||
t.Errorf("Test Failed. GetEventCounter: Total = %d", total)
|
||||
}
|
||||
if !RemoveEvent(one) {
|
||||
t.Error("Test Failed. GetEventCounter: Error, error removing event")
|
||||
}
|
||||
if !RemoveEvent(two) {
|
||||
t.Error("Test Failed. GetEventCounter: Error, error removing event")
|
||||
}
|
||||
if !RemoveEvent(three) {
|
||||
t.Error("Test Failed. GetEventCounter: Error, error removing event")
|
||||
}
|
||||
|
||||
total2, _ := GetEventCounter()
|
||||
if total2 != 0 {
|
||||
t.Errorf("Test Failed. GetEventCounter: Total = %d", total2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteAction(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
one, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
if err != nil {
|
||||
t.Fatalf("Test Failed. ExecuteAction: Error, %s", err)
|
||||
}
|
||||
isExecuted := Events[one].ExecuteAction()
|
||||
if !isExecuted {
|
||||
t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
}
|
||||
if !RemoveEvent(one) {
|
||||
t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
}
|
||||
|
||||
action := actionSMSNotify + "," + "ALL"
|
||||
one, err = AddEvent("ANX", "price", ">,==", pair, "SPOT", action)
|
||||
if err != nil {
|
||||
t.Fatalf("Test Failed. ExecuteAction: Error, %s", err)
|
||||
}
|
||||
|
||||
isExecuted = Events[one].ExecuteAction()
|
||||
if !isExecuted {
|
||||
t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
}
|
||||
if !RemoveEvent(one) {
|
||||
t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
}
|
||||
|
||||
action = actionSMSNotify + "," + "StyleGherkin"
|
||||
one, err = AddEvent("ANX", "price", ">,==", pair, "SPOT", action)
|
||||
if err != nil {
|
||||
t.Fatalf("Test Failed. ExecuteAction: Error, %s", err)
|
||||
}
|
||||
|
||||
isExecuted = Events[one].ExecuteAction()
|
||||
if !isExecuted {
|
||||
t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
}
|
||||
if !RemoveEvent(one) {
|
||||
t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
}
|
||||
// More tests when ExecuteAction is expanded
|
||||
}
|
||||
|
||||
func TestEventToString(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
one, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. EventToString: Error, %s", err)
|
||||
}
|
||||
|
||||
eventString := Events[one].String()
|
||||
if eventString != "If the BTCUSD [SPOT] price on ANX is > == then ACTION_TEST." {
|
||||
t.Error("Test Failed. EventToString: Error, incorrect return string")
|
||||
}
|
||||
|
||||
if !RemoveEvent(one) {
|
||||
t.Error("Test Failed. EventToString: Error, error removing event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckCondition(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
// Test invalid currency pair
|
||||
newPair := pair.NewCurrencyPair("A", "B")
|
||||
one, err := AddEvent("ANX", "price", ">=,10", newPair, "SPOT", actionTest)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. CheckCondition: Error, %s", err)
|
||||
}
|
||||
conditionBool := Events[one].CheckCondition()
|
||||
if conditionBool {
|
||||
t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
}
|
||||
|
||||
// Test last price == 0
|
||||
var tickerNew ticker.Price
|
||||
tickerNew.Last = 0
|
||||
newPair = pair.NewCurrencyPair("BTC", "USD")
|
||||
ticker.ProcessTicker("ANX", newPair, tickerNew, ticker.Spot)
|
||||
Events[one].Pair = newPair
|
||||
conditionBool = Events[one].CheckCondition()
|
||||
if conditionBool {
|
||||
t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
}
|
||||
|
||||
// Test last pricce > 0 and conditional logic
|
||||
tickerNew.Last = 11
|
||||
ticker.ProcessTicker("ANX", newPair, tickerNew, ticker.Spot)
|
||||
Events[one].Condition = ">,10"
|
||||
conditionBool = Events[one].CheckCondition()
|
||||
if !conditionBool {
|
||||
t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
}
|
||||
|
||||
// Test last price >= 10
|
||||
Events[one].Condition = ">=,10"
|
||||
conditionBool = Events[one].CheckCondition()
|
||||
if !conditionBool {
|
||||
t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
}
|
||||
|
||||
// Test last price <= 10
|
||||
Events[one].Condition = "<,100"
|
||||
conditionBool = Events[one].CheckCondition()
|
||||
if !conditionBool {
|
||||
t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
}
|
||||
|
||||
// Test last price <= 10
|
||||
Events[one].Condition = "<=,100"
|
||||
conditionBool = Events[one].CheckCondition()
|
||||
if !conditionBool {
|
||||
t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
}
|
||||
|
||||
Events[one].Condition = "==,11"
|
||||
conditionBool = Events[one].CheckCondition()
|
||||
if !conditionBool {
|
||||
t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
}
|
||||
|
||||
Events[one].Condition = "^,11"
|
||||
conditionBool = Events[one].CheckCondition()
|
||||
if conditionBool {
|
||||
t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
}
|
||||
|
||||
if !RemoveEvent(one) {
|
||||
t.Error("Test Failed. CheckCondition: Error, error removing event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidEvent(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
err := IsValidEvent("ANX", "price", ">,==", actionTest)
|
||||
if err != nil {
|
||||
t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
}
|
||||
err = IsValidEvent("ANX", "price", ">,", actionTest)
|
||||
if err == nil {
|
||||
t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
}
|
||||
err = IsValidEvent("ANX", "Testy", ">,==", actionTest)
|
||||
if err == nil {
|
||||
t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
}
|
||||
err = IsValidEvent("Testys", "price", ">,==", actionTest)
|
||||
if err == nil {
|
||||
t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
}
|
||||
|
||||
action := "blah,blah"
|
||||
err = IsValidEvent("ANX", "price", ">=,10", action)
|
||||
if err == nil {
|
||||
t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
}
|
||||
|
||||
action = "SMS,blah"
|
||||
err = IsValidEvent("ANX", "price", ">=,10", action)
|
||||
if err == nil {
|
||||
t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
}
|
||||
|
||||
//Function tests need to appended to this function when more actions are
|
||||
//implemented
|
||||
}
|
||||
|
||||
func TestCheckEvents(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
_, err := AddEvent("ANX", "price", ">=,10", pair, "SPOT", actionTest)
|
||||
if err != nil {
|
||||
t.Fatal("Test failed. TestChcheckEvents add event")
|
||||
}
|
||||
|
||||
go CheckEvents()
|
||||
}
|
||||
|
||||
func TestIsValidExchange(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
boolean := IsValidExchange("ANX")
|
||||
if !boolean {
|
||||
t.Error("Test Failed. IsValidExchange: Error, incorrect Exchange")
|
||||
}
|
||||
boolean = IsValidExchange("OBTUSE")
|
||||
if boolean {
|
||||
t.Error("Test Failed. IsValidExchange: Error, incorrect return")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidCondition(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
boolean := IsValidCondition(">")
|
||||
if !boolean {
|
||||
t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
}
|
||||
boolean = IsValidCondition(">=")
|
||||
if !boolean {
|
||||
t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
}
|
||||
boolean = IsValidCondition("<")
|
||||
if !boolean {
|
||||
t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
}
|
||||
boolean = IsValidCondition("<=")
|
||||
if !boolean {
|
||||
t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
}
|
||||
boolean = IsValidCondition("==")
|
||||
if !boolean {
|
||||
t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
}
|
||||
boolean = IsValidCondition("**********")
|
||||
if boolean {
|
||||
t.Error("Test Failed. IsValidCondition: Error, incorrect return")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidAction(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
boolean := IsValidAction("sms")
|
||||
if !boolean {
|
||||
t.Error("Test Failed. IsValidAction: Error, incorrect Action")
|
||||
}
|
||||
boolean = IsValidAction(actionTest)
|
||||
if !boolean {
|
||||
t.Error("Test Failed. IsValidAction: Error, incorrect Action")
|
||||
}
|
||||
boolean = IsValidAction("randomstring")
|
||||
if boolean {
|
||||
t.Error("Test Failed. IsValidAction: Error, incorrect return")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidItem(t *testing.T) {
|
||||
testSetup(t)
|
||||
|
||||
boolean := IsValidItem("price")
|
||||
if !boolean {
|
||||
t.Error("Test Failed. IsValidItem: Error, incorrect Item")
|
||||
}
|
||||
boolean = IsValidItem("obtuse")
|
||||
if boolean {
|
||||
t.Error("Test Failed. IsValidItem: Error, incorrect return")
|
||||
}
|
||||
}
|
||||
//
|
||||
// import (
|
||||
// "testing"
|
||||
//
|
||||
// "github.com/thrasher-/gocryptotrader/config"
|
||||
// "github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
// "github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
// "github.com/thrasher-/gocryptotrader/smsglobal"
|
||||
// )
|
||||
//
|
||||
// var (
|
||||
// loaded = false
|
||||
// )
|
||||
//
|
||||
// func testSetup(t *testing.T) {
|
||||
// if !loaded {
|
||||
// cfg := config.GetConfig()
|
||||
// err := cfg.LoadConfig("")
|
||||
// if err != nil {
|
||||
// t.Fatalf("Test failed. Failed to load config %s", err)
|
||||
// }
|
||||
// smsglobal.New(cfg.SMS.Username, cfg.SMS.Password, cfg.Name, cfg.SMS.Contacts)
|
||||
// loaded = true
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func TestAddEvent(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
// eventID, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
// if err != nil && eventID != 0 {
|
||||
// t.Errorf("Test Failed. AddEvent: Error, %s", err)
|
||||
// }
|
||||
// eventID, err = AddEvent("ANXX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
// if err == nil && eventID == 0 {
|
||||
// t.Error("Test Failed. AddEvent: Error, error not captured in Exchange")
|
||||
// }
|
||||
// eventID, err = AddEvent("ANX", "prices", ">,==", pair, "SPOT", actionTest)
|
||||
// if err == nil && eventID == 0 {
|
||||
// t.Error("Test Failed. AddEvent: Error, error not captured in Item")
|
||||
// }
|
||||
// eventID, err = AddEvent("ANX", "price", "3===D", pair, "SPOT", actionTest)
|
||||
// if err == nil && eventID == 0 {
|
||||
// t.Error("Test Failed. AddEvent: Error, error not captured in Condition")
|
||||
// }
|
||||
// eventID, err = AddEvent("ANX", "price", ">,==", pair, "SPOT", "console_prints")
|
||||
// if err == nil && eventID == 0 {
|
||||
// t.Error("Test Failed. AddEvent: Error, error not captured in Action")
|
||||
// }
|
||||
//
|
||||
// if !RemoveEvent(eventID) {
|
||||
// t.Error("Test Failed. RemoveEvent: Error, error removing event")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func TestRemoveEvent(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
// eventID, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
// if err != nil && eventID != 0 {
|
||||
// t.Errorf("Test Failed. RemoveEvent: Error, %s", err)
|
||||
// }
|
||||
// if !RemoveEvent(eventID) {
|
||||
// t.Error("Test Failed. RemoveEvent: Error, error removing event")
|
||||
// }
|
||||
// if RemoveEvent(1234) {
|
||||
// t.Error("Test Failed. RemoveEvent: Error, error removing event")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func TestGetEventCounter(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
// one, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
// if err != nil {
|
||||
// t.Errorf("Test Failed. GetEventCounter: Error, %s", err)
|
||||
// }
|
||||
// two, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
// if err != nil {
|
||||
// t.Errorf("Test Failed. GetEventCounter: Error, %s", err)
|
||||
// }
|
||||
// three, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
// if err != nil {
|
||||
// t.Errorf("Test Failed. GetEventCounter: Error, %s", err)
|
||||
// }
|
||||
//
|
||||
// Events[three-1].Executed = true
|
||||
//
|
||||
// total, _ := GetEventCounter()
|
||||
// if total <= 0 {
|
||||
// t.Errorf("Test Failed. GetEventCounter: Total = %d", total)
|
||||
// }
|
||||
// if !RemoveEvent(one) {
|
||||
// t.Error("Test Failed. GetEventCounter: Error, error removing event")
|
||||
// }
|
||||
// if !RemoveEvent(two) {
|
||||
// t.Error("Test Failed. GetEventCounter: Error, error removing event")
|
||||
// }
|
||||
// if !RemoveEvent(three) {
|
||||
// t.Error("Test Failed. GetEventCounter: Error, error removing event")
|
||||
// }
|
||||
//
|
||||
// total2, _ := GetEventCounter()
|
||||
// if total2 != 0 {
|
||||
// t.Errorf("Test Failed. GetEventCounter: Total = %d", total2)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func TestExecuteAction(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
// one, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
// if err != nil {
|
||||
// t.Fatalf("Test Failed. ExecuteAction: Error, %s", err)
|
||||
// }
|
||||
// isExecuted := Events[one].ExecuteAction()
|
||||
// if !isExecuted {
|
||||
// t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
// }
|
||||
// if !RemoveEvent(one) {
|
||||
// t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
// }
|
||||
//
|
||||
// action := actionSMSNotify + "," + "ALL"
|
||||
// one, err = AddEvent("ANX", "price", ">,==", pair, "SPOT", action)
|
||||
// if err != nil {
|
||||
// t.Fatalf("Test Failed. ExecuteAction: Error, %s", err)
|
||||
// }
|
||||
//
|
||||
// isExecuted = Events[one].ExecuteAction()
|
||||
// if !isExecuted {
|
||||
// t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
// }
|
||||
// if !RemoveEvent(one) {
|
||||
// t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
// }
|
||||
//
|
||||
// action = actionSMSNotify + "," + "StyleGherkin"
|
||||
// one, err = AddEvent("ANX", "price", ">,==", pair, "SPOT", action)
|
||||
// if err != nil {
|
||||
// t.Fatalf("Test Failed. ExecuteAction: Error, %s", err)
|
||||
// }
|
||||
//
|
||||
// isExecuted = Events[one].ExecuteAction()
|
||||
// if !isExecuted {
|
||||
// t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
// }
|
||||
// if !RemoveEvent(one) {
|
||||
// t.Error("Test Failed. ExecuteAction: Error, error removing event")
|
||||
// }
|
||||
// // More tests when ExecuteAction is expanded
|
||||
// }
|
||||
//
|
||||
// func TestEventToString(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
// one, err := AddEvent("ANX", "price", ">,==", pair, "SPOT", actionTest)
|
||||
// if err != nil {
|
||||
// t.Errorf("Test Failed. EventToString: Error, %s", err)
|
||||
// }
|
||||
//
|
||||
// eventString := Events[one].String()
|
||||
// if eventString != "If the BTCUSD [SPOT] price on ANX is > == then ACTION_TEST." {
|
||||
// t.Error("Test Failed. EventToString: Error, incorrect return string")
|
||||
// }
|
||||
//
|
||||
// if !RemoveEvent(one) {
|
||||
// t.Error("Test Failed. EventToString: Error, error removing event")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func TestCheckCondition(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// // Test invalid currency pair
|
||||
// newPair := pair.NewCurrencyPair("A", "B")
|
||||
// one, err := AddEvent("ANX", "price", ">=,10", newPair, "SPOT", actionTest)
|
||||
// if err != nil {
|
||||
// t.Errorf("Test Failed. CheckCondition: Error, %s", err)
|
||||
// }
|
||||
// conditionBool := Events[one].CheckCondition()
|
||||
// if conditionBool {
|
||||
// t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
// }
|
||||
//
|
||||
// // Test last price == 0
|
||||
// var tickerNew ticker.Price
|
||||
// tickerNew.Last = 0
|
||||
// newPair = pair.NewCurrencyPair("BTC", "USD")
|
||||
// ticker.ProcessTicker("ANX", newPair, tickerNew, ticker.Spot)
|
||||
// Events[one].Pair = newPair
|
||||
// conditionBool = Events[one].CheckCondition()
|
||||
// if conditionBool {
|
||||
// t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
// }
|
||||
//
|
||||
// // Test last pricce > 0 and conditional logic
|
||||
// tickerNew.Last = 11
|
||||
// ticker.ProcessTicker("ANX", newPair, tickerNew, ticker.Spot)
|
||||
// Events[one].Condition = ">,10"
|
||||
// conditionBool = Events[one].CheckCondition()
|
||||
// if !conditionBool {
|
||||
// t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
// }
|
||||
//
|
||||
// // Test last price >= 10
|
||||
// Events[one].Condition = ">=,10"
|
||||
// conditionBool = Events[one].CheckCondition()
|
||||
// if !conditionBool {
|
||||
// t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
// }
|
||||
//
|
||||
// // Test last price <= 10
|
||||
// Events[one].Condition = "<,100"
|
||||
// conditionBool = Events[one].CheckCondition()
|
||||
// if !conditionBool {
|
||||
// t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
// }
|
||||
//
|
||||
// // Test last price <= 10
|
||||
// Events[one].Condition = "<=,100"
|
||||
// conditionBool = Events[one].CheckCondition()
|
||||
// if !conditionBool {
|
||||
// t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
// }
|
||||
//
|
||||
// Events[one].Condition = "==,11"
|
||||
// conditionBool = Events[one].CheckCondition()
|
||||
// if !conditionBool {
|
||||
// t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
// }
|
||||
//
|
||||
// Events[one].Condition = "^,11"
|
||||
// conditionBool = Events[one].CheckCondition()
|
||||
// if conditionBool {
|
||||
// t.Error("Test Failed. CheckCondition: Error, wrong conditional.")
|
||||
// }
|
||||
//
|
||||
// if !RemoveEvent(one) {
|
||||
// t.Error("Test Failed. CheckCondition: Error, error removing event")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func TestIsValidEvent(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// err := IsValidEvent("ANX", "price", ">,==", actionTest)
|
||||
// if err != nil {
|
||||
// t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
// }
|
||||
// err = IsValidEvent("ANX", "price", ">,", actionTest)
|
||||
// if err == nil {
|
||||
// t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
// }
|
||||
// err = IsValidEvent("ANX", "Testy", ">,==", actionTest)
|
||||
// if err == nil {
|
||||
// t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
// }
|
||||
// err = IsValidEvent("Testys", "price", ">,==", actionTest)
|
||||
// if err == nil {
|
||||
// t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
// }
|
||||
//
|
||||
// action := "blah,blah"
|
||||
// err = IsValidEvent("ANX", "price", ">=,10", action)
|
||||
// if err == nil {
|
||||
// t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
// }
|
||||
//
|
||||
// action = "SMS,blah"
|
||||
// err = IsValidEvent("ANX", "price", ">=,10", action)
|
||||
// if err == nil {
|
||||
// t.Errorf("Test Failed. IsValidEvent: %s", err)
|
||||
// }
|
||||
//
|
||||
// //Function tests need to appended to this function when more actions are
|
||||
// //implemented
|
||||
// }
|
||||
//
|
||||
// func TestCheckEvents(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// pair := pair.NewCurrencyPair("BTC", "USD")
|
||||
// _, err := AddEvent("ANX", "price", ">=,10", pair, "SPOT", actionTest)
|
||||
// if err != nil {
|
||||
// t.Fatal("Test failed. TestChcheckEvents add event")
|
||||
// }
|
||||
//
|
||||
// go CheckEvents()
|
||||
// }
|
||||
//
|
||||
// func TestIsValidExchange(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// boolean := IsValidExchange("ANX")
|
||||
// if !boolean {
|
||||
// t.Error("Test Failed. IsValidExchange: Error, incorrect Exchange")
|
||||
// }
|
||||
// boolean = IsValidExchange("OBTUSE")
|
||||
// if boolean {
|
||||
// t.Error("Test Failed. IsValidExchange: Error, incorrect return")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func TestIsValidCondition(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// boolean := IsValidCondition(">")
|
||||
// if !boolean {
|
||||
// t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
// }
|
||||
// boolean = IsValidCondition(">=")
|
||||
// if !boolean {
|
||||
// t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
// }
|
||||
// boolean = IsValidCondition("<")
|
||||
// if !boolean {
|
||||
// t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
// }
|
||||
// boolean = IsValidCondition("<=")
|
||||
// if !boolean {
|
||||
// t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
// }
|
||||
// boolean = IsValidCondition("==")
|
||||
// if !boolean {
|
||||
// t.Error("Test Failed. IsValidCondition: Error, incorrect Condition")
|
||||
// }
|
||||
// boolean = IsValidCondition("**********")
|
||||
// if boolean {
|
||||
// t.Error("Test Failed. IsValidCondition: Error, incorrect return")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func TestIsValidAction(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// boolean := IsValidAction("sms")
|
||||
// if !boolean {
|
||||
// t.Error("Test Failed. IsValidAction: Error, incorrect Action")
|
||||
// }
|
||||
// boolean = IsValidAction(actionTest)
|
||||
// if !boolean {
|
||||
// t.Error("Test Failed. IsValidAction: Error, incorrect Action")
|
||||
// }
|
||||
// boolean = IsValidAction("randomstring")
|
||||
// if boolean {
|
||||
// t.Error("Test Failed. IsValidAction: Error, incorrect return")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func TestIsValidItem(t *testing.T) {
|
||||
// testSetup(t)
|
||||
//
|
||||
// boolean := IsValidItem("price")
|
||||
// if !boolean {
|
||||
// t.Error("Test Failed. IsValidItem: Error, incorrect Item")
|
||||
// }
|
||||
// boolean = IsValidItem("obtuse")
|
||||
// if boolean {
|
||||
// t.Error("Test Failed. IsValidItem: Error, incorrect return")
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -7,10 +7,11 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/thrasher-/gocryptotrader/common"
|
||||
"github.com/thrasher-/gocryptotrader/communications"
|
||||
"github.com/thrasher-/gocryptotrader/communications/base"
|
||||
"github.com/thrasher-/gocryptotrader/config"
|
||||
"github.com/thrasher-/gocryptotrader/currency/pair"
|
||||
"github.com/thrasher-/gocryptotrader/exchanges/ticker"
|
||||
"github.com/thrasher-/gocryptotrader/smsglobal"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -30,6 +31,9 @@ var (
|
||||
errInvalidCondition = errors.New("invalid conditional option")
|
||||
errInvalidAction = errors.New("invalid action")
|
||||
errExchangeDisabled = errors.New("desired exchange is disabled")
|
||||
|
||||
// NOTE comms is an interim implementation
|
||||
comms *communications.Communications
|
||||
)
|
||||
|
||||
// Event struct holds the event variables
|
||||
@@ -48,6 +52,12 @@ type Event struct {
|
||||
// appended
|
||||
var Events []*Event
|
||||
|
||||
// SetComms is an interim function that will support a median integration. This
|
||||
// sets the current comms package.
|
||||
func SetComms(commsP *communications.Communications) {
|
||||
comms = commsP
|
||||
}
|
||||
|
||||
// AddEvent adds an event to the Events chain and returns an index/eventID
|
||||
// and an error
|
||||
func AddEvent(Exchange, Item, Condition string, CurrencyPair pair.CurrencyPair, Asset, Action string) (int, error) {
|
||||
@@ -106,12 +116,8 @@ func (e *Event) ExecuteAction() bool {
|
||||
action := common.SplitStrings(e.Action, ",")
|
||||
if action[0] == actionSMSNotify {
|
||||
message := fmt.Sprintf("Event triggered: %s", e.String())
|
||||
s := smsglobal.SMSGlobal
|
||||
if action[1] == "ALL" {
|
||||
s.SendMessageToAll(message)
|
||||
} else {
|
||||
contact, _ := s.GetContactByName(action[1])
|
||||
s.SendMessage(contact.Number, message)
|
||||
comms.PushEvent(base.Event{TradeDetails: message})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -213,11 +219,7 @@ func IsValidEvent(Exchange, Item, Condition, Action string) error {
|
||||
}
|
||||
|
||||
if action[1] != "ALL" {
|
||||
s := smsglobal.SMSGlobal
|
||||
_, err := s.GetContactByName(action[1])
|
||||
if err != nil {
|
||||
return errInvalidAction
|
||||
}
|
||||
comms.PushEvent(base.Event{Type: action[1]})
|
||||
}
|
||||
} else {
|
||||
if Action != actionConsolePrint && Action != actionTest {
|
||||
|
||||
Reference in New Issue
Block a user