Skip to content

Commit

Permalink
add testcase names
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Lam committed Sep 20, 2023
1 parent 70caea1 commit d4c9f5e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
31 changes: 22 additions & 9 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ var (

func TestGetDestinationRPCEndpoint(t *testing.T) {
testCases := []struct {
name string
s DestinationSubnet
expectedResult string
}{
{
name: "No encrypt connection",
s: DestinationSubnet{
EncryptConnection: false,
APINodeHost: "127.0.0.1",
Expand All @@ -75,6 +77,7 @@ func TestGetDestinationRPCEndpoint(t *testing.T) {
expectedResult: fmt.Sprintf("http://127.0.0.1:9650/ext/bc/%s/rpc", testChainID),
},
{
name: "Encrypt connection",
s: DestinationSubnet{
EncryptConnection: true,
APINodeHost: "127.0.0.1",
Expand All @@ -85,6 +88,7 @@ func TestGetDestinationRPCEndpoint(t *testing.T) {
expectedResult: fmt.Sprintf("https://127.0.0.1:9650/ext/bc/%s/rpc", testChainID),
},
{
name: "No port",
s: DestinationSubnet{
EncryptConnection: false,
APINodeHost: "api.avax.network",
Expand All @@ -95,6 +99,7 @@ func TestGetDestinationRPCEndpoint(t *testing.T) {
expectedResult: fmt.Sprintf("http://api.avax.network/ext/bc/%s/rpc", testChainID),
},
{
name: "Primary subnet",
s: DestinationSubnet{
EncryptConnection: false,
APINodeHost: "127.0.0.1",
Expand All @@ -105,6 +110,7 @@ func TestGetDestinationRPCEndpoint(t *testing.T) {
expectedResult: "http://127.0.0.1:9650/ext/bc/C/rpc",
},
{
name: "Override with set rpc endpoint",
s: DestinationSubnet{
EncryptConnection: false,
APINodeHost: "127.0.0.1",
Expand All @@ -117,8 +123,8 @@ func TestGetDestinationRPCEndpoint(t *testing.T) {
},
}

for i, testCase := range testCases {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
res := testCase.s.GetNodeRPCEndpoint()
require.Equal(t, testCase.expectedResult, res)
})
Expand All @@ -127,11 +133,13 @@ func TestGetDestinationRPCEndpoint(t *testing.T) {

func TestGetSourceSubnetEndpoints(t *testing.T) {
testCases := []struct {
name string
s SourceSubnet
expectedWsResult string
expectedRpcResult string
}{
{
name: "No encrypt connection",
s: SourceSubnet{
EncryptConnection: false,
APINodeHost: "127.0.0.1",
Expand All @@ -143,6 +151,7 @@ func TestGetSourceSubnetEndpoints(t *testing.T) {
expectedRpcResult: fmt.Sprintf("http://127.0.0.1:9650/ext/bc/%s/rpc", testChainID),
},
{
name: "Encrypt connection",
s: SourceSubnet{
EncryptConnection: true,
APINodeHost: "127.0.0.1",
Expand All @@ -154,6 +163,7 @@ func TestGetSourceSubnetEndpoints(t *testing.T) {
expectedRpcResult: fmt.Sprintf("https://127.0.0.1:9650/ext/bc/%s/rpc", testChainID),
},
{
name: "No port",
s: SourceSubnet{
EncryptConnection: false,
APINodeHost: "api.avax.network",
Expand All @@ -165,6 +175,7 @@ func TestGetSourceSubnetEndpoints(t *testing.T) {
expectedRpcResult: fmt.Sprintf("http://api.avax.network/ext/bc/%s/rpc", testChainID),
},
{
name: "Primary subnet",
s: SourceSubnet{
EncryptConnection: false,
APINodeHost: "127.0.0.1",
Expand All @@ -176,6 +187,7 @@ func TestGetSourceSubnetEndpoints(t *testing.T) {
expectedRpcResult: "http://127.0.0.1:9650/ext/bc/C/rpc",
},
{
name: "Override with set endpoints",
s: SourceSubnet{
EncryptConnection: false,
APINodeHost: "127.0.0.1",
Expand All @@ -190,8 +202,8 @@ func TestGetSourceSubnetEndpoints(t *testing.T) {
},
}

for i, testCase := range testCases {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
require.Equal(t, testCase.expectedWsResult, testCase.s.GetNodeWSEndpoint())
require.Equal(t, testCase.expectedRpcResult, testCase.s.GetNodeRPCEndpoint())
})
Expand All @@ -206,11 +218,12 @@ func TestGetRelayerAccountInfo(t *testing.T) {
}

testCases := []struct {
name string
s DestinationSubnet
expectedResult retStruct
}{
// valid
{
name: "valid",
s: DestinationSubnet{
AccountPrivateKey: "56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027",
},
Expand All @@ -222,8 +235,8 @@ func TestGetRelayerAccountInfo(t *testing.T) {
err: nil,
},
},
// invalid, with 0x prefix. Should be sanitized before being set in DestinationSubnet
{
name: "invalid 0x prefix",
s: DestinationSubnet{
AccountPrivateKey: "0x56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027",
},
Expand All @@ -235,8 +248,8 @@ func TestGetRelayerAccountInfo(t *testing.T) {
err: ErrInvalidPrivateKey,
},
},
// invalid
{
name: "invalid private key",
s: DestinationSubnet{
AccountPrivateKey: "invalid56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027",
},
Expand All @@ -250,8 +263,8 @@ func TestGetRelayerAccountInfo(t *testing.T) {
},
}

for i, testCase := range testCases {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
pk, addr, err := testCase.s.GetRelayerAccountInfo()
require.Equal(t, testCase.expectedResult.err, err)
if err == nil {
Expand Down
32 changes: 22 additions & 10 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package utils

import (
"fmt"
"math/big"
"testing"

Expand All @@ -13,45 +12,51 @@ import (

func TestConvertProtocol(t *testing.T) {
testCases := []struct {
name string
urlString string
protocol string
expectedUrl string
expectedError bool
}{
{
name: "valid http to https",
urlString: "http://www.hello.com",
protocol: "https",
expectedUrl: "https://www.hello.com",
expectedError: false,
},
{
name: "valid https to http",
urlString: "https://www.hello.com",
protocol: "http",
expectedUrl: "http://www.hello.com",
expectedError: false,
},
{
name: "valid http to http",
urlString: "http://www.hello.com",
protocol: "http",
expectedUrl: "http://www.hello.com",
expectedError: false,
},
{
name: "valid https to https",
urlString: "https://www.hello.com",
protocol: "https",
expectedUrl: "https://www.hello.com",
expectedError: false,
},
{
name: "invalid protocol",
urlString: "http://www.hello.com",
protocol: "\n",
expectedUrl: "",
expectedError: true,
},
}

for i, testCase := range testCases {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
actualUrl, err := ConvertProtocol(testCase.urlString, testCase.protocol)

if testCase.expectedError {
Expand All @@ -66,27 +71,28 @@ func TestConvertProtocol(t *testing.T) {

func TestSanitizeHashString(t *testing.T) {
testCases := []struct {
name string
hash string
expectedResult string
}{
// Remove leading 0x from hex string
{
name: "remove leading 0x",
hash: "0x1234",
expectedResult: "1234",
},
// Return original hex string
{
name: "return original non leading 0x",
hash: "1234",
expectedResult: "1234",
},
// Return original string, leading 0x is not hex
{
name: "return original length not divisible by 2",
hash: "0x1234g",
expectedResult: "0x1234g",
},
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
actualResult := SanitizeHashString(testCase.hash)
require.Equal(t, testCase.expectedResult, actualResult)
})
Expand All @@ -95,50 +101,56 @@ func TestSanitizeHashString(t *testing.T) {

func TestCheckStakeWeightExceedsThreshold(t *testing.T) {
testCases := []struct {
name string
accumulatedSignatureWeight uint64
totalWeight uint64
quorumNumerator uint64
quorumDenominator uint64
expectedResult bool
}{
{
name: "zero case",
accumulatedSignatureWeight: 0,
totalWeight: 0,
quorumNumerator: 0,
quorumDenominator: 0,
expectedResult: true,
},
{
name: "valid case",
accumulatedSignatureWeight: 67_000_000,
totalWeight: 100_000_000,
quorumNumerator: 67,
quorumDenominator: 100,
expectedResult: true,
},
{
name: "invalid case",
accumulatedSignatureWeight: 66_999_999,
totalWeight: 100_000_000,
quorumNumerator: 67,
quorumDenominator: 100,
expectedResult: false,
},
{
name: "valid 100 percent case",
accumulatedSignatureWeight: 67_000_000,
totalWeight: 67_000_000,
quorumNumerator: 100,
quorumDenominator: 100,
expectedResult: true,
},
{
name: "invalid 100 percent case",
accumulatedSignatureWeight: 66_999_999,
totalWeight: 67_000_000,
quorumNumerator: 100,
quorumDenominator: 100,
expectedResult: false,
},
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
actualResult := CheckStakeWeightExceedsThreshold(new(big.Int).SetUint64(testCase.accumulatedSignatureWeight), testCase.totalWeight, testCase.quorumNumerator, testCase.quorumDenominator)
require.Equal(t, testCase.expectedResult, actualResult)
})
Expand Down

0 comments on commit d4c9f5e

Please sign in to comment.