Skip to content

Commit

Permalink
test: add tests for TestGetTokens & TestParseAddressBalance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Monika-Bitfly committed Feb 11, 2025
1 parent e39a9c2 commit f48b74c
Showing 1 changed file with 183 additions and 0 deletions.
183 changes: 183 additions & 0 deletions backend/pkg/commons/rpc/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
var (
aliceAddress = common.HexToAddress("0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5")
bobAddress = common.HexToAddress("0x388C818CA8B9251b393131C08a736A67ccB19297")
token = common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678")
token2 = common.HexToAddress("0xabcdef1234567890abcdef1234567890abcdef12")
)

func TestGetReceiver(t *testing.T) {
Expand Down Expand Up @@ -237,3 +239,184 @@ func TestGetLogsFromReceipts(t *testing.T) {
})
}
}

func TestGetTokens(t *testing.T) {
tests := []struct {
name string
tokenStr []string
expected []common.Address
}{
{
name: "single token address",
tokenStr: []string{token.Hex()},
expected: []common.Address{token},
},
{
name: "two token addresses",
tokenStr: []string{token.Hex(), token2.Hex()},
expected: []common.Address{token, token2},
},
{
name: "empty token addresses",
tokenStr: []string{},
expected: []common.Address{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := getTokens(tt.tokenStr)
if len(result) != len(tt.expected) {
t.Fatalf("got %v tokens, want %v tokens", len(result), len(tt.expected))
}
for i, token := range result {
if token != tt.expected[i] {
t.Errorf("got token %v, want %v", token, tt.expected[i])
}
}
})
}
}

func TestParseAddressBalance(t *testing.T) {
tests := []struct {
name string
tokens []common.
Address
address string
balances []*big.Int
expected []*types.Eth1AddressBalance
expectError bool
}{
{
name: "valid single token and balance",
tokens: []common.Address{token},
address: aliceAddress.Hex(),
balances: []*big.Int{
big.NewInt(100),
},
expected: []*types.Eth1AddressBalance{
{
Address: aliceAddress.Bytes(),
Token: token.Bytes(),
Balance: big.NewInt(100).Bytes(),
},
},
expectError: false,
},
{
name: "valid two tokens and balances",
tokens: []common.Address{token, token2},
address: aliceAddress.Hex(),
balances: []*big.Int{
big.NewInt(100),
big.NewInt(200),
},
expected: []*types.Eth1AddressBalance{
{
Address: aliceAddress.Bytes(),
Token: token.Bytes(),
Balance: big.NewInt(100).Bytes(),
},
{
Address: aliceAddress.Bytes(),
Token: token2.Bytes(),
Balance: big.NewInt(200).Bytes(),
},
},
expectError: false,
},
{
name: "no tokens",
tokens: []common.Address{},
address: aliceAddress.Hex(),
balances: []*big.Int{
big.NewInt(100),
},
expected: nil,
expectError: true,
},
{
name: "no balance",
tokens: []common.Address{token},
address: aliceAddress.Hex(),
balances: []*big.Int{},
expected: nil,
expectError: true,
},
{
name: "single token and two balances",
tokens: []common.Address{token},
address: aliceAddress.Hex(),
balances: []*big.Int{
big.NewInt(100),
big.NewInt(200),
},
expected: nil,
expectError: true,
},
{
name: "invalid address format",
tokens: []common.Address{token},
address: "invalid-address",
balances: []*big.Int{
big.NewInt(100),
},
expected: nil,
expectError: true,
},
{
name: "empty token address",
tokens: []common.Address{common.HexToAddress("")},
address: aliceAddress.Hex(),
balances: []*big.Int{
big.NewInt(100),
},
expected: nil,
expectError: true,
},
{
name: "nil balance",
tokens: []common.Address{token},
address: aliceAddress.Hex(),
balances: []*big.Int{
nil,
},
expected: nil,
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseAddressBalance(tt.tokens, tt.address, tt.balances)

if tt.expectError {
if err == nil {
t.Errorf("expected an error but got none")
}
if result != nil {
t.Errorf("expected result to be nil on error, got %v", result)
}
} else {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(result) != len(tt.expected) {
t.Fatalf("got %v balances, want %v balances", len(result), len(tt.expected))
}
for i, res := range result {
if !bytes.Equal(res.Address, tt.expected[i].Address) {
t.Errorf("got Address %v, want %v", res.Address, tt.expected[i].Address)
}
if !bytes.Equal(res.Token, tt.expected[i].Token) {
t.Errorf("got Token %v, want %v", res.Token, tt.expected[i].Token)
}
if !bytes.Equal(res.Balance, tt.expected[i].Balance) {
t.Errorf("got Balance %v, want %v", res.Balance, tt.expected[i].Balance)
}
}
}
})
}
}

0 comments on commit f48b74c

Please sign in to comment.