From b5b3b6f4cae5e2081b66b2b4c6b7638b086e4b9c Mon Sep 17 00:00:00 2001 From: loikg Date: Sun, 24 Sep 2023 17:02:27 +0800 Subject: [PATCH] test: add test for account balance, account create, keygen and token create --- .github/workflows/ci.yml | 18 ++++++---- cmd/account_balance_test.go | 18 ++++++++++ cmd/account_create_test.go | 12 ++----- cmd/account_show_test.go | 21 ++++-------- cmd/keygen_test.go | 22 +++++++++++++ cmd/testdata/account_balance.golden | 4 +++ cmd/testdata/account_show.golden | 7 ++++ cmd/token_create_test.go | 51 +++++++++++++++++++++++++++++ internal/testutils/cli.go | 33 +++++++++++++++++++ taskfile.yml | 9 ++--- 10 files changed, 160 insertions(+), 35 deletions(-) create mode 100644 cmd/account_balance_test.go create mode 100644 cmd/keygen_test.go create mode 100644 cmd/testdata/account_balance.golden create mode 100644 cmd/testdata/account_show.golden create mode 100644 cmd/token_create_test.go create mode 100644 internal/testutils/cli.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f1dac4c..076e33b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,23 +3,27 @@ on: [push] jobs: CI: runs-on: ubuntu-latest + env: + HEDERA_NODE_VERSION: 2.14.0 steps: - uses: actions/checkout@v4.1.0 - name: Set up Go uses: actions/setup-go@v4.1.0 with: go-version: 1.21.1 - - name: Run go vet - run: go vet ./... - - name: Run go build - run: go build -v ./... - uses: actions/setup-node@v3.8.1 with: + cache: npm + cache-dependency-path: .github/workflows/ci.yml node-version: 20.7.0 - - name: Install @hashgraph/hedera-local - run: npx --yes @hashgraph/hedera-local@v2.14.0 start --network local -d + - name: Run go vet + run: go vet ./... + - name: Run go build + run: go build -v + - name: Start hedera-local + run: npx --yes @hashgraph/hedera-local@v$HEDERA_NODE_VERSION start --network local -d - name: Run go test run: go test -v ./... - name: Stop hedera-local if: always() - run: npx --yes @hashgraph/hedera-local@v2.14.0 stop + run: npx --yes @hashgraph/hedera-local@v$HEDERA_NODE_VERSION stop diff --git a/cmd/account_balance_test.go b/cmd/account_balance_test.go new file mode 100644 index 0000000..0a539e9 --- /dev/null +++ b/cmd/account_balance_test.go @@ -0,0 +1,18 @@ +package cmd_test + +import ( + "testing" + + "github.com/loikg/hedera-cli/internal/testutils" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestAccountBalance(t *testing.T) { + expected := testutils.Testdata(t, "account_balance.golden") + + actual, err := testutils.RunCLI(t, "account", "balance", "--accountId", "0.0.1023") + require.NoError(t, err) + + assert.JSONEq(t, string(expected), string(actual)) +} diff --git a/cmd/account_create_test.go b/cmd/account_create_test.go index 8c8101c..ad9d94c 100644 --- a/cmd/account_create_test.go +++ b/cmd/account_create_test.go @@ -1,28 +1,22 @@ package cmd_test import ( - "bytes" "encoding/json" "testing" "github.com/stretchr/testify/require" - "github.com/loikg/hedera-cli/cmd" "github.com/loikg/hedera-cli/internal" "github.com/loikg/hedera-cli/internal/hederatest" + "github.com/loikg/hedera-cli/internal/testutils" ) func TestAccountCreateCommand(t *testing.T) { - actual := new(bytes.Buffer) - cmd.RootCmd.SetOut(actual) - cmd.RootCmd.SetErr(actual) - - cmd.RootCmd.SetArgs([]string{"account", "--network", "local", "create", "--balance", "10.5"}) - err := cmd.RootCmd.Execute() + actual, err := testutils.RunCLI(t, "account", "--network", "local", "create", "--balance", "10.5") require.NoError(t, err) var data internal.M - err = json.Unmarshal(actual.Bytes(), &data) + err = json.Unmarshal(actual, &data) require.NoError(t, err) hederatest.AssertValidAccountID(t, data["accountId"]) diff --git a/cmd/account_show_test.go b/cmd/account_show_test.go index 28cedc6..33b414d 100644 --- a/cmd/account_show_test.go +++ b/cmd/account_show_test.go @@ -1,28 +1,19 @@ package cmd_test import ( - "bytes" "testing" - "github.com/loikg/hedera-cli/cmd" + "github.com/loikg/hedera-cli/internal/testutils" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestAccountShowCommand(t *testing.T) { accountID := "0.0.1026" - actual := new(bytes.Buffer) - cmd.RootCmd.SetOut(actual) - cmd.RootCmd.SetErr(actual) + expectedOutput := testutils.Testdata(t, "account_show.golden") - cmd.RootCmd.SetArgs([]string{"account", "show", "--account-id", accountID}) - cmd.RootCmd.Execute() + actual, err := testutils.RunCLI(t, "--network", "local", "account", "show", "--account-id", accountID) + require.NoError(t, err) - expectedOutput := `{ - "accountId": "0.0.1026", - "accountMemo": "", - "isDeleted": false, - "ownedNfts": 0, - "tinyBarBalance": 1000000000000 -}` - assert.JSONEq(t, expectedOutput, actual.String()) + assert.JSONEq(t, string(expectedOutput), string(actual)) } diff --git a/cmd/keygen_test.go b/cmd/keygen_test.go new file mode 100644 index 0000000..7ac877d --- /dev/null +++ b/cmd/keygen_test.go @@ -0,0 +1,22 @@ +package cmd_test + +import ( + "encoding/json" + "testing" + + "github.com/loikg/hedera-cli/internal" + "github.com/loikg/hedera-cli/internal/hederatest" + "github.com/loikg/hedera-cli/internal/testutils" + "github.com/stretchr/testify/require" +) + +func TestKeygen(t *testing.T) { + actual, err := testutils.RunCLI(t, "keygen") + require.NoError(t, err) + + var data internal.M + err = json.Unmarshal(actual, &data) + require.NoError(t, err) + + hederatest.AssertValidKeyPair(t, data["privateKey"], data["publicKey"]) +} diff --git a/cmd/testdata/account_balance.golden b/cmd/testdata/account_balance.golden new file mode 100644 index 0000000..4fde30b --- /dev/null +++ b/cmd/testdata/account_balance.golden @@ -0,0 +1,4 @@ +{ + "tinybar": 1000000000000, + "tokens": {} +} \ No newline at end of file diff --git a/cmd/testdata/account_show.golden b/cmd/testdata/account_show.golden new file mode 100644 index 0000000..744d517 --- /dev/null +++ b/cmd/testdata/account_show.golden @@ -0,0 +1,7 @@ +{ + "accountId": "0.0.1026", + "accountMemo": "", + "isDeleted": false, + "ownedNfts": 0, + "tinyBarBalance": 1000000000000 +} \ No newline at end of file diff --git a/cmd/token_create_test.go b/cmd/token_create_test.go new file mode 100644 index 0000000..c43df75 --- /dev/null +++ b/cmd/token_create_test.go @@ -0,0 +1,51 @@ +package cmd_test + +import ( + "encoding/json" + "errors" + "os/exec" + "testing" + + "github.com/loikg/hedera-cli/internal" + "github.com/loikg/hedera-cli/internal/testutils" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestTokenCreate(t *testing.T) { + args := []string{ + "token", + "create", + "--balance", + "100", + "--decimals", + "2", + "--name", + "myToken", + "--symbol", + "MTK", + "--type", + "ft", + "--supply-type", + "infinite", + "--treasury-id", + "0.0.1022", + "--treasury-key", + "851a12c2561f12014d51e30bfa6342d34275c77866118f18a29659cdc12a485b", + } + actual, err := testutils.RunCLI(t, args...) + var exitErr *exec.ExitError + if errors.As(err, &exitErr) { + t.Log(string(exitErr.Stderr)) + } + require.NoError(t, err) + + var data internal.M + err = json.Unmarshal(actual, &data) + require.NoError(t, err) + + assert.Equal(t, "MTK", data["symbol"]) + assert.Equal(t, "myToken", data["name"]) + assert.Equal(t, "TOKEN_TYPE_FUNGIBLE_COMMON", data["tokenType"]) + assert.Equal(t, float64(0), data["totalSupply"]) +} diff --git a/internal/testutils/cli.go b/internal/testutils/cli.go new file mode 100644 index 0000000..4b103bd --- /dev/null +++ b/internal/testutils/cli.go @@ -0,0 +1,33 @@ +// Package testutils contain test helper function +package testutils + +import ( + "os" + "os/exec" + "path/filepath" + "slices" + "testing" + + "github.com/stretchr/testify/require" +) + +// Testdata is a test helper function to read a golden file. +func Testdata(t *testing.T, filename string) []byte { + t.Helper() + + b, err := os.ReadFile(filepath.Join("testdata", filename)) + require.NoError(t, err) + + return b +} + +// RunCLI is a test help function to execute the cli binary with the given args arguments. +func RunCLI(t *testing.T, args ...string) ([]byte, error) { + t.Helper() + + args = slices.Insert(args, 0, "--network") + args = slices.Insert(args, 1, "local") + + binPath := filepath.Join("../", "hedera-cli") + return exec.Command(binPath, args...).Output() +} diff --git a/taskfile.yml b/taskfile.yml index 3ca8f20..74cd36a 100644 --- a/taskfile.yml +++ b/taskfile.yml @@ -3,14 +3,15 @@ version: "3" tasks: build: cmds: - - go build cmd/ + - go build lint: cmds: - golangci-lint run ./... test: + deps: [build] cmds: - - hedera start -d -n local - - go test ./... - - hedera stop + - npx --yes @hashgraph/hedera-local@v2.14.0 start -d --network local + - go test -v ./... + - npx --yes @hashgraph/hedera-local@v2.14.0 stop