Compare commits

..

No commits in common. "2c361e5b96fe98e5c64964676cb2e6d4b57b8aac" and "dc782d87a8a0cf3e324fc9f3ce7b93b01a420803" have entirely different histories.

4 changed files with 82 additions and 496 deletions

View File

@ -72,7 +72,6 @@ type Handlers struct {
userdataStore utils.DataStore userdataStore utils.DataStore
flagManager *asm.FlagParser flagManager *asm.FlagParser
accountService server.AccountServiceInterface accountService server.AccountServiceInterface
prefixDb storage.PrefixDb
} }
func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, accountService server.AccountServiceInterface) (*Handlers, error) { func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, accountService server.AccountServiceInterface) (*Handlers, error) {
@ -82,14 +81,10 @@ func NewHandlers(appFlags *asm.FlagParser, userdataStore db.Db, accountService s
userDb := &utils.UserDataStore{ userDb := &utils.UserDataStore{
Db: userdataStore, Db: userdataStore,
} }
// Instantiate the SubPrefixDb with "vouchers" prefix
prefixDb := storage.NewSubPrefixDb(userdataStore, []byte("vouchers"))
h := &Handlers{ h := &Handlers{
userdataStore: userDb, userdataStore: userDb,
flagManager: appFlags, flagManager: appFlags,
accountService: accountService, accountService: accountService,
prefixDb: prefixDb,
} }
return h, nil return h, nil
} }
@ -1107,6 +1102,7 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
return res, nil return res, nil
} }
prefixdb := storage.NewSubPrefixDb(store, []byte("vouchers"))
data := processVouchers(vouchersResp.Result.Holdings) data := processVouchers(vouchersResp.Result.Holdings)
// Store all voucher data // Store all voucher data
@ -1118,7 +1114,7 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte)
} }
for key, value := range dataMap { for key, value := range dataMap {
if err := h.prefixDb.Put(ctx, []byte(key), []byte(value)); err != nil { if err := prefixdb.Put(ctx, []byte(key), []byte(value)); err != nil {
return res, nil return res, nil
} }
} }
@ -1156,9 +1152,12 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte)
var res resource.Result var res resource.Result
// Read vouchers from the store // Read vouchers from the store
voucherData, err := h.prefixDb.Get(ctx, []byte("sym")) store := h.userdataStore
prefixdb := storage.NewSubPrefixDb(store, []byte("vouchers"))
voucherData, err := prefixdb.Get(ctx, []byte("sym"))
if err != nil { if err != nil {
return res, err return res, nil
} }
res.Content = string(voucherData) res.Content = string(voucherData)
@ -1169,6 +1168,8 @@ func (h *Handlers) GetVoucherList(ctx context.Context, sym string, input []byte)
// ViewVoucher retrieves the token holding and balance from the subprefixDB // ViewVoucher retrieves the token holding and balance from the subprefixDB
func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
var res resource.Result var res resource.Result
store := h.userdataStore
sessionId, ok := ctx.Value("SessionId").(string) sessionId, ok := ctx.Value("SessionId").(string)
if !ok { if !ok {
return res, fmt.Errorf("missing session") return res, fmt.Errorf("missing session")
@ -1182,8 +1183,10 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
return res, nil return res, nil
} }
// Retrieve the voucher metadata using the PrefixDb interface prefixdb := storage.NewSubPrefixDb(store, []byte("vouchers"))
metadata, err := getVoucherData(ctx, h.prefixDb, inputStr)
// Retrieve the voucher metadata
metadata, err := getVoucherData(ctx, prefixdb, inputStr)
if err != nil { if err != nil {
return res, fmt.Errorf("failed to retrieve voucher data: %v", err) return res, fmt.Errorf("failed to retrieve voucher data: %v", err)
} }
@ -1204,7 +1207,7 @@ func (h *Handlers) ViewVoucher(ctx context.Context, sym string, input []byte) (r
} }
// getVoucherData retrieves and matches voucher data // getVoucherData retrieves and matches voucher data
func getVoucherData(ctx context.Context, db storage.PrefixDb, input string) (*VoucherMetadata, error) { func getVoucherData(ctx context.Context, db *storage.SubPrefixDb, input string) (*VoucherMetadata, error) {
keys := []string{"sym", "bal", "deci", "addr"} keys := []string{"sym", "bal", "deci", "addr"}
data := make(map[string]string) data := make(map[string]string)

View File

@ -1480,8 +1480,8 @@ func TestValidateAmount(t *testing.T) {
}, },
}, },
{ {
name: "Test with invalid amount format", name: "Test with invalid amount format",
input: []byte("0.02ms"), input: []byte("0.02ms"),
activeBal: []byte("5"), activeBal: []byte("5"),
expectedResult: resource.Result{ expectedResult: resource.Result{
FlagSet: []uint32{flag_invalid_amount}, FlagSet: []uint32{flag_invalid_amount},
@ -1810,6 +1810,67 @@ func TestConfirmPin(t *testing.T) {
} }
} }
func TestSetVoucher(t *testing.T) {
mockDataStore := new(mocks.MockUserDataStore)
sessionId := "session123"
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
// Define the temporary voucher data
temporarySym := []byte("tempSym")
temporaryBal := []byte("tempBal")
temporaryDecimal := []byte("2")
temporaryAddress := []byte("0x123456")
// Define the expected active entries
activeEntries := map[utils.DataTyp][]byte{
utils.DATA_ACTIVE_SYM: temporarySym,
utils.DATA_ACTIVE_BAL: temporaryBal,
utils.DATA_ACTIVE_DECIMAL: temporaryDecimal,
utils.DATA_ACTIVE_ADDRESS: temporaryAddress,
}
// Define the temporary entries to be cleared
tempEntries := map[utils.DataTyp][]byte{
utils.DATA_TEMPORARY_SYM: []byte(""),
utils.DATA_TEMPORARY_BAL: []byte(""),
utils.DATA_TEMPORARY_DECIMAL: []byte(""),
utils.DATA_TEMPORARY_ADDRESS: []byte(""),
}
// Mocking ReadEntry calls for temporary data retrieval
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_SYM).Return(temporarySym, nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_BAL).Return(temporaryBal, nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_DECIMAL).Return(temporaryDecimal, nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_ADDRESS).Return(temporaryAddress, nil)
// Mocking WriteEntry calls for setting active data
for key, value := range activeEntries {
mockDataStore.On("WriteEntry", ctx, sessionId, key, value).Return(nil)
}
// Mocking WriteEntry calls for clearing temporary data
for key, value := range tempEntries {
mockDataStore.On("WriteEntry", ctx, sessionId, key, value).Return(nil)
}
h := &Handlers{
userdataStore: mockDataStore,
}
// Call the method under test
res, err := h.SetVoucher(ctx, "someSym", []byte{})
// Assert that no errors occurred
assert.NoError(t, err)
// Assert that the result content is correct
assert.Equal(t, string(temporarySym), res.Content)
// Assert that expectations were met
mockDataStore.AssertExpectations(t)
}
func TestFetchCustodialBalances(t *testing.T) { func TestFetchCustodialBalances(t *testing.T) {
fm, err := NewFlagManager(flagsPath) fm, err := NewFlagManager(flagsPath)
if err != nil { if err != nil {
@ -1891,456 +1952,3 @@ func TestFetchCustodialBalances(t *testing.T) {
}) })
} }
} }
func TestSetDefaultVoucher(t *testing.T) {
fm, err := NewFlagManager(flagsPath)
if err != nil {
t.Logf(err.Error())
}
flag_no_active_voucher, err := fm.GetFlag("flag_no_active_voucher")
if err != nil {
t.Logf(err.Error())
}
// Define session ID and mock data
sessionId := "session123"
publicKey := "0X13242618721"
notFoundErr := db.ErrNotFound{}
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
tests := []struct {
name string
vouchersResp *models.VoucherHoldingResponse
expectedResult resource.Result
}{
{
name: "Test set default voucher when no active voucher exists",
vouchersResp: &models.VoucherHoldingResponse{
Ok: true,
Description: "Vouchers fetched successfully",
Result: struct {
Holdings []struct {
ContractAddress string `json:"contractAddress"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimals string `json:"tokenDecimals"`
Balance string `json:"balance"`
} `json:"holdings"`
}{
Holdings: []struct {
ContractAddress string `json:"contractAddress"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimals string `json:"tokenDecimals"`
Balance string `json:"balance"`
}{
{
ContractAddress: "0x123",
TokenSymbol: "TOKEN1",
TokenDecimals: "18",
Balance: "100",
},
},
},
},
expectedResult: resource.Result{},
},
{
name: "Test no vouchers available",
vouchersResp: &models.VoucherHoldingResponse{
Ok: true,
Description: "No vouchers available",
Result: struct {
Holdings []struct {
ContractAddress string `json:"contractAddress"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimals string `json:"tokenDecimals"`
Balance string `json:"balance"`
} `json:"holdings"`
}{
Holdings: []struct {
ContractAddress string `json:"contractAddress"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimals string `json:"tokenDecimals"`
Balance string `json:"balance"`
}{},
},
},
expectedResult: resource.Result{
FlagSet: []uint32{flag_no_active_voucher},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockDataStore := new(mocks.MockUserDataStore)
mockAccountService := new(mocks.MockAccountService)
h := &Handlers{
userdataStore: mockDataStore,
accountService: mockAccountService,
flagManager: fm.parser,
}
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM).Return([]byte(""), notFoundErr)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
mockAccountService.On("FetchVouchers", string(publicKey)).Return(tt.vouchersResp, nil)
if len(tt.vouchersResp.Result.Holdings) > 0 {
firstVoucher := tt.vouchersResp.Result.Holdings[0]
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM, []byte(firstVoucher.TokenSymbol)).Return(nil)
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACTIVE_BAL, []byte(firstVoucher.Balance)).Return(nil)
}
res, err := h.SetDefaultVoucher(ctx, "set_default_voucher", []byte("some-input"))
assert.NoError(t, err)
assert.Equal(t, res, tt.expectedResult, "Expected result should be equal to the actual result")
mockDataStore.AssertExpectations(t)
mockAccountService.AssertExpectations(t)
})
}
}
func TestCheckVouchers(t *testing.T) {
mockDataStore := new(mocks.MockUserDataStore)
mockAccountService := new(mocks.MockAccountService)
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
sessionId := "session123"
publicKey := "0X13242618721"
h := &Handlers{
userdataStore: mockDataStore,
accountService: mockAccountService,
prefixDb: mockSubPrefixDb,
}
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
mockVouchersResponse := &models.VoucherHoldingResponse{}
mockVouchersResponse.Result.Holdings = []struct {
ContractAddress string `json:"contractAddress"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimals string `json:"tokenDecimals"`
Balance string `json:"balance"`
}{
{ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"},
{ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
}
mockAccountService.On("FetchVouchers", string(publicKey)).Return(mockVouchersResponse, nil)
mockSubPrefixDb.On("Put", ctx, []byte("sym"), []byte("1:SRF\n2:MILO")).Return(nil)
mockSubPrefixDb.On("Put", ctx, []byte("bal"), []byte("1:100\n2:200")).Return(nil)
mockSubPrefixDb.On("Put", ctx, []byte("deci"), []byte("1:6\n2:4")).Return(nil)
mockSubPrefixDb.On("Put", ctx, []byte("addr"), []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa")).Return(nil)
_, err := h.CheckVouchers(ctx, "check_vouchers", []byte(""))
assert.NoError(t, err)
mockDataStore.AssertExpectations(t)
mockAccountService.AssertExpectations(t)
}
func TestProcessVouchers(t *testing.T) {
holdings := []struct {
ContractAddress string `json:"contractAddress"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimals string `json:"tokenDecimals"`
Balance string `json:"balance"`
}{
{ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"},
{ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
}
expectedResult := VoucherMetadata{
Symbol: "1:SRF\n2:MILO",
Balance: "1:100\n2:200",
Decimal: "1:6\n2:4",
Address: "1:0xd4c288865Ce\n2:0x41c188d63Qa",
}
result := processVouchers(holdings)
assert.Equal(t, expectedResult, result)
}
func TestGetVoucherList(t *testing.T) {
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
sessionId := "session123"
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
h := &Handlers{
prefixDb: mockSubPrefixDb,
}
mockSubPrefixDb.On("Get", ctx, []byte("sym")).Return([]byte("1:SRF\n2:MILO"), nil)
res, err := h.GetVoucherList(ctx, "", []byte(""))
assert.NoError(t, err)
assert.Contains(t, res.Content, "1:SRF\n2:MILO")
mockSubPrefixDb.AssertExpectations(t)
}
func TestViewVoucher(t *testing.T) {
fm, err := NewFlagManager(flagsPath)
if err != nil {
t.Logf(err.Error())
}
mockDataStore := new(mocks.MockUserDataStore)
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
sessionId := "session123"
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
h := &Handlers{
userdataStore: mockDataStore,
flagManager: fm.parser,
prefixDb: mockSubPrefixDb,
}
// Define mock voucher data
mockVoucherData := map[string]string{
"sym": "1:SRF",
"bal": "1:100",
"deci": "1:6",
"addr": "1:0xd4c288865Ce",
}
for key, value := range mockVoucherData {
mockSubPrefixDb.On("Get", ctx, []byte(key)).Return([]byte(value), nil)
}
// Set up expectations for mockDataStore
expectedData := map[utils.DataTyp]string{
utils.DATA_TEMPORARY_SYM: "SRF",
utils.DATA_TEMPORARY_BAL: "100",
utils.DATA_TEMPORARY_DECIMAL: "6",
utils.DATA_TEMPORARY_ADDRESS: "0xd4c288865Ce",
}
for dataType, dataValue := range expectedData {
mockDataStore.On("WriteEntry", ctx, sessionId, dataType, []byte(dataValue)).Return(nil)
}
res, err := h.ViewVoucher(ctx, "view_voucher", []byte("1"))
assert.NoError(t, err)
assert.Contains(t, res.Content, "SRF\n100")
mockDataStore.AssertExpectations(t)
mockSubPrefixDb.AssertExpectations(t)
}
func TestGetVoucherData(t *testing.T) {
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
ctx := context.Background()
// Mocked voucher data
mockData := map[string][]byte{
"sym": []byte("1:SRF\n2:MILO"),
"bal": []byte("1:100\n2:200"),
"deci": []byte("1:6\n2:4"),
"addr": []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"),
}
// Mock Get calls
for key, value := range mockData {
mockSubPrefixDb.On("Get", ctx, []byte(key)).Return(value, nil)
}
result, err := getVoucherData(ctx, mockSubPrefixDb, "1")
assert.NoError(t, err)
assert.Equal(t, "SRF", result.Symbol)
assert.Equal(t, "100", result.Balance)
assert.Equal(t, "6", result.Decimal)
assert.Equal(t, "0xd4c288865Ce", result.Address)
mockSubPrefixDb.AssertExpectations(t)
}
func TestMatchVoucher(t *testing.T) {
symbols := "1:SRF\n2:MILO"
balances := "1:100\n2:200"
decimals := "1:6\n2:4"
addresses := "1:0xd4c288865Ce\n2:0x41c188d63Qa"
// Test for valid voucher
symbol, balance, decimal, address := matchVoucher("2", symbols, balances, decimals, addresses)
// Assertions for valid voucher
assert.Equal(t, "MILO", symbol)
assert.Equal(t, "200", balance)
assert.Equal(t, "4", decimal)
assert.Equal(t, "0x41c188d63Qa", address)
// Test for non-existent voucher
symbol, balance, decimal, address = matchVoucher("3", symbols, balances, decimals, addresses)
// Assertions for non-match
assert.Equal(t, "", symbol)
assert.Equal(t, "", balance)
assert.Equal(t, "", decimal)
assert.Equal(t, "", address)
}
func TestStoreTemporaryVoucher(t *testing.T) {
mockDataStore := new(mocks.MockUserDataStore)
ctx := context.Background()
sessionId := "session123"
voucherData := &VoucherMetadata{
Symbol: "SRF",
Balance: "200",
Decimal: "6",
Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
}
// Define expected entries to be written
expectedEntries := map[utils.DataTyp][]byte{
utils.DATA_TEMPORARY_SYM: []byte("SRF"),
utils.DATA_TEMPORARY_BAL: []byte("200"),
utils.DATA_TEMPORARY_DECIMAL: []byte("6"),
utils.DATA_TEMPORARY_ADDRESS: []byte("0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9"),
}
// Mock WriteEntry calls
for key, value := range expectedEntries {
mockDataStore.On("WriteEntry", ctx, sessionId, key, value).Return(nil)
}
h := &Handlers{userdataStore: mockDataStore}
err := h.storeTemporaryVoucher(ctx, sessionId, voucherData)
assert.NoError(t, err)
mockDataStore.AssertExpectations(t)
}
func TestSetVoucher(t *testing.T) {
mockDataStore := new(mocks.MockUserDataStore)
sessionId := "session123"
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
// Define the temporary voucher data
tempData := &VoucherMetadata{
Symbol: "SRF",
Balance: "200",
Decimal: "6",
Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
}
// Define the expected active entries
activeEntries := map[utils.DataTyp][]byte{
utils.DATA_ACTIVE_SYM: []byte(tempData.Symbol),
utils.DATA_ACTIVE_BAL: []byte(tempData.Balance),
utils.DATA_ACTIVE_DECIMAL: []byte(tempData.Decimal),
utils.DATA_ACTIVE_ADDRESS: []byte(tempData.Address),
}
// Define the temporary entries to be cleared
tempEntries := map[utils.DataTyp][]byte{
utils.DATA_TEMPORARY_SYM: []byte(""),
utils.DATA_TEMPORARY_BAL: []byte(""),
utils.DATA_TEMPORARY_DECIMAL: []byte(""),
utils.DATA_TEMPORARY_ADDRESS: []byte(""),
}
// Mocking ReadEntry calls for temporary data retrieval
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_SYM).Return([]byte(tempData.Symbol), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_BAL).Return([]byte(tempData.Balance), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_DECIMAL).Return([]byte(tempData.Decimal), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_ADDRESS).Return([]byte(tempData.Address), nil)
// Mocking WriteEntry calls for setting active data
for key, value := range activeEntries {
mockDataStore.On("WriteEntry", ctx, sessionId, key, value).Return(nil)
}
// Mocking WriteEntry calls for clearing temporary data
for key, value := range tempEntries {
mockDataStore.On("WriteEntry", ctx, sessionId, key, value).Return(nil)
}
h := &Handlers{
userdataStore: mockDataStore,
}
res, err := h.SetVoucher(ctx, "someSym", []byte{})
assert.NoError(t, err)
assert.Equal(t, string(tempData.Symbol), res.Content)
mockDataStore.AssertExpectations(t)
}
func TestGetTemporaryVoucherData(t *testing.T) {
mockDataStore := new(mocks.MockUserDataStore)
sessionId := "session123"
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
h := &Handlers{userdataStore: mockDataStore}
// Mock temporary voucher data
tempData := &VoucherMetadata{
Symbol: "SRF",
Balance: "100",
Decimal: "6",
Address: "0xd4c288865Ce",
}
// Set up mocks for reading entries
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_SYM).Return([]byte(tempData.Symbol), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_BAL).Return([]byte(tempData.Balance), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_DECIMAL).Return([]byte(tempData.Decimal), nil)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_ADDRESS).Return([]byte(tempData.Address), nil)
data, err := h.getTemporaryVoucherData(ctx, sessionId)
assert.NoError(t, err)
assert.Equal(t, tempData, data)
mockDataStore.AssertExpectations(t)
}
func TestUpdateVoucherData(t *testing.T) {
mockDataStore := new(mocks.MockUserDataStore)
ctx := context.Background()
sessionId := "session123"
h := &Handlers{userdataStore: mockDataStore}
data := &VoucherMetadata{
Symbol: "SRF",
Balance: "100",
Decimal: "6",
Address: "0xd4c288865Ce",
}
// Mock WriteEntry for active data
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACTIVE_SYM, []byte(data.Symbol)).Return(nil)
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACTIVE_BAL, []byte(data.Balance)).Return(nil)
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACTIVE_DECIMAL, []byte(data.Decimal)).Return(nil)
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_ACTIVE_ADDRESS, []byte(data.Address)).Return(nil)
// Mock WriteEntry for clearing temporary data
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_SYM, []byte("")).Return(nil)
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_BAL, []byte("")).Return(nil)
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_DECIMAL, []byte("")).Return(nil)
mockDataStore.On("WriteEntry", ctx, sessionId, utils.DATA_TEMPORARY_ADDRESS, []byte("")).Return(nil)
err := h.updateVoucherData(ctx, sessionId, data)
assert.NoError(t, err)
mockDataStore.AssertExpectations(t)
}

View File

@ -1,21 +0,0 @@
package mocks
import (
"context"
"github.com/stretchr/testify/mock"
)
type MockSubPrefixDb struct {
mock.Mock
}
func (m *MockSubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
args := m.Called(ctx, key)
return args.Get(0).([]byte), args.Error(1)
}
func (m *MockSubPrefixDb) Put(ctx context.Context, key, val []byte) error {
args := m.Called(ctx, key, val)
return args.Error(0)
}

View File

@ -10,14 +10,6 @@ const (
DATATYPE_USERSUB = 64 DATATYPE_USERSUB = 64
) )
// PrefixDb interface abstracts the database operations.
type PrefixDb interface {
Get(ctx context.Context, key []byte) ([]byte, error)
Put(ctx context.Context, key []byte, val []byte) error
}
var _ PrefixDb = (*SubPrefixDb)(nil)
type SubPrefixDb struct { type SubPrefixDb struct {
store db.Db store db.Db
pfx []byte pfx []byte
@ -37,7 +29,11 @@ func (s *SubPrefixDb) toKey(k []byte) []byte {
func (s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) { func (s *SubPrefixDb) Get(ctx context.Context, key []byte) ([]byte, error) {
s.store.SetPrefix(DATATYPE_USERSUB) s.store.SetPrefix(DATATYPE_USERSUB)
key = s.toKey(key) key = s.toKey(key)
return s.store.Get(ctx, key) v, err := s.store.Get(ctx, key)
if err != nil {
return nil, err
}
return v, nil
} }
func (s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error { func (s *SubPrefixDb) Put(ctx context.Context, key []byte, val []byte) error {