Compare commits

..

2 Commits

Author SHA1 Message Date
lash
a3e2293047
Collect strings in consts in test 2024-11-03 16:25:43 +00:00
lash
984f50f905
Add voucherlist check in test 2024-11-03 15:37:46 +00:00

View File

@ -3,6 +3,8 @@ package nats
import ( import (
"bytes" "bytes"
"context" "context"
"fmt"
"strconv"
"testing" "testing"
"time" "time"
@ -15,13 +17,23 @@ import (
"git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/models" "git.grassecon.net/urdt/ussd/models"
"git.grassecon.net/term/lookup" "git.grassecon.net/term/lookup"
"git.grassecon.net/term/event"
) )
func init() {
}
const ( const (
txBlock = 42
tokenAddress = "0x765DE816845861e75A25fCA122bb6898B8B1282a"
tokenSymbol = "FOO"
tokenName = "Foo Token"
tokenDecimals = 6
txValue = 1337
tokenBalance = 362436
txTimestamp = 1730592500
txHash = "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
sinkAddress = "0xb42C5920014eE152F2225285219407938469BBfA"
aliceChecksum = "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70" aliceChecksum = "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70"
bobChecksum = "0xB3117202371853e24B725d4169D87616A7dDb127"
aliceSession = "5553425"
) )
// TODO: jetstream, would have been nice of you to provide an easier way to make a mock msg // TODO: jetstream, would have been nice of you to provide an easier way to make a mock msg
@ -96,10 +108,10 @@ func(m mockApi) FetchVouchers(ctx context.Context, publicKey string) ([]dataserv
logg.DebugCtxf(ctx, "mockapi fetchvouchers", "key", publicKey) logg.DebugCtxf(ctx, "mockapi fetchvouchers", "key", publicKey)
return []dataserviceapi.TokenHoldings{ return []dataserviceapi.TokenHoldings{
dataserviceapi.TokenHoldings{ dataserviceapi.TokenHoldings{
ContractAddress: "0xeE0A29AE1BB7a033c8277C04780c4aBcf4388E93", ContractAddress: tokenAddress,
TokenSymbol: "FOO", TokenSymbol: tokenSymbol,
TokenDecimals: "6", TokenDecimals: strconv.Itoa(tokenDecimals),
Balance: "362436", Balance: strconv.Itoa(tokenBalance),
}, },
}, nil }, nil
} }
@ -108,24 +120,24 @@ func(m mockApi) FetchTransactions(ctx context.Context, publicKey string) ([]data
logg.DebugCtxf(ctx, "mockapi fetchtransactions", "key", publicKey) logg.DebugCtxf(ctx, "mockapi fetchtransactions", "key", publicKey)
return []dataserviceapi.Last10TxResponse{ return []dataserviceapi.Last10TxResponse{
dataserviceapi.Last10TxResponse{ dataserviceapi.Last10TxResponse{
Sender: "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70", Sender: aliceChecksum,
Recipient: "B3117202371853e24B725d4169D87616A7dDb127", Recipient: bobChecksum,
TransferValue: "1337", TransferValue: strconv.Itoa(txValue),
ContractAddress: "0x765DE816845861e75A25fCA122bb6898B8B1282a", ContractAddress: tokenAddress,
TxHash: "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789", TxHash: txHash,
DateBlock: time.Unix(1730592500, 0), DateBlock: time.Unix(txTimestamp, 0),
TokenSymbol: "FOO", TokenSymbol: tokenSymbol,
TokenDecimals: "6", TokenDecimals: strconv.Itoa(tokenDecimals),
}, },
}, nil }, nil
} }
func(m mockApi) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) { func(m mockApi) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) {
return &models.VoucherDataResult{ return &models.VoucherDataResult{
TokenSymbol: "FOO", TokenSymbol: tokenSymbol,
TokenName: "Foo Token", TokenName: tokenName,
TokenDecimals: "6", TokenDecimals: strconv.Itoa(tokenDecimals),
SinkAddress: "0xb42C5920014eE152F2225285219407938469BBfA", SinkAddress: sinkAddress,
}, nil }, nil
} }
@ -149,7 +161,6 @@ func TestHandleMsg(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
aliceSession := "5553425"
userDb.SetSession(alice) userDb.SetSession(alice)
userDb.SetPrefix(db.DATATYPE_USERDATA) userDb.SetPrefix(db.DATATYPE_USERDATA)
err = userDb.Put(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(aliceSession)) err = userDb.Put(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(aliceSession))
@ -159,19 +170,19 @@ func TestHandleMsg(t *testing.T) {
sub := NewNatsSubscription(userDb) sub := NewNatsSubscription(userDb)
data := `{ data := fmt.Sprintf(`{
"block": 42, "block": %d,
"contractAddress": "0x765DE816845861e75A25fCA122bb6898B8B1282a", "contractAddress": "%s",
"success": true, "success": true,
"timestamp": 1730592500, "timestamp": %d,
"transactionHash": "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789", "transactionHash": "%s",
"transactionType": "TOKEN_TRANSFER", "transactionType": "TOKEN_TRANSFER",
"payload": { "payload": {
"from": "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70", "from": "%s",
"to": "B3117202371853e24B725d4169D87616A7dDb127", "to": "%s",
"value": "1337" "value": "%d"
} }
}` }`, txBlock, tokenAddress, txTimestamp, txHash, aliceChecksum, bobChecksum, txValue)
msg := &testMsg{ msg := &testMsg{
data: []byte(data), data: []byte(data),
} }
@ -184,24 +195,34 @@ func TestHandleMsg(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !bytes.Equal(v, []byte("FOO")) { if !bytes.Equal(v, []byte(tokenSymbol)) {
t.Fatalf("expected 'FOO', got %s", v) t.Fatalf("expected '%s', got %s", tokenSymbol, v)
} }
v, err = store.ReadEntry(ctx, aliceSession, common.DATA_ACTIVE_BAL) v, err = store.ReadEntry(ctx, aliceSession, common.DATA_ACTIVE_BAL)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !bytes.Equal(v, []byte("362436")) { if !bytes.Equal(v, []byte(strconv.Itoa(tokenBalance))) {
t.Fatalf("expected '362436', got %s", v) t.Fatalf("expected '%d', got %s", tokenBalance, v)
} }
v, err = store.ReadEntry(ctx, aliceSession, common.DATA_TRANSACTIONS) v, err = store.ReadEntry(ctx, aliceSession, common.DATA_TRANSACTIONS)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if len(v) == 0 { if !bytes.Contains(v, []byte("abcdef")) {
t.Fatal("no transaction data") t.Fatal("no transaction data")
} }
t.Logf("tx %s", v)
userDb.SetPrefix(event.DATATYPE_USERSUB)
userDb.SetSession(aliceSession)
k := append([]byte("vouchers"), []byte("sym")...)
v, err = userDb.Get(ctx, k)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(v, []byte(fmt.Sprintf("1:%s", tokenSymbol))) {
t.Fatalf("expected '1:%s', got %s", tokenSymbol, v)
}
} }