From 275b16ee859cc3a05e0a0f33067dce1b44cf004a Mon Sep 17 00:00:00 2001 From: Andrii Date: Thu, 26 Dec 2024 16:45:50 +0200 Subject: [PATCH 01/12] Changed response to consostent for block provider --- .../rest/websockets/data_providers/blocks_provider.go | 9 +++++++-- .../websockets/data_providers/blocks_provider_test.go | 7 +++++-- engine/access/rest/websockets/models/base_message.go | 7 +++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/blocks_provider.go b/engine/access/rest/websockets/data_providers/blocks_provider.go index 6c09c4a623a..43c9fcadfab 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider.go @@ -70,8 +70,13 @@ func (p *BlocksDataProvider) Run() error { return subscription.HandleSubscription( p.subscription, subscription.HandleResponse(p.send, func(block *flow.Block) (interface{}, error) { - return &models.BlockMessageResponse{ - Block: block, + + return &models.BaseDataProvidersResponse{ + ID: p.ID().String(), + Topic: p.Topic(), + Data: &models.BlockMessageResponse{ + Block: block, + }, }, nil }), ) diff --git a/engine/access/rest/websockets/data_providers/blocks_provider_test.go b/engine/access/rest/websockets/data_providers/blocks_provider_test.go index 85136ae5819..cefd2c51aa9 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider_test.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider_test.go @@ -203,8 +203,11 @@ func (s *BlocksProviderSuite) requireBlock(v interface{}, expected interface{}) expectedBlock, ok := expected.(*flow.Block) require.True(s.T(), ok, "unexpected type: %T", v) - actualResponse, ok := v.(*models.BlockMessageResponse) + actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "unexpected response type: %T", v) - s.Require().Equal(expectedBlock, actualResponse.Block) + actualResponseBlock, ok := actualResponse.Data.(*models.BlockMessageResponse) + require.True(s.T(), ok, "unexpected response data type: %T", v) + + s.Require().Equal(expectedBlock, actualResponseBlock.Block) } diff --git a/engine/access/rest/websockets/models/base_message.go b/engine/access/rest/websockets/models/base_message.go index f56d62fda8f..5d9fe89a48d 100644 --- a/engine/access/rest/websockets/models/base_message.go +++ b/engine/access/rest/websockets/models/base_message.go @@ -11,3 +11,10 @@ type BaseMessageResponse struct { Success bool `json:"success"` // Indicates success or failure ErrorMessage string `json:"error_message,omitempty"` // Error message, if any } + +// BaseDataProvidersResponse represents a base structure for responses from subscriptions. +type BaseDataProvidersResponse struct { + ID string `json:"id"` // Unique subscription ID + Topic string `json:"topic"` // Topic of the subscription + Data interface{} `json:"data"` // Data that's being returned within a subscription. +} From 9d2a761dbea8475946872997ec0f684de4725331 Mon Sep 17 00:00:00 2001 From: Andrii Date: Fri, 27 Dec 2024 15:44:42 +0200 Subject: [PATCH 02/12] Changed responses to be consistant for all block data providers --- .../data_providers/block_digests_provider.go | 9 +++++++-- .../data_providers/block_digests_provider_test.go | 11 +++++++---- .../data_providers/block_headers_provider.go | 8 ++++++-- .../data_providers/block_headers_provider_test.go | 7 +++++-- .../rest/websockets/data_providers/blocks_provider.go | 1 - .../websockets/data_providers/blocks_provider_test.go | 4 ++-- 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/block_digests_provider.go b/engine/access/rest/websockets/data_providers/block_digests_provider.go index 80307be6b64..5716aab940b 100644 --- a/engine/access/rest/websockets/data_providers/block_digests_provider.go +++ b/engine/access/rest/websockets/data_providers/block_digests_provider.go @@ -61,9 +61,14 @@ func (p *BlockDigestsDataProvider) Run() error { return subscription.HandleSubscription( p.subscription, subscription.HandleResponse(p.send, func(block *flow.BlockDigest) (interface{}, error) { - return &models.BlockDigestMessageResponse{ - Block: block, + return &models.BaseDataProvidersResponse{ + ID: p.ID().String(), + Topic: p.Topic(), + Data: &models.BlockDigestMessageResponse{ + Block: block, + }, }, nil + }), ) } diff --git a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go index 975716c74af..975c1ce7fa8 100644 --- a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go @@ -126,10 +126,13 @@ func (s *BlocksProviderSuite) requireBlockDigest(v interface{}, expected interfa expectedBlock, ok := expected.(*flow.Block) require.True(s.T(), ok, "unexpected type: %T", v) - actualResponse, ok := v.(*models.BlockDigestMessageResponse) + actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "unexpected response type: %T", v) - s.Require().Equal(expectedBlock.Header.ID(), actualResponse.Block.ID()) - s.Require().Equal(expectedBlock.Header.Height, actualResponse.Block.Height) - s.Require().Equal(expectedBlock.Header.Timestamp, actualResponse.Block.Timestamp) + actualResponseData, ok := actualResponse.Data.(*models.BlockDigestMessageResponse) + require.True(s.T(), ok, "unexpected response data type: %T", v) + + s.Require().Equal(expectedBlock.Header.ID(), actualResponseData.Block.ID()) + s.Require().Equal(expectedBlock.Header.Height, actualResponseData.Block.Height) + s.Require().Equal(expectedBlock.Header.Timestamp, actualResponseData.Block.Timestamp) } diff --git a/engine/access/rest/websockets/data_providers/block_headers_provider.go b/engine/access/rest/websockets/data_providers/block_headers_provider.go index 4fddeb499f2..92d68a9402f 100644 --- a/engine/access/rest/websockets/data_providers/block_headers_provider.go +++ b/engine/access/rest/websockets/data_providers/block_headers_provider.go @@ -61,8 +61,12 @@ func (p *BlockHeadersDataProvider) Run() error { return subscription.HandleSubscription( p.subscription, subscription.HandleResponse(p.send, func(header *flow.Header) (interface{}, error) { - return &models.BlockHeaderMessageResponse{ - Header: header, + return &models.BaseDataProvidersResponse{ + ID: p.ID().String(), + Topic: p.Topic(), + Data: &models.BlockHeaderMessageResponse{ + Header: header, + }, }, nil }), ) diff --git a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go index b929a46d076..0439c27cfa6 100644 --- a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go @@ -126,8 +126,11 @@ func (s *BlockHeadersProviderSuite) requireBlockHeader(v interface{}, expected i expectedBlock, ok := expected.(*flow.Block) require.True(s.T(), ok, "unexpected type: %T", v) - actualResponse, ok := v.(*models.BlockHeaderMessageResponse) + actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "unexpected response type: %T", v) - s.Require().Equal(expectedBlock.Header, actualResponse.Header) + actualResponseData, ok := actualResponse.Data.(*models.BlockHeaderMessageResponse) + require.True(s.T(), ok, "unexpected response data type: %T", v) + + s.Require().Equal(expectedBlock.Header, actualResponseData.Header) } diff --git a/engine/access/rest/websockets/data_providers/blocks_provider.go b/engine/access/rest/websockets/data_providers/blocks_provider.go index 43c9fcadfab..fb7f46a78c0 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider.go @@ -70,7 +70,6 @@ func (p *BlocksDataProvider) Run() error { return subscription.HandleSubscription( p.subscription, subscription.HandleResponse(p.send, func(block *flow.Block) (interface{}, error) { - return &models.BaseDataProvidersResponse{ ID: p.ID().String(), Topic: p.Topic(), diff --git a/engine/access/rest/websockets/data_providers/blocks_provider_test.go b/engine/access/rest/websockets/data_providers/blocks_provider_test.go index cefd2c51aa9..89d15b44a89 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider_test.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider_test.go @@ -206,8 +206,8 @@ func (s *BlocksProviderSuite) requireBlock(v interface{}, expected interface{}) actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "unexpected response type: %T", v) - actualResponseBlock, ok := actualResponse.Data.(*models.BlockMessageResponse) + actualResponseData, ok := actualResponse.Data.(*models.BlockMessageResponse) require.True(s.T(), ok, "unexpected response data type: %T", v) - s.Require().Equal(expectedBlock, actualResponseBlock.Block) + s.Require().Equal(expectedBlock, actualResponseData.Block) } From aafcc4de2f3adb902caf9f1c75875c0079011f4d Mon Sep 17 00:00:00 2001 From: Andrii Date: Fri, 27 Dec 2024 15:53:27 +0200 Subject: [PATCH 03/12] Refactored response type of account statuses data provider --- .../account_statuses_provider.go | 21 ++++++++++++++----- .../account_statuses_provider_test.go | 19 ++++++++++------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider.go b/engine/access/rest/websockets/data_providers/account_statuses_provider.go index 396dcbc7b9a..99e0b85820e 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider.go @@ -116,11 +116,22 @@ func (p *AccountStatusesDataProvider) handleResponse() func(accountStatusesRespo return status.Errorf(codes.Internal, "message index already incremented to %d", messageIndex.Value()) } - p.send <- &models.AccountStatusesResponse{ - BlockID: accountStatusesResponse.BlockID.String(), - Height: strconv.FormatUint(accountStatusesResponse.Height, 10), - AccountEvents: accountStatusesResponse.AccountEvents, - MessageIndex: index, + //p.send <- &models.AccountStatusesResponse{ + // BlockID: accountStatusesResponse.BlockID.String(), + // Height: strconv.FormatUint(accountStatusesResponse.Height, 10), + // AccountEvents: accountStatusesResponse.AccountEvents, + // MessageIndex: index, + //} + + p.send <- &models.BaseDataProvidersResponse{ + ID: p.ID().String(), + Topic: p.Topic(), + Data: &models.AccountStatusesResponse{ + BlockID: accountStatusesResponse.BlockID.String(), + Height: strconv.FormatUint(accountStatusesResponse.Height, 10), + AccountEvents: accountStatusesResponse.AccountEvents, + MessageIndex: index, + }, } return nil diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go index 8f689ca034a..a7be006be6e 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go @@ -146,14 +146,17 @@ func (s *AccountStatusesProviderSuite) requireAccountStatuses( expectedAccountStatusesResponse, ok := expectedResponse.(backend.AccountStatusesResponse) require.True(s.T(), ok, "unexpected type: %T", expectedResponse) - actualResponse, ok := v.(*models.AccountStatusesResponse) - require.True(s.T(), ok, "Expected *models.AccountStatusesResponse, got %T", v) + actualResponse, ok := v.(*models.BaseDataProvidersResponse) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", v) - require.Equal(s.T(), expectedAccountStatusesResponse.BlockID.String(), actualResponse.BlockID) - require.Equal(s.T(), len(expectedAccountStatusesResponse.AccountEvents), len(actualResponse.AccountEvents)) + actualResponseData, ok := actualResponse.Data.(*models.AccountStatusesResponse) + require.True(s.T(), ok, "unexpected response data type: %T", v) + + require.Equal(s.T(), expectedAccountStatusesResponse.BlockID.String(), actualResponseData.BlockID) + require.Equal(s.T(), len(expectedAccountStatusesResponse.AccountEvents), len(actualResponseData.AccountEvents)) for key, expectedEvents := range expectedAccountStatusesResponse.AccountEvents { - actualEvents, ok := actualResponse.AccountEvents[key] + actualEvents, ok := actualResponseData.AccountEvents[key] require.True(s.T(), ok, "Missing key in actual AccountEvents: %s", key) s.Require().Equal(expectedEvents, actualEvents, "Mismatch for key: %s", key) @@ -249,9 +252,11 @@ func (s *AccountStatusesProviderSuite) TestMessageIndexAccountStatusesProviderRe var responses []*models.AccountStatusesResponse for i := 0; i < accountStatusesCount; i++ { res := <-send - accountStatusesRes, ok := res.(*models.AccountStatusesResponse) + accountStatusesRes, ok := res.(*models.BaseDataProvidersResponse) + s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) + accountStatusesResData, ok := accountStatusesRes.Data.(*models.AccountStatusesResponse) s.Require().True(ok, "Expected *models.AccountStatusesResponse, got %T", res) - responses = append(responses, accountStatusesRes) + responses = append(responses, accountStatusesResData) } // Verifying that indices are starting from 0 From 453f8d72bb09ddaf14e27c3d2d6c246c4d580281 Mon Sep 17 00:00:00 2001 From: Andrii Date: Fri, 27 Dec 2024 16:03:01 +0200 Subject: [PATCH 04/12] Refactored respone type of events daya provider --- .../data_providers/account_statuses_provider.go | 7 ------- .../account_statuses_provider_test.go | 2 ++ .../websockets/data_providers/events_provider.go | 16 ++++++++++------ .../data_providers/events_provider_test.go | 15 +++++++++++---- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider.go b/engine/access/rest/websockets/data_providers/account_statuses_provider.go index 99e0b85820e..52b4a860122 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider.go @@ -116,13 +116,6 @@ func (p *AccountStatusesDataProvider) handleResponse() func(accountStatusesRespo return status.Errorf(codes.Internal, "message index already incremented to %d", messageIndex.Value()) } - //p.send <- &models.AccountStatusesResponse{ - // BlockID: accountStatusesResponse.BlockID.String(), - // Height: strconv.FormatUint(accountStatusesResponse.Height, 10), - // AccountEvents: accountStatusesResponse.AccountEvents, - // MessageIndex: index, - //} - p.send <- &models.BaseDataProvidersResponse{ ID: p.ID().String(), Topic: p.Topic(), diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go index a7be006be6e..20a5e1d1a53 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go @@ -254,8 +254,10 @@ func (s *AccountStatusesProviderSuite) TestMessageIndexAccountStatusesProviderRe res := <-send accountStatusesRes, ok := res.(*models.BaseDataProvidersResponse) s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) + accountStatusesResData, ok := accountStatusesRes.Data.(*models.AccountStatusesResponse) s.Require().True(ok, "Expected *models.AccountStatusesResponse, got %T", res) + responses = append(responses, accountStatusesResData) } diff --git a/engine/access/rest/websockets/data_providers/events_provider.go b/engine/access/rest/websockets/data_providers/events_provider.go index 318e8081d2c..ff925d9ba9c 100644 --- a/engine/access/rest/websockets/data_providers/events_provider.go +++ b/engine/access/rest/websockets/data_providers/events_provider.go @@ -102,12 +102,16 @@ func (p *EventsDataProvider) handleResponse() func(eventsResponse *backend.Event return fmt.Errorf("message index already incremented to: %d", messageIndex.Value()) } - p.send <- &models.EventResponse{ - BlockId: eventsResponse.BlockID.String(), - BlockHeight: strconv.FormatUint(eventsResponse.Height, 10), - BlockTimestamp: eventsResponse.BlockTimestamp, - Events: eventsResponse.Events, - MessageIndex: index, + p.send <- &models.BaseDataProvidersResponse{ + ID: p.ID().String(), + Topic: p.Topic(), + Data: &models.EventResponse{ + BlockId: eventsResponse.BlockID.String(), + BlockHeight: strconv.FormatUint(eventsResponse.Height, 10), + BlockTimestamp: eventsResponse.BlockTimestamp, + Events: eventsResponse.Events, + MessageIndex: index, + }, } return nil diff --git a/engine/access/rest/websockets/data_providers/events_provider_test.go b/engine/access/rest/websockets/data_providers/events_provider_test.go index 4902f3b35a6..6b75b9fa249 100644 --- a/engine/access/rest/websockets/data_providers/events_provider_test.go +++ b/engine/access/rest/websockets/data_providers/events_provider_test.go @@ -144,10 +144,13 @@ func (s *EventsProviderSuite) requireEvents(v interface{}, expectedResponse inte expectedEventsResponse, ok := expectedResponse.(backend.EventsResponse) require.True(s.T(), ok, "unexpected type: %T", expectedResponse) - actualResponse, ok := v.(*models.EventResponse) + actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.EventResponse, got %T", v) - s.Require().ElementsMatch(expectedEventsResponse.Events, actualResponse.Events) + actualResponseData, ok := actualResponse.Data.(*models.EventResponse) + require.True(s.T(), ok, "unexpected response data type: %T", v) + + s.Require().ElementsMatch(expectedEventsResponse.Events, actualResponseData.Events) } // invalidArgumentsTestCases returns a list of test cases with invalid argument combinations @@ -275,9 +278,13 @@ func (s *EventsProviderSuite) TestMessageIndexEventProviderResponse_HappyPath() var responses []*models.EventResponse for i := 0; i < eventsCount; i++ { res := <-send - eventRes, ok := res.(*models.EventResponse) + eventRes, ok := res.(*models.BaseDataProvidersResponse) + s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) + + eventResData, ok := eventRes.Data.(*models.EventResponse) s.Require().True(ok, "Expected *models.EventResponse, got %T", res) - responses = append(responses, eventRes) + + responses = append(responses, eventResData) } // Verifying that indices are starting from 1 From 92016bebc0050cee8f727ae447234d1212ff5c79 Mon Sep 17 00:00:00 2001 From: Andrii Date: Fri, 27 Dec 2024 16:11:02 +0200 Subject: [PATCH 05/12] Refactored transaction statuses data providers --- ...d_and_get_transaction_statuses_provider.go | 10 +++++++--- ..._get_transaction_statuses_provider_test.go | 10 ++++++---- .../transaction_statuses_provider.go | 10 +++++++--- .../transaction_statuses_provider_test.go | 20 +++++++++++++------ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go index f6db73ac4e0..8a85e5afb9c 100644 --- a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go @@ -93,9 +93,13 @@ func (p *SendAndGetTransactionStatusesDataProvider) handleResponse() func(txResu return status.Errorf(codes.Internal, "message index already incremented to %d", messageIndex.Value()) } - p.send <- &models.TransactionStatusesResponse{ - TransactionResult: txResults[i], - MessageIndex: index, + p.send <- &models.BaseDataProvidersResponse{ + ID: p.ID().String(), + Topic: p.Topic(), + Data: &models.TransactionStatusesResponse{ + TransactionResult: txResults[i], + MessageIndex: index, + }, } } diff --git a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go index 1776d7a873a..da58d49ff2c 100644 --- a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go @@ -104,12 +104,14 @@ func (s *SendTransactionStatusesProviderSuite) requireTransactionStatuses( expectedTxStatusesResponse, ok := expectedResponse.(*access.TransactionResult) require.True(s.T(), ok, "unexpected type: %T", expectedResponse) - actualResponse, ok := v.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "Expected *models.TransactionStatusesResponse, got %T", v) + actualResponse, ok := v.(*models.BaseDataProvidersResponse) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", v) - require.Equal(s.T(), expectedTxStatusesResponse.BlockID, actualResponse.TransactionResult.BlockID) - require.Equal(s.T(), expectedTxStatusesResponse.BlockHeight, actualResponse.TransactionResult.BlockHeight) + actualResponseData, ok := actualResponse.Data.(*models.TransactionStatusesResponse) + require.True(s.T(), ok, "unexpected response data type: %T", v) + require.Equal(s.T(), expectedTxStatusesResponse.BlockID, actualResponseData.TransactionResult.BlockID) + require.Equal(s.T(), expectedTxStatusesResponse.BlockHeight, actualResponseData.TransactionResult.BlockHeight) } // TestSendTransactionStatusesDataProvider_InvalidArguments tests the behavior of the send transaction statuses data provider diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go index 3b75bde006a..2cfc3a37929 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go @@ -104,9 +104,13 @@ func (p *TransactionStatusesDataProvider) handleResponse() func(txResults []*acc return status.Errorf(codes.Internal, "message index already incremented to %d", messageIndex.Value()) } - p.send <- &models.TransactionStatusesResponse{ - TransactionResult: txResults[i], - MessageIndex: index, + p.send <- &models.BaseDataProvidersResponse{ + ID: p.ID().String(), + Topic: p.Topic(), + Data: &models.TransactionStatusesResponse{ + TransactionResult: txResults[i], + MessageIndex: index, + }, } } diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go index c045553f4bc..3321f979554 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go @@ -134,11 +134,14 @@ func (s *TransactionStatusesProviderSuite) requireTransactionStatuses( expectedTxStatusesResponse, ok := expectedResponse.(*access.TransactionResult) require.True(s.T(), ok, "unexpected type: %T", expectedResponse) - actualResponse, ok := v.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "Expected *models.TransactionStatusesResponse, got %T", v) + actualResponse, ok := v.(*models.BaseDataProvidersResponse) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", v) - require.Equal(s.T(), expectedTxStatusesResponse.BlockID, actualResponse.TransactionResult.BlockID) - require.Equal(s.T(), expectedTxStatusesResponse.BlockHeight, actualResponse.TransactionResult.BlockHeight) + actualResponseData, ok := actualResponse.Data.(*models.TransactionStatusesResponse) + require.True(s.T(), ok, "unexpected response data type: %T", v) + + require.Equal(s.T(), expectedTxStatusesResponse.BlockID, actualResponseData.TransactionResult.BlockID) + require.Equal(s.T(), expectedTxStatusesResponse.BlockHeight, actualResponseData.TransactionResult.BlockHeight) } // TestTransactionStatusesDataProvider_InvalidArguments tests the behavior of the transaction statuses data provider @@ -273,9 +276,14 @@ func (s *TransactionStatusesProviderSuite) TestMessageIndexTransactionStatusesPr var responses []*models.TransactionStatusesResponse for i := 0; i < txStatusesCount; i++ { res := <-send - txStatusesRes, ok := res.(*models.TransactionStatusesResponse) + + txStatusesRes, ok := res.(*models.BaseDataProvidersResponse) + s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) + + txStatusesResData, ok := txStatusesRes.Data.(*models.TransactionStatusesResponse) s.Require().True(ok, "Expected *models.TransactionStatusesResponse, got %T", res) - responses = append(responses, txStatusesRes) + + responses = append(responses, txStatusesResData) } // Verifying that indices are starting from 0 From 4c3e5ae9963c7715caabf0660b8b3dc03510906e Mon Sep 17 00:00:00 2001 From: Andrii Date: Fri, 3 Jan 2025 11:57:03 +0200 Subject: [PATCH 06/12] Renamed fields naming --- .../websockets/data_providers/account_statuses_provider.go | 6 +++--- .../data_providers/account_statuses_provider_test.go | 4 ++-- .../websockets/data_providers/block_digests_provider.go | 6 +++--- .../data_providers/block_digests_provider_test.go | 2 +- .../websockets/data_providers/block_headers_provider.go | 6 +++--- .../data_providers/block_headers_provider_test.go | 2 +- .../rest/websockets/data_providers/blocks_provider.go | 6 +++--- .../rest/websockets/data_providers/blocks_provider_test.go | 2 +- .../rest/websockets/data_providers/events_provider.go | 6 +++--- .../rest/websockets/data_providers/events_provider_test.go | 4 ++-- .../send_and_get_transaction_statuses_provider.go | 6 +++--- .../send_and_get_transaction_statuses_provider_test.go | 2 +- .../data_providers/transaction_statuses_provider.go | 6 +++--- .../data_providers/transaction_statuses_provider_test.go | 4 ++-- engine/access/rest/websockets/models/base_message.go | 6 +++--- 15 files changed, 34 insertions(+), 34 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider.go b/engine/access/rest/websockets/data_providers/account_statuses_provider.go index 52b4a860122..d33c3374d74 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider.go @@ -117,9 +117,9 @@ func (p *AccountStatusesDataProvider) handleResponse() func(accountStatusesRespo } p.send <- &models.BaseDataProvidersResponse{ - ID: p.ID().String(), - Topic: p.Topic(), - Data: &models.AccountStatusesResponse{ + SubscriptionID: p.ID().String(), + Topic: p.Topic(), + Payload: &models.AccountStatusesResponse{ BlockID: accountStatusesResponse.BlockID.String(), Height: strconv.FormatUint(accountStatusesResponse.Height, 10), AccountEvents: accountStatusesResponse.AccountEvents, diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go index eeb5348b171..30e71becd49 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go @@ -149,7 +149,7 @@ func (s *AccountStatusesProviderSuite) requireAccountStatuses( actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", v) - actualResponseData, ok := actualResponse.Data.(*models.AccountStatusesResponse) + actualResponseData, ok := actualResponse.Payload.(*models.AccountStatusesResponse) require.True(s.T(), ok, "unexpected response data type: %T", v) require.Equal(s.T(), expectedAccountStatusesResponse.BlockID.String(), actualResponseData.BlockID) @@ -258,7 +258,7 @@ func (s *AccountStatusesProviderSuite) TestMessageIndexAccountStatusesProviderRe accountStatusesRes, ok := res.(*models.BaseDataProvidersResponse) s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) - accountStatusesResData, ok := accountStatusesRes.Data.(*models.AccountStatusesResponse) + accountStatusesResData, ok := accountStatusesRes.Payload.(*models.AccountStatusesResponse) s.Require().True(ok, "Expected *models.AccountStatusesResponse, got %T", res) responses = append(responses, accountStatusesResData) diff --git a/engine/access/rest/websockets/data_providers/block_digests_provider.go b/engine/access/rest/websockets/data_providers/block_digests_provider.go index 5716aab940b..78d8233d79a 100644 --- a/engine/access/rest/websockets/data_providers/block_digests_provider.go +++ b/engine/access/rest/websockets/data_providers/block_digests_provider.go @@ -62,9 +62,9 @@ func (p *BlockDigestsDataProvider) Run() error { p.subscription, subscription.HandleResponse(p.send, func(block *flow.BlockDigest) (interface{}, error) { return &models.BaseDataProvidersResponse{ - ID: p.ID().String(), - Topic: p.Topic(), - Data: &models.BlockDigestMessageResponse{ + SubscriptionID: p.ID().String(), + Topic: p.Topic(), + Payload: &models.BlockDigestMessageResponse{ Block: block, }, }, nil diff --git a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go index 975c1ce7fa8..8b774374502 100644 --- a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go @@ -129,7 +129,7 @@ func (s *BlocksProviderSuite) requireBlockDigest(v interface{}, expected interfa actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "unexpected response type: %T", v) - actualResponseData, ok := actualResponse.Data.(*models.BlockDigestMessageResponse) + actualResponseData, ok := actualResponse.Payload.(*models.BlockDigestMessageResponse) require.True(s.T(), ok, "unexpected response data type: %T", v) s.Require().Equal(expectedBlock.Header.ID(), actualResponseData.Block.ID()) diff --git a/engine/access/rest/websockets/data_providers/block_headers_provider.go b/engine/access/rest/websockets/data_providers/block_headers_provider.go index 92d68a9402f..a13c2e360d4 100644 --- a/engine/access/rest/websockets/data_providers/block_headers_provider.go +++ b/engine/access/rest/websockets/data_providers/block_headers_provider.go @@ -62,9 +62,9 @@ func (p *BlockHeadersDataProvider) Run() error { p.subscription, subscription.HandleResponse(p.send, func(header *flow.Header) (interface{}, error) { return &models.BaseDataProvidersResponse{ - ID: p.ID().String(), - Topic: p.Topic(), - Data: &models.BlockHeaderMessageResponse{ + SubscriptionID: p.ID().String(), + Topic: p.Topic(), + Payload: &models.BlockHeaderMessageResponse{ Header: header, }, }, nil diff --git a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go index 0439c27cfa6..62c9ece05b8 100644 --- a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go @@ -129,7 +129,7 @@ func (s *BlockHeadersProviderSuite) requireBlockHeader(v interface{}, expected i actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "unexpected response type: %T", v) - actualResponseData, ok := actualResponse.Data.(*models.BlockHeaderMessageResponse) + actualResponseData, ok := actualResponse.Payload.(*models.BlockHeaderMessageResponse) require.True(s.T(), ok, "unexpected response data type: %T", v) s.Require().Equal(expectedBlock.Header, actualResponseData.Header) diff --git a/engine/access/rest/websockets/data_providers/blocks_provider.go b/engine/access/rest/websockets/data_providers/blocks_provider.go index fb7f46a78c0..e1203d1fb61 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider.go @@ -71,9 +71,9 @@ func (p *BlocksDataProvider) Run() error { p.subscription, subscription.HandleResponse(p.send, func(block *flow.Block) (interface{}, error) { return &models.BaseDataProvidersResponse{ - ID: p.ID().String(), - Topic: p.Topic(), - Data: &models.BlockMessageResponse{ + SubscriptionID: p.ID().String(), + Topic: p.Topic(), + Payload: &models.BlockMessageResponse{ Block: block, }, }, nil diff --git a/engine/access/rest/websockets/data_providers/blocks_provider_test.go b/engine/access/rest/websockets/data_providers/blocks_provider_test.go index 89d15b44a89..774448182ae 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider_test.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider_test.go @@ -206,7 +206,7 @@ func (s *BlocksProviderSuite) requireBlock(v interface{}, expected interface{}) actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "unexpected response type: %T", v) - actualResponseData, ok := actualResponse.Data.(*models.BlockMessageResponse) + actualResponseData, ok := actualResponse.Payload.(*models.BlockMessageResponse) require.True(s.T(), ok, "unexpected response data type: %T", v) s.Require().Equal(expectedBlock, actualResponseData.Block) diff --git a/engine/access/rest/websockets/data_providers/events_provider.go b/engine/access/rest/websockets/data_providers/events_provider.go index ff925d9ba9c..afe7f0de329 100644 --- a/engine/access/rest/websockets/data_providers/events_provider.go +++ b/engine/access/rest/websockets/data_providers/events_provider.go @@ -103,9 +103,9 @@ func (p *EventsDataProvider) handleResponse() func(eventsResponse *backend.Event } p.send <- &models.BaseDataProvidersResponse{ - ID: p.ID().String(), - Topic: p.Topic(), - Data: &models.EventResponse{ + SubscriptionID: p.ID().String(), + Topic: p.Topic(), + Payload: &models.EventResponse{ BlockId: eventsResponse.BlockID.String(), BlockHeight: strconv.FormatUint(eventsResponse.Height, 10), BlockTimestamp: eventsResponse.BlockTimestamp, diff --git a/engine/access/rest/websockets/data_providers/events_provider_test.go b/engine/access/rest/websockets/data_providers/events_provider_test.go index b3ea709ab9e..ea84df5349a 100644 --- a/engine/access/rest/websockets/data_providers/events_provider_test.go +++ b/engine/access/rest/websockets/data_providers/events_provider_test.go @@ -147,7 +147,7 @@ func (s *EventsProviderSuite) requireEvents(v interface{}, expectedResponse inte actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.EventResponse, got %T", v) - actualResponseData, ok := actualResponse.Data.(*models.EventResponse) + actualResponseData, ok := actualResponse.Payload.(*models.EventResponse) require.True(s.T(), ok, "unexpected response data type: %T", v) s.Require().ElementsMatch(expectedEventsResponse.Events, actualResponseData.Events) @@ -285,7 +285,7 @@ func (s *EventsProviderSuite) TestMessageIndexEventProviderResponse_HappyPath() eventRes, ok := res.(*models.BaseDataProvidersResponse) s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) - eventResData, ok := eventRes.Data.(*models.EventResponse) + eventResData, ok := eventRes.Payload.(*models.EventResponse) s.Require().True(ok, "Expected *models.EventResponse, got %T", res) responses = append(responses, eventResData) diff --git a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go index 8a85e5afb9c..0384ecd80e0 100644 --- a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go @@ -94,9 +94,9 @@ func (p *SendAndGetTransactionStatusesDataProvider) handleResponse() func(txResu } p.send <- &models.BaseDataProvidersResponse{ - ID: p.ID().String(), - Topic: p.Topic(), - Data: &models.TransactionStatusesResponse{ + SubscriptionID: p.ID().String(), + Topic: p.Topic(), + Payload: &models.TransactionStatusesResponse{ TransactionResult: txResults[i], MessageIndex: index, }, diff --git a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go index da58d49ff2c..b4e58ce6122 100644 --- a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go @@ -107,7 +107,7 @@ func (s *SendTransactionStatusesProviderSuite) requireTransactionStatuses( actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", v) - actualResponseData, ok := actualResponse.Data.(*models.TransactionStatusesResponse) + actualResponseData, ok := actualResponse.Payload.(*models.TransactionStatusesResponse) require.True(s.T(), ok, "unexpected response data type: %T", v) require.Equal(s.T(), expectedTxStatusesResponse.BlockID, actualResponseData.TransactionResult.BlockID) diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go index 2cfc3a37929..3a65f27be12 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go @@ -105,9 +105,9 @@ func (p *TransactionStatusesDataProvider) handleResponse() func(txResults []*acc } p.send <- &models.BaseDataProvidersResponse{ - ID: p.ID().String(), - Topic: p.Topic(), - Data: &models.TransactionStatusesResponse{ + SubscriptionID: p.ID().String(), + Topic: p.Topic(), + Payload: &models.TransactionStatusesResponse{ TransactionResult: txResults[i], MessageIndex: index, }, diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go index 94d6371cdbd..7a9bcdbedd8 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go @@ -137,7 +137,7 @@ func (s *TransactionStatusesProviderSuite) requireTransactionStatuses( actualResponse, ok := v.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", v) - actualResponseData, ok := actualResponse.Data.(*models.TransactionStatusesResponse) + actualResponseData, ok := actualResponse.Payload.(*models.TransactionStatusesResponse) require.True(s.T(), ok, "unexpected response data type: %T", v) require.Equal(s.T(), expectedTxStatusesResponse.BlockID, actualResponseData.TransactionResult.BlockID) @@ -284,7 +284,7 @@ func (s *TransactionStatusesProviderSuite) TestMessageIndexTransactionStatusesPr txStatusesRes, ok := res.(*models.BaseDataProvidersResponse) s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) - txStatusesResData, ok := txStatusesRes.Data.(*models.TransactionStatusesResponse) + txStatusesResData, ok := txStatusesRes.Payload.(*models.TransactionStatusesResponse) s.Require().True(ok, "Expected *models.TransactionStatusesResponse, got %T", res) responses = append(responses, txStatusesResData) diff --git a/engine/access/rest/websockets/models/base_message.go b/engine/access/rest/websockets/models/base_message.go index ae694c68652..1e62f4fc828 100644 --- a/engine/access/rest/websockets/models/base_message.go +++ b/engine/access/rest/websockets/models/base_message.go @@ -22,7 +22,7 @@ type BaseMessageResponse struct { // BaseDataProvidersResponse represents a base structure for responses from subscriptions. type BaseDataProvidersResponse struct { - ID string `json:"id"` // Unique subscription ID - Topic string `json:"topic"` // Topic of the subscription - Data interface{} `json:"data"` // Data that's being returned within a subscription. + SubscriptionID string `json:"subscription_id"` // Unique subscriptionID + Topic string `json:"topic"` // Topic of the subscription + Payload interface{} `json:"payload"` // Payload that's being returned within a subscription. } From 5d8189c5c3353de6f6106abd3551ab38f1d72a0b Mon Sep 17 00:00:00 2001 From: Andrii Date: Tue, 7 Jan 2025 16:33:50 +0200 Subject: [PATCH 07/12] Chanaged expected types in data provider tests to be consistent, added topic check in tests --- .../account_statuses_provider.go | 4 +-- .../account_statuses_provider_test.go | 29 ++++++++++++------- .../data_providers/base_provider.go | 16 +++++----- .../data_providers/block_digests_provider.go | 4 +-- .../block_digests_provider_test.go | 8 ++--- .../data_providers/block_headers_provider.go | 4 +-- .../block_headers_provider_test.go | 10 +++---- .../data_providers/blocks_provider.go | 4 +-- .../data_providers/blocks_provider_test.go | 6 ++-- .../data_providers/events_provider.go | 4 +-- .../data_providers/events_provider_test.go | 22 +++++++++----- ...d_and_get_transaction_statuses_provider.go | 4 +-- ..._get_transaction_statuses_provider_test.go | 14 +++++---- .../transaction_statuses_provider.go | 4 +-- .../transaction_statuses_provider_test.go | 24 ++++++++++----- .../rest/websockets/models/base_message.go | 2 +- 16 files changed, 91 insertions(+), 68 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider.go b/engine/access/rest/websockets/data_providers/account_statuses_provider.go index 3607a7f3de1..182db1cc0d7 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider.go @@ -26,7 +26,7 @@ type accountStatusesArguments struct { } type AccountStatusesDataProvider struct { - *BaseDataProvider + *baseDataProvider logger zerolog.Logger stateStreamApi state_stream.API @@ -62,7 +62,7 @@ func NewAccountStatusesDataProvider( subCtx, cancel := context.WithCancel(ctx) - p.BaseDataProvider = newBaseDataProvider( + p.baseDataProvider = newBaseDataProvider( topic, cancel, send, diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go index 5db1b86b61f..eb12fc615ca 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go @@ -138,21 +138,25 @@ func (s *AccountStatusesProviderSuite) subscribeAccountStatusesDataProviderTestC // requireAccountStatuses ensures that the received account statuses information matches the expected data. func (s *AccountStatusesProviderSuite) requireAccountStatuses(actual interface{}, expected interface{}) { - expectedResponse, ok := expected.(*models.AccountStatusesResponse) - require.True(s.T(), ok, "Expected *models.AccountStatusesResponse, got %T", expected) + expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) + + expectedResponsePayload, ok := expectedResponse.Payload.(*models.AccountStatusesResponse) + require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) actualResponse, ok := actual.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) actualResponsePayload, ok := actualResponse.Payload.(*models.AccountStatusesResponse) - require.True(s.T(), ok, "unexpected response payload type: %T", actualResponse.Payload) + require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) - require.Equal(s.T(), expectedResponse.BlockID, actualResponsePayload.BlockID) - require.Equal(s.T(), len(expectedResponse.AccountEvents), len(actualResponsePayload.AccountEvents)) - require.Equal(s.T(), expectedResponse.MessageIndex, actualResponsePayload.MessageIndex) - require.Equal(s.T(), expectedResponse.Height, actualResponsePayload.Height) + require.Equal(s.T(), expectedResponsePayload.BlockID, actualResponsePayload.BlockID) + require.Equal(s.T(), len(expectedResponsePayload.AccountEvents), len(actualResponsePayload.AccountEvents)) + require.Equal(s.T(), expectedResponsePayload.MessageIndex, actualResponsePayload.MessageIndex) + require.Equal(s.T(), expectedResponsePayload.Height, actualResponsePayload.Height) + require.Equal(s.T(), expectedResponse.Topic, actualResponse.Topic) - for key, expectedEvents := range expectedResponse.AccountEvents { + for key, expectedEvents := range expectedResponsePayload.AccountEvents { actualEvents, ok := actualResponsePayload.AccountEvents[key] require.True(s.T(), ok, "Missing key in actual AccountEvents: %s", key) @@ -294,10 +298,13 @@ func (s *AccountStatusesProviderSuite) expectedAccountStatusesResponses(backendR expectedResponses := make([]interface{}, len(backendResponses)) for i, resp := range backendResponses { - var expectedResponse models.AccountStatusesResponse - expectedResponse.Build(resp, uint64(i)) + var expectedResponsePayload models.AccountStatusesResponse + expectedResponsePayload.Build(resp, uint64(i)) - expectedResponses[i] = &expectedResponse + expectedResponses[i] = &models.BaseDataProvidersResponse{ + Topic: AccountStatusesTopic, + Payload: &expectedResponsePayload, + } } return expectedResponses diff --git a/engine/access/rest/websockets/data_providers/base_provider.go b/engine/access/rest/websockets/data_providers/base_provider.go index ee228fd4adf..0ee040cd4ac 100644 --- a/engine/access/rest/websockets/data_providers/base_provider.go +++ b/engine/access/rest/websockets/data_providers/base_provider.go @@ -8,8 +8,8 @@ import ( "github.com/onflow/flow-go/engine/access/subscription" ) -// BaseDataProvider holds common objects for the provider -type BaseDataProvider struct { +// baseDataProvider holds common objects for the provider +type baseDataProvider struct { id uuid.UUID topic string cancel context.CancelFunc @@ -17,14 +17,14 @@ type BaseDataProvider struct { subscription subscription.Subscription } -// newBaseDataProvider creates a new instance of BaseDataProvider. +// newBaseDataProvider creates a new instance of baseDataProvider. func newBaseDataProvider( topic string, cancel context.CancelFunc, send chan<- interface{}, subscription subscription.Subscription, -) *BaseDataProvider { - return &BaseDataProvider{ +) *baseDataProvider { + return &baseDataProvider{ id: uuid.New(), topic: topic, cancel: cancel, @@ -34,18 +34,18 @@ func newBaseDataProvider( } // ID returns the unique identifier of the data provider. -func (b *BaseDataProvider) ID() uuid.UUID { +func (b *baseDataProvider) ID() uuid.UUID { return b.id } // Topic returns the topic associated with the data provider. -func (b *BaseDataProvider) Topic() string { +func (b *baseDataProvider) Topic() string { return b.topic } // Close terminates the data provider. // // No errors are expected during normal operations. -func (b *BaseDataProvider) Close() { +func (b *baseDataProvider) Close() { b.cancel() } diff --git a/engine/access/rest/websockets/data_providers/block_digests_provider.go b/engine/access/rest/websockets/data_providers/block_digests_provider.go index b9651cc69da..8d9603306e2 100644 --- a/engine/access/rest/websockets/data_providers/block_digests_provider.go +++ b/engine/access/rest/websockets/data_providers/block_digests_provider.go @@ -15,7 +15,7 @@ import ( // BlockDigestsDataProvider is responsible for providing block digests type BlockDigestsDataProvider struct { - *BaseDataProvider + *baseDataProvider logger zerolog.Logger api access.API @@ -44,7 +44,7 @@ func NewBlockDigestsDataProvider( } subCtx, cancel := context.WithCancel(ctx) - p.BaseDataProvider = newBaseDataProvider( + p.baseDataProvider = newBaseDataProvider( topic, cancel, send, diff --git a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go index 7bad20ff750..72c95f39e27 100644 --- a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go @@ -139,16 +139,16 @@ func (s *BlockDigestsProviderSuite) TestBlockDigestsDataProvider_HappyPath() { // requireBlockDigest ensures that the received block header information matches the expected data. func (s *BlocksProviderSuite) requireBlockDigest(actual interface{}, expected interface{}) { expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "unexpected response type: %T", expected) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) expectedResponsePayload, ok := expectedResponse.Payload.(*models.BlockDigest) - require.True(s.T(), ok, "unexpected response type: %T", expectedResponse.Payload) + require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "unexpected response type: %T", actual) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) actualResponsePayload, ok := actualResponse.Payload.(*models.BlockDigest) - require.True(s.T(), ok, "unexpected response payload type: %T", actualResponse.Payload) + require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) s.Require().Equal(expectedResponse.Topic, actualResponse.Topic) s.Require().Equal(expectedResponsePayload, actualResponsePayload) diff --git a/engine/access/rest/websockets/data_providers/block_headers_provider.go b/engine/access/rest/websockets/data_providers/block_headers_provider.go index 5f1e5c4a448..291e3fd76d9 100644 --- a/engine/access/rest/websockets/data_providers/block_headers_provider.go +++ b/engine/access/rest/websockets/data_providers/block_headers_provider.go @@ -16,7 +16,7 @@ import ( // BlockHeadersDataProvider is responsible for providing block headers type BlockHeadersDataProvider struct { - *BaseDataProvider + *baseDataProvider logger zerolog.Logger api access.API @@ -45,7 +45,7 @@ func NewBlockHeadersDataProvider( } subCtx, cancel := context.WithCancel(ctx) - p.BaseDataProvider = newBaseDataProvider( + p.baseDataProvider = newBaseDataProvider( topic, cancel, send, diff --git a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go index b860e16615e..ab194c395cc 100644 --- a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go @@ -60,8 +60,6 @@ func (s *BlockHeadersProviderSuite) validBlockHeadersArgumentsTestCases() []test var header commonmodels.BlockHeader header.Build(b.Header) - //expectedResponses[i] = &models.BlockHeaderMessageResponse{Header: &header} - expectedResponses[i] = &models.BaseDataProvidersResponse{ Topic: BlockHeadersTopic, Payload: &header, @@ -140,16 +138,16 @@ func (s *BlockHeadersProviderSuite) TestBlockHeadersDataProvider_HappyPath() { // requireBlockHeaders ensures that the received block header information matches the expected data. func (s *BlockHeadersProviderSuite) requireBlockHeader(actual interface{}, expected interface{}) { expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "unexpected response type: %T", expected) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) expectedResponsePayload, ok := expectedResponse.Payload.(*commonmodels.BlockHeader) - require.True(s.T(), ok, "unexpected response type: %T", expectedResponse.Payload) + require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "unexpected response type: %T", actual) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) actualResponsePayload, ok := actualResponse.Payload.(*commonmodels.BlockHeader) - require.True(s.T(), ok, "unexpected response payload type: %T", actualResponse.Payload) + require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) s.Require().Equal(expectedResponse.Topic, actualResponse.Topic) s.Require().Equal(expectedResponsePayload, actualResponsePayload) diff --git a/engine/access/rest/websockets/data_providers/blocks_provider.go b/engine/access/rest/websockets/data_providers/blocks_provider.go index 49e837ce226..c0de76d0727 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider.go @@ -25,7 +25,7 @@ type blocksArguments struct { // BlocksDataProvider is responsible for providing blocks type BlocksDataProvider struct { - *BaseDataProvider + *baseDataProvider logger zerolog.Logger api access.API @@ -59,7 +59,7 @@ func NewBlocksDataProvider( } subCtx, cancel := context.WithCancel(ctx) - p.BaseDataProvider = newBaseDataProvider( + p.baseDataProvider = newBaseDataProvider( topic, cancel, send, diff --git a/engine/access/rest/websockets/data_providers/blocks_provider_test.go b/engine/access/rest/websockets/data_providers/blocks_provider_test.go index 18b664de14b..f28e9a82e04 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider_test.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider_test.go @@ -241,13 +241,13 @@ func (s *BlocksProviderSuite) TestBlocksDataProvider_HappyPath() { // requireBlocks ensures that the received block information matches the expected data. func (s *BlocksProviderSuite) requireBlock(actual interface{}, expected interface{}) { expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "unexpected response type: %T", expected) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) expectedResponsePayload, ok := expectedResponse.Payload.(*commonmodels.Block) - require.True(s.T(), ok, "unexpected response type: %T", expectedResponse.Payload) + require.True(s.T(), ok, "unexpected response payload type: %T", expectedResponse.Payload) actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "unexpected response type: %T", actual) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) actualResponsePayload, ok := actualResponse.Payload.(*commonmodels.Block) require.True(s.T(), ok, "unexpected response payload type: %T", actualResponse.Payload) diff --git a/engine/access/rest/websockets/data_providers/events_provider.go b/engine/access/rest/websockets/data_providers/events_provider.go index 90c14859165..b28bf4e2e95 100644 --- a/engine/access/rest/websockets/data_providers/events_provider.go +++ b/engine/access/rest/websockets/data_providers/events_provider.go @@ -25,7 +25,7 @@ type eventsArguments struct { // EventsDataProvider is responsible for providing events type EventsDataProvider struct { - *BaseDataProvider + *baseDataProvider logger zerolog.Logger stateStreamApi state_stream.API @@ -61,7 +61,7 @@ func NewEventsDataProvider( subCtx, cancel := context.WithCancel(ctx) - p.BaseDataProvider = newBaseDataProvider( + p.baseDataProvider = newBaseDataProvider( topic, cancel, send, diff --git a/engine/access/rest/websockets/data_providers/events_provider_test.go b/engine/access/rest/websockets/data_providers/events_provider_test.go index f75c020dad7..91b09239f1a 100644 --- a/engine/access/rest/websockets/data_providers/events_provider_test.go +++ b/engine/access/rest/websockets/data_providers/events_provider_test.go @@ -138,17 +138,20 @@ func (s *EventsProviderSuite) subscribeEventsDataProviderTestCases(backendRespon // requireEvents ensures that the received event information matches the expected data. func (s *EventsProviderSuite) requireEvents(actual interface{}, expected interface{}) { - expectedResponse, ok := expected.(*models.EventResponse) - require.True(s.T(), ok, "Expected *models.EventResponse, got %T", expected) + expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) + + expectedResponsePayload, ok := expectedResponse.Payload.(*models.EventResponse) + require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) actualResponse, ok := actual.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) actualResponsePayload, ok := actualResponse.Payload.(*models.EventResponse) - require.True(s.T(), ok, "unexpected response payload type: %T", actualResponse.Payload) + require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) - s.Require().ElementsMatch(expectedResponse.Events, actualResponsePayload.Events) - s.Require().Equal(expectedResponse.MessageIndex, actualResponsePayload.MessageIndex) + s.Require().ElementsMatch(expectedResponsePayload.Events, actualResponsePayload.Events) + s.Require().Equal(expectedResponsePayload.MessageIndex, actualResponsePayload.MessageIndex) } // invalidArgumentsTestCases returns a list of test cases with invalid argument combinations @@ -324,10 +327,13 @@ func (s *EventsProviderSuite) expectedEventsResponses( expectedResponses := make([]interface{}, len(backendResponses)) for i, resp := range backendResponses { - var expectedResponse models.EventResponse - expectedResponse.Build(resp, uint64(i)) + var expectedResponsePayload models.EventResponse + expectedResponsePayload.Build(resp, uint64(i)) - expectedResponses[i] = &expectedResponse + expectedResponses[i] = &models.BaseDataProvidersResponse{ + Topic: EventsTopic, + Payload: &expectedResponsePayload, + } } return expectedResponses } diff --git a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go index 40e49c657c1..d1b9f973d1d 100644 --- a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go @@ -26,7 +26,7 @@ type sendAndGetTransactionStatusesArguments struct { } type SendAndGetTransactionStatusesDataProvider struct { - *BaseDataProvider + *baseDataProvider logger zerolog.Logger api access.API @@ -58,7 +58,7 @@ func NewSendAndGetTransactionStatusesDataProvider( subCtx, cancel := context.WithCancel(ctx) - p.BaseDataProvider = newBaseDataProvider( + p.baseDataProvider = newBaseDataProvider( topic, cancel, send, diff --git a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go index df8811b4a2f..a8a1a89d86a 100644 --- a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go @@ -73,7 +73,7 @@ func (s *TransactionStatusesProviderSuite) TestSendTransactionStatusesDataProvid ) backendResponse := backendTransactionStatusesResponse(s.rootBlock) - expectedResponse := s.expectedTransactionStatusesResponses(backendResponse) + expectedResponse := s.expectedTransactionStatusesResponses(backendResponse, SendAndGetTransactionStatusesTopic) sendTxStatutesTestCases := []testType{ { @@ -111,16 +111,20 @@ func (s *SendTransactionStatusesProviderSuite) requireTransactionStatuses( actual interface{}, expected interface{}, ) { - expectedTxStatusesResponse, ok := expected.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "expected *models.TransactionStatusesResponse, got %T", expected) + expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) + + expectedResponsePayload, ok := expectedResponse.Payload.(*models.TransactionStatusesResponse) + require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) actualResponse, ok := actual.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) actualResponsePayload, ok := actualResponse.Payload.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "unexpected response payload type: %T", actualResponse.Payload) + require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) - require.Equal(s.T(), expectedTxStatusesResponse.TransactionResult.BlockId, actualResponsePayload.TransactionResult.BlockId) + require.Equal(s.T(), expectedResponse.Topic, actualResponse.Topic) + require.Equal(s.T(), expectedResponsePayload.TransactionResult.BlockId, actualResponsePayload.TransactionResult.BlockId) } // TestSendTransactionStatusesDataProvider_InvalidArguments tests the behavior of the send transaction statuses data provider diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go index 1540fa55146..3b86dd39042 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go @@ -29,7 +29,7 @@ type transactionStatusesArguments struct { // TransactionStatusesDataProvider is responsible for providing tx statuses type TransactionStatusesDataProvider struct { - *BaseDataProvider + *baseDataProvider logger zerolog.Logger api access.API @@ -61,7 +61,7 @@ func NewTransactionStatusesDataProvider( subCtx, cancel := context.WithCancel(ctx) - p.BaseDataProvider = newBaseDataProvider( + p.baseDataProvider = newBaseDataProvider( topic, cancel, send, diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go index 4775050931c..05c1c7a63ce 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go @@ -90,7 +90,7 @@ func (s *TransactionStatusesProviderSuite) TestTransactionStatusesDataProvider_H } func (s *TransactionStatusesProviderSuite) subscribeTransactionStatusesDataProviderTestCases(backendResponses []*access.TransactionResult) []testType { - expectedResponses := s.expectedTransactionStatusesResponses(backendResponses) + expectedResponses := s.expectedTransactionStatusesResponses(backendResponses, TransactionStatusesTopic) return []testType{ { @@ -146,29 +146,37 @@ func (s *TransactionStatusesProviderSuite) requireTransactionStatuses( actual interface{}, expected interface{}, ) { - expectedTxStatusesResponse, ok := expected.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "expected *models.TransactionStatusesResponse, got %T", expected) + expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) + require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) + + expectedResponsePayload, ok := expectedResponse.Payload.(*models.TransactionStatusesResponse) + require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) actualResponse, ok := actual.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) actualResponsePayload, ok := actualResponse.Payload.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "unexpected response payload type: %T", actualResponse.Payload) + require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) - require.Equal(s.T(), expectedTxStatusesResponse.TransactionResult.BlockId, actualResponsePayload.TransactionResult.BlockId) + require.Equal(s.T(), expectedResponse.Topic, actualResponse.Topic) + require.Equal(s.T(), expectedResponsePayload.TransactionResult.BlockId, actualResponsePayload.TransactionResult.BlockId) } // expectedTransactionStatusesResponses creates the expected responses for the provided backend responses. func (s *TransactionStatusesProviderSuite) expectedTransactionStatusesResponses( backendResponses []*access.TransactionResult, + topic string, ) []interface{} { expectedResponses := make([]interface{}, len(backendResponses)) for i, resp := range backendResponses { - var expectedResponse models.TransactionStatusesResponse - expectedResponse.Build(s.linkGenerator, resp, uint64(i)) + var expectedResponsePayload models.TransactionStatusesResponse + expectedResponsePayload.Build(s.linkGenerator, resp, uint64(i)) - expectedResponses[i] = &expectedResponse + expectedResponses[i] = &models.BaseDataProvidersResponse{ + Topic: topic, + Payload: &expectedResponsePayload, + } } return expectedResponses diff --git a/engine/access/rest/websockets/models/base_message.go b/engine/access/rest/websockets/models/base_message.go index e0b7fb78cc2..edac10cfb9b 100644 --- a/engine/access/rest/websockets/models/base_message.go +++ b/engine/access/rest/websockets/models/base_message.go @@ -28,7 +28,7 @@ type BaseDataProvidersResponse struct { } // Build creates BaseDataProvidersResponse instance for consistent responses of the data providers. -//func (b *BaseDataProvidersResponse) Build(baseDataProvider *data_providers.BaseDataProvider, payload interface{}) { +//func (b *BaseDataProvidersResponse) Build(baseDataProvider *data_providers.baseDataProvider, payload interface{}) { // *b = BaseDataProvidersResponse{ // SubscriptionID: baseDataProvider.ID().String(), // Topic: baseDataProvider.Topic(), From 26485fa549979c70e7fd20b7baa5867aba09e3fe Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 8 Jan 2025 13:35:59 +0200 Subject: [PATCH 08/12] Removed wrong Build function --- engine/access/rest/websockets/models/base_message.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/engine/access/rest/websockets/models/base_message.go b/engine/access/rest/websockets/models/base_message.go index edac10cfb9b..c22ac3d8a57 100644 --- a/engine/access/rest/websockets/models/base_message.go +++ b/engine/access/rest/websockets/models/base_message.go @@ -28,14 +28,6 @@ type BaseDataProvidersResponse struct { } // Build creates BaseDataProvidersResponse instance for consistent responses of the data providers. -//func (b *BaseDataProvidersResponse) Build(baseDataProvider *data_providers.baseDataProvider, payload interface{}) { -// *b = BaseDataProvidersResponse{ -// SubscriptionID: baseDataProvider.ID().String(), -// Topic: baseDataProvider.Topic(), -// Payload: payload, -// } -//} - func (b *BaseDataProvidersResponse) Build(subscriptionID string, topic string, payload interface{}) { *b = BaseDataProvidersResponse{ SubscriptionID: subscriptionID, From 3b22d5d265963cbad375695907ba3b680ca14125 Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 8 Jan 2025 16:50:42 +0200 Subject: [PATCH 09/12] Refactored data providers tests --- .../account_statuses_provider_test.go | 34 ++-- .../block_digests_provider_test.go | 80 ++++---- .../block_headers_provider_test.go | 80 ++++---- .../data_providers/blocks_provider_test.go | 168 ++++++++--------- .../data_providers/events_provider_test.go | 174 ++++++++--------- .../transaction_statuses_provider_test.go | 176 +++++++++--------- 6 files changed, 356 insertions(+), 356 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go index eb12fc615ca..8cd0bda8f30 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go @@ -164,6 +164,23 @@ func (s *AccountStatusesProviderSuite) requireAccountStatuses(actual interface{} } } +// expectedAccountStatusesResponses creates the expected responses for the provided events and backend responses. +func (s *AccountStatusesProviderSuite) expectedAccountStatusesResponses(backendResponses []*backend.AccountStatusesResponse) []interface{} { + expectedResponses := make([]interface{}, len(backendResponses)) + + for i, resp := range backendResponses { + var expectedResponsePayload models.AccountStatusesResponse + expectedResponsePayload.Build(resp, uint64(i)) + + expectedResponses[i] = &models.BaseDataProvidersResponse{ + Topic: AccountStatusesTopic, + Payload: &expectedResponsePayload, + } + } + + return expectedResponses +} + // TestAccountStatusesDataProvider_InvalidArguments tests the behavior of the account statuses data provider // when invalid arguments are provided. It verifies that appropriate errors are returned // for missing or conflicting arguments. @@ -292,20 +309,3 @@ func (s *AccountStatusesProviderSuite) backendAccountStatusesResponses(events [] return responses } - -// expectedAccountStatusesResponses creates the expected responses for the provided events and backend responses. -func (s *AccountStatusesProviderSuite) expectedAccountStatusesResponses(backendResponses []*backend.AccountStatusesResponse) []interface{} { - expectedResponses := make([]interface{}, len(backendResponses)) - - for i, resp := range backendResponses { - var expectedResponsePayload models.AccountStatusesResponse - expectedResponsePayload.Build(resp, uint64(i)) - - expectedResponses[i] = &models.BaseDataProvidersResponse{ - Topic: AccountStatusesTopic, - Payload: &expectedResponsePayload, - } - } - - return expectedResponses -} diff --git a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go index 72c95f39e27..1be927b0fb3 100644 --- a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go @@ -28,27 +28,23 @@ func (s *BlockDigestsProviderSuite) SetupTest() { s.BlocksProviderSuite.SetupTest() } -// TestBlockDigestsDataProvider_InvalidArguments tests the behavior of the block digests data provider -// when invalid arguments are provided. It verifies that appropriate errors are returned -// for missing or conflicting arguments. -// This test covers the test cases: -// 1. Missing 'block_status' argument. -// 2. Invalid 'block_status' argument. -// 3. Providing both 'start_block_id' and 'start_block_height' simultaneously. -func (s *BlockDigestsProviderSuite) TestBlockDigestsDataProvider_InvalidArguments() { - ctx := context.Background() - send := make(chan interface{}) - - topic := BlockDigestsTopic - - for _, test := range s.invalidArgumentsTestCases() { - s.Run(test.name, func() { - provider, err := NewBlockDigestsDataProvider(ctx, s.log, s.api, topic, test.arguments, send) - s.Require().Nil(provider) - s.Require().Error(err) - s.Require().Contains(err.Error(), test.expectedErrorMsg) - }) - } +// TestBlockDigestsDataProvider_HappyPath tests the behavior of the block digests data provider +// when it is configured correctly and operating under normal conditions. It +// validates that block digests are correctly streamed to the channel and ensures +// no unexpected errors occur. +func (s *BlockDigestsProviderSuite) TestBlockDigestsDataProvider_HappyPath() { + testHappyPath( + s.T(), + BlockDigestsTopic, + s.factory, + s.validBlockDigestsArgumentsTestCases(), + func(dataChan chan interface{}) { + for _, block := range s.blocks { + dataChan <- flow.NewBlockDigest(block.Header.ID(), block.Header.Height, block.Header.Timestamp) + } + }, + s.requireBlockDigest, + ) } // validBlockDigestsArgumentsTestCases defines test happy cases for block digests data providers. @@ -117,25 +113,6 @@ func (s *BlockDigestsProviderSuite) validBlockDigestsArgumentsTestCases() []test } } -// TestBlockDigestsDataProvider_HappyPath tests the behavior of the block digests data provider -// when it is configured correctly and operating under normal conditions. It -// validates that block digests are correctly streamed to the channel and ensures -// no unexpected errors occur. -func (s *BlockDigestsProviderSuite) TestBlockDigestsDataProvider_HappyPath() { - testHappyPath( - s.T(), - BlockDigestsTopic, - s.factory, - s.validBlockDigestsArgumentsTestCases(), - func(dataChan chan interface{}) { - for _, block := range s.blocks { - dataChan <- flow.NewBlockDigest(block.Header.ID(), block.Header.Height, block.Header.Timestamp) - } - }, - s.requireBlockDigest, - ) -} - // requireBlockDigest ensures that the received block header information matches the expected data. func (s *BlocksProviderSuite) requireBlockDigest(actual interface{}, expected interface{}) { expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) @@ -153,3 +130,26 @@ func (s *BlocksProviderSuite) requireBlockDigest(actual interface{}, expected in s.Require().Equal(expectedResponse.Topic, actualResponse.Topic) s.Require().Equal(expectedResponsePayload, actualResponsePayload) } + +// TestBlockDigestsDataProvider_InvalidArguments tests the behavior of the block digests data provider +// when invalid arguments are provided. It verifies that appropriate errors are returned +// for missing or conflicting arguments. +// This test covers the test cases: +// 1. Missing 'block_status' argument. +// 2. Invalid 'block_status' argument. +// 3. Providing both 'start_block_id' and 'start_block_height' simultaneously. +func (s *BlockDigestsProviderSuite) TestBlockDigestsDataProvider_InvalidArguments() { + ctx := context.Background() + send := make(chan interface{}) + + topic := BlockDigestsTopic + + for _, test := range s.invalidArgumentsTestCases() { + s.Run(test.name, func() { + provider, err := NewBlockDigestsDataProvider(ctx, s.log, s.api, topic, test.arguments, send) + s.Require().Nil(provider) + s.Require().Error(err) + s.Require().Contains(err.Error(), test.expectedErrorMsg) + }) + } +} diff --git a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go index ab194c395cc..adadffd4638 100644 --- a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go @@ -29,27 +29,23 @@ func (s *BlockHeadersProviderSuite) SetupTest() { s.BlocksProviderSuite.SetupTest() } -// TestBlockHeadersDataProvider_InvalidArguments tests the behavior of the block headers data provider -// when invalid arguments are provided. It verifies that appropriate errors are returned -// for missing or conflicting arguments. -// This test covers the test cases: -// 1. Missing 'block_status' argument. -// 2. Invalid 'block_status' argument. -// 3. Providing both 'start_block_id' and 'start_block_height' simultaneously. -func (s *BlockHeadersProviderSuite) TestBlockHeadersDataProvider_InvalidArguments() { - ctx := context.Background() - send := make(chan interface{}) - - topic := BlockHeadersTopic - - for _, test := range s.invalidArgumentsTestCases() { - s.Run(test.name, func() { - provider, err := NewBlockHeadersDataProvider(ctx, s.log, s.api, topic, test.arguments, send) - s.Require().Nil(provider) - s.Require().Error(err) - s.Require().Contains(err.Error(), test.expectedErrorMsg) - }) - } +// TestBlockHeadersDataProvider_HappyPath tests the behavior of the block headers data provider +// when it is configured correctly and operating under normal conditions. It +// validates that block headers are correctly streamed to the channel and ensures +// no unexpected errors occur. +func (s *BlockHeadersProviderSuite) TestBlockHeadersDataProvider_HappyPath() { + testHappyPath( + s.T(), + BlockHeadersTopic, + s.factory, + s.validBlockHeadersArgumentsTestCases(), + func(dataChan chan interface{}) { + for _, block := range s.blocks { + dataChan <- block.Header + } + }, + s.requireBlockHeader, + ) } // validBlockHeadersArgumentsTestCases defines test happy cases for block headers data providers. @@ -116,25 +112,6 @@ func (s *BlockHeadersProviderSuite) validBlockHeadersArgumentsTestCases() []test } } -// TestBlockHeadersDataProvider_HappyPath tests the behavior of the block headers data provider -// when it is configured correctly and operating under normal conditions. It -// validates that block headers are correctly streamed to the channel and ensures -// no unexpected errors occur. -func (s *BlockHeadersProviderSuite) TestBlockHeadersDataProvider_HappyPath() { - testHappyPath( - s.T(), - BlockHeadersTopic, - s.factory, - s.validBlockHeadersArgumentsTestCases(), - func(dataChan chan interface{}) { - for _, block := range s.blocks { - dataChan <- block.Header - } - }, - s.requireBlockHeader, - ) -} - // requireBlockHeaders ensures that the received block header information matches the expected data. func (s *BlockHeadersProviderSuite) requireBlockHeader(actual interface{}, expected interface{}) { expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) @@ -152,3 +129,26 @@ func (s *BlockHeadersProviderSuite) requireBlockHeader(actual interface{}, expec s.Require().Equal(expectedResponse.Topic, actualResponse.Topic) s.Require().Equal(expectedResponsePayload, actualResponsePayload) } + +// TestBlockHeadersDataProvider_InvalidArguments tests the behavior of the block headers data provider +// when invalid arguments are provided. It verifies that appropriate errors are returned +// for missing or conflicting arguments. +// This test covers the test cases: +// 1. Missing 'block_status' argument. +// 2. Invalid 'block_status' argument. +// 3. Providing both 'start_block_id' and 'start_block_height' simultaneously. +func (s *BlockHeadersProviderSuite) TestBlockHeadersDataProvider_InvalidArguments() { + ctx := context.Background() + send := make(chan interface{}) + + topic := BlockHeadersTopic + + for _, test := range s.invalidArgumentsTestCases() { + s.Run(test.name, func() { + provider, err := NewBlockHeadersDataProvider(ctx, s.log, s.api, topic, test.arguments, send) + s.Require().Nil(provider) + s.Require().Error(err) + s.Require().Contains(err.Error(), test.expectedErrorMsg) + }) + } +} diff --git a/engine/access/rest/websockets/data_providers/blocks_provider_test.go b/engine/access/rest/websockets/data_providers/blocks_provider_test.go index f28e9a82e04..d6471e735b4 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider_test.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider_test.go @@ -82,61 +82,34 @@ func (s *BlocksProviderSuite) SetupTest() { s.Require().NotNil(s.factory) } -// invalidArgumentsTestCases returns a list of test cases with invalid argument combinations -// for testing the behavior of block, block headers, block digests data providers. Each test case includes a name, -// a set of input arguments, and the expected error message that should be returned. -// -// The test cases cover scenarios such as: -// 1. Missing the required 'block_status' argument. -// 2. Providing an unknown or invalid 'block_status' value. -// 3. Supplying both 'start_block_id' and 'start_block_height' simultaneously, which is not allowed. -func (s *BlocksProviderSuite) invalidArgumentsTestCases() []testErrType { - return []testErrType{ - { - name: "missing 'block_status' argument", - arguments: models.Arguments{ - "start_block_id": s.rootBlock.ID().String(), - }, - expectedErrorMsg: "'block_status' must be provided", - }, - { - name: "unknown 'block_status' argument", - arguments: models.Arguments{ - "block_status": unknownBlockStatus, - }, - expectedErrorMsg: fmt.Sprintf("invalid 'block_status', must be '%s' or '%s'", parser.Finalized, parser.Sealed), - }, - { - name: "provide both 'start_block_id' and 'start_block_height' arguments", - arguments: models.Arguments{ - "block_status": parser.Finalized, - "start_block_id": s.rootBlock.ID().String(), - "start_block_height": fmt.Sprintf("%d", s.rootBlock.Header.Height), - }, - expectedErrorMsg: "can only provide either 'start_block_id' or 'start_block_height'", +// TestBlocksDataProvider_HappyPath tests the behavior of the block data provider +// when it is configured correctly and operating under normal conditions. It +// validates that blocks are correctly streamed to the channel and ensures +// no unexpected errors occur. +func (s *BlocksProviderSuite) TestBlocksDataProvider_HappyPath() { + s.linkGenerator.On("BlockLink", mock.AnythingOfType("flow.Identifier")).Return( + func(id flow.Identifier) (string, error) { + for _, block := range s.blocks { + if block.ID() == id { + return fmt.Sprintf("/v1/blocks/%s", id), nil + } + } + return "", assert.AnError }, - } -} - -// TestBlocksDataProvider_InvalidArguments tests the behavior of the block data provider -// when invalid arguments are provided. It verifies that appropriate errors are returned -// for missing or conflicting arguments. -// This test covers the test cases: -// 1. Missing 'block_status' argument. -// 2. Invalid 'block_status' argument. -// 3. Providing both 'start_block_id' and 'start_block_height' simultaneously. -func (s *BlocksProviderSuite) TestBlocksDataProvider_InvalidArguments() { - ctx := context.Background() - send := make(chan interface{}) + ) - for _, test := range s.invalidArgumentsTestCases() { - s.Run(test.name, func() { - provider, err := NewBlocksDataProvider(ctx, s.log, s.api, nil, BlocksTopic, test.arguments, send) - s.Require().Nil(provider) - s.Require().Error(err) - s.Require().Contains(err.Error(), test.expectedErrorMsg) - }) - } + testHappyPath( + s.T(), + BlocksTopic, + s.factory, + s.validBlockArgumentsTestCases(), + func(dataChan chan interface{}) { + for _, block := range s.blocks { + dataChan <- block + } + }, + s.requireBlock, + ) } // validBlockArgumentsTestCases defines test happy cases for block data providers. @@ -208,37 +181,7 @@ func (s *BlocksProviderSuite) validBlockArgumentsTestCases() []testType { } } -// TestBlocksDataProvider_HappyPath tests the behavior of the block data provider -// when it is configured correctly and operating under normal conditions. It -// validates that blocks are correctly streamed to the channel and ensures -// no unexpected errors occur. -func (s *BlocksProviderSuite) TestBlocksDataProvider_HappyPath() { - s.linkGenerator.On("BlockLink", mock.AnythingOfType("flow.Identifier")).Return( - func(id flow.Identifier) (string, error) { - for _, block := range s.blocks { - if block.ID() == id { - return fmt.Sprintf("/v1/blocks/%s", id), nil - } - } - return "", assert.AnError - }, - ) - - testHappyPath( - s.T(), - BlocksTopic, - s.factory, - s.validBlockArgumentsTestCases(), - func(dataChan chan interface{}) { - for _, block := range s.blocks { - dataChan <- block - } - }, - s.requireBlock, - ) -} - -// requireBlocks ensures that the received block information matches the expected data. +// requireBlock ensures that the received block information matches the expected data. func (s *BlocksProviderSuite) requireBlock(actual interface{}, expected interface{}) { expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) @@ -276,3 +219,60 @@ func (s *BlocksProviderSuite) expectedBlockResponses( return responses } + +// TestBlocksDataProvider_InvalidArguments tests the behavior of the block data provider +// when invalid arguments are provided. It verifies that appropriate errors are returned +// for missing or conflicting arguments. +// This test covers the test cases: +// 1. Missing 'block_status' argument. +// 2. Invalid 'block_status' argument. +// 3. Providing both 'start_block_id' and 'start_block_height' simultaneously. +func (s *BlocksProviderSuite) TestBlocksDataProvider_InvalidArguments() { + ctx := context.Background() + send := make(chan interface{}) + + for _, test := range s.invalidArgumentsTestCases() { + s.Run(test.name, func() { + provider, err := NewBlocksDataProvider(ctx, s.log, s.api, nil, BlocksTopic, test.arguments, send) + s.Require().Nil(provider) + s.Require().Error(err) + s.Require().Contains(err.Error(), test.expectedErrorMsg) + }) + } +} + +// invalidArgumentsTestCases returns a list of test cases with invalid argument combinations +// for testing the behavior of block, block headers, block digests data providers. Each test case includes a name, +// a set of input arguments, and the expected error message that should be returned. +// +// The test cases cover scenarios such as: +// 1. Missing the required 'block_status' argument. +// 2. Providing an unknown or invalid 'block_status' value. +// 3. Supplying both 'start_block_id' and 'start_block_height' simultaneously, which is not allowed. +func (s *BlocksProviderSuite) invalidArgumentsTestCases() []testErrType { + return []testErrType{ + { + name: "missing 'block_status' argument", + arguments: models.Arguments{ + "start_block_id": s.rootBlock.ID().String(), + }, + expectedErrorMsg: "'block_status' must be provided", + }, + { + name: "unknown 'block_status' argument", + arguments: models.Arguments{ + "block_status": unknownBlockStatus, + }, + expectedErrorMsg: fmt.Sprintf("invalid 'block_status', must be '%s' or '%s'", parser.Finalized, parser.Sealed), + }, + { + name: "provide both 'start_block_id' and 'start_block_height' arguments", + arguments: models.Arguments{ + "block_status": parser.Finalized, + "start_block_id": s.rootBlock.ID().String(), + "start_block_height": fmt.Sprintf("%d", s.rootBlock.Header.Height), + }, + expectedErrorMsg: "can only provide either 'start_block_id' or 'start_block_height'", + }, + } +} diff --git a/engine/access/rest/websockets/data_providers/events_provider_test.go b/engine/access/rest/websockets/data_providers/events_provider_test.go index 91b09239f1a..20a8ab98e15 100644 --- a/engine/access/rest/websockets/data_providers/events_provider_test.go +++ b/engine/access/rest/websockets/data_providers/events_provider_test.go @@ -154,72 +154,38 @@ func (s *EventsProviderSuite) requireEvents(actual interface{}, expected interfa s.Require().Equal(expectedResponsePayload.MessageIndex, actualResponsePayload.MessageIndex) } -// invalidArgumentsTestCases returns a list of test cases with invalid argument combinations -// for testing the behavior of events data providers. Each test case includes a name, -// a set of input arguments, and the expected error message that should be returned. -// -// The test cases cover scenarios such as: -// 1. Supplying both 'start_block_id' and 'start_block_height' simultaneously, which is not allowed. -// 2. Providing invalid 'start_block_id' value. -// 3. Providing invalid 'start_block_height' value. -func invalidArgumentsTestCases() []testErrType { - return []testErrType{ - { - name: "provide both 'start_block_id' and 'start_block_height' arguments", - arguments: models.Arguments{ - "start_block_id": unittest.BlockFixture().ID().String(), - "start_block_height": fmt.Sprintf("%d", unittest.BlockFixture().Header.Height), - }, - expectedErrorMsg: "can only provide either 'start_block_id' or 'start_block_height'", - }, - { - name: "invalid 'start_block_id' argument", - arguments: map[string]interface{}{ - "start_block_id": "invalid_block_id", - }, - expectedErrorMsg: "invalid ID format", - }, - { - name: "invalid 'start_block_height' argument", - arguments: map[string]interface{}{ - "start_block_height": "-1", - }, - expectedErrorMsg: "value must be an unsigned 64 bit integer", - }, +// backendEventsResponses creates backend events responses based on the provided events. +func (s *EventsProviderSuite) backendEventsResponses(events []flow.Event) []*backend.EventsResponse { + responses := make([]*backend.EventsResponse, len(events)) + + for i := range events { + responses[i] = &backend.EventsResponse{ + Height: s.rootBlock.Header.Height, + BlockID: s.rootBlock.ID(), + Events: events, + BlockTimestamp: s.rootBlock.Header.Timestamp, + } } + + return responses } -// TestEventsDataProvider_InvalidArguments tests the behavior of the event data provider -// when invalid arguments are provided. It verifies that appropriate errors are returned -// for missing or conflicting arguments. -// This test covers the test cases: -// 1. Providing both 'start_block_id' and 'start_block_height' simultaneously. -// 2. Invalid 'start_block_id' argument. -// 3. Invalid 'start_block_height' argument. -func (s *EventsProviderSuite) TestEventsDataProvider_InvalidArguments() { - ctx := context.Background() - send := make(chan interface{}) +// expectedEventsResponses creates the expected responses for the provided backend responses. +func (s *EventsProviderSuite) expectedEventsResponses( + backendResponses []*backend.EventsResponse, +) []interface{} { + expectedResponses := make([]interface{}, len(backendResponses)) - topic := EventsTopic + for i, resp := range backendResponses { + var expectedResponsePayload models.EventResponse + expectedResponsePayload.Build(resp, uint64(i)) - for _, test := range invalidArgumentsTestCases() { - s.Run(test.name, func() { - provider, err := NewEventsDataProvider( - ctx, - s.log, - s.api, - topic, - test.arguments, - send, - s.chain, - state_stream.DefaultEventFilterConfig, - subscription.DefaultHeartbeatInterval, - ) - s.Require().Nil(provider) - s.Require().Error(err) - s.Require().Contains(err.Error(), test.expectedErrorMsg) - }) + expectedResponses[i] = &models.BaseDataProvidersResponse{ + Topic: EventsTopic, + Payload: &expectedResponsePayload, + } } + return expectedResponses } // TestMessageIndexEventProviderResponse_HappyPath tests that MessageIndex values in response are strictly increasing. @@ -304,36 +270,70 @@ func (s *EventsProviderSuite) TestMessageIndexEventProviderResponse_HappyPath() } } -// backendEventsResponses creates backend events responses based on the provided events. -func (s *EventsProviderSuite) backendEventsResponses(events []flow.Event) []*backend.EventsResponse { - responses := make([]*backend.EventsResponse, len(events)) +// TestEventsDataProvider_InvalidArguments tests the behavior of the event data provider +// when invalid arguments are provided. It verifies that appropriate errors are returned +// for missing or conflicting arguments. +// This test covers the test cases: +// 1. Providing both 'start_block_id' and 'start_block_height' simultaneously. +// 2. Invalid 'start_block_id' argument. +// 3. Invalid 'start_block_height' argument. +func (s *EventsProviderSuite) TestEventsDataProvider_InvalidArguments() { + ctx := context.Background() + send := make(chan interface{}) - for i := range events { - responses[i] = &backend.EventsResponse{ - Height: s.rootBlock.Header.Height, - BlockID: s.rootBlock.ID(), - Events: events, - BlockTimestamp: s.rootBlock.Header.Timestamp, - } - } + topic := EventsTopic - return responses + for _, test := range invalidArgumentsTestCases() { + s.Run(test.name, func() { + provider, err := NewEventsDataProvider( + ctx, + s.log, + s.api, + topic, + test.arguments, + send, + s.chain, + state_stream.DefaultEventFilterConfig, + subscription.DefaultHeartbeatInterval, + ) + s.Require().Nil(provider) + s.Require().Error(err) + s.Require().Contains(err.Error(), test.expectedErrorMsg) + }) + } } -// expectedEventsResponses creates the expected responses for the provided backend responses. -func (s *EventsProviderSuite) expectedEventsResponses( - backendResponses []*backend.EventsResponse, -) []interface{} { - expectedResponses := make([]interface{}, len(backendResponses)) - - for i, resp := range backendResponses { - var expectedResponsePayload models.EventResponse - expectedResponsePayload.Build(resp, uint64(i)) - - expectedResponses[i] = &models.BaseDataProvidersResponse{ - Topic: EventsTopic, - Payload: &expectedResponsePayload, - } +// invalidArgumentsTestCases returns a list of test cases with invalid argument combinations +// for testing the behavior of events data providers. Each test case includes a name, +// a set of input arguments, and the expected error message that should be returned. +// +// The test cases cover scenarios such as: +// 1. Supplying both 'start_block_id' and 'start_block_height' simultaneously, which is not allowed. +// 2. Providing invalid 'start_block_id' value. +// 3. Providing invalid 'start_block_height' value. +func invalidArgumentsTestCases() []testErrType { + return []testErrType{ + { + name: "provide both 'start_block_id' and 'start_block_height' arguments", + arguments: models.Arguments{ + "start_block_id": unittest.BlockFixture().ID().String(), + "start_block_height": fmt.Sprintf("%d", unittest.BlockFixture().Header.Height), + }, + expectedErrorMsg: "can only provide either 'start_block_id' or 'start_block_height'", + }, + { + name: "invalid 'start_block_id' argument", + arguments: map[string]interface{}{ + "start_block_id": "invalid_block_id", + }, + expectedErrorMsg: "invalid ID format", + }, + { + name: "invalid 'start_block_height' argument", + arguments: map[string]interface{}{ + "start_block_height": "-1", + }, + expectedErrorMsg: "value must be an unsigned 64 bit integer", + }, } - return expectedResponses } diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go index 05c1c7a63ce..b145630763f 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go @@ -162,6 +162,30 @@ func (s *TransactionStatusesProviderSuite) requireTransactionStatuses( require.Equal(s.T(), expectedResponsePayload.TransactionResult.BlockId, actualResponsePayload.TransactionResult.BlockId) } +func backendTransactionStatusesResponse(block flow.Block) []*access.TransactionResult { + id := unittest.IdentifierFixture() + cid := unittest.IdentifierFixture() + txr := access.TransactionResult{ + Status: flow.TransactionStatusSealed, + StatusCode: 10, + Events: []flow.Event{ + unittest.EventFixture(flow.EventAccountCreated, 1, 0, id, 200), + }, + ErrorMessage: "", + BlockID: block.ID(), + CollectionID: cid, + BlockHeight: block.Header.Height, + } + + var expectedTxResultsResponses []*access.TransactionResult + + for i := 0; i < 2; i++ { + expectedTxResultsResponses = append(expectedTxResultsResponses, &txr) + } + + return expectedTxResultsResponses +} + // expectedTransactionStatusesResponses creates the expected responses for the provided backend responses. func (s *TransactionStatusesProviderSuite) expectedTransactionStatusesResponses( backendResponses []*access.TransactionResult, @@ -182,76 +206,6 @@ func (s *TransactionStatusesProviderSuite) expectedTransactionStatusesResponses( return expectedResponses } -// TestTransactionStatusesDataProvider_InvalidArguments tests the behavior of the transaction statuses data provider -// when invalid arguments are provided. It verifies that appropriate errors are returned -// for missing or conflicting arguments. -func (s *TransactionStatusesProviderSuite) TestTransactionStatusesDataProvider_InvalidArguments() { - ctx := context.Background() - send := make(chan interface{}) - - topic := TransactionStatusesTopic - - for _, test := range invalidTransactionStatusesArgumentsTestCases() { - s.Run(test.name, func() { - provider, err := NewTransactionStatusesDataProvider( - ctx, - s.log, - s.api, - s.linkGenerator, - topic, - test.arguments, - send, - ) - s.Require().Nil(provider) - s.Require().Error(err) - s.Require().Contains(err.Error(), test.expectedErrorMsg) - }) - } -} - -// invalidTransactionStatusesArgumentsTestCases returns a list of test cases with invalid argument combinations -// for testing the behavior of transaction statuses data providers. Each test case includes a name, -// a set of input arguments, and the expected error message that should be returned. -// -// The test cases cover scenarios such as: -// 1. Providing both 'start_block_id' and 'start_block_height' simultaneously. -// 2. Providing invalid 'tx_id' value. -// 3. Providing invalid 'start_block_id' value. -// 4. Invalid 'start_block_id' argument. -func invalidTransactionStatusesArgumentsTestCases() []testErrType { - return []testErrType{ - { - name: "provide both 'start_block_id' and 'start_block_height' arguments", - arguments: models.Arguments{ - "start_block_id": unittest.BlockFixture().ID().String(), - "start_block_height": fmt.Sprintf("%d", unittest.BlockFixture().Header.Height), - }, - expectedErrorMsg: "can only provide either 'start_block_id' or 'start_block_height'", - }, - { - name: "invalid 'tx_id' argument", - arguments: map[string]interface{}{ - "tx_id": "invalid_tx_id", - }, - expectedErrorMsg: "invalid ID format", - }, - { - name: "invalid 'start_block_id' argument", - arguments: map[string]interface{}{ - "start_block_id": "invalid_block_id", - }, - expectedErrorMsg: "invalid ID format", - }, - { - name: "invalid 'start_block_height' argument", - arguments: map[string]interface{}{ - "start_block_height": "-1", - }, - expectedErrorMsg: "value must be an unsigned 64 bit integer", - }, - } -} - // TestMessageIndexTransactionStatusesProviderResponse_HappyPath tests that MessageIndex values in response are strictly increasing. func (s *TransactionStatusesProviderSuite) TestMessageIndexTransactionStatusesProviderResponse_HappyPath() { ctx := context.Background() @@ -348,26 +302,72 @@ func (s *TransactionStatusesProviderSuite) TestMessageIndexTransactionStatusesPr } } -func backendTransactionStatusesResponse(block flow.Block) []*access.TransactionResult { - id := unittest.IdentifierFixture() - cid := unittest.IdentifierFixture() - txr := access.TransactionResult{ - Status: flow.TransactionStatusSealed, - StatusCode: 10, - Events: []flow.Event{ - unittest.EventFixture(flow.EventAccountCreated, 1, 0, id, 200), - }, - ErrorMessage: "", - BlockID: block.ID(), - CollectionID: cid, - BlockHeight: block.Header.Height, - } +// TestTransactionStatusesDataProvider_InvalidArguments tests the behavior of the transaction statuses data provider +// when invalid arguments are provided. It verifies that appropriate errors are returned +// for missing or conflicting arguments. +func (s *TransactionStatusesProviderSuite) TestTransactionStatusesDataProvider_InvalidArguments() { + ctx := context.Background() + send := make(chan interface{}) - var expectedTxResultsResponses []*access.TransactionResult + topic := TransactionStatusesTopic - for i := 0; i < 2; i++ { - expectedTxResultsResponses = append(expectedTxResultsResponses, &txr) + for _, test := range invalidTransactionStatusesArgumentsTestCases() { + s.Run(test.name, func() { + provider, err := NewTransactionStatusesDataProvider( + ctx, + s.log, + s.api, + s.linkGenerator, + topic, + test.arguments, + send, + ) + s.Require().Nil(provider) + s.Require().Error(err) + s.Require().Contains(err.Error(), test.expectedErrorMsg) + }) } +} - return expectedTxResultsResponses +// invalidTransactionStatusesArgumentsTestCases returns a list of test cases with invalid argument combinations +// for testing the behavior of transaction statuses data providers. Each test case includes a name, +// a set of input arguments, and the expected error message that should be returned. +// +// The test cases cover scenarios such as: +// 1. Providing both 'start_block_id' and 'start_block_height' simultaneously. +// 2. Providing invalid 'tx_id' value. +// 3. Providing invalid 'start_block_id' value. +// 4. Invalid 'start_block_id' argument. +func invalidTransactionStatusesArgumentsTestCases() []testErrType { + return []testErrType{ + { + name: "provide both 'start_block_id' and 'start_block_height' arguments", + arguments: models.Arguments{ + "start_block_id": unittest.BlockFixture().ID().String(), + "start_block_height": fmt.Sprintf("%d", unittest.BlockFixture().Header.Height), + }, + expectedErrorMsg: "can only provide either 'start_block_id' or 'start_block_height'", + }, + { + name: "invalid 'tx_id' argument", + arguments: map[string]interface{}{ + "tx_id": "invalid_tx_id", + }, + expectedErrorMsg: "invalid ID format", + }, + { + name: "invalid 'start_block_id' argument", + arguments: map[string]interface{}{ + "start_block_id": "invalid_block_id", + }, + expectedErrorMsg: "invalid ID format", + }, + { + name: "invalid 'start_block_height' argument", + arguments: map[string]interface{}{ + "start_block_height": "-1", + }, + expectedErrorMsg: "value must be an unsigned 64 bit integer", + }, + } } From c8684815905c4f58c4fce7535de6ea2083a85dfe Mon Sep 17 00:00:00 2001 From: Andrii Date: Fri, 10 Jan 2025 14:40:19 +0200 Subject: [PATCH 10/12] Refactored Build function BaseMessage for webSockets --- .../websockets/data_providers/account_statuses_provider.go | 2 +- .../websockets/data_providers/block_digests_provider.go | 2 +- .../websockets/data_providers/block_headers_provider.go | 2 +- .../rest/websockets/data_providers/blocks_provider.go | 2 +- .../rest/websockets/data_providers/events_provider.go | 2 +- .../send_and_get_transaction_statuses_provider.go | 2 +- .../data_providers/transaction_statuses_provider.go | 2 +- engine/access/rest/websockets/models/base_message.go | 6 ++++-- 8 files changed, 11 insertions(+), 9 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider.go b/engine/access/rest/websockets/data_providers/account_statuses_provider.go index 182db1cc0d7..fff6a60b818 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider.go @@ -119,7 +119,7 @@ func (p *AccountStatusesDataProvider) handleResponse() func(accountStatusesRespo accountStatusesPayload.Build(accountStatusesResponse, index) var response models.BaseDataProvidersResponse - response.Build(p.ID().String(), p.Topic(), &accountStatusesPayload) + response.Build(p.ID(), p.Topic(), &accountStatusesPayload) p.send <- &response diff --git a/engine/access/rest/websockets/data_providers/block_digests_provider.go b/engine/access/rest/websockets/data_providers/block_digests_provider.go index 8d9603306e2..14a2d9d8e15 100644 --- a/engine/access/rest/websockets/data_providers/block_digests_provider.go +++ b/engine/access/rest/websockets/data_providers/block_digests_provider.go @@ -66,7 +66,7 @@ func (p *BlockDigestsDataProvider) Run() error { var response models.BaseDataProvidersResponse response.Build( - p.ID().String(), + p.ID(), p.Topic(), &block, ) diff --git a/engine/access/rest/websockets/data_providers/block_headers_provider.go b/engine/access/rest/websockets/data_providers/block_headers_provider.go index 291e3fd76d9..0fc953c20c6 100644 --- a/engine/access/rest/websockets/data_providers/block_headers_provider.go +++ b/engine/access/rest/websockets/data_providers/block_headers_provider.go @@ -67,7 +67,7 @@ func (p *BlockHeadersDataProvider) Run() error { var response models.BaseDataProvidersResponse response.Build( - p.ID().String(), + p.ID(), p.Topic(), &header, ) diff --git a/engine/access/rest/websockets/data_providers/blocks_provider.go b/engine/access/rest/websockets/data_providers/blocks_provider.go index c0de76d0727..938ad00ce8f 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider.go @@ -85,7 +85,7 @@ func (p *BlocksDataProvider) Run() error { } var response models.BaseDataProvidersResponse - response.Build(p.ID().String(), p.Topic(), &block) + response.Build(p.ID(), p.Topic(), &block) return &response, nil }), diff --git a/engine/access/rest/websockets/data_providers/events_provider.go b/engine/access/rest/websockets/data_providers/events_provider.go index b28bf4e2e95..fd589c4b8e4 100644 --- a/engine/access/rest/websockets/data_providers/events_provider.go +++ b/engine/access/rest/websockets/data_providers/events_provider.go @@ -105,7 +105,7 @@ func (p *EventsDataProvider) handleResponse() func(eventsResponse *backend.Event eventsPayload.Build(eventsResponse, index) var response models.BaseDataProvidersResponse - response.Build(p.ID().String(), p.Topic(), &eventsPayload) + response.Build(p.ID(), p.Topic(), &eventsPayload) p.send <- &response diff --git a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go index d1b9f973d1d..f50b6509eb1 100644 --- a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider.go @@ -101,7 +101,7 @@ func (p *SendAndGetTransactionStatusesDataProvider) handleResponse() func(txResu txStatusesPayload.Build(p.linkGenerator, txResults[i], index) var response models.BaseDataProvidersResponse - response.Build(p.ID().String(), p.Topic(), &txStatusesPayload) + response.Build(p.ID(), p.Topic(), &txStatusesPayload) p.send <- &response } diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go index 3b86dd39042..1205be3d5d9 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider.go @@ -112,7 +112,7 @@ func (p *TransactionStatusesDataProvider) handleResponse() func(txResults []*acc txStatusesPayload.Build(p.linkGenerator, txResults[i], index) var response models.BaseDataProvidersResponse - response.Build(p.ID().String(), p.Topic(), &txStatusesPayload) + response.Build(p.ID(), p.Topic(), &txStatusesPayload) p.send <- &response } diff --git a/engine/access/rest/websockets/models/base_message.go b/engine/access/rest/websockets/models/base_message.go index c22ac3d8a57..cfb4fdbae26 100644 --- a/engine/access/rest/websockets/models/base_message.go +++ b/engine/access/rest/websockets/models/base_message.go @@ -1,5 +1,7 @@ package models +import "github.com/google/uuid" + const ( SubscribeAction = "subscribe" UnsubscribeAction = "unsubscribe" @@ -28,9 +30,9 @@ type BaseDataProvidersResponse struct { } // Build creates BaseDataProvidersResponse instance for consistent responses of the data providers. -func (b *BaseDataProvidersResponse) Build(subscriptionID string, topic string, payload interface{}) { +func (b *BaseDataProvidersResponse) Build(subscriptionID uuid.UUID, topic string, payload interface{}) { *b = BaseDataProvidersResponse{ - SubscriptionID: subscriptionID, + SubscriptionID: subscriptionID.String(), Topic: topic, Payload: payload, } From d41ce4b61904b759ea18553458f8e9f5002e9ece Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 22 Jan 2025 16:10:17 +0200 Subject: [PATCH 11/12] Refactored tests to not duplicate code --- .../account_statuses_provider_test.go | 13 ++----------- .../block_digests_provider_test.go | 14 ++------------ .../block_headers_provider_test.go | 14 ++------------ .../data_providers/blocks_provider_test.go | 14 ++------------ .../data_providers/events_provider_test.go | 17 ++++------------- ...nd_get_transaction_statuses_provider_test.go | 13 ++----------- .../transaction_statuses_provider_test.go | 13 ++----------- .../rest/websockets/data_providers/unit_test.go | 11 +++++++++++ 8 files changed, 27 insertions(+), 82 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go index 6c1de11c414..cbd04373cb1 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go @@ -139,17 +139,8 @@ func (s *AccountStatusesProviderSuite) subscribeAccountStatusesDataProviderTestC // requireAccountStatuses ensures that the received account statuses information matches the expected data. func (s *AccountStatusesProviderSuite) requireAccountStatuses(actual interface{}, expected interface{}) { - expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) - - expectedResponsePayload, ok := expectedResponse.Payload.(*models.AccountStatusesResponse) - require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) - - actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) - - actualResponsePayload, ok := actualResponse.Payload.(*models.AccountStatusesResponse) - require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) + expectedResponse, expectedResponsePayload := extractPayload[*models.AccountStatusesResponse](s.T(), expected) + actualResponse, actualResponsePayload := extractPayload[*models.AccountStatusesResponse](s.T(), actual) require.Equal(s.T(), expectedResponsePayload.BlockID, actualResponsePayload.BlockID) require.Equal(s.T(), len(expectedResponsePayload.AccountEvents), len(actualResponsePayload.AccountEvents)) diff --git a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go index a1b42b85abb..c220217e724 100644 --- a/engine/access/rest/websockets/data_providers/block_digests_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_digests_provider_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/onflow/flow-go/engine/access/rest/common/parser" @@ -115,17 +114,8 @@ func (s *BlockDigestsProviderSuite) validBlockDigestsArgumentsTestCases() []test // requireBlockDigest ensures that the received block header information matches the expected data. func (s *BlocksProviderSuite) requireBlockDigest(actual interface{}, expected interface{}) { - expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) - - expectedResponsePayload, ok := expectedResponse.Payload.(*models.BlockDigest) - require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) - - actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) - - actualResponsePayload, ok := actualResponse.Payload.(*models.BlockDigest) - require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) + expectedResponse, expectedResponsePayload := extractPayload[*models.BlockDigest](s.T(), expected) + actualResponse, actualResponsePayload := extractPayload[*models.BlockDigest](s.T(), actual) s.Require().Equal(expectedResponse.Topic, actualResponse.Topic) s.Require().Equal(expectedResponsePayload, actualResponsePayload) diff --git a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go index bd6d8c5f027..ec0a222c93d 100644 --- a/engine/access/rest/websockets/data_providers/block_headers_provider_test.go +++ b/engine/access/rest/websockets/data_providers/block_headers_provider_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" commonmodels "github.com/onflow/flow-go/engine/access/rest/common/models" @@ -114,17 +113,8 @@ func (s *BlockHeadersProviderSuite) validBlockHeadersArgumentsTestCases() []test // requireBlockHeaders ensures that the received block header information matches the expected data. func (s *BlockHeadersProviderSuite) requireBlockHeader(actual interface{}, expected interface{}) { - expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) - - expectedResponsePayload, ok := expectedResponse.Payload.(*commonmodels.BlockHeader) - require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) - - actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) - - actualResponsePayload, ok := actualResponse.Payload.(*commonmodels.BlockHeader) - require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) + expectedResponse, expectedResponsePayload := extractPayload[*commonmodels.BlockHeader](s.T(), expected) + actualResponse, actualResponsePayload := extractPayload[*commonmodels.BlockHeader](s.T(), actual) s.Require().Equal(expectedResponse.Topic, actualResponse.Topic) s.Require().Equal(expectedResponsePayload, actualResponsePayload) diff --git a/engine/access/rest/websockets/data_providers/blocks_provider_test.go b/engine/access/rest/websockets/data_providers/blocks_provider_test.go index fd745f09ed4..41ba3bf7a0a 100644 --- a/engine/access/rest/websockets/data_providers/blocks_provider_test.go +++ b/engine/access/rest/websockets/data_providers/blocks_provider_test.go @@ -9,7 +9,6 @@ import ( "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" accessmock "github.com/onflow/flow-go/access/mock" @@ -183,17 +182,8 @@ func (s *BlocksProviderSuite) validBlockArgumentsTestCases() []testType { // requireBlock ensures that the received block information matches the expected data. func (s *BlocksProviderSuite) requireBlock(actual interface{}, expected interface{}) { - expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) - - expectedResponsePayload, ok := expectedResponse.Payload.(*commonmodels.Block) - require.True(s.T(), ok, "unexpected response payload type: %T", expectedResponse.Payload) - - actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) - - actualResponsePayload, ok := actualResponse.Payload.(*commonmodels.Block) - require.True(s.T(), ok, "unexpected response payload type: %T", actualResponse.Payload) + expectedResponse, expectedResponsePayload := extractPayload[*commonmodels.Block](s.T(), expected) + actualResponse, actualResponsePayload := extractPayload[*commonmodels.Block](s.T(), actual) s.Require().Equal(expectedResponse.Topic, actualResponse.Topic) s.Require().Equal(expectedResponsePayload, actualResponsePayload) diff --git a/engine/access/rest/websockets/data_providers/events_provider_test.go b/engine/access/rest/websockets/data_providers/events_provider_test.go index d0659a7db5d..9a8e2c7e42a 100644 --- a/engine/access/rest/websockets/data_providers/events_provider_test.go +++ b/engine/access/rest/websockets/data_providers/events_provider_test.go @@ -9,7 +9,6 @@ import ( "github.com/rs/zerolog" "github.com/stretchr/testify/mock" - "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" "github.com/onflow/flow-go/engine/access/rest/websockets/models" @@ -139,20 +138,12 @@ func (s *EventsProviderSuite) subscribeEventsDataProviderTestCases(backendRespon // requireEvents ensures that the received event information matches the expected data. func (s *EventsProviderSuite) requireEvents(actual interface{}, expected interface{}) { - expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) + expectedResponse, expectedResponsePayload := extractPayload[*models.EventResponse](s.T(), expected) + actualResponse, actualResponsePayload := extractPayload[*models.EventResponse](s.T(), actual) - expectedResponsePayload, ok := expectedResponse.Payload.(*models.EventResponse) - require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) - - actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) - - actualResponsePayload, ok := actualResponse.Payload.(*models.EventResponse) - require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) - - s.Require().ElementsMatch(expectedResponsePayload.Events, actualResponsePayload.Events) + s.Require().Equal(expectedResponse.Topic, actualResponse.Topic) s.Require().Equal(expectedResponsePayload.MessageIndex, actualResponsePayload.MessageIndex) + s.Require().ElementsMatch(expectedResponsePayload.Events, actualResponsePayload.Events) } // backendEventsResponses creates backend events responses based on the provided events. diff --git a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go index 7a93acb8521..353164eb723 100644 --- a/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/send_and_get_transaction_statuses_provider_test.go @@ -111,17 +111,8 @@ func (s *SendTransactionStatusesProviderSuite) requireTransactionStatuses( actual interface{}, expected interface{}, ) { - expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) - - expectedResponsePayload, ok := expectedResponse.Payload.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) - - actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) - - actualResponsePayload, ok := actualResponse.Payload.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) + expectedResponse, expectedResponsePayload := extractPayload[*models.TransactionStatusesResponse](s.T(), expected) + actualResponse, actualResponsePayload := extractPayload[*models.TransactionStatusesResponse](s.T(), actual) require.Equal(s.T(), expectedResponse.Topic, actualResponse.Topic) require.Equal(s.T(), expectedResponsePayload.TransactionResult.BlockId, actualResponsePayload.TransactionResult.BlockId) diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go index ff2fc3d5281..ed8ddac8826 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go @@ -147,17 +147,8 @@ func (s *TransactionStatusesProviderSuite) requireTransactionStatuses( actual interface{}, expected interface{}, ) { - expectedResponse, ok := expected.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", expected) - - expectedResponsePayload, ok := expectedResponse.Payload.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "Unexpected response payload type: %T", expectedResponse.Payload) - - actualResponse, ok := actual.(*models.BaseDataProvidersResponse) - require.True(s.T(), ok, "Expected *models.BaseDataProvidersResponse, got %T", actual) - - actualResponsePayload, ok := actualResponse.Payload.(*models.TransactionStatusesResponse) - require.True(s.T(), ok, "Unexpected response payload type: %T", actualResponse.Payload) + expectedResponse, expectedResponsePayload := extractPayload[*models.TransactionStatusesResponse](s.T(), expected) + actualResponse, actualResponsePayload := extractPayload[*models.TransactionStatusesResponse](s.T(), actual) require.Equal(s.T(), expectedResponse.Topic, actualResponse.Topic) require.Equal(s.T(), expectedResponsePayload.TransactionResult.BlockId, actualResponsePayload.TransactionResult.BlockId) diff --git a/engine/access/rest/websockets/data_providers/unit_test.go b/engine/access/rest/websockets/data_providers/unit_test.go index cbc75393db9..334155e45af 100644 --- a/engine/access/rest/websockets/data_providers/unit_test.go +++ b/engine/access/rest/websockets/data_providers/unit_test.go @@ -100,3 +100,14 @@ func testHappyPath( }) } } + +// extractPayload extracts the BaseDataProvidersResponse and its typed Payload. +func extractPayload[T any](t *testing.T, v interface{}) (*models.BaseDataProvidersResponse, T) { + response, ok := v.(*models.BaseDataProvidersResponse) + require.True(t, ok, "Expected *models.BaseDataProvidersResponse, got %T", v) + + payload, ok := response.Payload.(T) + require.True(t, ok, "Unexpected response payload type: %T", response.Payload) + + return response, payload +} From cc5412a4f0ba3ae8833118bb8abc3c13e4d3317e Mon Sep 17 00:00:00 2001 From: Andrii Date: Wed, 22 Jan 2025 19:10:26 +0200 Subject: [PATCH 12/12] Refactored MessageIndexTests for data providers --- .../data_providers/account_statuses_provider_test.go | 7 ++----- .../rest/websockets/data_providers/events_provider_test.go | 5 +---- .../data_providers/transaction_statuses_provider_test.go | 6 +----- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go index cbd04373cb1..641fba5116c 100644 --- a/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/account_statuses_provider_test.go @@ -269,13 +269,10 @@ func (s *AccountStatusesProviderSuite) TestMessageIndexAccountStatusesProviderRe var responses []*models.AccountStatusesResponse for i := 0; i < accountStatusesCount; i++ { res := <-send - accountStatusesRes, ok := res.(*models.BaseDataProvidersResponse) - s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) - accountStatusesResData, ok := accountStatusesRes.Payload.(*models.AccountStatusesResponse) - s.Require().True(ok, "Expected *models.AccountStatusesResponse, got %T", res) + _, accStatusesResponsePayload := extractPayload[*models.AccountStatusesResponse](s.T(), res) - responses = append(responses, accountStatusesResData) + responses = append(responses, accStatusesResponsePayload) } // Wait for the provider goroutine to finish diff --git a/engine/access/rest/websockets/data_providers/events_provider_test.go b/engine/access/rest/websockets/data_providers/events_provider_test.go index 9a8e2c7e42a..f58a976c3e5 100644 --- a/engine/access/rest/websockets/data_providers/events_provider_test.go +++ b/engine/access/rest/websockets/data_providers/events_provider_test.go @@ -245,11 +245,8 @@ func (s *EventsProviderSuite) TestMessageIndexEventProviderResponse_HappyPath() var responses []*models.EventResponse for i := 0; i < eventsCount; i++ { res := <-send - eventRes, ok := res.(*models.BaseDataProvidersResponse) - s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) - eventResData, ok := eventRes.Payload.(*models.EventResponse) - s.Require().True(ok, "Expected *models.EventResponse, got %T", res) + _, eventResData := extractPayload[*models.EventResponse](s.T(), res) responses = append(responses, eventResData) } diff --git a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go index ed8ddac8826..82667d288b9 100644 --- a/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go +++ b/engine/access/rest/websockets/data_providers/transaction_statuses_provider_test.go @@ -277,11 +277,7 @@ func (s *TransactionStatusesProviderSuite) TestMessageIndexTransactionStatusesPr for i := 0; i < txStatusesCount; i++ { res := <-send - txStatusesRes, ok := res.(*models.BaseDataProvidersResponse) - s.Require().True(ok, "Expected *models.BaseDataProvidersResponse, got %T", res) - - txStatusesResData, ok := txStatusesRes.Payload.(*models.TransactionStatusesResponse) - s.Require().True(ok, "Expected *models.TransactionStatusesResponse, got %T", res) + _, txStatusesResData := extractPayload[*models.TransactionStatusesResponse](s.T(), res) responses = append(responses, txStatusesResData) }