Skip to content

Commit

Permalink
[Feature] Unit test (#15)
Browse files Browse the repository at this point in the history
* add-test-1

* add test keys

* add tests

* add test

* fix fail test

* fix function

* add tunnel relayer test

* refac keys.go

* fix conflict

* update go mod version

* fix band types and add testcases

* add provider test

* fix multiply gas logic

* refactor app_test and add chaincmdtest

* merge main into unit-test

* declare a new viper variable

* fix band testcase

* fix config test

* fix provider test

* fix unit-test keys

* refactor tunnel_relayer_test.go

* fix app test

* fix test

* fix tunnel relayer test

* change func name

* remove t.parallel and new viper object

---------

Co-authored-by: Tanut Lertwarachai <tanutlertwarachai@Tanuts-MacBook-Pro.local>
Co-authored-by: nkitlabs <natthakunk0109@gmail.com>
  • Loading branch information
3 people authored Jan 21, 2025
1 parent fdedade commit adec895
Show file tree
Hide file tree
Showing 26 changed files with 3,265 additions and 404 deletions.
128 changes: 128 additions & 0 deletions cmd/chains_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package cmd_test

import (
"os"
"path"
"regexp"
"testing"

"github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/require"

"github.com/bandprotocol/falcon/internal/relayertest"
)

func TestChainsListEmpty(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
require.NoError(t, res.Err)

res = sys.RunWithInput(t, "chains", "list")
require.Empty(t, res.Stdout.String())
}

func TestChainsAdd(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
require.NoError(t, res.Err)

chainCfgPath := path.Join(sys.HomeDir, "chain_config.toml")
err := os.WriteFile(chainCfgPath, []byte(relayertest.ChainCfgText), 0o600)
require.NoError(t, err)

require.FileExists(t, chainCfgPath)

// Add chain
res = sys.RunWithInput(t, "chains", "add", "testnet", chainCfgPath)
require.Empty(t, res.Stdout.String())
require.Empty(t, res.Stderr.String())

// Add another chain
res = sys.RunWithInput(t, "ch", "a", "testnet2", chainCfgPath)
require.Empty(t, res.Stdout.String())
require.Empty(t, res.Stderr.String())

// Add existing chain
res = sys.RunWithInput(t, "ch", "a", "testnet", chainCfgPath)
require.Empty(t, res.Stdout.String())
require.Error(t, res.Err, "existing chain name")

// List chains to check
res = sys.RunWithInput(t, "chains", "list")
require.Regexp(t, regexp.MustCompile(`\d+: ([\w-]+) -> type\((\w+)\)`), res.Stdout.String())
require.Empty(t, res.Stderr.String())
}

func TestChainsDelete(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
require.NoError(t, res.Err)

chainCfgPath := path.Join(sys.HomeDir, "chain_config.toml")
err := os.WriteFile(chainCfgPath, []byte(relayertest.ChainCfgText), 0o600)
require.NoError(t, err)

require.FileExists(t, chainCfgPath)

// Add chain
res = sys.RunWithInput(t, "chains", "add", "testnet", chainCfgPath)
require.Empty(t, res.Stdout.String())
require.Empty(t, res.Stderr.String())

// Add another chain
res = sys.RunWithInput(t, "chains", "add", "testnet2", chainCfgPath)
require.Empty(t, res.Stdout.String())
require.Empty(t, res.Stderr.String())

// List chains
res = sys.RunWithInput(t, "chains", "list")
require.Regexp(t, regexp.MustCompile(`\d+: ([\w-]+) -> type\((\w+)\)`), res.Stdout.String())
require.Empty(t, res.Stderr.String())

// Delete chain
res = sys.RunWithInput(t, "chains", "delete", "testnet")
require.Empty(t, res.Stdout.String())
require.Empty(t, res.Stderr.String())

res = sys.RunWithInput(t, "ch", "d", "testnet2")
require.Empty(t, res.Stdout.String())
require.Empty(t, res.Stderr.String())

// List chain with shorthand command
res = sys.RunWithInput(t, "ch", "l")
require.Empty(t, res.Stdout.String())
}

func TestChainsShow(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
require.NoError(t, res.Err)

chainCfgPath := path.Join(sys.HomeDir, "chain_config.toml")
err := os.WriteFile(chainCfgPath, []byte(relayertest.ChainCfgText), 0o600)
require.NoError(t, err)

require.FileExists(t, chainCfgPath)

// Add chain
res = sys.RunWithInput(t, "chains", "add", "testnet", chainCfgPath)
require.Empty(t, res.Stdout.String())
require.Empty(t, res.Stderr.String())

// Show chain configuration
res = sys.RunWithInput(t, "chains", "show", "testnet")

var expectedChainCfg map[string]interface{}
err = toml.Unmarshal(res.Stdout.Bytes(), &expectedChainCfg)
require.NoError(t, err)

var actualChainCfg map[string]interface{}
err = toml.Unmarshal([]byte(relayertest.ChainCfgText), &actualChainCfg)
require.NoError(t, err)

require.Equal(t, expectedChainCfg, actualChainCfg)
}
18 changes: 9 additions & 9 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/bandprotocol/falcon/internal/relayertest"
)

func TestShowConfigCmd(t *testing.T) {
func TestConfigShow(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
Expand All @@ -23,14 +23,14 @@ func TestShowConfigCmd(t *testing.T) {
require.Equal(t, relayertest.DefaultCfgText+"\n", actual)
}

func TestShowConfigCmdNotInit(t *testing.T) {
func TestConfigShowNotInit(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "show")
require.ErrorContains(t, res.Err, "config does not exist:")
}

func TestInitCmdDefault(t *testing.T) {
func TestConfigInitDefault(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
Expand All @@ -46,7 +46,7 @@ func TestInitCmdDefault(t *testing.T) {
require.Equal(t, relayertest.DefaultCfgText, string(actualBytes))
}

func TestInitCmdWithFileShortFlag(t *testing.T) {
func TestConfigInitWithFileShortFlag(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
Expand All @@ -66,7 +66,7 @@ func TestInitCmdWithFileShortFlag(t *testing.T) {
require.Equal(t, relayertest.CustomCfgText, string(actualBytes))
}

func TestInitCmdWithFileLongFlag(t *testing.T) {
func TestConfigInitWithFileLongFlag(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
Expand All @@ -85,7 +85,7 @@ func TestInitCmdWithFileLongFlag(t *testing.T) {
require.Equal(t, relayertest.CustomCfgText, string(actualBytes))
}

func TestInitCmdWithFileTimeString(t *testing.T) {
func TestConfigInitWithFileTimeString(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
Expand All @@ -104,7 +104,7 @@ func TestInitCmdWithFileTimeString(t *testing.T) {
require.Equal(t, relayertest.CustomCfgText, string(actualBytes))
}

func TestInitCmdInvalidFile(t *testing.T) {
func TestConfigInitInvalidFile(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
Expand All @@ -115,15 +115,15 @@ func TestInitCmdInvalidFile(t *testing.T) {
require.ErrorContains(t, res.Err, "error toml: expected newline")
}

func TestInitCmdNoCustomFile(t *testing.T) {
func TestConfigInitNoCustomFile(t *testing.T) {
sys := relayertest.NewSystem(t)

customCfgPath := path.Join(sys.HomeDir, "custom.toml")
res := sys.RunWithInput(t, "config", "init", "--file", customCfgPath)
require.ErrorContains(t, res.Err, "no such file or directory")
}

func TestInitCmdAlreadyExist(t *testing.T) {
func TestConfigInitAlreadyExist(t *testing.T) {
sys := relayertest.NewSystem(t)

res := sys.RunWithInput(t, "config", "init")
Expand Down
3 changes: 3 additions & 0 deletions internal/relayertest/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ var ChainCfgText string

//go:embed testdata/default_with_chain_config.toml
var DefaultCfgTextWithChainCfg string

//go:embed testdata/chain_config_invalid_chain_type.toml
var ChainCfgInvalidChainTypeText string
Loading

0 comments on commit adec895

Please sign in to comment.