Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
feat(BUX-156): add broadcaster unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Aug 8, 2023
1 parent 57471bd commit 2e9f9b1
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions broadcast/internal/arc/arc_submit_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,96 @@ func TestSubmitTransaction(t *testing.T) {
})
}
}

func TestSubmitBatchTransactions(t *testing.T) {
testCases := []struct {
name string
transactions []*broadcast.Transaction
httpResponse *http.Response
httpError error
expectedResult []*broadcast.SubmitTxResponse
expectedError error
}{
{
name: "successful request",
transactions: []*broadcast.Transaction{
{RawTx: "abc123"},
{RawTx: "cba321"},
},
httpResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`
[
{
"txStatus": "CONFIRMED"
},
{
"txStatus": "CONFIRMED"
}
]`)),
},
expectedResult: []*broadcast.SubmitTxResponse{
{TxStatus: broadcast.Confirmed},
{TxStatus: broadcast.Confirmed},
},
},
{
name: "error in HTTP request",
transactions: []*broadcast.Transaction{
{RawTx: "abc123"},
{RawTx: "cba321"},
},
httpError: errors.New("some error"),
expectedError: errors.New("some error"),
},
{
name: "missing txStatus in response",
transactions: []*broadcast.Transaction{
{RawTx: "abc123"},
{RawTx: "cba321"},
},
httpResponse: &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString(`
[
{
"dummyField": "dummyValue"
},
{
"txStatus": "CONFIRMED"
}
]`)),
},
expectedError: broadcast.ErrMissingStatus,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// given
mockHttpClient := new(MockHttpClient)

body, _ := createSubmitBatchTxsBody(tc.transactions)
expectedPayload := httpclient.NewPayload(httpclient.POST, "http://example.com"+arcSubmitBatchTxsRoute, "someToken", body)
appendSubmitTxHeaders(&expectedPayload, tc.transactions[0])

mockHttpClient.On("DoRequest", context.Background(), expectedPayload).
Return(tc.httpResponse, tc.httpError).Once()

client := &ArcClient{
HTTPClient: mockHttpClient,
apiURL: "http://example.com",
token: "someToken",
}

// when
result, err := client.SubmitBatchTransactions(context.Background(), tc.transactions)

// then
assert.Equal(t, tc.expectedResult, result)
assert.Equal(t, tc.expectedError, err)

// assert Expectations on the mock
mockHttpClient.AssertExpectations(t)
})
}
}

0 comments on commit 2e9f9b1

Please sign in to comment.