cli: add tests for Storage.Find invocations

It returns values of different types we better be sure that
compiler emits correct code.
This commit is contained in:
Evgeniy Stratonikov 2021-01-12 15:52:08 +03:00 committed by Roman Khimov
parent 9b1a7021ba
commit 50d515a3e5
2 changed files with 53 additions and 0 deletions

View file

@ -6,11 +6,13 @@ import (
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"testing"
"github.com/nspcc-dev/neo-go/internal/random"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/interop/storage"
"github.com/nspcc-dev/neo-go/pkg/core/state"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
@ -18,6 +20,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract/nef"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/stretchr/testify/require"
)
@ -254,6 +257,41 @@ func TestComlileAndInvokeFunction(t *testing.T) {
})
})
t.Run("test Storage.Find", func(t *testing.T) {
cmd := []string{"neo-go", "contract", "testinvokefunction",
"--rpc-endpoint", "http://" + e.RPC.Addr,
h.StringLE(), "testFind"}
t.Run("keys only", func(t *testing.T) {
e.Run(t, append(cmd, strconv.FormatInt(storage.FindKeysOnly, 10))...)
res := new(result.Invoke)
require.NoError(t, json.Unmarshal(e.Out.Bytes(), res))
require.Equal(t, vm.HaltState.String(), res.State)
require.Len(t, res.Stack, 1)
require.Equal(t, []stackitem.Item{
stackitem.Make("findkey1"),
stackitem.Make("findkey2"),
}, res.Stack[0].Value())
})
t.Run("both", func(t *testing.T) {
e.Run(t, append(cmd, strconv.FormatInt(storage.FindDefault, 10))...)
res := new(result.Invoke)
require.NoError(t, json.Unmarshal(e.Out.Bytes(), res))
require.Equal(t, vm.HaltState.String(), res.State)
require.Len(t, res.Stack, 1)
arr, ok := res.Stack[0].Value().([]stackitem.Item)
require.True(t, ok)
require.Len(t, arr, 2)
require.Equal(t, []stackitem.Item{
stackitem.Make("findkey1"), stackitem.Make("value1"),
}, arr[0].Value())
require.Equal(t, []stackitem.Item{
stackitem.Make("findkey2"), stackitem.Make("value2"),
}, arr[1].Value())
})
})
t.Run("Update", func(t *testing.T) {
nefName := path.Join(tmpDir, "updated.nef")
manifestName := path.Join(tmpDir, "updated.manifest.json")

View file

@ -4,6 +4,7 @@ import (
"github.com/nspcc-dev/neo-go/cli/testdata/deploy/sub"
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
)
@ -46,3 +47,17 @@ func GetValue() string {
val2 := storage.Get(ctx, sub.Key)
return val1.(string) + "|" + val2.(string)
}
// TestFind finds items with the specified prefix.
func TestFind(f storage.FindFlags) []interface{} {
ctx := storage.GetContext()
storage.Put(ctx, "findkey1", "value1")
storage.Put(ctx, "findkey2", "value2")
var result []interface{}
iter := storage.Find(ctx, "findkey", f)
for iterator.Next(iter) {
result = append(result, iterator.Value(iter))
}
return result
}