From a993026380c511fc830fb4505d674bba01e0c973 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Thu, 26 Sep 2024 23:07:48 +0300 Subject: [PATCH 1/3] update tests --- internal/handlers/ussd/menuhandler_test.go | 159 ++++++++------------- 1 file changed, 60 insertions(+), 99 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index d0367f0..57767ef 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -9,6 +9,7 @@ import ( "testing" "git.defalsify.org/vise.git/db" + "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/state" "git.grassecon.net/urdt/ussd/internal/mocks" @@ -16,6 +17,7 @@ import ( "git.grassecon.net/urdt/ussd/internal/utils" "github.com/alecthomas/assert/v2" testdataloader "github.com/peteole/testdata-loader" + "github.com/stretchr/testify/require" ) var ( @@ -94,6 +96,28 @@ func TestCreateAccount(t *testing.T) { mockDataStore.AssertExpectations(t) } + + +func TestWithPersister(t *testing.T) { + // Test case: Setting a persister + h := &Handlers{} + p := &persist.Persister{} + + result := h.WithPersister(p) + + assert.Equal(t, p, h.pe, "The persister should be set correctly.") + assert.Equal(t, h, result, "The returned handler should be the same instance.") +} + +func TestWithPersister_PanicWhenAlreadySet(t *testing.T) { + // Test case: Panic on multiple calls + h := &Handlers{pe: &persist.Persister{}} + require.Panics(t, func() { + h.WithPersister(&persist.Persister{}) + }, "Should panic when trying to set a persister again.") +} + + func TestSaveFirstname(t *testing.T) { // Create a new instance of MockMyDataStore mockStore := new(mocks.MockUserDataStore) @@ -295,6 +319,7 @@ func TestSaveOfferings(t *testing.T) { func TestSaveGender(t *testing.T) { // Create a new instance of MockMyDataStore mockStore := new(mocks.MockUserDataStore) + mockState := state.NewState(16) // Define the session ID and context sessionId := "session123" @@ -302,35 +327,39 @@ func TestSaveGender(t *testing.T) { // Define test cases tests := []struct { - name string - input []byte - expectedGender string - expectCall bool + name string + input []byte + expectedGender string + expectCall bool + executingSymbol string }{ { - name: "Valid Male Input", - input: []byte("1"), - expectedGender: "Male", - expectCall: true, + name: "Valid Male Input", + input: []byte("1"), + expectedGender: "male", + executingSymbol: "set_male", + expectCall: true, }, { name: "Valid Female Input", input: []byte("2"), - expectedGender: "Female", + expectedGender: "female", + executingSymbol: "set_female", expectCall: true, }, { name: "Valid Unspecified Input", input: []byte("3"), - expectedGender: "Unspecified", + executingSymbol: "set_unspecified", + expectedGender: "unspecified", expectCall: true, }, - { - name: "Empty Input", - input: []byte(""), - expectedGender: "", - expectCall: false, - }, + // { + // name: "Empty Input", + // input: []byte(""), + // expectedGender: "", + // expectCall: false, + // }, } for _, tt := range tests { @@ -342,14 +371,15 @@ func TestSaveGender(t *testing.T) { } else { mockStore.On("WriteEntry", ctx, sessionId, utils.DATA_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, } // Call the method - _, err := h.SaveGender(ctx, "someSym", tt.input) + _, err := h.SaveGender(ctx, "save_gender", tt.input) // Assert no error assert.NoError(t, err) @@ -538,13 +568,13 @@ func TestSetLanguage(t *testing.T) { } // Define test cases tests := []struct { - name string - execPath []string - expectedResult resource.Result + name string + execPath []string + expectedResult resource.Result }{ { name: "Set Default Language (English)", - execPath: []string{"set_default"}, + execPath: []string{"set_eng"}, expectedResult: resource.Result{ FlagSet: []uint32{state.FLAG_LANG, 8}, Content: "eng", @@ -558,13 +588,13 @@ func TestSetLanguage(t *testing.T) { Content: "swa", }, }, - { - name: "Unhandled path", - execPath: []string{""}, - expectedResult: resource.Result{ - FlagSet: []uint32{8}, - }, - }, + // { + // name: "Unhandled path", + // execPath: []string{""}, + // expectedResult: resource.Result{ + // FlagSet: []uint32{8}, + // }, + // }, } for _, tt := range tests { @@ -592,76 +622,6 @@ func TestSetLanguage(t *testing.T) { }) } } - -func TestSetResetSingleEdit(t *testing.T) { - fm, err := NewFlagManager(flagsPath) - - flag_allow_update, _ := fm.parser.GetFlag("flag_allow_update") - flag_single_edit, _ := fm.parser.GetFlag("flag_single_edit") - - if err != nil { - log.Fatal(err) - } - // Define test cases - tests := []struct { - name string - input []byte - expectedResult resource.Result - }{ - { - name: "Set single Edit", - input: []byte("2"), - expectedResult: resource.Result{ - FlagSet: []uint32{flag_single_edit}, - FlagReset: []uint32{flag_allow_update}, - }, - }, - { - name: "Set single Edit", - input: []byte("3"), - expectedResult: resource.Result{ - FlagSet: []uint32{flag_single_edit}, - FlagReset: []uint32{flag_allow_update}, - }, - }, - { - name: "Set single edit", - input: []byte("4"), - expectedResult: resource.Result{ - FlagReset: []uint32{flag_allow_update}, - FlagSet: []uint32{flag_single_edit}, - }, - }, - { - name: "No single edit set", - input: []byte("1"), - expectedResult: resource.Result{ - FlagReset: []uint32{flag_single_edit}, - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - - // Create the Handlers instance with the mock flag manager - h := &Handlers{ - flagManager: fm.parser, - } - - // Call the method - res, err := h.SetResetSingleEdit(context.Background(), "set_reset_single_edit", tt.input) - - if err != nil { - t.Error(err) - } - // Assert that the Result FlagSet has the required flags after language switch - assert.Equal(t, res, tt.expectedResult, "Flags should match reset edit") - - }) - } -} - func TestResetAllowUpdate(t *testing.T) { fm, err := NewFlagManager(flagsPath) @@ -1648,6 +1608,7 @@ func TestGetProfile(t *testing.T) { mockDataStore := new(mocks.MockUserDataStore) mockCreateAccountService := new(mocks.MockAccountService) + h := &Handlers{ userdataStore: mockDataStore, accountService: mockCreateAccountService, From ea52a7cf0a434f1322331f0ce14cd5710f1f34bc Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Thu, 26 Sep 2024 23:11:37 +0300 Subject: [PATCH 2/3] update tests --- internal/handlers/ussd/menuhandler_test.go | 44 +++++++++++----------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 57767ef..283104a 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -1440,7 +1440,7 @@ func TestValidateAmount(t *testing.T) { if err != nil { t.Logf(err.Error()) } - //flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount") + flag_invalid_amount, _ := fm.parser.GetFlag("flag_invalid_amount") mockDataStore := new(mocks.MockUserDataStore) mockCreateAccountService := new(mocks.MockAccountService) @@ -1469,26 +1469,26 @@ func TestValidateAmount(t *testing.T) { Content: "0.001", }, }, - // { - // name: "Test with amount larger than balance", - // input: []byte("0.02"), - // balance: "0.003 CELO", - // publicKey: []byte("0xrqeqrequuq"), - // expectedResult: resource.Result{ - // FlagSet: []uint32{flag_invalid_amount}, - // Content: "0.02", - // }, - // }, - // { - // name: "Test with invalid amount", - // input: []byte("0.02ms"), - // balance: "0.003 CELO", - // publicKey: []byte("0xrqeqrequuq"), - // expectedResult: resource.Result{ - // FlagSet: []uint32{flag_invalid_amount}, - // Content: "0.02ms", - // }, - // }, + { + name: "Test with amount larger than balance", + input: []byte("0.02"), + balance: "0.003 CELO", + publicKey: []byte("0xrqeqrequuq"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_invalid_amount}, + Content: "0.02", + }, + }, + { + name: "Test with invalid amount", + input: []byte("0.02ms"), + balance: "0.003 CELO", + publicKey: []byte("0xrqeqrequuq"), + expectedResult: resource.Result{ + FlagSet: []uint32{flag_invalid_amount}, + Content: "0.02ms", + }, + }, } for _, tt := range tests { @@ -1496,7 +1496,7 @@ func TestValidateAmount(t *testing.T) { mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return(tt.publicKey, nil) mockCreateAccountService.On("CheckBalance", string(tt.publicKey)).Return(tt.balance, nil) - mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, tt.input).Return(nil) + mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_AMOUNT, tt.input).Return(nil).Maybe() // Call the method under test res, _ := h.ValidateAmount(ctx, "test_validate_amount", tt.input) From a3ff3be5b17549a8178d69da6c0312a4c94bf9b4 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 27 Sep 2024 16:32:47 +0300 Subject: [PATCH 3/3] clean up --- internal/handlers/ussd/menuhandler_test.go | 32 ++++++---------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 283104a..83e6f0c 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -96,8 +96,6 @@ func TestCreateAccount(t *testing.T) { mockDataStore.AssertExpectations(t) } - - func TestWithPersister(t *testing.T) { // Test case: Setting a persister h := &Handlers{} @@ -117,7 +115,6 @@ func TestWithPersister_PanicWhenAlreadySet(t *testing.T) { }, "Should panic when trying to set a persister again.") } - func TestSaveFirstname(t *testing.T) { // Create a new instance of MockMyDataStore mockStore := new(mocks.MockUserDataStore) @@ -341,25 +338,19 @@ func TestSaveGender(t *testing.T) { expectCall: true, }, { - name: "Valid Female Input", - input: []byte("2"), - expectedGender: "female", + name: "Valid Female Input", + input: []byte("2"), + expectedGender: "female", executingSymbol: "set_female", - expectCall: true, + expectCall: true, }, { - name: "Valid Unspecified Input", - input: []byte("3"), + name: "Valid Unspecified Input", + input: []byte("3"), executingSymbol: "set_unspecified", - expectedGender: "unspecified", - expectCall: true, + expectedGender: "unspecified", + expectCall: true, }, - // { - // name: "Empty Input", - // input: []byte(""), - // expectedGender: "", - // expectCall: false, - // }, } for _, tt := range tests { @@ -588,13 +579,6 @@ func TestSetLanguage(t *testing.T) { Content: "swa", }, }, - // { - // name: "Unhandled path", - // execPath: []string{""}, - // expectedResult: resource.Result{ - // FlagSet: []uint32{8}, - // }, - // }, } for _, tt := range tests {