add test profile

This commit is contained in:
Carlosokumu 2024-08-31 14:55:54 +03:00
parent 143c9b14a1
commit 2559b4e604
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953

View File

@ -3,6 +3,7 @@ package ussd
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -690,7 +691,6 @@ func TestSaveOfferings(t *testing.T) {
} }
} }
func TestSaveGender(t *testing.T) { func TestSaveGender(t *testing.T) {
// Create a new instance of MockAccountFileHandler // Create a new instance of MockAccountFileHandler
mockFileHandler := new(mocks.MockAccountFileHandler) mockFileHandler := new(mocks.MockAccountFileHandler)
@ -732,7 +732,7 @@ func TestSaveGender(t *testing.T) {
expectedError: nil, expectedError: nil,
expectedGender: "Unspecified", expectedGender: "Unspecified",
}, },
{ {
name: "Empty Input", name: "Empty Input",
input: []byte{}, input: []byte{},
@ -752,7 +752,7 @@ func TestSaveGender(t *testing.T) {
mockFileHandler.On("WriteAccountData", mock.MatchedBy(func(data map[string]string) bool { mockFileHandler.On("WriteAccountData", mock.MatchedBy(func(data map[string]string) bool {
return data["Gender"] == tt.expectedGender return data["Gender"] == tt.expectedGender
})).Return(tt.writeError) })).Return(tt.writeError)
} else if len(tt.input) == 0 { } else if len(tt.input) == 0 {
// For empty input, no WriteAccountData call should be made // For empty input, no WriteAccountData call should be made
mockFileHandler.On("WriteAccountData", mock.Anything).Maybe().Return(tt.writeError) mockFileHandler.On("WriteAccountData", mock.Anything).Maybe().Return(tt.writeError)
} }
@ -876,3 +876,95 @@ func TestGetAmount(t *testing.T) {
} }
} }
func TestGetProfileInfo(t *testing.T) {
tests := []struct {
name string
accountData map[string]string
readError error
expectedResult resource.Result
expectedError error
}{
{
name: "Complete Profile",
accountData: map[string]string{
"FirstName": "John",
"FamilyName": "Doe",
"Gender": "Male",
"YOB": "1980",
"Location": "Mombasa",
"Offerings": "Product A",
},
readError: nil,
expectedResult: resource.Result{
Content: fmt.Sprintf(
"Name: %s %s\nGender: %s\nAge: %d\nLocation: %s\nYou provide: %s\n",
"John", "Doe", "Male", 44, "Mombasa", "Product A",
),
},
expectedError: nil,
},
{
name: "Profile with Not Provided Fields",
accountData: map[string]string{
"FirstName": "Not provided",
"FamilyName": "Doe",
"Gender": "Female",
"YOB": "1995",
"Location": "Not provided",
"Offerings": "Service B",
},
readError: nil,
expectedResult: resource.Result{
Content: fmt.Sprintf(
"Name: %s\nGender: %s\nAge: %d\nLocation: %s\nYou provide: %s\n",
"Not provided", "Female", 29, "Not provided", "Service B",
),
},
expectedError: nil,
},
{
name: "Profile with YOB as Not provided",
accountData: map[string]string{
"FirstName": "Not provided",
"FamilyName": "Doe",
"Gender": "Female",
"YOB": "Not provided",
"Location": "Not provided",
"Offerings": "Service B",
},
readError: nil,
expectedResult: resource.Result{
Content: fmt.Sprintf(
"Name: %s\nGender: %s\nAge: %s\nLocation: %s\nYou provide: %s\n",
"Not provided", "Female", "Not provided", "Not provided", "Service B",
),
},
expectedError: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a new instance of MockAccountFileHandler
mockFileHandler := new(mocks.MockAccountFileHandler)
// Set up the mock expectations
mockFileHandler.On("ReadAccountData").Return(tt.accountData, tt.readError)
// Create the Handlers instance with the mock file handler
h := &Handlers{
accountFileHandler: mockFileHandler,
}
// Call the method
result, err := h.GetProfileInfo(context.Background(), "get_profile_info", nil)
// Assert the results
assert.Equal(t, tt.expectedResult, result)
assert.Equal(t, tt.expectedError, err)
// Assert all expectations were met
mockFileHandler.AssertExpectations(t)
})
}
}