Compare commits

..

No commits in common. "d1d5c897d1e0ad724a303c8517ca6f6fdb0430fe" and "de24ca0648fe88a5be88336a75cfa91915649e5a" have entirely different histories.

4 changed files with 117 additions and 76 deletions

View File

@ -4,62 +4,84 @@ import (
"context" "context"
"testing" "testing"
"git.grassecon.net/urdt/ussd/internal/handlers/ussd/mocks" "git.defalsify.org/vise.git/lang"
"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
@ -448,7 +470,7 @@ import (
func TestSaveFirstname(t *testing.T) { func TestSaveFirstname(t *testing.T) {
// Create a mock database // Create a mock database
mockDb := new(mocks.MockDb) mockDb := new(MockDb)
// Create a Handlers instance with the mock database // Create a Handlers instance with the mock database
h := &Handlers{ h := &Handlers{
@ -462,13 +484,13 @@ func TestSaveFirstname(t *testing.T) {
name string name string
input []byte input []byte
expectError bool expectError bool
setupMock func(*mocks.MockDb) setupMock func(*MockDb)
}{ }{
{ {
name: "Valid first name", name: "Valid first name",
input: []byte("John"), input: []byte("John"),
expectError: false, expectError: false,
setupMock: func(m *mocks.MockDb) { setupMock: func(m *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)
@ -478,7 +500,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 *mocks.MockDb) {}, setupMock: func(m *MockDb) {},
}, },
} }
@ -506,7 +528,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(mocks.MockDb) mockDb := new(MockDb)
// Create a Handlers instance with the mock database // Create a Handlers instance with the mock database
h := &Handlers{ h := &Handlers{
@ -520,13 +542,13 @@ func TestSaveFamilyname(t *testing.T) {
name string name string
input []byte input []byte
expectError bool expectError bool
setupMock func(*mocks.MockDb) setupMock func(*MockDb)
}{ }{
{ {
name: "Valid family name", name: "Valid family name",
input: []byte("Smith"), input: []byte("Smith"),
expectError: false, expectError: false,
setupMock: func(m *mocks.MockDb) { setupMock: func(m *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)
@ -536,7 +558,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 *mocks.MockDb) {}, setupMock: func(m *MockDb) {},
}, },
} }

View File

@ -4,6 +4,7 @@ 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"
) )
@ -12,7 +13,7 @@ type MockDb struct {
} }
func (m *MockDb) SetPrefix(prefix uint8) { func (m *MockDb) SetPrefix(prefix uint8) {
m.Called(prefix) m.Called(utils.DATA_FAMILY_NAME)
} }
func (m *MockDb) Prefix() uint8 { func (m *MockDb) Prefix() uint8 {

View File

@ -0,0 +1,44 @@
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

@ -1,26 +0,0 @@
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)
}