From d1d5c897d1e0ad724a303c8517ca6f6fdb0430fe Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Sat, 7 Sep 2024 09:55:59 +0300 Subject: [PATCH] refactor --- internal/handlers/ussd/menuhandler_test.go | 120 +++++++++------------ 1 file changed, 49 insertions(+), 71 deletions(-) diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index b13da16..e496773 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -4,84 +4,62 @@ import ( "context" "testing" - "git.defalsify.org/vise.git/lang" - "git.grassecon.net/urdt/ussd/internal/models" - + "git.grassecon.net/urdt/ussd/internal/handlers/ussd/mocks" "github.com/alecthomas/assert/v2" "github.com/stretchr/testify/mock" ) -// MockAccountService implements AccountServiceInterface for testing -type MockAccountService struct { - mock.Mock -} - -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) -} - // MockDb is a mock implementation of the db.Db interface -type MockDb struct { - mock.Mock -} +//type MockDb struct { +// mock.Mock +// } -func (m *MockDb) SetPrefix(prefix uint8) { - m.Called(prefix) -} +// func (m *MockDb) SetPrefix(prefix uint8) { +// m.Called(prefix) +// } -func (m *MockDb) Prefix() uint8 { - args := m.Called() - return args.Get(0).(uint8) -} +// func (m *MockDb) Prefix() uint8 { +// args := m.Called() +// return args.Get(0).(uint8) +// } -func (m *MockDb) Safe() bool { - args := m.Called() - return args.Get(0).(bool) -} +// func (m *MockDb) Safe() bool { +// args := m.Called() +// return args.Get(0).(bool) +// } -func (m *MockDb) SetLanguage(language *lang.Language) { - m.Called(language) -} +// func (m *MockDb) SetLanguage(language *lang.Language) { +// m.Called(language) +// } -func (m *MockDb) SetLock(uint8, bool) error { - args := m.Called() - return args.Error(0) -} +// func (m *MockDb) SetLock(uint8, bool) error { +// args := m.Called() +// return args.Error(0) +// } -func (m *MockDb) Connect(ctx context.Context, connectionStr string) error { - args := m.Called(ctx, connectionStr) - return args.Error(0) -} +// func (m *MockDb) Connect(ctx context.Context, connectionStr string) error { +// args := m.Called(ctx, connectionStr) +// return args.Error(0) +// } -func (m *MockDb) SetSession(sessionId string) { - m.Called(sessionId) -} +// func (m *MockDb) SetSession(sessionId string) { +// m.Called(sessionId) +// } -func (m *MockDb) Put(ctx context.Context, key, value []byte) error { - args := m.Called(ctx, key, value) - return args.Error(0) -} +// func (m *MockDb) Put(ctx context.Context, key, value []byte) error { +// args := m.Called(ctx, key, value) +// return args.Error(0) +// } -func (m *MockDb) Get(ctx context.Context, key []byte) ([]byte, error) { - args := m.Called(ctx, key) - return nil, args.Error(0) -} +// func (m *MockDb) Get(ctx context.Context, key []byte) ([]byte, error) { +// args := m.Called(ctx, key) +// return nil, args.Error(0) +// } -func (m *MockDb) Close() error { - args := m.Called(nil) - return args.Error(0) -} +// func (m *MockDb) Close() error { +// args := m.Called(nil) +// return args.Error(0) +// } // func TestCreateAccount(t *testing.T) { // // Setup @@ -470,7 +448,7 @@ func (m *MockDb) Close() error { func TestSaveFirstname(t *testing.T) { // Create a mock database - mockDb := new(MockDb) + mockDb := new(mocks.MockDb) // Create a Handlers instance with the mock database h := &Handlers{ @@ -484,13 +462,13 @@ func TestSaveFirstname(t *testing.T) { name string input []byte expectError bool - setupMock func(*MockDb) + setupMock func(*mocks.MockDb) }{ { name: "Valid first name", input: []byte("John"), expectError: false, - setupMock: func(m *MockDb) { + setupMock: func(m *mocks.MockDb) { m.On("SetPrefix", uint8(0x20)).Return(nil) m.On("SetSession", "test-session").Return(nil) m.On("Put", mock.Anything, mock.Anything, []byte("John")).Return(nil) @@ -500,7 +478,7 @@ func TestSaveFirstname(t *testing.T) { name: "Empty first name", input: []byte{}, expectError: false, // Note: The function doesn't return an error for empty input - setupMock: func(m *MockDb) {}, + setupMock: func(m *mocks.MockDb) {}, }, } @@ -528,7 +506,7 @@ func TestSaveFirstname(t *testing.T) { func TestSaveFamilyname(t *testing.T) { // Create a mock database - mockDb := new(MockDb) + mockDb := new(mocks.MockDb) // Create a Handlers instance with the mock database h := &Handlers{ @@ -542,13 +520,13 @@ func TestSaveFamilyname(t *testing.T) { name string input []byte expectError bool - setupMock func(*MockDb) + setupMock func(*mocks.MockDb) }{ { name: "Valid family name", input: []byte("Smith"), expectError: false, - setupMock: func(m *MockDb) { + setupMock: func(m *mocks.MockDb) { m.On("SetPrefix", uint8(0x20)).Return(nil) m.On("SetSession", "test-session").Return(nil) m.On("Put", mock.Anything, mock.Anything, []byte("Smith")).Return(nil) @@ -558,7 +536,7 @@ func TestSaveFamilyname(t *testing.T) { name: "Empty family name", input: []byte{}, expectError: true, - setupMock: func(m *MockDb) {}, + setupMock: func(m *mocks.MockDb) {}, }, }