Skip to content

Commit

Permalink
SQS-375 | Unit test createFormattedLimitOrder
Browse files Browse the repository at this point in the history
Requested changes: cover test cases with different addresses
  • Loading branch information
deividaspetraitis committed Sep 9, 2024
1 parent be65040 commit 4e1e945
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions orderbook/usecase/orderbook_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,21 +616,23 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
}

testCases := []struct {
name string
poolID uint64
order orderbookdomain.Order
quoteAsset orderbookdomain.Asset
baseAsset orderbookdomain.Asset
setupMocks func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock)
expectedError error
expectedOrder orderbookdomain.LimitOrder
name string
poolID uint64
order orderbookdomain.Order
quoteAsset orderbookdomain.Asset
baseAsset orderbookdomain.Asset
orderbookAddress string
setupMocks func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock)
expectedError error
expectedOrder orderbookdomain.LimitOrder
}{
{
name: "tick not found",
order: orderbookdomain.Order{
TickId: 99, // Non-existent tick ID
},
expectedError: &types.TickForOrderbookNotFoundError{},
orderbookAddress: "osmo10dl92ghwn3v44pd8w24c3htqn2mj29549zcsn06usr56ng9ppp0qe6wd0r",
expectedError: &types.TickForOrderbookNotFoundError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(orderbookdomain.OrderbookTick{}, false)
},
Expand All @@ -640,7 +642,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
order: orderbookdomain.Order{
Quantity: "invalid", // Invalid quantity
},
expectedError: &types.ParsingQuantityError{},
orderbookAddress: "osmo1xvmtylht48gyvwe2s5rf3w6kn5g9rc4s0da0v0md82t9ldx447gsk07thg",
expectedError: &types.ParsingQuantityError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("6431", 935, "ask"), true)
},
Expand All @@ -653,7 +656,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
Etas: "500",
ClaimBounty: "10",
},
expectedError: &types.ParsingQuantityError{},
orderbookAddress: "osmo1rummy6vy4pfm82ctzmz4rr6fxgk0y4jf8h5s7zsadr2znwtuvq7slvl7p4",
expectedError: &types.ParsingQuantityError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("500", 100, "bid"), true)
},
Expand All @@ -664,7 +668,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
Quantity: "1000",
PlacedQuantity: "invalid", // Invalid placed quantity
},
expectedError: &types.ParsingPlacedQuantityError{},
orderbookAddress: "osmo1pwnxmmynz4esx79qv60cshhxkuu0glmzltsaykhccnq7jmj7tvsqdumey8",
expectedError: &types.ParsingPlacedQuantityError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("813", 1331, "bid"), true)
},
Expand All @@ -677,7 +682,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
Etas: "500",
ClaimBounty: "10",
},
expectedError: &types.ParsingPlacedQuantityError{},
orderbookAddress: "osmo1z6h6etav6mfljq66vej7eqwsu4kummg9dfkvs969syw09fm0592s3fwgcs",
expectedError: &types.ParsingPlacedQuantityError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("500", 100, "bid"), true)
},
Expand All @@ -688,7 +694,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
Quantity: "1000",
PlacedQuantity: "0", // division by zero
},
expectedError: &types.InvalidPlacedQuantityError{},
orderbookAddress: "osmo1w8jm03vws7h448yvh83utd8p43j02npydy2jll0r0k7f6w7hjspsvw2u42",
expectedError: &types.InvalidPlacedQuantityError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("813", 1331, "bid"), true)
},
Expand All @@ -699,7 +706,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
Quantity: "931",
PlacedQuantity: "183",
},
expectedError: &types.GettingSpotPriceScalingFactorError{},
orderbookAddress: "osmo197hxw89l3gqn5ake3l5as0zh2ls6e52ata2sgq80lep0854dwe5sstljsp",
expectedError: &types.GettingSpotPriceScalingFactorError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("130", 13, "ask"), true)
tokensusecase.GetSpotPriceScalingFactorByDenomFunc = s.GetSpotPriceScalingFactorByDenomFunc(1, assert.AnError)
Expand All @@ -712,7 +720,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
PlacedQuantity: "131",
OrderDirection: "bid",
},
expectedError: &types.ParsingTickValuesError{},
orderbookAddress: "osmo1s552kx03vsr7ha5ck0k9tmg74gn4w72fmmjcqgr4ky3wf96wwpcqlg7vn9",
expectedError: &types.ParsingTickValuesError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("invalid", 13, "bid"), true)
},
Expand All @@ -724,7 +733,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
PlacedQuantity: "131",
OrderDirection: "ask",
},
expectedError: &types.ParsingTickValuesError{},
orderbookAddress: "osmo1yuz6952hrcx0hadq4mgg6fq3t04d4kxhzwsfezlvvsvhq053qyys5udd8z",
expectedError: &types.ParsingTickValuesError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("invalid", 1, "ask"), true)
},
Expand All @@ -736,7 +746,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
PlacedQuantity: "153",
OrderDirection: "bid",
},
expectedError: &types.ParsingUnrealizedCancelsError{},
orderbookAddress: "osmo1apmfjhycfh4cyvc7e6px4vtfwhnl5k4l0ssjq9el4rqx8kxzh2mq5gm3j9",
expectedError: &types.ParsingUnrealizedCancelsError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("15", 0, "bid"), true)
},
Expand All @@ -748,7 +759,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
PlacedQuantity: "313",
OrderDirection: "ask",
},
expectedError: &types.ParsingUnrealizedCancelsError{},
orderbookAddress: "osmo17qvca7z822w5hy6jxzvaut46k44tlyk4fshx9aklkzq6prze4s9q73u4wz",
expectedError: &types.ParsingUnrealizedCancelsError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("13", 0, "ask"), true)
},
Expand All @@ -761,7 +773,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
OrderDirection: "bid",
Etas: "invalid", // Invalid ETAs
},
expectedError: &types.ParsingEtasError{},
orderbookAddress: "osmo1dkqnzv7r5wgq08yaj7cxpqy766mwneec2z2agke2l59x7qxff5sqzd2y5l",
expectedError: &types.ParsingEtasError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("386", 830, "bid"), true)
},
Expand All @@ -775,7 +788,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
Etas: overflowDecStr(), // overflow value for ETAs
ClaimBounty: "10",
},
expectedError: &types.ParsingEtasError{},
orderbookAddress: "osmo1nkt9lwky3l3gnrdjw075u557fhzxn9ke085uxnxvtkpj6kz2asrqkd65ra",
expectedError: &types.ParsingEtasError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("500", 100, "bid"), true)
},
Expand All @@ -789,7 +803,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
OrderDirection: "ask",
Etas: "100",
},
expectedError: &types.ConvertingTickToPriceError{},
orderbookAddress: "osmo1nzpy57uftd877avsgfqjnqtsg5jhnzt8uv8mmytnku7lt76qa4lqds80nn",
expectedError: &types.ConvertingTickToPriceError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("190", 150, "ask"), true)
},
Expand All @@ -804,7 +819,8 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
Etas: "100",
PlacedAt: "invalid", // Invalid timestamp
},
expectedError: &types.ParsingPlacedAtError{},
orderbookAddress: "osmo1ewuvnvtvh5jrcve9v8txr9eqnnq9x9vq82ujct53yzt2jpc8usjsyx72sr",
expectedError: &types.ParsingPlacedAtError{},
setupMocks: func(orderbookrepository *mocks.OrderbookRepositoryMock, tokensusecase *mocks.TokensUsecaseMock) {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("100", 100, "ask"), true)
tokensusecase.GetSpotPriceScalingFactorByDenomFunc = s.GetSpotPriceScalingFactorByDenomFunc(10, nil)
Expand All @@ -817,8 +833,9 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
orderbookrepository.GetTickByIDFunc = s.GetTickByIDFunc(s.NewTick("500", 100, "bid"), true)
tokensusecase.GetSpotPriceScalingFactorByDenomFunc = s.GetSpotPriceScalingFactorByDenomFunc(1, nil)
},
expectedError: nil,
expectedOrder: s.NewLimitOrder().LimitOrder,
orderbookAddress: "osmo1kfct7fcu3qqc9jlxeku873p7t5vucfzw5ujn0dh97hypg24t2w6qe9q5zs",
expectedError: nil,
expectedOrder: s.NewLimitOrder().WithOrderbookAddress("osmo1kfct7fcu3qqc9jlxeku873p7t5vucfzw5ujn0dh97hypg24t2w6qe9q5zs").LimitOrder,
},
}

Expand All @@ -841,7 +858,7 @@ func (s *OrderbookUsecaseTestSuite) TestCreateFormattedLimitOrder() {
)

// Call the method under test
result, err := usecase.CreateFormattedLimitOrder(tc.poolID, tc.order, tc.quoteAsset, tc.baseAsset, "someOrderbookAddress")
result, err := usecase.CreateFormattedLimitOrder(tc.poolID, tc.order, tc.quoteAsset, tc.baseAsset, tc.orderbookAddress)

// Assert the results
if tc.expectedError != nil {
Expand Down

0 comments on commit 4e1e945

Please sign in to comment.