add new test helper to create balance map objects

Signed-off-by: c9s <yoanlin93@gmail.com>
This commit is contained in:
c9s 2024-10-04 23:32:11 +08:00
parent 14fa561f6e
commit 8f0d58aee9
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 55 additions and 12 deletions

View File

@ -215,19 +215,19 @@ func Test_usdFiatBalances(t *testing.T) {
}{
{
args: args{
balances: types.BalanceMap{
"USDC": types.Balance{Currency: "USDC", Available: Number(70.0 + 80.0)},
"USDT": types.Balance{Currency: "USDT", Available: Number(100.0)},
"BTC": types.Balance{Currency: "BTC", Available: Number(0.01)},
},
},
wantFiats: types.BalanceMap{
"USDC": types.Balance{Currency: "USDC", Available: Number(70.0 + 80.0)},
"USDT": types.Balance{Currency: "USDT", Available: Number(100.0)},
},
wantRest: types.BalanceMap{
"BTC": types.Balance{Currency: "BTC", Available: Number(0.01)},
balances: BalancesFromText(`
USDC, 150.0
USDT, 100.0
BTC, 0.01
`),
},
wantFiats: BalancesFromText(`
USDC, 150.0
USDT, 100.0
`),
wantRest: BalancesFromText(`
BTC, 0.01
`),
},
}
for _, tt := range tests {

View File

@ -0,0 +1,43 @@
package testhelper
import (
"strings"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func BalancesFromText(str string) types.BalanceMap {
balances := make(types.BalanceMap)
lines := strings.Split(str, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
cols := strings.SplitN(line, ",", 2)
if len(cols) < 2 {
panic("column length should be 2")
}
currency := strings.TrimSpace(cols[0])
available := fixedpoint.MustNewFromString(strings.TrimSpace(cols[1]))
balances[currency] = Balance(currency, available)
}
return balances
}
// Balance returns a balance object with the given currency and available amount
func Balance(currency string, available fixedpoint.Value) types.Balance {
return types.Balance{
Currency: currency,
Available: available,
Locked: fixedpoint.Zero,
Borrowed: fixedpoint.Zero,
Interest: fixedpoint.Zero,
NetAsset: fixedpoint.Zero,
MaxWithdrawAmount: fixedpoint.Zero,
}
}