This commit is contained in:
Carlosokumu 2024-09-07 09:55:59 +03:00
parent 04ea11dd6d
commit d1d5c897d1
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953

View File

@ -4,84 +4,62 @@ import (
"context" "context"
"testing" "testing"
"git.defalsify.org/vise.git/lang" "git.grassecon.net/urdt/ussd/internal/handlers/ussd/mocks"
"git.grassecon.net/urdt/ussd/internal/models"
"github.com/alecthomas/assert/v2" "github.com/alecthomas/assert/v2"
"github.com/stretchr/testify/mock" "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 // MockDb is a mock implementation of the db.Db interface
type MockDb struct { //type MockDb struct {
mock.Mock // mock.Mock
} // }
func (m *MockDb) SetPrefix(prefix uint8) { // func (m *MockDb) SetPrefix(prefix uint8) {
m.Called(prefix) // m.Called(prefix)
} // }
func (m *MockDb) Prefix() uint8 { // func (m *MockDb) Prefix() uint8 {
args := m.Called() // args := m.Called()
return args.Get(0).(uint8) // return args.Get(0).(uint8)
} // }
func (m *MockDb) Safe() bool { // func (m *MockDb) Safe() bool {
args := m.Called() // args := m.Called()
return args.Get(0).(bool) // return args.Get(0).(bool)
} // }
func (m *MockDb) SetLanguage(language *lang.Language) { // func (m *MockDb) SetLanguage(language *lang.Language) {
m.Called(language) // m.Called(language)
} // }
func (m *MockDb) SetLock(uint8, bool) error { // func (m *MockDb) SetLock(uint8, bool) error {
args := m.Called() // args := m.Called()
return args.Error(0) // return args.Error(0)
} // }
func (m *MockDb) Connect(ctx context.Context, connectionStr string) error { // func (m *MockDb) Connect(ctx context.Context, connectionStr string) error {
args := m.Called(ctx, connectionStr) // args := m.Called(ctx, connectionStr)
return args.Error(0) // return args.Error(0)
} // }
func (m *MockDb) SetSession(sessionId string) { // func (m *MockDb) SetSession(sessionId string) {
m.Called(sessionId) // m.Called(sessionId)
} // }
func (m *MockDb) Put(ctx context.Context, key, value []byte) error { // func (m *MockDb) Put(ctx context.Context, key, value []byte) error {
args := m.Called(ctx, key, value) // args := m.Called(ctx, key, value)
return args.Error(0) // return args.Error(0)
} // }
func (m *MockDb) Get(ctx context.Context, key []byte) ([]byte, error) { // func (m *MockDb) Get(ctx context.Context, key []byte) ([]byte, error) {
args := m.Called(ctx, key) // args := m.Called(ctx, key)
return nil, args.Error(0) // return nil, args.Error(0)
} // }
func (m *MockDb) Close() error { // func (m *MockDb) Close() error {
args := m.Called(nil) // args := m.Called(nil)
return args.Error(0) // return args.Error(0)
} // }
// func TestCreateAccount(t *testing.T) { // func TestCreateAccount(t *testing.T) {
// // Setup // // Setup
@ -470,7 +448,7 @@ func (m *MockDb) Close() error {
func TestSaveFirstname(t *testing.T) { func TestSaveFirstname(t *testing.T) {
// Create a mock database // Create a mock database
mockDb := new(MockDb) mockDb := new(mocks.MockDb)
// Create a Handlers instance with the mock database // Create a Handlers instance with the mock database
h := &Handlers{ h := &Handlers{
@ -484,13 +462,13 @@ func TestSaveFirstname(t *testing.T) {
name string name string
input []byte input []byte
expectError bool expectError bool
setupMock func(*MockDb) setupMock func(*mocks.MockDb)
}{ }{
{ {
name: "Valid first name", name: "Valid first name",
input: []byte("John"), input: []byte("John"),
expectError: false, expectError: false,
setupMock: func(m *MockDb) { setupMock: func(m *mocks.MockDb) {
m.On("SetPrefix", uint8(0x20)).Return(nil) m.On("SetPrefix", uint8(0x20)).Return(nil)
m.On("SetSession", "test-session").Return(nil) m.On("SetSession", "test-session").Return(nil)
m.On("Put", mock.Anything, mock.Anything, []byte("John")).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", name: "Empty first name",
input: []byte{}, input: []byte{},
expectError: false, // Note: The function doesn't return an error for empty input 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) { func TestSaveFamilyname(t *testing.T) {
// Create a mock database // Create a mock database
mockDb := new(MockDb) mockDb := new(mocks.MockDb)
// Create a Handlers instance with the mock database // Create a Handlers instance with the mock database
h := &Handlers{ h := &Handlers{
@ -542,13 +520,13 @@ func TestSaveFamilyname(t *testing.T) {
name string name string
input []byte input []byte
expectError bool expectError bool
setupMock func(*MockDb) setupMock func(*mocks.MockDb)
}{ }{
{ {
name: "Valid family name", name: "Valid family name",
input: []byte("Smith"), input: []byte("Smith"),
expectError: false, expectError: false,
setupMock: func(m *MockDb) { setupMock: func(m *mocks.MockDb) {
m.On("SetPrefix", uint8(0x20)).Return(nil) m.On("SetPrefix", uint8(0x20)).Return(nil)
m.On("SetSession", "test-session").Return(nil) m.On("SetSession", "test-session").Return(nil)
m.On("Put", mock.Anything, mock.Anything, []byte("Smith")).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", name: "Empty family name",
input: []byte{}, input: []byte{},
expectError: true, expectError: true,
setupMock: func(m *MockDb) {}, setupMock: func(m *mocks.MockDb) {},
}, },
} }