From 6004114696de7da34f5d1893fd13aaaf176da39d Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 28 Oct 2024 14:22:34 +0800 Subject: [PATCH] add test helper for price side quantity assertion --- pkg/strategy/liquiditymaker/generator_test.go | 27 ++++++++++ pkg/testing/testhelper/assert_priceside.go | 49 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/pkg/strategy/liquiditymaker/generator_test.go b/pkg/strategy/liquiditymaker/generator_test.go index 995f33bd7..4b67ab91d 100644 --- a/pkg/strategy/liquiditymaker/generator_test.go +++ b/pkg/strategy/liquiditymaker/generator_test.go @@ -111,4 +111,31 @@ func TestLiquidityOrderGenerator(t *testing.T) { {Side: types.SideTypeBuy, Price: Number("1.9600"), Quantity: Number("620.54")}, }, orders[28:30]) }) + + t.Run("bid orders 2", func(t *testing.T) { + orders := g.Generate(types.SideTypeBuy, Number(1000.0), Number(0.29), Number(0.20), 30, scale) + assert.Len(t, orders, 30) + + totalQuoteQuantity := fixedpoint.NewFromInt(0) + for _, o := range orders { + totalQuoteQuantity = totalQuoteQuantity.Add(o.Quantity.Mul(o.Price)) + } + assert.InDelta(t, 1000.0, totalQuoteQuantity.Float64(), 1.0) + + AssertOrdersPriceSideQuantityFromText(t, ` + BUY,0.2899,65.41 + BUY,0.2868,68.61 + BUY,0.2837,71.97 + BUY,0.2806,75.5 + BUY,0.2775,79.2 + BUY,0.2744,83.07 + BUY,0.2713,87.14 + BUY,0.2682,91.41 + BUY,0.2651,95.88 + BUY,0.262,100.58 + BUY,0.2589,105.5 + BUY,0.2558,110.67 + BUY,0.2527,116.09 + `, orders[0:13]) + }) } diff --git a/pkg/testing/testhelper/assert_priceside.go b/pkg/testing/testhelper/assert_priceside.go index 0d7f74df1..592ed8c29 100644 --- a/pkg/testing/testhelper/assert_priceside.go +++ b/pkg/testing/testhelper/assert_priceside.go @@ -1,6 +1,8 @@ package testhelper import ( + "fmt" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -28,6 +30,53 @@ type PriceSideQuantityAssert struct { Quantity fixedpoint.Value } +func ParsePriceSideQuantityAssertions(text string) []PriceSideQuantityAssert { + var asserts []PriceSideQuantityAssert + lines := strings.Split(text, "\n") + for _, line := range lines { + line = strings.TrimSpace(line) + if line == "" { + continue + } + + cols := strings.SplitN(line, ",", 3) + if len(cols) < 3 { + panic(fmt.Errorf("column length should be 3, got %d", len(cols))) + } + + side := strings.TrimSpace(cols[0]) + price := fixedpoint.MustNewFromString(strings.TrimSpace(cols[1])) + quantity := fixedpoint.MustNewFromString(strings.TrimSpace(cols[2])) + asserts = append(asserts, PriceSideQuantityAssert{ + Price: price, + Side: types.SideType(side), + Quantity: quantity, + }) + } + + return asserts +} + +func AssertOrdersPriceSideQuantityFromText( + t *testing.T, text string, orders []types.SubmitOrder, +) { + asserts := ParsePriceSideQuantityAssertions(text) + assert.Equalf(t, len(asserts), len(orders), "expecting %d orders", len(asserts)) + actualInText := "Actual Orders:\n" + for i, a := range asserts { + order := orders[i] + assert.Equalf(t, a.Price.Float64(), order.Price.Float64(), "order #%d price should be %f", i+1, a.Price.Float64()) + assert.Equalf(t, a.Quantity.Float64(), order.Quantity.Float64(), "order #%d quantity should be %f", i+1, a.Quantity.Float64()) + assert.Equalf(t, a.Side, orders[i].Side, "order at price %f should be %s", a.Price.Float64(), a.Side) + + actualInText += fmt.Sprintf("%s,%s,%s\n", order.Side, order.Price.String(), order.Quantity.String()) + } + + if t.Failed() { + t.Log(actualInText) + } +} + // AssertOrdersPriceSide asserts the orders with the given price and side (slice) func AssertOrdersPriceSideQuantity( t *testing.T, asserts []PriceSideQuantityAssert, orders []types.SubmitOrder,