mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 17:13:51 +00:00
173 lines
5.4 KiB
Go
173 lines
5.4 KiB
Go
package ftx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
)
|
|
|
|
func Test_rawResponse_toSubscribedResp(t *testing.T) {
|
|
input := `{"type": "subscribed", "channel": "orderbook", "market": "BTC/USDT"}`
|
|
var m rawResponse
|
|
assert.NoError(t, json.Unmarshal([]byte(input), &m))
|
|
r := m.toSubscribedResp()
|
|
assert.Equal(t, subscribedRespType, r.Type)
|
|
assert.Equal(t, orderbook, r.Channel)
|
|
assert.Equal(t, "BTC/USDT", r.Market)
|
|
}
|
|
|
|
func Test_rawResponse_toDataResponse(t *testing.T) {
|
|
f, err := ioutil.ReadFile("./orderbook_snapshot.json")
|
|
assert.NoError(t, err)
|
|
var m rawResponse
|
|
assert.NoError(t, json.Unmarshal(f, &m))
|
|
r, err := m.toOrderBookResponse()
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, partialRespType, r.Type)
|
|
assert.Equal(t, orderbook, r.Channel)
|
|
assert.Equal(t, "BTC/USDT", r.Market)
|
|
assert.Equal(t, int64(1614520368), r.Timestamp.Unix())
|
|
assert.Equal(t, uint32(2150525410), r.Checksum)
|
|
assert.Len(t, r.Bids, 100)
|
|
assert.Equal(t, []json.Number{"44555.0", "3.3968"}, r.Bids[0])
|
|
assert.Equal(t, []json.Number{"44554.0", "0.0561"}, r.Bids[1])
|
|
assert.Len(t, r.Asks, 100)
|
|
assert.Equal(t, []json.Number{"44574.0", "0.4591"}, r.Asks[0])
|
|
assert.Equal(t, []json.Number{"44579.0", "0.15"}, r.Asks[1])
|
|
}
|
|
|
|
func Test_orderBookResponse_toGlobalOrderBook(t *testing.T) {
|
|
f, err := ioutil.ReadFile("./orderbook_snapshot.json")
|
|
assert.NoError(t, err)
|
|
var m rawResponse
|
|
assert.NoError(t, json.Unmarshal(f, &m))
|
|
r, err := m.toOrderBookResponse()
|
|
assert.NoError(t, err)
|
|
|
|
b, err := toGlobalOrderBook(r)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "BTC/USDT", b.Symbol)
|
|
isValid, err := b.IsValid()
|
|
assert.True(t, isValid)
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, b.Bids, 100)
|
|
assert.Equal(t, types.PriceVolume{
|
|
Price: fixedpoint.MustNewFromString("44555.0"),
|
|
Volume: fixedpoint.MustNewFromString("3.3968"),
|
|
}, b.Bids[0])
|
|
assert.Equal(t, types.PriceVolume{
|
|
Price: fixedpoint.MustNewFromString("44222.0"),
|
|
Volume: fixedpoint.MustNewFromString("0.0002"),
|
|
}, b.Bids[99])
|
|
|
|
assert.Len(t, b.Asks, 100)
|
|
assert.Equal(t, types.PriceVolume{
|
|
Price: fixedpoint.MustNewFromString("44574.0"),
|
|
Volume: fixedpoint.MustNewFromString("0.4591"),
|
|
}, b.Asks[0])
|
|
assert.Equal(t, types.PriceVolume{
|
|
Price: fixedpoint.MustNewFromString("45010.0"),
|
|
Volume: fixedpoint.MustNewFromString("0.0003"),
|
|
}, b.Asks[99])
|
|
|
|
}
|
|
|
|
func Test_checksumString(t *testing.T) {
|
|
type args struct {
|
|
bids [][]json.Number
|
|
asks [][]json.Number
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
}{
|
|
{
|
|
name: "more bids",
|
|
args: args{
|
|
bids: [][]json.Number{{"5000.5", "10"}, {"4995.0", "5"}},
|
|
asks: [][]json.Number{{"5001.0", "6"}},
|
|
},
|
|
want: "5000.5:10:5001.0:6:4995.0:5",
|
|
},
|
|
{
|
|
name: "lengths of bids and asks are the same",
|
|
args: args{
|
|
bids: [][]json.Number{{"5000.5", "10"}, {"4995.0", "5"}},
|
|
asks: [][]json.Number{{"5001.0", "6"}, {"5002.0", "7"}},
|
|
},
|
|
want: "5000.5:10:5001.0:6:4995.0:5:5002.0:7",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := checksumString(tt.args.bids, tt.args.asks); got != tt.want {
|
|
t.Errorf("checksumString() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_orderBookResponse_verifyChecksum(t *testing.T) {
|
|
for _, file := range []string{"./orderbook_snapshot.json"} {
|
|
f, err := ioutil.ReadFile(file)
|
|
assert.NoError(t, err)
|
|
var m rawResponse
|
|
assert.NoError(t, json.Unmarshal(f, &m))
|
|
r, err := m.toOrderBookResponse()
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, r.verifyChecksum(), "filename: "+file)
|
|
}
|
|
}
|
|
|
|
func Test_removePrice(t *testing.T) {
|
|
pairs := [][]json.Number{{"123.99", "2.0"}, {"2234.12", "3.1"}}
|
|
assert.Equal(t, pairs, removePrice(pairs, "99333"))
|
|
|
|
pairs = removePrice(pairs, "2234.12")
|
|
assert.Equal(t, [][]json.Number{{"123.99", "2.0"}}, pairs)
|
|
assert.Equal(t, [][]json.Number{}, removePrice(pairs, "123.99"))
|
|
}
|
|
|
|
func Test_orderBookResponse_update(t *testing.T) {
|
|
ob := &orderBookResponse{Bids: nil, Asks: nil}
|
|
|
|
ob.update(orderBookResponse{
|
|
Bids: [][]json.Number{{"1.0", "0"}, {"10.0", "1"}, {"11.0", "1"}},
|
|
Asks: [][]json.Number{{"1.0", "1"}},
|
|
})
|
|
assert.Equal(t, [][]json.Number{{"11.0", "1"}, {"10.0", "1"}}, ob.Bids)
|
|
assert.Equal(t, [][]json.Number{{"1.0", "1"}}, ob.Asks)
|
|
ob.update(orderBookResponse{
|
|
Bids: [][]json.Number{{"9.0", "1"}, {"12.0", "1"}, {"10.5", "1"}},
|
|
Asks: [][]json.Number{{"1.0", "0"}},
|
|
})
|
|
assert.Equal(t, [][]json.Number{{"12.0", "1"}, {"11.0", "1"}, {"10.5", "1"}, {"10.0", "1"}, {"9.0", "1"}}, ob.Bids)
|
|
assert.Equal(t, [][]json.Number{}, ob.Asks)
|
|
|
|
// remove them
|
|
ob.update(orderBookResponse{
|
|
Bids: [][]json.Number{{"9.0", "0"}, {"12.0", "0"}, {"10.5", "0"}},
|
|
Asks: [][]json.Number{{"9.0", "1"}, {"12.0", "1"}, {"10.5", "1"}},
|
|
})
|
|
assert.Equal(t, [][]json.Number{{"11.0", "1"}, {"10.0", "1"}}, ob.Bids)
|
|
assert.Equal(t, [][]json.Number{{"9.0", "1"}, {"10.5", "1"}, {"12.0", "1"}}, ob.Asks)
|
|
}
|
|
|
|
func Test_insertAt(t *testing.T) {
|
|
r := insertAt([][]json.Number{{"1.2", "2"}, {"1.4", "2"}}, 1, []json.Number{"1.3", "2"})
|
|
assert.Equal(t, [][]json.Number{{"1.2", "2"}, {"1.3", "2"}, {"1.4", "2"}}, r)
|
|
|
|
r = insertAt([][]json.Number{{"1.2", "2"}, {"1.4", "2"}}, 0, []json.Number{"1.1", "2"})
|
|
assert.Equal(t, [][]json.Number{{"1.1", "2"}, {"1.2", "2"}, {"1.4", "2"}}, r)
|
|
|
|
r = insertAt([][]json.Number{{"1.2", "2"}, {"1.4", "2"}}, 2, []json.Number{"1.5", "2"})
|
|
assert.Equal(t, [][]json.Number{{"1.2", "2"}, {"1.4", "2"}, {"1.5", "2"}}, r)
|
|
}
|