Files
gocryptotrader/engine/withdraw_test.go
Andrew b26ec86d43 Withdraw additional functionality (validation/submission/tracking) (#409)
* reworked request struct and exchange response started work on validation system

* removed import cycle until work around

* Added intial withdraw support via CLI added

* Added Crypto command to gctcli

* moved var declartion to single line

* Test updates for binance and anx

* All exchange tests have been updated test coverage added to validate

* First pass at adding withdrawl select from database

* started adding basic lru cache system

* Added basic LRU cache including Add Get Remove Contains ContainsOrAdd Clear

* wording changes on comments

* removed exported var's in strut as they are not required

* Added README

* README updates

* corrected ID on commands

* rm line :D

* merged in origin/cache

* linter fixes (gofmt)

* Added basic cache lookup to events

* swapped to mutex over rwmutex updated comments

* unexported getNewest & getOldest

* unexported getNewest & getOldest

* Updated comments and cited references in source

* updated comments

* WIP

* Migrated exchange WithdrawFiat wrapper to new struct response

* Migrated exchange WithdrawFiat wrapper to new struct response

* started work on bank management

* Added exchange level banking details back with migration to banking package

* Removed broken tests for now

* Added validation to bank accounts

* removed duplicate bank details from withdraw struct

* Test coverage increased

* gofmt

* merged upstream/master with clean up

* First pass at adding command line linking to gctcli

* added validation for crypto address, added gctcli support to retreive previous withdrawal requests

* general cleanup

* general cleanup

* reordered imports

* Increased test coverage moved to database sublogger

* Pass incorrect currency no longer return error from c.CheckBankAccountConfig

* remove TestMain() for now as other tests in this package will need to be reworked

* Happy little race car

* Reverted to upstream tests

* Added test covarege for validation method, corrected response on cli protobuf

* query clean up and removal of duplicated code

* cleaned up queries into singlem ethod increased test coverage

* Migrated international fund withdraw to new exchange response and added cache size override

* Migrated international fund withdraw to new exchange response and added cache size override

* Extended gctcli commands

* lowered default cache to 25

* small code clean up

* added get by date method

* add returned error

* cli commands cleaing return error on nil results to fix out of bounds

* merged write & read helpers into one for test coverage and increased engine/withdraw test coverage

* gofmt

* Added test coverage for valid ID

* removed unused param

* converted to use timestamp package from protobuf

* gofmt

* use built in RFC3339 timestamp

* remove setting of CreatedAt & UpdatedAt and allow ORm to take care of it

* also use ptype on byid

* code flow improvements

* remove inverse conditional check and linters run

* removed test data

* removed comment

* removed comment

* also write failures to database for auditing

* converted to use default time for start & end

* Default to time.Now() minus 30 days

* Default to time.Now() minus 30 days

* small code clean up

* fixed missing semicolon on migrations, code clean up

* updated sqlite migrations

* Added additonal check for exchange level bank account if global is not found

* case sensativity fix for currency names

* use correct compare

* test coverage fixed

* removed space

* return pointer to banking.Account

* return pointer to banking.Account

* added else check back to validate()

* Added empty string as default to migration over NULL due to retrivial of data
2020-02-26 15:45:13 +11:00

192 lines
4.7 KiB
Go

package engine
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/portfolio/banking"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
const (
exchangeName = "BTC Markets"
bankAccountID = "test-bank-01"
)
var (
settings = Settings{
ConfigFile: filepath.Join("..", "testdata", "configtest.json"),
EnableDryRun: true,
DataDir: filepath.Join("..", "testdata", "gocryptotrader"),
Verbose: false,
EnableGRPC: false,
EnableDeprecatedRPC: false,
EnableWebsocketRPC: false,
}
)
func setupEngine() (err error) {
Bot, err = NewFromSettings(&settings)
if err != nil {
return err
}
return Bot.Start()
}
func cleanup() {
err := os.RemoveAll(settings.DataDir)
if err != nil {
fmt.Printf("Clean up failed to remove file: %v manual removal may be required", err)
}
}
func TestSubmitWithdrawal(t *testing.T) {
err := setupEngine()
if err != nil {
t.Fatal(err)
}
banking.Accounts = append(banking.Accounts,
banking.Account{
Enabled: true,
ID: "test-bank-01",
BankName: "Test Bank",
BankAddress: "42 Bank Street",
BankPostalCode: "13337",
BankPostalCity: "Satoshiville",
BankCountry: "Japan",
AccountName: "Satoshi Nakamoto",
AccountNumber: "0234",
BSBNumber: "123456",
SWIFTCode: "91272837",
IBAN: "98218738671897",
SupportedCurrencies: "AUD,USD",
SupportedExchanges: exchangeName,
},
)
bank, err := banking.GetBankAccountByID(bankAccountID)
if err != nil {
t.Fatal(err)
}
req := &withdraw.Request{
Exchange: exchangeName,
Currency: currency.AUD,
Description: exchangeName,
Amount: 1.0,
Type: 1,
Fiat: &withdraw.FiatRequest{
Bank: bank,
},
}
_, err = SubmitWithdrawal(exchangeName, req)
if err != nil {
t.Fatal(err)
}
_, err = SubmitWithdrawal(exchangeName, nil)
if err != nil {
if err.Error() != withdraw.ErrRequestCannotBeNil.Error() {
t.Fatal(err)
}
}
cleanup()
}
func TestWithdrawEventByID(t *testing.T) {
tempResp := &withdraw.Response{
ID: withdraw.DryRunID,
}
_, err := WithdrawalEventByID(withdraw.DryRunID.String())
if err != nil {
if err.Error() != fmt.Errorf(ErrWithdrawRequestNotFound, withdraw.DryRunID.String()).Error() {
t.Fatal(err)
}
}
withdraw.Cache.Add(withdraw.DryRunID.String(), tempResp)
v, err := WithdrawalEventByID(withdraw.DryRunID.String())
if err != nil {
if err != fmt.Errorf(ErrWithdrawRequestNotFound, withdraw.DryRunID.String()) {
t.Fatal(err)
}
}
if v == nil {
t.Fatal("expected WithdrawalEventByID() to return data from cache")
}
}
func TestWithdrawalEventByExchange(t *testing.T) {
_, err := WithdrawalEventByExchange(exchangeName, 1)
if err == nil {
t.Fatal(err)
}
}
func TestWithdrawEventByDate(t *testing.T) {
_, err := WithdrawEventByDate(exchangeName, time.Now(), time.Now(), 1)
if err == nil {
t.Fatal(err)
}
}
func TestWithdrawalEventByExchangeID(t *testing.T) {
_, err := WithdrawalEventByExchangeID(exchangeName, exchangeName)
if err == nil {
t.Fatal(err)
}
}
func TestParseEvents(t *testing.T) {
var testData []*withdraw.Response
for x := 0; x < 5; x++ {
test := fmt.Sprintf("test-%v", x)
resp := &withdraw.Response{
ID: withdraw.DryRunID,
Exchange: &withdraw.ExchangeResponse{
Name: test,
ID: test,
Status: test,
},
RequestDetails: &withdraw.Request{
Exchange: test,
Description: test,
Amount: 1.0,
},
}
if x%2 == 0 {
resp.RequestDetails.Currency = currency.AUD
resp.RequestDetails.Type = 1
resp.RequestDetails.Fiat = new(withdraw.FiatRequest)
resp.RequestDetails.Fiat.Bank = new(banking.Account)
} else {
resp.RequestDetails.Currency = currency.BTC
resp.RequestDetails.Type = 0
resp.RequestDetails.Crypto = new(withdraw.CryptoRequest)
resp.RequestDetails.Crypto.Address = test
resp.RequestDetails.Crypto.FeeAmount = 0
resp.RequestDetails.Crypto.AddressTag = test
}
testData = append(testData, resp)
}
v := parseMultipleEvents(testData)
if reflect.TypeOf(v).String() != "*gctrpc.WithdrawalEventsByExchangeResponse" {
t.Fatal("expected type to be *gctrpc.WithdrawalEventsByExchangeResponse")
}
v = parseSingleEvents(testData[0])
if reflect.TypeOf(v).String() != "*gctrpc.WithdrawalEventsByExchangeResponse" {
t.Fatal("expected type to be *gctrpc.WithdrawalEventsByExchangeResponse")
}
v = parseSingleEvents(testData[1])
if v.Event[0].Request.Type != 0 {
t.Fatal("Expected second entry in slice to return a Request.Type of Crypto")
}
}