From d42df851f0a81577fb26419bb9177952a712f820 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Thu, 2 May 2024 10:59:29 -0700 Subject: [PATCH] Fix tests --- config/json/deploy.go | 23 +++++++++++------------ config/json/deploy_test.go | 3 ++- events.go | 12 +----------- flowkit_test.go | 12 +++++++----- mocks/services_mock.go | 3 ++- 5 files changed, 23 insertions(+), 30 deletions(-) diff --git a/config/json/deploy.go b/config/json/deploy.go index 157ac3898..16810e2b9 100644 --- a/config/json/deploy.go +++ b/config/json/deploy.go @@ -20,7 +20,6 @@ package json import ( "encoding/json" - "fmt" "github.com/invopop/jsonschema" "github.com/onflow/cadence" @@ -103,18 +102,18 @@ func transformDeploymentsToJSON(configDeployments config.Deployments) jsonDeploy } else { args := make([]map[string]any, 0) for _, arg := range c.Args { - switch arg.Type().ID() { - case "Bool": - args = append(args, map[string]any{ - "type": arg.Type().ID(), - "value": arg.ToGoValue(), - }) - default: - args = append(args, map[string]any{ - "type": arg.Type().ID(), - "value": fmt.Sprintf("%v", arg.ToGoValue()), - }) + jsonEncoded, err := jsoncdc.Encode(arg) + if err != nil { + panic(err) } + + jsonMap := make(map[string]any) + err = json.Unmarshal(jsonEncoded, &jsonMap) + if err != nil { + panic(err) + } + + args = append(args, jsonMap) } deployments = append(deployments, deployment{ diff --git a/config/json/deploy_test.go b/config/json/deploy_test.go index 41b0c8805..95eb39885 100644 --- a/config/json/deploy_test.go +++ b/config/json/deploy_test.go @@ -23,6 +23,7 @@ import ( "strings" "testing" + "github.com/onflow/cadence" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -147,7 +148,7 @@ func Test_DeploymentAdvanced(t *testing.T) { assert.Equal(t, `"Hello World"`, alice.Contracts[0].Args[0].String()) assert.Equal(t, "10", alice.Contracts[0].Args[1].String()) assert.Equal(t, "Bool", alice.Contracts[0].Args[2].Type().ID()) - assert.False(t, alice.Contracts[0].Args[2].ToGoValue().(bool)) + assert.False(t, bool(alice.Contracts[0].Args[2].(cadence.Bool))) assert.Equal(t, "KittyItemsMarket", alice.Contracts[1].Name) assert.Len(t, alice.Contracts[1].Args, 0) } diff --git a/events.go b/events.go index 7cf6e7679..7137dadbf 100644 --- a/events.go +++ b/events.go @@ -49,19 +49,9 @@ func EventsFromTransaction(tx *flow.TransactionResult) Events { } func NewEvent(event flow.Event) Event { - var names []string - - for _, eventType := range event.Value.EventType.Fields { - names = append(names, eventType.Identifier) - } - values := make(map[string]cadence.Value) - for id, field := range event.Value.Fields { - values[names[id]] = field - } - return Event{ Type: event.Type, - Values: values, + Values: cadence.FieldsMappedByName(event.Value), } } diff --git a/flowkit_test.go b/flowkit_test.go index 7eae437cf..6dddddee9 100644 --- a/flowkit_test.go +++ b/flowkit_test.go @@ -1096,12 +1096,12 @@ func TestProject(t *testing.T) { argCode := tx.Arguments[1] decodeCode, _ := jsoncdc.Decode(nil, argCode) - code := decodeCode.ToGoValue().(string) + code := string(decodeCode.(cadence.String)) argName := tx.Arguments[0] decodeName, _ := jsoncdc.Decode(nil, argName) - testCode, found := resolved[decodeName.ToGoValue().(string)] + testCode, found := resolved[string(decodeName.(cadence.String))] require.True(t, found) assert.True(t, strings.Contains(string(code), testCode)) @@ -1173,12 +1173,12 @@ func TestProject(t *testing.T) { argCode := tx.Arguments[1] decodeCode, _ := jsoncdc.Decode(nil, argCode) - code, _ := decodeCode.ToGoValue().(string) + code := string(decodeCode.(cadence.String)) argName := tx.Arguments[0] decodeName, _ := jsoncdc.Decode(nil, argName) - testCode, found := resolved[decodeName.ToGoValue().(string)] + testCode, found := resolved[string(decodeName.(cadence.String))] require.True(t, found) assert.True(t, strings.Contains(string(code), testCode)) @@ -1372,7 +1372,9 @@ func TestScripts(t *testing.T) { gw.ExecuteScript.Run(func(args mock.Arguments) { assert.Len(t, string(args.Get(1).([]byte)), 86) assert.Equal(t, "\"Foo\"", args.Get(2).([]cadence.Value)[0].String()) - gw.ExecuteScript.Return(cadence.MustConvertValue(""), nil) + retVal, err := cadence.NewString("") + require.NoError(t, err) + gw.ExecuteScript.Return(retVal, nil) }) args := []cadence.Value{cadence.String("Foo")} diff --git a/mocks/services_mock.go b/mocks/services_mock.go index 8790b2c53..5bc325bf9 100644 --- a/mocks/services_mock.go +++ b/mocks/services_mock.go @@ -207,7 +207,8 @@ func DefaultMockServices() *MockServices { }) t.ExecuteScript.Run(func(args mock.Arguments) { - t.ExecuteScript.Return(cadence.MustConvertValue(""), nil) + retVal, err := cadence.NewString("") + t.ExecuteScript.Return(retVal, err) }) t.GetTransactionByID.Return(tests.NewTransaction(), nil)