Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: improve GraphQL error messages #20354

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions common/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,43 @@ package common
import (
"bytes"
"testing"

checker "gopkg.in/check.v1"
)

type BytesSuite struct{}

var _ = checker.Suite(&BytesSuite{})
func TestCopyBytes(t *testing.T) {
input := []byte{1, 2, 3, 4}

func (s *BytesSuite) TestCopyBytes(c *checker.C) {
data1 := []byte{1, 2, 3, 4}
exp1 := []byte{1, 2, 3, 4}
res1 := CopyBytes(data1)
c.Assert(res1, checker.DeepEquals, exp1)
v := CopyBytes(input)
if !bytes.Equal(v, []byte{1, 2, 3, 4}) {
t.Fatal("not equal after copy")
}
v[0] = 99
if bytes.Equal(v, input) {
t.Fatal("result is not a copy")
}
}

func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
val1 := []byte{1, 2, 3, 4}
exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}

res1 := LeftPadBytes(val1, 8)
res2 := LeftPadBytes(val1, 2)
func TestLeftPadBytes(t *testing.T) {
val := []byte{1, 2, 3, 4}
padded := []byte{0, 0, 0, 0, 1, 2, 3, 4}

c.Assert(res1, checker.DeepEquals, exp1)
c.Assert(res2, checker.DeepEquals, val1)
if r := LeftPadBytes(val, 8); !bytes.Equal(r, padded) {
t.Fatalf("LeftPadBytes(%v, 8) == %v", val, r)
}
if r := LeftPadBytes(val, 2); !bytes.Equal(r, val) {
t.Fatalf("LeftPadBytes(%v, 2) == %v", val, r)
}
}

func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
func TestRightPadBytes(t *testing.T) {
val := []byte{1, 2, 3, 4}
exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}

resstd := RightPadBytes(val, 8)
resshrt := RightPadBytes(val, 2)
padded := []byte{1, 2, 3, 4, 0, 0, 0, 0}

c.Assert(resstd, checker.DeepEquals, exp)
c.Assert(resshrt, checker.DeepEquals, val)
if r := RightPadBytes(val, 8); !bytes.Equal(r, padded) {
t.Fatalf("RightPadBytes(%v, 8) == %v", val, r)
}
if r := RightPadBytes(val, 2); !bytes.Equal(r, val) {
t.Fatalf("RightPadBytes(%v, 2) == %v", val, r)
}
}

func TestFromHex(t *testing.T) {
Expand Down
25 changes: 0 additions & 25 deletions common/main_test.go

This file was deleted.

9 changes: 5 additions & 4 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"database/sql/driver"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"math/rand"
Expand Down Expand Up @@ -142,7 +143,7 @@ func (h Hash) Value() (driver.Value, error) {
}

// ImplementsGraphQLType returns true if Hash implements the specified GraphQL type.
func (_ Hash) ImplementsGraphQLType(name string) bool { return name == "Bytes32" }
func (Hash) ImplementsGraphQLType(name string) bool { return name == "Bytes32" }

// UnmarshalGraphQL unmarshals the provided GraphQL query data.
func (h *Hash) UnmarshalGraphQL(input interface{}) error {
Expand All @@ -151,7 +152,7 @@ func (h *Hash) UnmarshalGraphQL(input interface{}) error {
case string:
err = h.UnmarshalText([]byte(input))
default:
err = fmt.Errorf("Unexpected type for Bytes32: %v", input)
err = fmt.Errorf("unexpected type %T for Hash", input)
}
return err
}
Expand Down Expand Up @@ -290,7 +291,7 @@ func (a *Address) UnmarshalGraphQL(input interface{}) error {
case string:
err = a.UnmarshalText([]byte(input))
default:
err = fmt.Errorf("Unexpected type for Address: %v", input)
err = fmt.Errorf("unexpected type %T for Address", input)
}
return err
}
Expand Down Expand Up @@ -323,7 +324,7 @@ func NewMixedcaseAddress(addr Address) MixedcaseAddress {
// NewMixedcaseAddressFromString is mainly meant for unit-testing
func NewMixedcaseAddressFromString(hexaddr string) (*MixedcaseAddress, error) {
if !IsHexAddress(hexaddr) {
return nil, fmt.Errorf("Invalid address")
return nil, errors.New("invalid address")
}
a := FromHex(hexaddr)
return &MixedcaseAddress{addr: BytesToAddress(a), original: hexaddr}, nil
Expand Down