Skip to content

Commit

Permalink
Merge pull request #30 from loikg/test/improve-testing-suite
Browse files Browse the repository at this point in the history
test: add test for account balance, account create, keygen and token …
  • Loading branch information
loikg authored Sep 24, 2023
2 parents f31907b + b5b3b6f commit 651c45f
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 35 deletions.
18 changes: 11 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions cmd/account_balance_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
12 changes: 3 additions & 9 deletions cmd/account_create_test.go
Original file line number Diff line number Diff line change
@@ -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"])
Expand Down
21 changes: 6 additions & 15 deletions cmd/account_show_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
22 changes: 22 additions & 0 deletions cmd/keygen_test.go
Original file line number Diff line number Diff line change
@@ -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"])
}
4 changes: 4 additions & 0 deletions cmd/testdata/account_balance.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"tinybar": 1000000000000,
"tokens": {}
}
7 changes: 7 additions & 0 deletions cmd/testdata/account_show.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"accountId": "0.0.1026",
"accountMemo": "",
"isDeleted": false,
"ownedNfts": 0,
"tinyBarBalance": 1000000000000
}
51 changes: 51 additions & 0 deletions cmd/token_create_test.go
Original file line number Diff line number Diff line change
@@ -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"])
}
33 changes: 33 additions & 0 deletions internal/testutils/cli.go
Original file line number Diff line number Diff line change
@@ -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()
}
9 changes: 5 additions & 4 deletions taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 651c45f

Please sign in to comment.