Skip to content

Commit

Permalink
BE-586 | Add tests for slices, orderbook packages
Browse files Browse the repository at this point in the history
  • Loading branch information
deividaspetraitis committed Oct 14, 2024
1 parent 3d1c9b6 commit c548460
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 1 deletion.
2 changes: 1 addition & 1 deletion domain/cosmos/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import (

cosmosClient "github.com/cosmos/cosmos-sdk/client"
txclient "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"

gogogrpc "github.com/cosmos/gogoproto/grpc"
)
Expand Down
110 changes: 110 additions & 0 deletions domain/orderbook/order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

orderbookdomain "github.com/osmosis-labs/sqs/domain/orderbook"

"github.com/osmosis-labs/osmosis/osmomath"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -75,3 +77,111 @@ func TestOrderStatus(t *testing.T) {
})
}
}

func TestOrdersByDirection(t *testing.T) {
testCases := []struct {
name string
orders orderbookdomain.Orders
direction string
expectedOrders orderbookdomain.Orders
}{
{
name: "Filter buy orders",
orders: orderbookdomain.Orders{
{OrderDirection: "buy", OrderId: 1},
{OrderDirection: "sell", OrderId: 2},
{OrderDirection: "buy", OrderId: 3},
},
direction: "buy",
expectedOrders: orderbookdomain.Orders{
{OrderDirection: "buy", OrderId: 1},
{OrderDirection: "buy", OrderId: 3},
},
},
{
name: "Filter sell orders",
orders: orderbookdomain.Orders{
{OrderDirection: "buy", OrderId: 1},
{OrderDirection: "sell", OrderId: 2},
{OrderDirection: "buy", OrderId: 3},
{OrderDirection: "sell", OrderId: 4},
},
direction: "sell",
expectedOrders: orderbookdomain.Orders{
{OrderDirection: "sell", OrderId: 2},
{OrderDirection: "sell", OrderId: 4},
},
},
{
name: "No matching orders",
orders: orderbookdomain.Orders{
{OrderDirection: "buy", OrderId: 1},
{OrderDirection: "buy", OrderId: 2},
},
direction: "sell",
expectedOrders: nil,
},
{
name: "Empty orders slice",
orders: orderbookdomain.Orders{},
direction: "buy",
expectedOrders: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := tc.orders.OrderByDirection(tc.direction)
assert.Equal(t, tc.expectedOrders, result)
})
}
}

func TestLimitOrder_IsClaimable(t *testing.T) {
tests := []struct {
name string
order orderbookdomain.LimitOrder
threshold osmomath.Dec
want bool
}{
{
name: "Fully filled order",
order: orderbookdomain.LimitOrder{
PercentFilled: osmomath.NewDec(1),
},
threshold: osmomath.NewDecWithPrec(4, 1), // 0.4
want: true,
},
{
name: "Partially filled order above threshold",
order: orderbookdomain.LimitOrder{
PercentFilled: osmomath.NewDecWithPrec(75, 2), // 0.75
},
threshold: osmomath.NewDecWithPrec(6, 1), // 0.6
want: true,
},
{
name: "Partially filled order below threshold",
order: orderbookdomain.LimitOrder{
PercentFilled: osmomath.NewDecWithPrec(85, 2), // 0.85
},
threshold: osmomath.NewDecWithPrec(9, 1), // 0.9
want: false,
},
{
name: "Unfilled order",
order: orderbookdomain.LimitOrder{
PercentFilled: osmomath.NewDec(0),
},
threshold: osmomath.NewDecWithPrec(1, 1), // 0.1
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.order.IsClaimable(tt.threshold)
assert.Equal(t, tt.want, got)
})
}
}
63 changes: 63 additions & 0 deletions domain/slices/slices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package slices_test

import (
"reflect"
"testing"

"github.com/osmosis-labs/sqs/domain/slices"
)

func TestSplit(t *testing.T) {
tests := []struct {
name string
input []int
size int
expected [][]int
}{
{
name: "empty slice",
input: []int{},
size: 3,
expected: nil,
},
{
name: "slice smaller than chunk size",
input: []int{1, 2},
size: 3,
expected: [][]int{{1, 2}},
},
{
name: "slice equal to chunk size",
input: []int{1, 2, 3},
size: 3,
expected: [][]int{{1, 2, 3}},
},
{
name: "slice larger than chunk size",
input: []int{1, 2, 3, 4, 5},
size: 2,
expected: [][]int{{1, 2}, {3, 4}, {5}},
},
{
name: "slice multiple of chunk size",
input: []int{1, 2, 3, 4, 5, 6},
size: 2,
expected: [][]int{{1, 2}, {3, 4}, {5, 6}},
},
{
name: "chunk size of 1",
input: []int{1, 2, 3},
size: 1,
expected: [][]int{{1}, {2}, {3}},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := slices.Split(tt.input, tt.size)
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("Split(%v, %d) = %v, want %v", tt.input, tt.size, result, tt.expected)
}
})
}
}

0 comments on commit c548460

Please sign in to comment.