From 2ace3606bc4045998c4ad0524c595d9b386983bd Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 13 Mar 2025 13:19:58 +0300 Subject: [PATCH] Added TestUpdateSwapFromVoucherData --- store/swap_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/store/swap_test.go b/store/swap_test.go index f2e254d..5e29df1 100644 --- a/store/swap_test.go +++ b/store/swap_test.go @@ -8,6 +8,8 @@ import ( memdb "git.defalsify.org/vise.git/db/mem" storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db" "github.com/alecthomas/assert/v2" + dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" + "github.com/stretchr/testify/require" ) func TestReadSwapData(t *testing.T) { @@ -128,3 +130,40 @@ func TestGetSwapFromVoucherData(t *testing.T) { assert.Equal(t, "6", result.TokenDecimals) assert.Equal(t, "0xc7B78Ac9ACB9E025C8234621FC515bC58179dEAe", result.ContractAddress) } + +func TestUpdateSwapFromVoucherData(t *testing.T) { + ctx, store := InitializeTestDb(t) + sessionId := "session123" + + // New swap from voucher data + newData := &dataserviceapi.TokenHoldings{ + TokenSymbol: "AMANI", + TokenDecimals: "6", + ContractAddress: "0xc7B78Ac9ACB9E025C8234621FC515bC58179dEAe", + } + + // Old temporary data + tempData := &dataserviceapi.TokenHoldings{ + TokenSymbol: "OLD", + TokenDecimals: "8", + ContractAddress: "0xold", + } + require.NoError(t, StoreTemporaryVoucher(ctx, store, sessionId, tempData)) + + // Execute update + err := UpdateSwapFromVoucherData(ctx, store, sessionId, newData) + require.NoError(t, err) + + // Verify active swap from data was stored correctly + activeEntries := map[storedb.DataTyp][]byte{ + storedb.DATA_ACTIVE_SWAP_FROM_SYM: []byte(newData.TokenSymbol), + storedb.DATA_ACTIVE_SWAP_FROM_DECIMAL: []byte(newData.TokenDecimals), + storedb.DATA_ACTIVE_SWAP_FROM_ADDRESS: []byte(newData.ContractAddress), + } + + for key, expectedValue := range activeEntries { + storedValue, err := store.ReadEntry(ctx, sessionId, key) + require.NoError(t, err) + require.Equal(t, expectedValue, storedValue, "Active swap from data mismatch for key %v", key) + } +} \ No newline at end of file