-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,485 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package assert | ||
|
||
// Nop returns an assertion that does not assert anything. | ||
func Nop() Assertion { | ||
return AssertionFunc(func(v interface{}) error { | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package queryutil | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/zoncoen/query-go" | ||
"github.com/zoncoen/scenarigo/protocol/grpc/proto" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
"google.golang.org/protobuf/types/dynamicpb" | ||
) | ||
|
||
func TestKeyExtractor_ExtractByKey(t *testing.T) { | ||
comp := proto.NewCompiler([]string{}) | ||
fds, err := comp.Compile(context.Background(), []string{"testdata/test.proto"}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
svc, err := fds.ResolveService("scenarigo.testdata.test.Test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
method := svc.Methods().ByName("Echo") | ||
if method == nil { | ||
t.Fatal("failed to get method") | ||
} | ||
msg := dynamicpb.NewMessage(method.Input()) | ||
msg.Set(method.Input().Fields().ByName("message_id"), protoreflect.ValueOf("1")) | ||
|
||
t.Run("success", func(t *testing.T) { | ||
tests := map[string]struct { | ||
key string | ||
opts []query.Option | ||
expect any | ||
}{ | ||
"name": { | ||
key: "message_id", | ||
expect: "1", | ||
}, | ||
"name (case insensitive)": { | ||
key: "MESSAGE_ID", | ||
opts: []query.Option{query.CaseInsensitive()}, | ||
expect: "1", | ||
}, | ||
"json": { | ||
key: "messageId", | ||
expect: "1", | ||
}, | ||
"json (case insensitive)": { | ||
key: "messageid", | ||
opts: []query.Option{query.CaseInsensitive()}, | ||
expect: "1", | ||
}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
v, err := New(test.opts...).Key(test.key).Extract(msg) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got, expect := v, test.expect; got != expect { | ||
t.Errorf("expect %v but got %v", expect, got) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("failure", func(t *testing.T) { | ||
tests := map[string]struct { | ||
key string | ||
opts []query.Option | ||
expect string | ||
}{ | ||
"not found": { | ||
key: "messageid", | ||
expect: `".messageid" not found`, | ||
}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
_, err := New(test.opts...).Key(test.key).Extract(msg) | ||
if err == nil { | ||
t.Fatal("no error") | ||
} | ||
if got, expect := err.Error(), test.expect; !strings.Contains(got, test.expect) { | ||
t.Errorf("expect %q but got %q", expect, got) | ||
} | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
syntax = "proto3"; | ||
|
||
package scenarigo.testdata.test; | ||
|
||
service Test { | ||
rpc Echo(EchoRequest) returns (EchoResponse) {}; | ||
} | ||
|
||
message EchoRequest { | ||
string message_id = 1; | ||
string message_body = 2; | ||
} | ||
|
||
message EchoResponse { | ||
string message_id = 1; | ||
string message_body = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package yamlutil | ||
|
||
import ( | ||
"encoding/hex" | ||
"strings" | ||
"unicode/utf8" | ||
|
||
"google.golang.org/grpc/metadata" | ||
|
||
"github.com/goccy/go-yaml" | ||
) | ||
|
||
func NewMDMarshaler(md metadata.MD) *MDMarshaler { return (*MDMarshaler)(&md) } | ||
|
||
type MDMarshaler metadata.MD | ||
|
||
func (m *MDMarshaler) MarshalYAML() ([]byte, error) { | ||
mp := make(metadata.MD, len(*m)) | ||
for k, vs := range *m { | ||
if !strings.HasSuffix(k, "-bin") { | ||
mp[k] = vs | ||
continue | ||
} | ||
s := make([]string, len(vs)) | ||
for i, v := range vs { | ||
if !utf8.ValidString(v) { | ||
v = hex.EncodeToString([]byte(v)) | ||
} | ||
s[i] = v | ||
} | ||
mp[k] = s | ||
} | ||
return yaml.Marshal(mp) | ||
} |
Oops, something went wrong.