From 8b399781e893ab6b678b0d38b7b960f0b60a9a4a Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 30 Oct 2024 18:40:03 +0300 Subject: [PATCH] make the VoucherMetadata more descriptive --- internal/handlers/ussd/menuhandler.go | 16 ++++----------- internal/handlers/ussd/menuhandler_test.go | 24 +++++++++++----------- internal/utils/vouchers.go | 18 ++++++++-------- internal/utils/vouchers_test.go | 8 ++++---- 4 files changed, 29 insertions(+), 37 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 231da95..3de4590 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -58,14 +58,6 @@ func (fm *FlagManager) GetFlag(label string) (uint32, error) { return fm.parser.GetFlag(label) } -// VoucherMetadata helps organize voucher data fields -type VoucherMetadata struct { - Symbol string - Balance string - Decimal string - Address string -} - type Handlers struct { pe *persist.Persister st *state.State @@ -1122,10 +1114,10 @@ func (h *Handlers) CheckVouchers(ctx context.Context, sym string, input []byte) // Store all voucher data dataMap := map[string]string{ - "sym": data.Symbol, - "bal": data.Balance, - "deci": data.Decimal, - "addr": data.Address, + "sym": data.Symbols, + "bal": data.Balances, + "deci": data.Decimals, + "addr": data.Addresses, } for key, value := range dataMap { diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 2b76bf1..12f155e 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -2096,19 +2096,19 @@ func TestSetVoucher(t *testing.T) { ctx := context.WithValue(context.Background(), "SessionId", sessionId) // Define the temporary voucher data - tempData := &VoucherMetadata{ - Symbol: "SRF", - Balance: "200", - Decimal: "6", - Address: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9", + tempData := &dataserviceapi.TokenHoldings{ + TokenSymbol: "SRF", + Balance: "200", + TokenDecimals: "6", + ContractAddress: "0xd4c288865Ce0985a481Eef3be02443dF5E2e4Ea9", } // Define the expected active entries activeEntries := map[utils.DataTyp][]byte{ - utils.DATA_ACTIVE_SYM: []byte(tempData.Symbol), + utils.DATA_ACTIVE_SYM: []byte(tempData.TokenSymbol), utils.DATA_ACTIVE_BAL: []byte(tempData.Balance), - utils.DATA_ACTIVE_DECIMAL: []byte(tempData.Decimal), - utils.DATA_ACTIVE_ADDRESS: []byte(tempData.Address), + utils.DATA_ACTIVE_DECIMAL: []byte(tempData.TokenDecimals), + utils.DATA_ACTIVE_ADDRESS: []byte(tempData.ContractAddress), } // Define the temporary entries to be cleared @@ -2120,10 +2120,10 @@ func TestSetVoucher(t *testing.T) { } // 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_SYM).Return([]byte(tempData.TokenSymbol), 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) + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_DECIMAL).Return([]byte(tempData.TokenDecimals), nil) + mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_TEMPORARY_ADDRESS).Return([]byte(tempData.ContractAddress), nil) // Mocking WriteEntry calls for setting active data for key, value := range activeEntries { @@ -2143,7 +2143,7 @@ func TestSetVoucher(t *testing.T) { assert.NoError(t, err) - assert.Equal(t, string(tempData.Symbol), res.Content) + assert.Equal(t, string(tempData.TokenSymbol), res.Content) mockDataStore.AssertExpectations(t) } diff --git a/internal/utils/vouchers.go b/internal/utils/vouchers.go index 2aed42a..73a95a6 100644 --- a/internal/utils/vouchers.go +++ b/internal/utils/vouchers.go @@ -9,12 +9,12 @@ import ( dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" ) -// VoucherMetadata helps organize voucher data fields +// VoucherMetadata helps organize data fields type VoucherMetadata struct { - Symbol string - Balance string - Decimal string - Address string + Symbols string + Balances string + Decimals string + Addresses string } // ProcessVouchers converts holdings into formatted strings @@ -29,10 +29,10 @@ func ProcessVouchers(holdings []dataserviceapi.TokenHoldings) VoucherMetadata { addresses = append(addresses, fmt.Sprintf("%d:%s", i+1, h.ContractAddress)) } - data.Symbol = strings.Join(symbols, "\n") - data.Balance = strings.Join(balances, "\n") - data.Decimal = strings.Join(decimals, "\n") - data.Address = strings.Join(addresses, "\n") + data.Symbols = strings.Join(symbols, "\n") + data.Balances = strings.Join(balances, "\n") + data.Decimals = strings.Join(decimals, "\n") + data.Addresses = strings.Join(addresses, "\n") return data } diff --git a/internal/utils/vouchers_test.go b/internal/utils/vouchers_test.go index 8f8f18e..953182e 100644 --- a/internal/utils/vouchers_test.go +++ b/internal/utils/vouchers_test.go @@ -68,10 +68,10 @@ func TestProcessVouchers(t *testing.T) { } expectedResult := VoucherMetadata{ - Symbol: "1:SRF\n2:MILO", - Balance: "1:100\n2:200", - Decimal: "1:6\n2:4", - Address: "1:0xd4c288865Ce\n2:0x41c188d63Qa", + Symbols: "1:SRF\n2:MILO", + Balances: "1:100\n2:200", + Decimals: "1:6\n2:4", + Addresses: "1:0xd4c288865Ce\n2:0x41c188d63Qa", } result := ProcessVouchers(holdings)