Compare commits

..

4 Commits

Author SHA1 Message Date
d1d5c897d1
refactor 2024-09-07 09:55:59 +03:00
04ea11dd6d
setup db mock 2024-09-07 09:55:47 +03:00
5722552395
remove old mocks 2024-09-07 09:55:13 +03:00
421fbe5543
setup mock for the acccount service 2024-09-07 09:55:00 +03:00
4 changed files with 76 additions and 117 deletions

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) {},
}, },
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"git.defalsify.org/vise.git/lang" "git.defalsify.org/vise.git/lang"
"git.grassecon.net/urdt/ussd/internal/utils"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
) )
@ -13,7 +12,7 @@ type MockDb struct {
} }
func (m *MockDb) SetPrefix(prefix uint8) { func (m *MockDb) SetPrefix(prefix uint8) {
m.Called(utils.DATA_FAMILY_NAME) m.Called(prefix)
} }
func (m *MockDb) Prefix() uint8 { func (m *MockDb) Prefix() uint8 {

View File

@ -1,44 +0,0 @@
package mocks
import (
"git.grassecon.net/urdt/ussd/internal/models"
"github.com/stretchr/testify/mock"
)
type MockAccountFileHandler struct {
mock.Mock
}
func (m *MockAccountFileHandler) EnsureFileExists() error {
args := m.Called()
return args.Error(0)
}
func (m *MockAccountFileHandler) ReadAccountData() (map[string]string, error) {
args := m.Called()
return args.Get(0).(map[string]string), args.Error(1)
}
func (m *MockAccountFileHandler) WriteAccountData(data map[string]string) error {
args := m.Called(data)
return args.Error(0)
}
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) CheckAccountStatus(TrackingId string) (string, error) {
args := m.Called()
return args.Get(0).(string), args.Error(1)
}
func (m *MockAccountService) CheckBalance(PublicKey string) (string, error) {
args := m.Called()
return args.Get(0).(string), args.Error(1)
}

View File

@ -0,0 +1,26 @@
package mocks
import (
"git.grassecon.net/urdt/ussd/internal/models"
"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)
}