Remove the subprefixdbmock

This commit is contained in:
Alfred Kamanda 2024-11-01 13:17:45 +03:00
parent 45945ae9c5
commit cfe3e526df
Signed by: Alfred-mk
GPG Key ID: 7EA3D01708908703
2 changed files with 59 additions and 49 deletions

View File

@ -16,6 +16,7 @@ import (
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/state" "git.defalsify.org/vise.git/state"
"git.grassecon.net/urdt/ussd/internal/models" "git.grassecon.net/urdt/ussd/internal/models"
"git.grassecon.net/urdt/ussd/internal/storage"
"git.grassecon.net/urdt/ussd/internal/testutil/mocks" "git.grassecon.net/urdt/ussd/internal/testutil/mocks"
"git.grassecon.net/urdt/ussd/internal/testutil/testservice" "git.grassecon.net/urdt/ussd/internal/testutil/testservice"
@ -26,6 +27,7 @@ import (
testdataloader "github.com/peteole/testdata-loader" testdataloader "github.com/peteole/testdata-loader"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
memdb "git.defalsify.org/vise.git/db/mem"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
) )
@ -2021,19 +2023,24 @@ func TestSetDefaultVoucher(t *testing.T) {
func TestCheckVouchers(t *testing.T) { func TestCheckVouchers(t *testing.T) {
mockDataStore := new(mocks.MockUserDataStore) mockDataStore := new(mocks.MockUserDataStore)
mockAccountService := new(mocks.MockAccountService) mockAccountService := new(mocks.MockAccountService)
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
sessionId := "session123" sessionId := "session123"
publicKey := "0X13242618721" publicKey := "0X13242618721"
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
db := memdb.NewMemDb()
err := db.Connect(ctx, "")
if err != nil {
t.Fatal(err)
}
spdb := storage.NewSubPrefixDb(db, []byte("vouchers"))
h := &Handlers{ h := &Handlers{
userdataStore: mockDataStore, userdataStore: mockDataStore,
accountService: mockAccountService, accountService: mockAccountService,
prefixDb: mockSubPrefixDb, prefixDb: spdb,
} }
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil) mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_PUBLIC_KEY).Return([]byte(publicKey), nil)
mockVouchersResponse := &models.VoucherHoldingResponse{} mockVouchersResponse := &models.VoucherHoldingResponse{}
@ -2042,38 +2049,53 @@ func TestCheckVouchers(t *testing.T) {
{ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"}, {ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
} }
expectedSym := []byte("1:SRF\n2:MILO")
mockAccountService.On("FetchVouchers", string(publicKey)).Return(mockVouchersResponse, nil) mockAccountService.On("FetchVouchers", string(publicKey)).Return(mockVouchersResponse, nil)
mockSubPrefixDb.On("Put", ctx, []byte("sym"), []byte("1:SRF\n2:MILO")).Return(nil) _, err = h.CheckVouchers(ctx, "check_vouchers", []byte(""))
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) assert.NoError(t, err)
// Read voucher sym data from the store
voucherData, err := spdb.Get(ctx, []byte("sym"))
if err != nil {
t.Fatal(err)
}
// assert that the data is stored correctly
assert.Equal(t, expectedSym, voucherData)
mockDataStore.AssertExpectations(t) mockDataStore.AssertExpectations(t)
mockAccountService.AssertExpectations(t) mockAccountService.AssertExpectations(t)
} }
func TestGetVoucherList(t *testing.T) { func TestGetVoucherList(t *testing.T) {
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
sessionId := "session123" sessionId := "session123"
ctx := context.WithValue(context.Background(), "SessionId", sessionId) ctx := context.WithValue(context.Background(), "SessionId", sessionId)
db := memdb.NewMemDb()
err := db.Connect(ctx, "")
if err != nil {
t.Fatal(err)
}
spdb := storage.NewSubPrefixDb(db, []byte("vouchers"))
h := &Handlers{ h := &Handlers{
prefixDb: mockSubPrefixDb, prefixDb: spdb,
} }
mockSubPrefixDb.On("Get", ctx, []byte("sym")).Return([]byte("1:SRF\n2:MILO"), nil) expectedSym := []byte("1:SRF\n2:MILO")
// Put voucher sym data from the store
err = spdb.Put(ctx, []byte("sym"), expectedSym)
if err != nil {
t.Fatal(err)
}
res, err := h.GetVoucherList(ctx, "", []byte("")) res, err := h.GetVoucherList(ctx, "", []byte(""))
assert.NoError(t, err)
assert.Contains(t, res.Content, "1:SRF\n2:MILO")
mockSubPrefixDb.AssertExpectations(t) assert.NoError(t, err)
assert.Equal(t, res.Content, string(expectedSym))
} }
func TestViewVoucher(t *testing.T) { func TestViewVoucher(t *testing.T) {
@ -2082,27 +2104,37 @@ func TestViewVoucher(t *testing.T) {
t.Logf(err.Error()) t.Logf(err.Error())
} }
mockDataStore := new(mocks.MockUserDataStore) mockDataStore := new(mocks.MockUserDataStore)
mockSubPrefixDb := new(mocks.MockSubPrefixDb)
sessionId := "session123" sessionId := "session123"
ctx := context.WithValue(context.Background(), "SessionId", sessionId) ctx := context.WithValue(context.Background(), "SessionId", sessionId)
db := memdb.NewMemDb()
err = db.Connect(ctx, "")
if err != nil {
t.Fatal(err)
}
spdb := storage.NewSubPrefixDb(db, []byte("vouchers"))
h := &Handlers{ h := &Handlers{
userdataStore: mockDataStore, userdataStore: mockDataStore,
flagManager: fm.parser, flagManager: fm.parser,
prefixDb: mockSubPrefixDb, prefixDb: spdb,
} }
// Define mock voucher data // Define mock voucher data
mockVoucherData := map[string]string{ mockData := map[string][]byte{
"sym": "1:SRF", "sym": []byte("1:SRF\n2:MILO"),
"bal": "1:100", "bal": []byte("1:100\n2:200"),
"deci": "1:6", "deci": []byte("1:6\n2:4"),
"addr": "1:0xd4c288865Ce", "addr": []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"),
} }
for key, value := range mockVoucherData { // Put the data
mockSubPrefixDb.On("Get", ctx, []byte(key)).Return([]byte(value), nil) for key, value := range mockData {
err = spdb.Put(ctx, []byte(key), []byte(value))
if err != nil {
t.Fatal(err)
}
} }
// Set up expectations for mockDataStore // Set up expectations for mockDataStore
@ -2115,7 +2147,6 @@ func TestViewVoucher(t *testing.T) {
assert.Contains(t, res.Content, "SRF\n100") assert.Contains(t, res.Content, "SRF\n100")
mockDataStore.AssertExpectations(t) mockDataStore.AssertExpectations(t)
mockSubPrefixDb.AssertExpectations(t)
} }
func TestSetVoucher(t *testing.T) { func TestSetVoucher(t *testing.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)
}