update tests

This commit is contained in:
Carlosokumu 2024-10-31 17:05:06 +03:00
parent 3ce25d0e14
commit ce917d9e89
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953

View File

@ -19,8 +19,8 @@ import (
"git.grassecon.net/urdt/ussd/internal/testutil/mocks"
"git.grassecon.net/urdt/ussd/internal/testutil/testservice"
"git.grassecon.net/urdt/ussd/internal/utils"
"git.grassecon.net/urdt/ussd/common"
"git.grassecon.net/urdt/ussd/internal/utils"
"github.com/alecthomas/assert/v2"
"github.com/grassrootseconomics/eth-custodial/pkg/api"
testdataloader "github.com/peteole/testdata-loader"
@ -174,8 +174,11 @@ func TestWithPersister_PanicWhenAlreadySet(t *testing.T) {
}
func TestSaveFirstname(t *testing.T) {
// Create a new instance of MockMyDataStore
// Create new mocks
mockStore := new(mocks.MockUserDataStore)
mockState := state.NewState(16)
fm, err := NewFlagManager(flagsPath)
// Define test data
sessionId := "session123"
@ -183,11 +186,13 @@ func TestSaveFirstname(t *testing.T) {
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
// Set up the expected behavior of the mock
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_FIRST_NAME, []byte(firstName)).Return(nil)
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_FIRST_NAME, []byte(firstName)).Return(nil)
// Create the Handlers instance with the mock store
h := &Handlers{
userdataStore: mockStore,
flagManager: fm.parser,
st: mockState,
}
// Call the method
@ -204,6 +209,9 @@ func TestSaveFirstname(t *testing.T) {
func TestSaveFamilyname(t *testing.T) {
// Create a new instance of UserDataStore
mockStore := new(mocks.MockUserDataStore)
mockState := state.NewState(16)
fm, err := NewFlagManager(flagsPath)
// Define test data
sessionId := "session123"
@ -211,11 +219,13 @@ func TestSaveFamilyname(t *testing.T) {
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
// Set up the expected behavior of the mock
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_FAMILY_NAME, []byte(familyName)).Return(nil)
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_FAMILY_NAME, []byte(familyName)).Return(nil)
// Create the Handlers instance with the mock store
h := &Handlers{
userdataStore: mockStore,
st: mockState,
flagManager: fm.parser,
}
// Call the method
@ -288,8 +298,11 @@ func TestSaveTemporaryPin(t *testing.T) {
}
func TestSaveYoB(t *testing.T) {
// Create a new instance of MockMyDataStore
// Create new instances
mockStore := new(mocks.MockUserDataStore)
mockState := state.NewState(16)
fm, err := NewFlagManager(flagsPath)
// Define test data
sessionId := "session123"
@ -297,11 +310,13 @@ func TestSaveYoB(t *testing.T) {
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
// Set up the expected behavior of the mock
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_YOB, []byte(yob)).Return(nil)
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_YOB, []byte(yob)).Return(nil)
// Create the Handlers instance with the mock store
h := &Handlers{
userdataStore: mockStore,
st: mockState,
flagManager: fm.parser,
}
// Call the method
@ -318,6 +333,9 @@ func TestSaveYoB(t *testing.T) {
func TestSaveLocation(t *testing.T) {
// Create a new instance of MockMyDataStore
mockStore := new(mocks.MockUserDataStore)
mockState := state.NewState(16)
fm, err := NewFlagManager(flagsPath)
// Define test data
sessionId := "session123"
@ -325,11 +343,13 @@ func TestSaveLocation(t *testing.T) {
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
// Set up the expected behavior of the mock
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_LOCATION, []byte(yob)).Return(nil)
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_LOCATION, []byte(yob)).Return(nil)
// Create the Handlers instance with the mock store
h := &Handlers{
userdataStore: mockStore,
st: mockState,
flagManager: fm.parser,
}
// Call the method
@ -346,6 +366,9 @@ func TestSaveLocation(t *testing.T) {
func TestSaveOfferings(t *testing.T) {
// Create a new instance of MockUserDataStore
mockStore := new(mocks.MockUserDataStore)
mockState := state.NewState(16)
fm, err := NewFlagManager(flagsPath)
// Define test data
sessionId := "session123"
@ -353,11 +376,13 @@ func TestSaveOfferings(t *testing.T) {
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
// Set up the expected behavior of the mock
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_OFFERINGS, []byte(offerings)).Return(nil)
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_OFFERINGS, []byte(offerings)).Return(nil)
// Create the Handlers instance with the mock store
h := &Handlers{
userdataStore: mockStore,
st: mockState,
flagManager: fm.parser,
}
// Call the method
@ -372,10 +397,12 @@ func TestSaveOfferings(t *testing.T) {
}
func TestSaveGender(t *testing.T) {
// Create a new instance of MockMyDataStore
// Create a new mock instances
mockStore := new(mocks.MockUserDataStore)
mockState := state.NewState(16)
fm, _ := NewFlagManager(flagsPath)
// Define the session ID and context
sessionId := "session123"
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
@ -415,16 +442,17 @@ func TestSaveGender(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
// Set up expectations for the mock database
if tt.expectCall {
expectedKey := utils.DATA_GENDER
expectedKey := utils.DATA_TEMPORARY_GENDER
mockStore.On("WriteEntry", ctx, sessionId, expectedKey, []byte(tt.expectedGender)).Return(nil)
} else {
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_GENDER, []byte(tt.expectedGender)).Return(nil)
mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_GENDER, []byte(tt.expectedGender)).Return(nil)
}
mockState.ExecPath = append(mockState.ExecPath, tt.executingSymbol)
// Create the Handlers instance with the mock store
h := &Handlers{
userdataStore: mockStore,
st: mockState,
flagManager: fm.parser,
}
// Call the method
@ -435,9 +463,9 @@ func TestSaveGender(t *testing.T) {
// Verify expectations
if tt.expectCall {
mockStore.AssertCalled(t, "WriteEntry", ctx, sessionId, utils.DATA_GENDER, []byte(tt.expectedGender))
mockStore.AssertCalled(t, "WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_GENDER, []byte(tt.expectedGender))
} else {
mockStore.AssertNotCalled(t, "WriteEntry", ctx, sessionId, utils.DATA_GENDER, []byte(tt.expectedGender))
mockStore.AssertNotCalled(t, "WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_GENDER, []byte(tt.expectedGender))
}
})
}