Skip to content

Commit

Permalink
add some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantani authored and Pantani committed Feb 10, 2024
1 parent 1db1795 commit a252c1a
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 22 deletions.
32 changes: 24 additions & 8 deletions official/wasm/cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -11,18 +12,33 @@ func Test_relativePath(t *testing.T) {
name string
appPath string
want string
err error
}{
// TODO: Add test cases.
{
name: "Relative path within current directory",
appPath: "subdir/file.txt",
want: "subdir/file.txt",
},
{
name: "Relative path outside current directory",
appPath: "/path/file.txt",
want: "../../../../../../../../../../../path/file.txt",
},
{
name: "App path is current directory",
appPath: ".",
want: ".",
},
{
name: "App path is parent directory",
appPath: "..",
want: "..",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := relativePath(tt.appPath)
if tt.err != nil {
require.Error(t, err)
require.ErrorIs(t, err, tt.err)
return
}
absPath, err := filepath.Abs(tt.appPath)
require.NoError(t, err)
got, err := relativePath(absPath)
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
Expand Down
87 changes: 75 additions & 12 deletions official/wasm/pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package config

import (
"os"
"strings"
"testing"

"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/stretchr/testify/require"
)

Expand All @@ -16,32 +19,92 @@ func TestAddWasm(t *testing.T) {
args args
err error
}{
// TODO: Add test cases.
{
name: "Add wasm parameters to the config file",
args: args{
configPath: "testdata/config_without_wasm.toml",
options: []Option{
WithSmartQueryGasLimit(77),
WithMemoryCacheSize(888),
WithSimulationGasLimit(9999),
},
},
},
{
name: "Config file already has wasm section",
args: args{
configPath: "testdata/config_with_wasm.toml",
options: []Option{
WithSmartQueryGasLimit(77),
WithMemoryCacheSize(888),
WithSimulationGasLimit(9999),
},
},
err: errors.New("config file already have wasm testdata/config_with_wasm.toml"),
},
{
name: "Invalid config file path",
args: args{
configPath: "nonexistent_directory/nonexistent_config.toml",
options: []Option{
WithSmartQueryGasLimit(77),
WithMemoryCacheSize(888),
WithSimulationGasLimit(9999),
},
},
err: errors.New("open nonexistent_directory/nonexistent_config.toml: no such file or directory"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
content, _ := os.ReadFile(tt.args.configPath)
err := AddWasm(tt.args.configPath, tt.args.options...)
require.Equal(t, tt.err, err)
if tt.err != nil {
require.Error(t, err)
require.Equal(t, tt.err.Error(), err.Error())
return
}
require.NoError(t, err)
require.True(t, hasWasm(tt.args.configPath))

withWasm, err := os.ReadFile("testdata/config_with_wasm.toml")
require.NoError(t, err)
noWasm, err := os.ReadFile(tt.args.configPath)
require.NoError(t, err)
require.Equal(t, strings.TrimSpace(string(withWasm)), strings.TrimSpace(string(noWasm)))

require.NoError(t, os.WriteFile(tt.args.configPath, content, 0o644))
require.False(t, hasWasm(tt.args.configPath))
})
}
}

func Test_hasWasm(t *testing.T) {
type args struct {
configPath string
}
tests := []struct {
name string
args args
want bool
name string
configPath string
want bool
}{
// TODO: Add test cases.
{
name: "Config file with wasm section",
configPath: "testdata/config_with_wasm.toml",
want: true,
},
{
name: "Config file without wasm section",
configPath: "testdata/config_without_wasm.toml",
want: false,
},
{
name: "Non-existent config file",
configPath: "testdata/nonexistent_config.toml",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hasWasm(tt.args.configPath); got != tt.want {
t.Errorf("hasWasm() = %v, want %v", got, tt.want)
}
got := hasWasm(tt.configPath)
require.Equal(t, tt.want, got)
})
}
}
30 changes: 30 additions & 0 deletions official/wasm/pkg/config/testdata/config_with_wasm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[instrumentation]

# When true, Prometheus metrics are served under /metrics on
# PrometheusListenAddr.
# Check out the documentation for the list of available metrics.
prometheus = false

# Address to listen for Prometheus collector(s) connections
prometheus_listen_addr = ":26660"

# Maximum number of simultaneous connections.
# If you want to accept a larger number than the default, make sure
# you increase your OS limits.
# 0 - unlimited.
max_open_connections = 3

# Instrumentation namespace
namespace = "cometbft"

[wasm]
# Smart query gas limit is the max gas to be used in a smart query contract call
query_gas_limit = 77

# in-memory cache for Wasm contracts. Set to 0 to disable.
# The value is in MiB not bytes
memory_cache_size = 888

# Simulation gas limit is the max gas to be used in a tx simulation call.
# When not set the consensus max block gas is used instead
simulation_gas_limit = 9999
18 changes: 18 additions & 0 deletions official/wasm/pkg/config/testdata/config_without_wasm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[instrumentation]

# When true, Prometheus metrics are served under /metrics on
# PrometheusListenAddr.
# Check out the documentation for the list of available metrics.
prometheus = false

# Address to listen for Prometheus collector(s) connections
prometheus_listen_addr = ":26660"

# Maximum number of simultaneous connections.
# If you want to accept a larger number than the default, make sure
# you increase your OS limits.
# 0 - unlimited.
max_open_connections = 3

# Instrumentation namespace
namespace = "cometbft"
36 changes: 34 additions & 2 deletions official/wasm/services/scaffolder/scaffolder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,53 @@ import (
"testing"

"github.com/ignite/cli/v28/ignite/pkg/cosmosver"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

func Test_assertSupportedCosmosSDKVersion(t *testing.T) {
v100, err := cosmosver.Parse("v1.0.0")
require.NoError(t, err)
v0501, err := cosmosver.Parse("v0.50.1")
require.NoError(t, err)
v0450, err := cosmosver.Parse("v0.45.0")
require.NoError(t, err)
v0391, err := cosmosver.Parse("v0.39.1")
require.NoError(t, err)

tests := []struct {
name string
v cosmosver.Version
err error
}{
// TODO: Add test cases.
{
name: "Supported Cosmos SDK version (equal)",
v: v0501,
},
{
name: "Supported Cosmos SDK version (greater than)",
v: v100,
},
{
name: "Unsupported Cosmos SDK version",
v: v0450,
err: errors.Errorf(errOldCosmosSDKVersionStr, v0450),
},
{
name: "Unsupported Cosmos SDK version",
v: v0391,
err: errors.Errorf(errOldCosmosSDKVersionStr, v0391),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := assertSupportedCosmosSDKVersion(tt.v)
require.Equal(t, tt.err, err)
if tt.err != nil {
require.Error(t, err)
require.Equal(t, tt.err.Error(), err.Error())
return
}
require.NoError(t, err)
})
}
}

0 comments on commit a252c1a

Please sign in to comment.