From 812bc9400e8e86e7155600310427b97969272081 Mon Sep 17 00:00:00 2001 From: kameshraj23 Date: Mon, 23 Sep 2024 20:32:47 +0100 Subject: [PATCH 1/3] ARCO-153: Adding unit test for utxo creator --- internal/broadcaster/utxo_creator_test.go | 85 +++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 internal/broadcaster/utxo_creator_test.go diff --git a/internal/broadcaster/utxo_creator_test.go b/internal/broadcaster/utxo_creator_test.go new file mode 100644 index 000000000..730ca8cea --- /dev/null +++ b/internal/broadcaster/utxo_creator_test.go @@ -0,0 +1,85 @@ +package broadcaster_test + +import ( + "errors" + "testing" + + "github.com/bitcoin-sv/arc/internal/broadcaster/mocks" + "github.com/stretchr/testify/require" +) + +func TestUTXOCreator_Start_WithMock(t *testing.T) { + tt := []struct { + name string + outputs int + satoshisPerOutput uint64 + startFunc func(outputs int, satoshisPerOutput uint64) error + expectedError error + expectedStartCalls int + }{ + { + name: "successful start", + outputs: 5, + satoshisPerOutput: 1000, + startFunc: func(outputs int, satoshisPerOutput uint64) error { + return nil + }, + expectedError: nil, + expectedStartCalls: 1, + }, + { + name: "error on start", + outputs: 5, + satoshisPerOutput: 1000, + startFunc: func(outputs int, satoshisPerOutput uint64) error { + return errors.New("failed to start UTXO creation") + }, + expectedError: errors.New("failed to start UTXO creation"), + expectedStartCalls: 1, + }, + { + name: "invalid input - zero outputs", + outputs: 0, + satoshisPerOutput: 1000, + startFunc: func(outputs int, satoshisPerOutput uint64) error { + return nil // Simulate success to test input handling + }, + expectedError: nil, // Start should handle zero outputs gracefully + expectedStartCalls: 1, + }, + { + name: "invalid input - zero satoshis per output", + outputs: 5, + satoshisPerOutput: 0, + startFunc: func(outputs int, satoshisPerOutput uint64) error { + return nil // Simulate success to test input handling + }, + expectedError: nil, // Start should handle zero satoshis gracefully + expectedStartCalls: 1, + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + // Create the mock + mockedCreator := &mocks.CreatorMock{ + StartFunc: tc.startFunc, + } + + // Call the Start method with mock + err := mockedCreator.Start(tc.outputs, tc.satoshisPerOutput) + + // Verify if the error is as expected + if tc.expectedError != nil { + require.Error(t, err) + require.EqualError(t, err, tc.expectedError.Error()) + } else { + require.NoError(t, err) + } + + // Verify the number of times Start was called + require.Equal(t, tc.expectedStartCalls, len(mockedCreator.StartCalls())) + + }) + } +} From 93ba3b68b23d84394cf7ef95e78d3abfe8cdfb2a Mon Sep 17 00:00:00 2001 From: kameshraj23 Date: Mon, 23 Sep 2024 20:34:35 +0100 Subject: [PATCH 2/3] ARCO-153: Adding unit test for utxo creator --- internal/broadcaster/utxo_creator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/broadcaster/utxo_creator_test.go b/internal/broadcaster/utxo_creator_test.go index 730ca8cea..16fb2a25b 100644 --- a/internal/broadcaster/utxo_creator_test.go +++ b/internal/broadcaster/utxo_creator_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/require" ) -func TestUTXOCreator_Start_WithMock(t *testing.T) { +func TestUTXOCreator(t *testing.T) { tt := []struct { name string outputs int From 3574f9e807b7bda6ef33945021b4945029a594f8 Mon Sep 17 00:00:00 2001 From: kameshraj23 Date: Wed, 2 Oct 2024 12:30:07 +0100 Subject: [PATCH 3/3] ARCO-153: Adrresing review comments --- internal/broadcaster/utxo_creator_test.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/internal/broadcaster/utxo_creator_test.go b/internal/broadcaster/utxo_creator_test.go index 16fb2a25b..6929be7f1 100644 --- a/internal/broadcaster/utxo_creator_test.go +++ b/internal/broadcaster/utxo_creator_test.go @@ -61,15 +61,14 @@ func TestUTXOCreator(t *testing.T) { for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { - // Create the mock + // Given mockedCreator := &mocks.CreatorMock{ StartFunc: tc.startFunc, } - - // Call the Start method with mock + // When err := mockedCreator.Start(tc.outputs, tc.satoshisPerOutput) - // Verify if the error is as expected + // Then if tc.expectedError != nil { require.Error(t, err) require.EqualError(t, err, tc.expectedError.Error()) @@ -77,7 +76,6 @@ func TestUTXOCreator(t *testing.T) { require.NoError(t, err) } - // Verify the number of times Start was called require.Equal(t, tc.expectedStartCalls, len(mockedCreator.StartCalls())) })