From 76185c3aa154ba6d459563db2b8c33ca1b217629 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 30 Aug 2024 10:45:04 +0300 Subject: [PATCH] Use AccountService in the TestCreateAccount --- internal/handlers/ussd/menuhandler_test.go | 71 ++++++++++++---------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 6fbd27f..bce359d 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -15,14 +15,24 @@ import ( "github.com/stretchr/testify/mock" ) -// MockAccountCreator implements AccountCreator for testing -type MockAccountCreator struct { - mockResponse *models.AccountResponse - mockError error +// MockAccountService implements AccountServiceInterface for testing +type MockAccountService struct { + mock.Mock } -func (m *MockAccountCreator) CreateAccount() (*models.AccountResponse, error) { - return m.mockResponse, m.mockError +func (m *MockAccountService) CreateAccount() (*models.AccountResponse, error) { + args := m.Called() + return args.Get(0).(*models.AccountResponse), args.Error(1) +} + +func (m *MockAccountService) CheckBalance(publicKey string) (string, error) { + args := m.Called(publicKey) + return args.String(0), args.Error(1) +} + +func (m *MockAccountService) CheckAccountStatus(trackingId string) (string, error) { + args := m.Called(trackingId) + return args.String(0), args.Error(1) } func TestCreateAccount(t *testing.T) { @@ -41,26 +51,29 @@ func TestCreateAccount(t *testing.T) { // Initialize account file handler accountFileHandler := utils.NewAccountFileHandler(accountFilePath) - // Create a mock account creator - mockAccountCreator := &MockAccountCreator{ - mockResponse: &models.AccountResponse{ - Ok: true, - Result: struct { - CustodialId json.Number `json:"custodialId"` - PublicKey string `json:"publicKey"` - TrackingId string `json:"trackingId"` - }{ - CustodialId: "test-custodial-id", - PublicKey: "test-public-key", - TrackingId: "test-tracking-id", - }, + // Create a mock account service + mockAccountService := &MockAccountService{} + mockAccountResponse := &models.AccountResponse{ + Ok: true, + Result: struct { + CustodialId json.Number `json:"custodialId"` + PublicKey string `json:"publicKey"` + TrackingId string `json:"trackingId"` + }{ + CustodialId: "test-custodial-id", + PublicKey: "test-public-key", + TrackingId: "test-tracking-id", }, } - // Initialize Handlers with mock account creator + // Set up expectations for the mock account service + mockAccountService.On("CreateAccount").Return(mockAccountResponse, nil) + + // Initialize Handlers with mock account service h := &Handlers{ + fs: &FSData{Path: accountFilePath}, accountFileHandler: accountFileHandler, - accountCreator: mockAccountCreator, + accountService: mockAccountService, } tests := []struct { @@ -102,9 +115,7 @@ func TestCreateAccount(t *testing.T) { "FirstName": "Not provided", "FamilyName": "Not provided", }, - expectedResult: resource.Result{ - FlagSet: []uint32{models.USERFLAG_ACCOUNT_CREATED}, - }, + expectedResult: resource.Result{}, expectedData: map[string]string{ "TrackingId": "test-tracking-id", "PublicKey": "test-public-key", @@ -173,8 +184,6 @@ func TestCreateAccount(t *testing.T) { } } - - func TestCreateAccount_Success(t *testing.T) { mockAccountFileHandler := new(mocks.MockAccountFileHandler) mockCreateAccountService := new(mocks.MockAccountService) @@ -203,21 +212,17 @@ func TestCreateAccount_Success(t *testing.T) { mockAccountFileHandler.On("WriteAccountData", mock.Anything).Return(nil) handlers := &Handlers{ - accountService: mockCreateAccountService, + accountService: mockCreateAccountService, } - actualResponse, err := handlers.accountService.CreateAccount() // Assert results assert.NoError(t, err) - assert.Equal(t,expectedAccountResp.Ok,true) - assert.Equal(t,expectedAccountResp,actualResponse) - - + assert.Equal(t, expectedAccountResp.Ok, true) + assert.Equal(t, expectedAccountResp, actualResponse) } - func TestSavePin(t *testing.T) { // Setup tempDir, err := os.MkdirTemp("", "test_save_pin")