Files
gocryptotrader/engine/events_test.go
Andrew f6fd94ea69 Engine: Scripting support (#383)
* WIP

* updated appveyor and increased deadline 5 seconds due to increased linters being added

* revert files to upstream/engine

* WIP

* WIP

* mod file changes

* added script manager

* Added manager/and cli interfaces to scripting

* Added script task handler

* WIP - Added timer/repeat support and fleshed out wrapper further

* autoload support added + WIP

* WIP commit

* added account balance info

* btc markets temp work around

* WIP - merged with upstream for new order package BTC Markets responses broken

* Cancel order wrapper WIP

*  order wrapper update

* Added test coverage for VM

* moved to map for VM List shutdown of all VM now handled added gctcli commands for list and stop of running scripts

* added override to load/execute for path

* fixed incorrect channel shutdown added further test coverage and restructured gctcli commands into sub commands

* increased test coverage for packages

* Added docs cleaned up tests and example scripts

* Test coverage increased for module/gct/exchange package

* windows fixes

* merged upstream/engine

* WIP

* logger fixes - removed pointer to bool check removed duplicate test check for logger

* remove unused mutex

* added inital upload support

* fix linter issues for go-fmt

* added zip support for uploading and added base for fund withdrawing

* changed error return types and also log errors, fix zip path issue

* improved error outputs and code flow

* pairs response fix added protobuf defs for stop all and list all

* added stop all running scripts general clean up and moved across to OrderManager

* linter fixes (gofmt)

* added list all command

* rewrote zip handler to be cleaner also fixed file overwrite on upload

* added query command reworked tests

* added further error checking to compileandrun corrected use of pointers for accountinfo

* bumped tengo version

* Removed named returns reworded log messages removed unused falseptr

* WIP

* Added virutal machine limit improved config options

* added model for script event added upload validation

* script_event table has been completed, tests for wrapper functions implemented

* README updates

* reverted changes opened new PR to move withdraw struct outs

* intial work on adding withdraw support after merger of withdraw package

* started work on examples

* Added crypto withdraw support

* fix switch case assignment and gofmt project

* Reworking Fiat withdraw request pending #402

* removed double pointer call

* added withdraw support for fiat  currencies

* added tests for withdraw methods increased readme

* removed local tengo require and also fix linter issues

* Added default log size const added basic test for invalid script execution

* First pass at moving wrapper to validator package to allow proper validation of uploaded scripts

* Added script details to README added config test added test for no file extension

* moved tests to const and fixed incorrect pathing

* added test coverage to withdraw package

* corrected file close handling

* point to included configtest.json

* extended validator support when a script is uploaded

* Bug fix on bool logic

* Added mutex

* Don't create autit events on test execution

* reverted common to master

* moved file rename to unix timestamp format

* converted logger enabled back to pointer as i need nilness check also moved scriptid to text over blob

* started work on autoload add/remove support

* First round of PR fixes (mostly commented exports)

* Moved GCTScript load to last, removed unneeded error from cleanup()

* Comment clairty for AuitEventID

* added autoload add/remove command to cli

* added tests for autoload

* Test updates for Exchanges

* linter fixes (gofmt)

* Removed double check of engine pointer

* remove possible nil pointer on GetSpecificTicker

* Fixed not closing file handler on write that causes archive removal to fail

* file handler Close clean ups

* corrected spelling on error return and return invalid name n autoload

* moved strings to cosnt moved bool pointer creation to convert package

* new zip extractor added

* Validation has been added to archive uploads

* removed shadow var on err

* added ok check to conversion

* converted condition check

* basic test for zip extract added

* new zip handler

* reverted back to old atomic loading system

* removed shadow err

* lets add a new line

* added space to error return

* command line toggle for script now works properly

* readme updated

* set configLoaded to true

* check for configLoaded condition

* added mutex to allow for multiple access on virtual machine increased test coverage disable script manager if scripting is disabled

* linked up to enable/disablesubsystem commands

* added start/stop example to readme

* reworked logic on test as check should be done on Load()

* updated to tengo v2

* linters

* lower time on ntp client to stop slippage

* remove all fails if any fail validtion from an archive

* remove vm from list if timer is invalid

* removed shadow on err

* remove config creation from NTPCheck test

* WIP testing DB changes

* add unique constraint

* WIP: created has many model

* linters run

* basic sqlite3 support added for new database format

* linters run

* Added test coverage for script repo

* removed unused print

* updated env vars for CI instances

* updated env vars for CI instances

* Updated test packages

* Test updates for postgresql

* removed invalid tests from postgres

* remove duplication of struct and improved code flow

* general cleanup

* wording changes on log output

* use databasemgr logger and add support for autoload without file extension

* corrected test naming

* return correct error

* return correct error again version 82

* store scriptdata on creation

* Hello

* Errorln -> Errorf

* Removed unused vars

* Read me updates

* testing without parallel

* comment on exported type

* added nil check against VM for test

* add debugging information

* gofmt

* remove verbose and data sent to channel

* Added debug information

* linter fixes (gofmt)

* remove unused CompileAndRun() call

* test sleep to see if issue is timing related

* semi-concurrent map fixes

* one day i will run gofmt or setup precommit hooks

* new line :D

* increased test coverage

* added correct sleep time

* Moved over to sync map

* linter fixes (gofmt)

* goimports

* moved VM related methods to vm.go

* new line at end of file

* trying increased timeout on golangci-lint for appveyor

* add debugging information

* removed timeout

* reworked timeout logic

* linter fixes (gofmt)

* increased test coverage

* increased test coverage

* one day i will run gofmt or setup precommit hooks

* removed unused exchange test

* increased golangci-lint timeout

* Added nil check on shutdown and test coverage for it lowered timeout back to 1:30

* reworked ID system

* removed script hash as it was unused

* added comments on exported methods and read me update

* reorder code

* removed to atomic.value for test execution flag

* increased test coverage

* move add further up execution

* point to correct script file
2020-01-23 13:54:04 +11:00

348 lines
7.6 KiB
Go

package engine
import (
"testing"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
)
const (
testExchange = "Bitstamp"
)
var (
configLoaded = false
)
func addValidEvent() (int64, error) {
return Add(testExchange,
ItemPrice,
EventConditionParams{Condition: ConditionGreaterThan, Price: 1},
currency.NewPair(currency.BTC, currency.USD),
asset.Spot,
"SMS,test")
}
func TestAdd(t *testing.T) {
if !configLoaded {
loadConfig(t)
}
_, err := Add("", "", EventConditionParams{}, currency.Pair{}, "", "")
if err == nil {
t.Error("should err on invalid params")
}
_, err = addValidEvent()
if err != nil {
t.Error("unexpected result", err)
}
_, err = addValidEvent()
if err != nil {
t.Error("unexpected result", err)
}
if len(Events) != 2 {
t.Error("2 events should be stored")
}
}
func TestRemove(t *testing.T) {
if !configLoaded {
loadConfig(t)
}
id, err := addValidEvent()
if err != nil {
t.Error("unexpected result", err)
}
if s := Remove(id); !s {
t.Error("unexpected result")
}
if s := Remove(id); s {
t.Error("unexpected result")
}
}
func TestGetEventCounter(t *testing.T) {
if !configLoaded {
loadConfig(t)
}
_, err := addValidEvent()
if err != nil {
t.Error("unexpected result", err)
}
n, e := GetEventCounter()
if n == 0 || e > 0 {
t.Error("unexpected result")
}
Events[0].Executed = true
n, e = GetEventCounter()
if n == 0 || e == 0 {
t.Error("unexpected result")
}
}
func TestExecuteAction(t *testing.T) {
t.Parallel()
if Bot == nil {
Bot = new(Engine)
}
var e Event
if r := e.ExecuteAction(); !r {
t.Error("unexpected result")
}
e.Action = "SMS,test"
if r := e.ExecuteAction(); !r {
t.Error("unexpected result")
}
e.Action = "SMS,ALL"
if r := e.ExecuteAction(); !r {
t.Error("unexpected result")
}
}
func TestString(t *testing.T) {
t.Parallel()
e := Event{
Exchange: testExchange,
Item: ItemPrice,
Condition: EventConditionParams{
Condition: ConditionGreaterThan,
Price: 1,
},
Pair: currency.NewPair(currency.BTC, currency.USD),
Asset: asset.Spot,
Action: "SMS,ALL",
}
if r := e.String(); r != "If the BTCUSD [SPOT] PRICE on Bitstamp meets the following {> 1 false false 0} then SMS,ALL." {
t.Error("unexpected result")
}
}
func TestProcessTicker(t *testing.T) {
if Bot == nil {
Bot = new(Engine)
}
Bot.Settings.Verbose = true
e := Event{
Exchange: testExchange,
Pair: currency.NewPair(currency.BTC, currency.USD),
Asset: asset.Spot,
Condition: EventConditionParams{
Condition: ConditionGreaterThan,
Price: 1,
},
}
// now populate it with a 0 entry
tick := ticker.Price{
Pair: currency.NewPair(currency.BTC, currency.USD),
Last: 0,
}
if err := ticker.ProcessTicker(e.Exchange, &tick, e.Asset); err != nil {
t.Fatal("unexpected result:", err)
}
if r := e.processTicker(); r {
t.Error("unexpected result")
}
// now populate it with a number > 0
tick.Last = 1337
if err := ticker.ProcessTicker(e.Exchange, &tick, e.Asset); err != nil {
t.Fatal("unexpected result:", err)
}
if r := e.processTicker(); !r {
t.Error("unexpected result")
}
}
func TestProcessCondition(t *testing.T) {
t.Parallel()
var e Event
tester := []struct {
Condition string
Actual float64
Threshold float64
ExpectedResult bool
}{
{ConditionGreaterThan, 1, 2, false},
{ConditionGreaterThan, 2, 1, true},
{ConditionGreaterThanOrEqual, 1, 2, false},
{ConditionGreaterThanOrEqual, 2, 1, true},
{ConditionIsEqual, 1, 1, true},
{ConditionIsEqual, 1, 2, false},
{ConditionLessThan, 1, 2, true},
{ConditionLessThan, 2, 1, false},
{ConditionLessThanOrEqual, 1, 2, true},
{ConditionLessThanOrEqual, 2, 1, false},
}
for x := range tester {
e.Condition.Condition = tester[x].Condition
if r := e.processCondition(tester[x].Actual, tester[x].Threshold); r != tester[x].ExpectedResult {
t.Error("unexpected result")
}
}
}
func TestProcessOrderbook(t *testing.T) {
if Bot == nil {
Bot = new(Engine)
}
Bot.Settings.Verbose = true
e := Event{
Exchange: testExchange,
Pair: currency.NewPair(currency.BTC, currency.USD),
Asset: asset.Spot,
Condition: EventConditionParams{
Condition: ConditionGreaterThan,
CheckBidsAndAsks: true,
OrderbookAmount: 100,
},
}
// now populate it with a 0 entry
o := orderbook.Base{
Pair: currency.NewPair(currency.BTC, currency.USD),
Bids: []orderbook.Item{{Amount: 24, Price: 23}},
Asks: []orderbook.Item{{Amount: 24, Price: 23}},
ExchangeName: e.Exchange,
AssetType: e.Asset,
}
if err := o.Process(); err != nil {
t.Fatal("unexpected result:", err)
}
if r := e.processOrderbook(); !r {
t.Error("unexpected result")
}
}
func TestCheckEventCondition(t *testing.T) {
t.Parallel()
if Bot == nil {
Bot = new(Engine)
}
Bot.Settings.Verbose = true
e := Event{
Item: ItemPrice,
}
if r := e.CheckEventCondition(); r {
t.Error("unexpected result")
}
e.Item = ItemOrderbook
if r := e.CheckEventCondition(); r {
t.Error("unexpected result")
}
}
func TestIsValidEvent(t *testing.T) {
if !configLoaded {
loadConfig(t)
}
// invalid exchange name
if err := IsValidEvent("meow", "", EventConditionParams{}, ""); err != errExchangeDisabled {
t.Error("unexpected result:", err)
}
// invalid item
if err := IsValidEvent(testExchange, "", EventConditionParams{}, ""); err != errInvalidItem {
t.Error("unexpected result:", err)
}
// invalid condition
if err := IsValidEvent(testExchange, ItemPrice, EventConditionParams{}, ""); err != errInvalidCondition {
t.Error("unexpected result:", err)
}
// valid condition but empty price which will still throw an errInvalidCondition
c := EventConditionParams{
Condition: ConditionGreaterThan,
}
if err := IsValidEvent(testExchange, ItemPrice, c, ""); err != errInvalidCondition {
t.Error("unexpected result:", err)
}
// valid condition but empty orderbook amount will still still throw an errInvalidCondition
if err := IsValidEvent(testExchange, ItemOrderbook, c, ""); err != errInvalidCondition {
t.Error("unexpected result:", err)
}
// test action splitting, but invalid
c.OrderbookAmount = 1337
if err := IsValidEvent(testExchange, ItemOrderbook, c, "a,meow"); err != errInvalidAction {
t.Error("unexpected result:", err)
}
// check for invalid action without splitting
if err := IsValidEvent(testExchange, ItemOrderbook, c, "hi"); err != errInvalidAction {
t.Error("unexpected result:", err)
}
// valid event
if err := IsValidEvent(testExchange, ItemOrderbook, c, "SMS,test"); err != nil {
t.Error("unexpected result:", err)
}
}
func TestIsValidExchange(t *testing.T) {
t.Parallel()
if s := IsValidExchange("invalidexchangerino"); s {
t.Error("unexpected result")
}
if !configLoaded {
loadConfig(t)
}
if s := IsValidExchange(testExchange); !s {
t.Error("unexpected result")
}
}
func TestIsValidCondition(t *testing.T) {
t.Parallel()
if s := IsValidCondition("invalidconditionerino"); s {
t.Error("unexpected result")
}
if s := IsValidCondition(ConditionGreaterThan); !s {
t.Error("unexpected result")
}
}
func TestIsValidAction(t *testing.T) {
t.Parallel()
if s := IsValidAction("invalidactionerino"); s {
t.Error("unexpected result")
}
if s := IsValidAction(ActionSMSNotify); !s {
t.Error("unexpected result")
}
}
func TestIsValidItem(t *testing.T) {
t.Parallel()
if s := IsValidItem("invaliditemerino"); s {
t.Error("unexpected result")
}
if s := IsValidItem(ItemPrice); !s {
t.Error("unexpected result")
}
}