Skip to content

Commit

Permalink
Fixes failing tests and improves Error() resp
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagodeev committed Nov 6, 2024
1 parent e578814 commit 629890b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 30 deletions.
11 changes: 7 additions & 4 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ var (
//
// none
func TestMain(m *testing.M) {
flag.StringVar(&testEnv, "env", "mock", "set the test environment")
flag.StringVar(&testEnv, "env", "devnet", "set the test environment")
flag.Parse()
if testEnv == "mock" {
return
}
base = os.Getenv("INTEGRATION_BASE")
base = "http://localhost:5050/"
if base == "" {
if err := godotenv.Load(fmt.Sprintf(".env.%s", testEnv)); err != nil {
panic(fmt.Sprintf("Failed to load .env.%s, err: %s", testEnv, err))
Expand Down Expand Up @@ -1067,7 +1067,7 @@ func TestWaitForTransactionReceipt(t *testing.T) {
type testSetType struct {
Timeout int
Hash *felt.Felt
ExpectedErr error
ExpectedErr *rpc.RPCError
ExpectedReceipt rpc.TransactionReceipt
}
testSet := map[string][]testSetType{
Expand All @@ -1087,7 +1087,10 @@ func TestWaitForTransactionReceipt(t *testing.T) {

resp, err := acnt.WaitForTransactionReceipt(ctx, test.Hash, 1*time.Second)
if test.ExpectedErr != nil {
require.Equal(t, test.ExpectedErr.Error(), err.Error())
rpcErr, ok := err.(*rpc.RPCError)
require.True(t, ok)
require.Equal(t, test.ExpectedErr.Code, rpcErr.Code)
require.Equal(t, test.ExpectedErr.Message, rpcErr.Message)
} else {
require.Equal(t, test.ExpectedReceipt.ExecutionStatus, (*resp).ExecutionStatus)
}
Expand Down
7 changes: 5 additions & 2 deletions rpc/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCall(t *testing.T) {
FunctionCall FunctionCall
BlockID BlockID
ExpectedPatternResult *felt.Felt
ExpectedError error
ExpectedError *RPCError
}
testSet := map[string][]testSetType{
"devnet": {
Expand Down Expand Up @@ -111,7 +111,10 @@ func TestCall(t *testing.T) {
require := require.New(t)
output, err := testConfig.provider.Call(context.Background(), FunctionCall(test.FunctionCall), test.BlockID)
if test.ExpectedError != nil {
require.EqualError(test.ExpectedError, err.Error())
rpcErr, ok := err.(*RPCError)
require.True(ok)
require.Equal(test.ExpectedError.Code, rpcErr.Code)
require.Equal(test.ExpectedError.Message, rpcErr.Message)
} else {
require.NoError(err)
require.NotEmpty(output, "should return an output")
Expand Down
7 changes: 5 additions & 2 deletions rpc/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ func TestEstimateMessageFee(t *testing.T) {
MsgFromL1
BlockID
ExpectedFeeEst *FeeEstimation
ExpectedError error
ExpectedError *RPCError
}

// https://sepolia.voyager.online/message/0x273f4e20fc522098a60099e5872ab3deeb7fb8321a03dadbd866ac90b7268361
Expand Down Expand Up @@ -470,7 +470,10 @@ func TestEstimateMessageFee(t *testing.T) {
for _, test := range testSet {
resp, err := testConfig.provider.EstimateMessageFee(context.Background(), test.MsgFromL1, test.BlockID)
if err != nil {
require.EqualError(t, test.ExpectedError, err.Error())
rpcErr, ok := err.(*RPCError)
require.True(t, ok)
require.Equal(t, test.ExpectedError.Code, rpcErr.Code)
require.Equal(t, test.ExpectedError.Message, rpcErr.Message)
} else {
require.Exactly(t, test.ExpectedFeeEst, resp)
}
Expand Down
9 changes: 5 additions & 4 deletions rpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"encoding/json"
"fmt"

"github.com/NethermindEth/juno/core/felt"
)
Expand Down Expand Up @@ -76,10 +77,10 @@ type RPCError struct {
}

func (e RPCError) Error() string {
if e.Data == nil {
if e.Data == nil || e.Data.Message == "" {
return e.Message
}
return e.Message + e.Data.Message
return e.Message + ": " + e.Data.Message
}

type RPCData struct {
Expand Down Expand Up @@ -113,7 +114,7 @@ func (rpcData *RPCData) UnmarshalJSON(data []byte) error {
return nil
}

return nil
return fmt.Errorf("failed to unmarshal RPCData")
}

func (rpcData RPCData) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -295,7 +296,7 @@ func (contractEx *ContractExecutionError) UnmarshalJSON(data []byte) error {
return nil
}

return nil
return fmt.Errorf("failed to unmarshal ContractExecutionError")
}

type ContractExecutionErrorStruct struct {
Expand Down
10 changes: 6 additions & 4 deletions rpc/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rpc

import (
"context"
"errors"
"testing"

"github.com/NethermindEth/juno/core/felt"
Expand All @@ -17,7 +16,7 @@ func TestDeclareTransaction(t *testing.T) {
type testSetType struct {
DeclareTx BroadcastDeclareTxnType
ExpectedResp AddDeclareTransactionResponse
ExpectedError error
ExpectedError *RPCError
}
testSet := map[string][]testSetType{
"devnet": {},
Expand All @@ -40,15 +39,18 @@ func TestDeclareTransaction(t *testing.T) {
DeclareTx: BroadcastDeclareTxnV1{},
ExpectedResp: AddDeclareTransactionResponse{
TransactionHash: utils.TestHexToFelt(t, "0x55b094dc5c84c2042e067824f82da90988674314d37e45cb0032aca33d6e0b9")},
ExpectedError: errors.New("Invalid Params"),
ExpectedError: &RPCError{Code: InvalidParams, Message: "Invalid Params"},
},
},
}[testEnv]

for _, test := range testSet {
resp, err := testConfig.provider.AddDeclareTransaction(context.Background(), test.DeclareTx)
if err != nil {
require.Equal(t, test.ExpectedError.Error(), err.Error())
rpcErr, ok := err.(*RPCError)
require.True(t, ok)
require.Equal(t, test.ExpectedError.Code, rpcErr.Code)
require.Equal(t, test.ExpectedError.Message, rpcErr.Message)
} else {
require.Equal(t, (*resp.TransactionHash).String(), (*test.ExpectedResp.TransactionHash).String())
}
Expand Down
14 changes: 0 additions & 14 deletions utils/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"encoding/json"
"fmt"
)

func UnwrapJSON(data map[string]interface{}, tag string) (map[string]interface{}, error) {
Expand All @@ -19,16 +18,3 @@ func UnwrapJSON(data map[string]interface{}, tag string) (map[string]interface{}
}
return data, nil
}

func GetTypedFieldFromJSONMap[T any](data map[string]json.RawMessage, tag string) (resp T, err error) {
rawResp, ok := data[tag]
if !ok {
return resp, fmt.Errorf("missing '%s' field in json object", tag)
}

if err := json.Unmarshal(rawResp, &resp); err != nil {
return resp, err
}

return resp, nil
}

0 comments on commit 629890b

Please sign in to comment.