Files
gocryptotrader/exchanges/account/account_test.go
Yordan Miladinov 9e745d20a3 Account: Add account.Balance.Available() method (#777)
* add account.Balance.Available() method that returns the amount of an asset not locked/held

* account: comment Balance.Available()

* account: test Balance.Available()
2021-09-09 09:35:10 +10:00

163 lines
2.9 KiB
Go

package account
import (
"sync"
"testing"
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/dispatch"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
)
func TestHoldings(t *testing.T) {
err := dispatch.Start(dispatch.DefaultMaxWorkers, dispatch.DefaultJobsLimit)
if err != nil {
t.Fatal(err)
}
err = Process(nil)
if err == nil {
t.Error("error cannot be nil")
}
err = Process(&Holdings{})
if err == nil {
t.Error("error cannot be nil")
}
holdings := Holdings{
Exchange: "Test",
}
err = Process(&holdings)
if err != nil {
t.Error(err)
}
err = Process(&Holdings{
Exchange: "Test",
Accounts: []SubAccount{{
AssetType: asset.Spot,
ID: "1337",
Currencies: []Balance{
{
CurrencyName: currency.BTC,
TotalValue: 100,
Hold: 20,
},
},
}},
})
if err != nil {
t.Error(err)
}
_, err = GetHoldings("", asset.Spot)
if err == nil {
t.Error("error cannot be nil")
}
_, err = GetHoldings("bla", asset.Spot)
if err == nil {
t.Error("error cannot be nil")
}
_, err = GetHoldings("bla", asset.Item("hi"))
if err == nil {
t.Error("error cannot be nil since an invalid asset type is provided")
}
u, err := GetHoldings("Test", asset.Spot)
if err != nil {
t.Error(err)
}
if u.Accounts[0].ID != "1337" {
t.Errorf("expecting 1337 but received %s", u.Accounts[0].ID)
}
if u.Accounts[0].Currencies[0].CurrencyName != currency.BTC {
t.Errorf("expecting BTC but received %s",
u.Accounts[0].Currencies[0].CurrencyName)
}
if u.Accounts[0].Currencies[0].TotalValue != 100 {
t.Errorf("expecting 100 but received %f",
u.Accounts[0].Currencies[0].TotalValue)
}
if u.Accounts[0].Currencies[0].Hold != 20 {
t.Errorf("expecting 20 but received %f",
u.Accounts[0].Currencies[0].Hold)
}
_, err = SubscribeToExchangeAccount("nonsense")
if err == nil {
t.Fatal("error cannot be nil")
}
p, err := SubscribeToExchangeAccount("Test")
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func(p dispatch.Pipe, wg *sync.WaitGroup) {
for i := 0; i < 2; i++ {
c := time.NewTimer(time.Second)
select {
case <-p.C:
case <-c.C:
}
}
wg.Done()
}(p, &wg)
err = Process(&Holdings{
Exchange: "Test",
Accounts: []SubAccount{{
ID: "1337",
Currencies: []Balance{
{
CurrencyName: currency.BTC,
TotalValue: 100000,
Hold: 20,
},
},
}},
})
if err != nil {
t.Error(err)
}
wg.Wait()
}
func TestBalance_Available(t *testing.T) {
t.Parallel()
b := Balance{
CurrencyName: currency.BTC,
TotalValue: 16,
Hold: 0,
}
if have := b.Available(); have != 16 {
t.Errorf("have %f, want 16", have)
}
b.Hold = 8
if have := b.Available(); have != 8 {
t.Errorf("have %f, want 8", have)
}
b.Hold = 16
if have := b.Available(); have != 0 {
t.Errorf("have %f, want 0", have)
}
}