-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from loikg/test/improve-testing-suite
test: add test for account balance, account create, keygen and token …
- Loading branch information
Showing
10 changed files
with
160 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"tinybar": 1000000000000, | ||
"tokens": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters