Skip to content

Commit f8d711f

Browse files
Added happy case unit tests for providers
1 parent ff3851b commit f8d711f

File tree

4 files changed

+326
-11
lines changed

4 files changed

+326
-11
lines changed

engine/access/rest/websockets/data_providers/block_digests_provider_test.go

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ package data_providers
22

33
import (
44
"context"
5+
"strconv"
56
"testing"
67

8+
"github.com/stretchr/testify/mock"
9+
"github.com/stretchr/testify/require"
710
"github.com/stretchr/testify/suite"
11+
12+
"github.com/onflow/flow-go/engine/access/rest/common/parser"
13+
"github.com/onflow/flow-go/engine/access/rest/websockets/models"
14+
statestreamsmock "github.com/onflow/flow-go/engine/access/state_stream/mock"
15+
"github.com/onflow/flow-go/model/flow"
816
)
917

1018
type BlockDigestsProviderSuite struct {
@@ -43,4 +51,79 @@ func (s *BlockDigestsProviderSuite) TestBlockDigestsDataProvider_InvalidArgument
4351
}
4452
}
4553

46-
// TODO: add tests for responses after the WebsocketController is ready
54+
// validBlockDigestsArgumentsTestCases defines test happy cases for block digests data providers.
55+
// Each test case specifies input arguments, and setup functions for the mock API used in the test.
56+
func (s *BlockDigestsProviderSuite) validBlockDigestsArgumentsTestCases() []testType {
57+
return []testType{
58+
{
59+
name: "happy path with start_block_id argument",
60+
arguments: models.Arguments{
61+
"start_block_id": s.rootBlock.ID().String(),
62+
"block_status": parser.Finalized,
63+
},
64+
setupBackend: func(sub *statestreamsmock.Subscription) {
65+
s.api.On(
66+
"SubscribeBlockDigestsFromStartBlockID",
67+
mock.Anything,
68+
s.rootBlock.ID(),
69+
flow.BlockStatusFinalized,
70+
).Return(sub).Once()
71+
},
72+
},
73+
{
74+
name: "happy path with start_block_height argument",
75+
arguments: models.Arguments{
76+
"start_block_height": strconv.FormatUint(s.rootBlock.Header.Height, 10),
77+
"block_status": parser.Finalized,
78+
},
79+
setupBackend: func(sub *statestreamsmock.Subscription) {
80+
s.api.On(
81+
"SubscribeBlockDigestsFromStartHeight",
82+
mock.Anything,
83+
s.rootBlock.Header.Height,
84+
flow.BlockStatusFinalized,
85+
).Return(sub).Once()
86+
},
87+
},
88+
{
89+
name: "happy path without any start argument",
90+
arguments: models.Arguments{
91+
"block_status": parser.Finalized,
92+
},
93+
setupBackend: func(sub *statestreamsmock.Subscription) {
94+
s.api.On(
95+
"SubscribeBlockDigestsFromLatest",
96+
mock.Anything,
97+
flow.BlockStatusFinalized,
98+
).Return(sub).Once()
99+
},
100+
},
101+
}
102+
}
103+
104+
// TestBlockDigestsDataProvider_HappyPath tests the behavior of the block digests data provider
105+
// when it is configured correctly and operating under normal conditions. It
106+
// validates that block digests are correctly streamed to the channel and ensures
107+
// no unexpected errors occur.
108+
func (s *BlockDigestsProviderSuite) TestBlockDigestsDataProvider_HappyPath() {
109+
s.testHappyPath(
110+
BlockDigestsTopic,
111+
s.validBlockDigestsArgumentsTestCases(),
112+
func(dataChan chan interface{}, blocks []*flow.Block) {
113+
for _, block := range blocks {
114+
dataChan <- flow.NewBlockDigest(block.Header.ID(), block.Header.Height, block.Header.Timestamp)
115+
}
116+
},
117+
s.requireBlockDigests,
118+
)
119+
}
120+
121+
// requireBlockHeaders ensures that the received block header information matches the expected data.
122+
func (s *BlocksProviderSuite) requireBlockDigests(v interface{}, expectedBlock *flow.Block) {
123+
actualResponse, ok := v.(*models.BlockDigestMessageResponse)
124+
require.True(s.T(), ok, "unexpected response type: %T", v)
125+
126+
s.Require().Equal(expectedBlock.Header.ID(), actualResponse.Block.ID())
127+
s.Require().Equal(expectedBlock.Header.Height, actualResponse.Block.Height)
128+
s.Require().Equal(expectedBlock.Header.Timestamp, actualResponse.Block.Timestamp)
129+
}

engine/access/rest/websockets/data_providers/block_headers_provider_test.go

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ package data_providers
22

33
import (
44
"context"
5+
"strconv"
56
"testing"
67

8+
"github.com/stretchr/testify/mock"
9+
"github.com/stretchr/testify/require"
710
"github.com/stretchr/testify/suite"
11+
12+
"github.com/onflow/flow-go/engine/access/rest/common/parser"
13+
"github.com/onflow/flow-go/engine/access/rest/websockets/models"
14+
statestreamsmock "github.com/onflow/flow-go/engine/access/state_stream/mock"
15+
"github.com/onflow/flow-go/model/flow"
816
)
917

1018
type BlockHeadersProviderSuite struct {
@@ -43,4 +51,77 @@ func (s *BlockHeadersProviderSuite) TestBlockHeadersDataProvider_InvalidArgument
4351
}
4452
}
4553

46-
// TODO: add tests for responses after the WebsocketController is ready
54+
// validBlockHeadersArgumentsTestCases defines test happy cases for block headers data providers.
55+
// Each test case specifies input arguments, and setup functions for the mock API used in the test.
56+
func (s *BlockHeadersProviderSuite) validBlockHeadersArgumentsTestCases() []testType {
57+
return []testType{
58+
{
59+
name: "happy path with start_block_id argument",
60+
arguments: models.Arguments{
61+
"start_block_id": s.rootBlock.ID().String(),
62+
"block_status": parser.Finalized,
63+
},
64+
setupBackend: func(sub *statestreamsmock.Subscription) {
65+
s.api.On(
66+
"SubscribeBlockHeadersFromStartBlockID",
67+
mock.Anything,
68+
s.rootBlock.ID(),
69+
flow.BlockStatusFinalized,
70+
).Return(sub).Once()
71+
},
72+
},
73+
{
74+
name: "happy path with start_block_height argument",
75+
arguments: models.Arguments{
76+
"start_block_height": strconv.FormatUint(s.rootBlock.Header.Height, 10),
77+
"block_status": parser.Finalized,
78+
},
79+
setupBackend: func(sub *statestreamsmock.Subscription) {
80+
s.api.On(
81+
"SubscribeBlockHeadersFromStartHeight",
82+
mock.Anything,
83+
s.rootBlock.Header.Height,
84+
flow.BlockStatusFinalized,
85+
).Return(sub).Once()
86+
},
87+
},
88+
{
89+
name: "happy path without any start argument",
90+
arguments: models.Arguments{
91+
"block_status": parser.Finalized,
92+
},
93+
setupBackend: func(sub *statestreamsmock.Subscription) {
94+
s.api.On(
95+
"SubscribeBlockHeadersFromLatest",
96+
mock.Anything,
97+
flow.BlockStatusFinalized,
98+
).Return(sub).Once()
99+
},
100+
},
101+
}
102+
}
103+
104+
// TestBlockHeadersDataProvider_HappyPath tests the behavior of the block headers data provider
105+
// when it is configured correctly and operating under normal conditions. It
106+
// validates that block headers are correctly streamed to the channel and ensures
107+
// no unexpected errors occur.
108+
func (s *BlockHeadersProviderSuite) TestBlockHeadersDataProvider_HappyPath() {
109+
s.testHappyPath(
110+
BlockHeadersTopic,
111+
s.validBlockHeadersArgumentsTestCases(),
112+
func(dataChan chan interface{}, blocks []*flow.Block) {
113+
for _, block := range blocks {
114+
dataChan <- block.Header
115+
}
116+
},
117+
s.requireBlockHeaders,
118+
)
119+
}
120+
121+
// requireBlockHeaders ensures that the received block header information matches the expected data.
122+
func (s *BlockHeadersProviderSuite) requireBlockHeaders(v interface{}, expectedBlock *flow.Block) {
123+
actualResponse, ok := v.(*models.BlockHeaderMessageResponse)
124+
require.True(s.T(), ok, "unexpected response type: %T", v)
125+
126+
s.Require().Equal(expectedBlock.Header, actualResponse.Header)
127+
}

0 commit comments

Comments
 (0)