From 4e1e9457636004e1f5bf701246a10526abad37a4 Mon Sep 17 00:00:00 2001 From: Deividas Petraitis Date: Mon, 9 Sep 2024 12:07:01 +0300 Subject: [PATCH] SQS-375 | Unit test createFormattedLimitOrder Requested changes: cover test cases with different addresses --- orderbook/usecase/orderbook_usecase_test.go | 69 +++++++++++++-------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/orderbook/usecase/orderbook_usecase_test.go b/orderbook/usecase/orderbook_usecase_test.go index e963476e8..40b4b76f9 100644 --- a/orderbook/usecase/orderbook_usecase_test.go +++ b/orderbook/usecase/orderbook_usecase_test.go @@ -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) }, @@ -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) }, @@ -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) }, @@ -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) }, @@ -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) }, @@ -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) }, @@ -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) @@ -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) }, @@ -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) }, @@ -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) }, @@ -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) }, @@ -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) }, @@ -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) }, @@ -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) }, @@ -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) @@ -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, }, } @@ -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 {