From 2c6f70dfa5273fbd757e32594b02dd276ded81f8 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 30 Aug 2024 13:52:25 +0300 Subject: [PATCH] Add tests for GetSender and GetAmount --- internal/handlers/ussd/menuhandler_test.go | 100 ++++++++++++++++++++- 1 file changed, 96 insertions(+), 4 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index f690873..7ff65a9 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -333,8 +333,6 @@ func TestSavePin(t *testing.T) { } } - - func TestSaveLocation(t *testing.T) { // Create a new instance of MockAccountFileHandler mockFileHandler := new(mocks.MockAccountFileHandler) @@ -374,7 +372,7 @@ func TestSaveLocation(t *testing.T) { mockFileHandler.On("WriteAccountData", mock.MatchedBy(func(data map[string]string) bool { return data["Location"] == string(tt.input) })).Return(tt.writeError) - }else if len(tt.input) == 0 { + } else if len(tt.input) == 0 { // For empty input, no WriteAccountData call should be made mockFileHandler.On("WriteAccountData", mock.Anything).Maybe().Return(tt.writeError) } @@ -395,4 +393,98 @@ func TestSaveLocation(t *testing.T) { mockFileHandler.AssertExpectations(t) }) } -} \ No newline at end of file +} + +func TestGetSender(t *testing.T) { + mockAccountFileHandler := new(mocks.MockAccountFileHandler) + h := &Handlers{ + accountFileHandler: mockAccountFileHandler, + } + + tests := []struct { + name string + expectedResult resource.Result + accountData map[string]string + }{ + { + name: "Valid public key", + expectedResult: resource.Result{ + Content: "test-public-key", + }, + accountData: map[string]string{ + "PublicKey": "test-public-key", + }, + }, + { + name: "Missing public key", + expectedResult: resource.Result{ + Content: "", + }, + accountData: map[string]string{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Reset the mock state + mockAccountFileHandler.Mock = mock.Mock{} + + mockAccountFileHandler.On("ReadAccountData").Return(tt.accountData, nil) + + result, err := h.GetSender(context.Background(), "", nil) + + if err != nil { + t.Fatalf("Error occurred: %v", err) + } + + assert.Equal(t, tt.expectedResult.Content, result.Content) + mockAccountFileHandler.AssertCalled(t, "ReadAccountData") + }) + } +} + +func TestGetAmount(t *testing.T) { + mockAccountFileHandler := new(mocks.MockAccountFileHandler) + h := &Handlers{ + accountFileHandler: mockAccountFileHandler, + } + + tests := []struct { + name string + expectedResult resource.Result + accountData map[string]string + }{ + { + name: "Valid amount", + expectedResult: resource.Result{ + Content: "0.003", + }, + accountData: map[string]string{ + "Amount": "0.003", + }, + }, + { + name: "Missing amount", + expectedResult: resource.Result{}, + accountData: map[string]string{ + "Amount": "", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Reset the mock state + mockAccountFileHandler.Mock = mock.Mock{} + + mockAccountFileHandler.On("ReadAccountData").Return(tt.accountData, nil) + + result, err := h.GetAmount(context.Background(), "", nil) + + assert.NoError(t, err) + assert.Equal(t, tt.expectedResult.Content, result.Content) + + mockAccountFileHandler.AssertCalled(t, "ReadAccountData") + }) + } +}