validator_test.go 1.57 KB
Newer Older
1
package service
2 3 4 5 6 7 8

import (
	"testing"

	"github.com/stretchr/testify/require"
)

9
func TestParseValidateLimit(t *testing.T) {
10
	v := Validator{}
11

12
	// (1) Happy case
13
	limit := "100"
14
	_, err := v.ParseValidateLimit(limit)
15 16
	require.NoError(t, err, "limit should be valid")

17
	// (2) Boundary validation
18
	limit = "0"
19
	_, err = v.ParseValidateLimit(limit)
20 21
	require.Error(t, err, "limit must be greater than 0")

22
	// (3) Type validation
23
	limit = "abc"
24
	_, err = v.ParseValidateLimit(limit)
25 26
	require.Error(t, err, "limit must be an integer value")
}
27

28
func TestParseValidateAddress(t *testing.T) {
29 30 31
	v := Validator{}

	// (1) Happy case
32
	addr := "0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5"
33 34 35 36 37 38 39 40 41
	_, err := v.ParseValidateAddress(addr)
	require.NoError(t, err, "address should be pass")

	// (2) Invalid hex
	addr = "🫡"
	_, err = v.ParseValidateAddress(addr)
	require.Error(t, err, "address must be represented as a valid hexadecimal string")

	// (3) Zero address
42
	addr = "0x0000000000000000000000000000000000000000"
43 44 45
	_, err = v.ParseValidateAddress(addr)
	require.Error(t, err, "address cannot be black-hole value")
}
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

func Test_ParseValidateCursor(t *testing.T) {
	v := Validator{}

	// (1) Happy case
	cursor := "0xf3fd2eb696dab4263550b938726f9b3606e334cce6ebe27446bc26cb700b94e0"
	err := v.ValidateCursor(cursor)
	require.NoError(t, err, "cursor should be pass")

	// (2) Invalid length
	cursor = "0x000"
	err = v.ValidateCursor(cursor)
	require.Error(t, err, "cursor must be 32 byte hex string")

	// (3) Invalid hex
	cursor = "0🫡"
	err = v.ValidateCursor(cursor)
	require.Error(t, err, "cursor must start with 0x")
}