Skip to content

Commit ced4961

Browse files
committed
test: add tests for TestGetTokens & TestParseAddressBalance methods
1 parent e1d61ab commit ced4961

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

backend/pkg/commons/rpc/utils_test.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
var (
1414
aliceAddress = common.HexToAddress("0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5")
1515
bobAddress = common.HexToAddress("0x388C818CA8B9251b393131C08a736A67ccB19297")
16+
token = common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678")
17+
token2 = common.HexToAddress("0xabcdef1234567890abcdef1234567890abcdef12")
1618
)
1719

1820
func TestGetReceiver(t *testing.T) {
@@ -237,3 +239,184 @@ func TestGetLogsFromReceipts(t *testing.T) {
237239
})
238240
}
239241
}
242+
243+
func TestGetTokens(t *testing.T) {
244+
tests := []struct {
245+
name string
246+
tokenStr []string
247+
expected []common.Address
248+
}{
249+
{
250+
name: "single token address",
251+
tokenStr: []string{token.Hex()},
252+
expected: []common.Address{token},
253+
},
254+
{
255+
name: "two token addresses",
256+
tokenStr: []string{token.Hex(), token2.Hex()},
257+
expected: []common.Address{token, token2},
258+
},
259+
{
260+
name: "empty token addresses",
261+
tokenStr: []string{},
262+
expected: []common.Address{},
263+
},
264+
}
265+
266+
for _, tt := range tests {
267+
t.Run(tt.name, func(t *testing.T) {
268+
result := getTokens(tt.tokenStr)
269+
if len(result) != len(tt.expected) {
270+
t.Fatalf("got %v tokens, want %v tokens", len(result), len(tt.expected))
271+
}
272+
for i, token := range result {
273+
if token != tt.expected[i] {
274+
t.Errorf("got token %v, want %v", token, tt.expected[i])
275+
}
276+
}
277+
})
278+
}
279+
}
280+
281+
func TestParseAddressBalance(t *testing.T) {
282+
tests := []struct {
283+
name string
284+
tokens []common.
285+
Address
286+
address string
287+
balances []*big.Int
288+
expected []*types.Eth1AddressBalance
289+
expectError bool
290+
}{
291+
{
292+
name: "valid single token and balance",
293+
tokens: []common.Address{token},
294+
address: aliceAddress.Hex(),
295+
balances: []*big.Int{
296+
big.NewInt(100),
297+
},
298+
expected: []*types.Eth1AddressBalance{
299+
{
300+
Address: aliceAddress.Bytes(),
301+
Token: token.Bytes(),
302+
Balance: big.NewInt(100).Bytes(),
303+
},
304+
},
305+
expectError: false,
306+
},
307+
{
308+
name: "valid two tokens and balances",
309+
tokens: []common.Address{token, token2},
310+
address: aliceAddress.Hex(),
311+
balances: []*big.Int{
312+
big.NewInt(100),
313+
big.NewInt(200),
314+
},
315+
expected: []*types.Eth1AddressBalance{
316+
{
317+
Address: aliceAddress.Bytes(),
318+
Token: token.Bytes(),
319+
Balance: big.NewInt(100).Bytes(),
320+
},
321+
{
322+
Address: aliceAddress.Bytes(),
323+
Token: token2.Bytes(),
324+
Balance: big.NewInt(200).Bytes(),
325+
},
326+
},
327+
expectError: false,
328+
},
329+
{
330+
name: "no tokens",
331+
tokens: []common.Address{},
332+
address: aliceAddress.Hex(),
333+
balances: []*big.Int{
334+
big.NewInt(100),
335+
},
336+
expected: nil,
337+
expectError: true,
338+
},
339+
{
340+
name: "no balance",
341+
tokens: []common.Address{token},
342+
address: aliceAddress.Hex(),
343+
balances: []*big.Int{},
344+
expected: nil,
345+
expectError: true,
346+
},
347+
{
348+
name: "single token and two balances",
349+
tokens: []common.Address{token},
350+
address: aliceAddress.Hex(),
351+
balances: []*big.Int{
352+
big.NewInt(100),
353+
big.NewInt(200),
354+
},
355+
expected: nil,
356+
expectError: true,
357+
},
358+
{
359+
name: "invalid address format",
360+
tokens: []common.Address{token},
361+
address: "invalid-address",
362+
balances: []*big.Int{
363+
big.NewInt(100),
364+
},
365+
expected: nil,
366+
expectError: true,
367+
},
368+
{
369+
name: "empty token address",
370+
tokens: []common.Address{common.HexToAddress("")},
371+
address: aliceAddress.Hex(),
372+
balances: []*big.Int{
373+
big.NewInt(100),
374+
},
375+
expected: nil,
376+
expectError: true,
377+
},
378+
{
379+
name: "nil balance",
380+
tokens: []common.Address{token},
381+
address: aliceAddress.Hex(),
382+
balances: []*big.Int{
383+
nil,
384+
},
385+
expected: nil,
386+
expectError: true,
387+
},
388+
}
389+
390+
for _, tt := range tests {
391+
t.Run(tt.name, func(t *testing.T) {
392+
result, err := parseAddressBalance(tt.tokens, tt.address, tt.balances)
393+
394+
if tt.expectError {
395+
if err == nil {
396+
t.Errorf("expected an error but got none")
397+
}
398+
if result != nil {
399+
t.Errorf("expected result to be nil on error, got %v", result)
400+
}
401+
} else {
402+
if err != nil {
403+
t.Errorf("unexpected error: %v", err)
404+
}
405+
if len(result) != len(tt.expected) {
406+
t.Fatalf("got %v balances, want %v balances", len(result), len(tt.expected))
407+
}
408+
for i, res := range result {
409+
if !bytes.Equal(res.Address, tt.expected[i].Address) {
410+
t.Errorf("got Address %v, want %v", res.Address, tt.expected[i].Address)
411+
}
412+
if !bytes.Equal(res.Token, tt.expected[i].Token) {
413+
t.Errorf("got Token %v, want %v", res.Token, tt.expected[i].Token)
414+
}
415+
if !bytes.Equal(res.Balance, tt.expected[i].Balance) {
416+
t.Errorf("got Balance %v, want %v", res.Balance, tt.expected[i].Balance)
417+
}
418+
}
419+
}
420+
})
421+
}
422+
}

0 commit comments

Comments
 (0)