ussd/common/vouchers_test.go

201 lines
5.5 KiB
Go
Raw Permalink Normal View History

2024-10-31 14:19:44 +01:00
package common
2024-10-29 22:53:13 +01:00
import (
"context"
"fmt"
2024-10-29 22:53:13 +01:00
"testing"
"github.com/alecthomas/assert/v2"
"github.com/stretchr/testify/require"
visedb "git.defalsify.org/vise.git/db"
2024-10-29 22:53:13 +01:00
memdb "git.defalsify.org/vise.git/db/mem"
"git.grassecon.net/urdt/ussd/internal/storage"
2024-10-30 16:30:55 +01:00
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
2024-10-29 22:53:13 +01:00
)
// InitializeTestDb sets up and returns an in-memory database and store.
func InitializeTestDb(t *testing.T) (context.Context, *UserDataStore) {
ctx := context.Background()
// Initialize memDb
db := memdb.NewMemDb()
err := db.Connect(ctx, "")
require.NoError(t, err, "Failed to connect to memDb")
// Create UserDataStore with memDb
store := &UserDataStore{Db: db}
t.Cleanup(func() {
db.Close() // Ensure the DB is closed after each test
})
return ctx, store
}
2024-10-29 22:53:13 +01:00
func TestMatchVoucher(t *testing.T) {
2024-12-13 09:40:39 +01:00
symbols := "1:SRF\n2:MILO"
2024-10-29 22:53:13 +01:00
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 TestProcessVouchers(t *testing.T) {
2024-10-30 16:30:55 +01:00
holdings := []dataserviceapi.TokenHoldings{
2024-11-16 12:52:24 +01:00
{ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100000000"},
{ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200000000"},
2024-10-29 22:53:13 +01:00
}
expectedResult := VoucherMetadata{
2024-12-13 09:40:39 +01:00
Symbols: "1:SRF\n2:MILO",
2024-11-16 12:52:24 +01:00
Balances: "1:100\n2:20000",
Decimals: "1:6\n2:4",
Addresses: "1:0xd4c288865Ce\n2:0x41c188d63Qa",
2024-10-29 22:53:13 +01:00
}
result := ProcessVouchers(holdings)
assert.Equal(t, expectedResult, result)
}
func TestGetVoucherData(t *testing.T) {
ctx := context.Background()
db := memdb.NewMemDb()
err := db.Connect(ctx, "")
if err != nil {
t.Fatal(err)
}
prefix := ToBytes(visedb.DATATYPE_USERDATA)
spdb := storage.NewSubPrefixDb(db, prefix)
2024-10-29 22:53:13 +01:00
// Test voucher data
2024-12-04 18:55:03 +01:00
mockData := map[DataTyp][]byte{
2024-12-13 09:40:39 +01:00
DATA_VOUCHER_SYMBOLS: []byte("1:SRF\n2:MILO"),
DATA_VOUCHER_BALANCES: []byte("1:100\n2:200"),
DATA_VOUCHER_DECIMALS: []byte("1:6\n2:4"),
DATA_VOUCHER_ADDRESSES: []byte("1:0xd4c288865Ce\n2:0x41c188d63Qa"),
2024-10-29 22:53:13 +01:00
}
// Put the data
for key, value := range mockData {
err = spdb.Put(ctx, []byte(ToBytes(key)), []byte(value))
2024-10-29 22:53:13 +01:00
if err != nil {
t.Fatal(err)
}
}
result, err := GetVoucherData(ctx, spdb, "1")
assert.NoError(t, err)
2024-10-30 16:30:55 +01:00
assert.Equal(t, "SRF", result.TokenSymbol)
2024-10-29 22:53:13 +01:00
assert.Equal(t, "100", result.Balance)
2024-10-30 16:30:55 +01:00
assert.Equal(t, "6", result.TokenDecimals)
assert.Equal(t, "0xd4c288865Ce", result.ContractAddress)
2024-10-29 22:53:13 +01:00
}
func TestStoreTemporaryVoucher(t *testing.T) {
ctx, store := InitializeTestDb(t)
2024-10-29 22:53:13 +01:00
sessionId := "session123"
// Test data
2024-10-30 16:30:55 +01:00
voucherData := &dataserviceapi.TokenHoldings{
TokenSymbol: "SRF",
Balance: "200",
TokenDecimals: "6",
ContractAddress: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
2024-10-29 22:53:13 +01:00
}
// Execute the function being tested
err := StoreTemporaryVoucher(ctx, store, sessionId, voucherData)
require.NoError(t, err)
2024-10-29 22:53:13 +01:00
// Verify stored data
expectedData := fmt.Sprintf("%s,%s,%s,%s", "SRF", "200", "6", "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9")
2024-10-29 22:53:13 +01:00
storedValue, err := store.ReadEntry(ctx, sessionId, DATA_TEMPORARY_VALUE)
require.NoError(t, err)
require.Equal(t, expectedData, string(storedValue), "Mismatch for key %v", DATA_TEMPORARY_VALUE)
2024-10-29 22:53:13 +01:00
}
func TestGetTemporaryVoucherData(t *testing.T) {
ctx, store := InitializeTestDb(t)
2024-10-29 22:53:13 +01:00
sessionId := "session123"
// Test voucher data
2024-10-30 16:30:55 +01:00
tempData := &dataserviceapi.TokenHoldings{
TokenSymbol: "SRF",
Balance: "200",
TokenDecimals: "6",
ContractAddress: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
2024-10-29 22:53:13 +01:00
}
// Store the data
err := StoreTemporaryVoucher(ctx, store, sessionId, tempData)
require.NoError(t, err)
2024-10-29 22:53:13 +01:00
// Execute the function being tested
data, err := GetTemporaryVoucherData(ctx, store, sessionId)
require.NoError(t, err)
require.Equal(t, tempData, data)
2024-10-29 22:53:13 +01:00
}
func TestUpdateVoucherData(t *testing.T) {
ctx, store := InitializeTestDb(t)
2024-10-29 22:53:13 +01:00
sessionId := "session123"
// New voucher data
2024-10-30 16:30:55 +01:00
newData := &dataserviceapi.TokenHoldings{
TokenSymbol: "SRF",
Balance: "200",
TokenDecimals: "6",
ContractAddress: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9",
2024-10-29 22:53:13 +01:00
}
// Old temporary data
2024-10-30 16:30:55 +01:00
tempData := &dataserviceapi.TokenHoldings{
TokenSymbol: "OLD",
Balance: "100",
TokenDecimals: "8",
ContractAddress: "0xold",
2024-10-29 22:53:13 +01:00
}
require.NoError(t, StoreTemporaryVoucher(ctx, store, sessionId, tempData))
2024-10-29 22:53:13 +01:00
// Execute update
err := UpdateVoucherData(ctx, store, sessionId, newData)
require.NoError(t, err)
2024-10-29 22:53:13 +01:00
// Verify active data was stored correctly
activeEntries := map[DataTyp][]byte{
DATA_ACTIVE_SYM: []byte(newData.TokenSymbol),
DATA_ACTIVE_BAL: []byte(newData.Balance),
DATA_ACTIVE_DECIMAL: []byte(newData.TokenDecimals),
DATA_ACTIVE_ADDRESS: []byte(newData.ContractAddress),
2024-10-29 22:53:13 +01:00
}
for key, expectedValue := range activeEntries {
storedValue, err := store.ReadEntry(ctx, sessionId, key)
require.NoError(t, err)
require.Equal(t, expectedValue, storedValue, "Active data mismatch for key %v", key)
2024-10-29 22:53:13 +01:00
}
}