-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver_test.go
55 lines (50 loc) · 1.39 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"context"
"testing"
"github.com/golang/protobuf/proto"
"github.com/google/go-cmp/cmp"
pb "github.com/honeycombio/microservices-demo/src/productcatalogservice/demo/msdemo"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func TestServer(t *testing.T) {
ctx := context.Background()
addr := run("0")
conn, err := grpc.Dial(addr,
grpc.WithInsecure(),
)
if err != nil {
t.Fatal(err)
}
defer func(conn *grpc.ClientConn) {
_ = conn.Close()
}(conn)
client := pb.NewProductCatalogServiceClient(conn)
res, err := client.ListProducts(ctx, &pb.Empty{})
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(res.Products, parseCatalog(), cmp.Comparer(proto.Equal)); diff != "" {
t.Error(diff)
}
got, err := client.GetProduct(ctx, &pb.GetProductRequest{Id: "OLJCESPC7Z"})
if err != nil {
t.Fatal(err)
}
if want := parseCatalog()[0]; !proto.Equal(got, want) {
t.Errorf("got %v, want %v", got, want)
}
_, err = client.GetProduct(ctx, &pb.GetProductRequest{Id: "N/A"})
if got, want := status.Code(err), codes.NotFound; got != want {
t.Errorf("got %s, want %s", got, want)
}
sres, err := client.SearchProducts(ctx, &pb.SearchProductsRequest{Query: "typewriter"})
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(sres.Results, []*pb.Product{parseCatalog()[0]}, cmp.Comparer(proto.Equal)); diff != "" {
t.Error(diff)
}
}