You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

181 lines
4.8 KiB

  1. // Copyright 2017, Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // AUTO-GENERATED CODE. DO NOT EDIT.
  15. package videointelligence
  16. import (
  17. videointelligencepb "google.golang.org/genproto/googleapis/cloud/videointelligence/v1"
  18. longrunningpb "google.golang.org/genproto/googleapis/longrunning"
  19. )
  20. import (
  21. "flag"
  22. "fmt"
  23. "io"
  24. "log"
  25. "net"
  26. "os"
  27. "strings"
  28. "testing"
  29. "github.com/golang/protobuf/proto"
  30. "github.com/golang/protobuf/ptypes"
  31. "golang.org/x/net/context"
  32. "google.golang.org/api/option"
  33. status "google.golang.org/genproto/googleapis/rpc/status"
  34. "google.golang.org/grpc"
  35. "google.golang.org/grpc/codes"
  36. "google.golang.org/grpc/metadata"
  37. gstatus "google.golang.org/grpc/status"
  38. )
  39. var _ = io.EOF
  40. var _ = ptypes.MarshalAny
  41. var _ status.Status
  42. type mockVideoIntelligenceServer struct {
  43. // Embed for forward compatibility.
  44. // Tests will keep working if more methods are added
  45. // in the future.
  46. videointelligencepb.VideoIntelligenceServiceServer
  47. reqs []proto.Message
  48. // If set, all calls return this error.
  49. err error
  50. // responses to return if err == nil
  51. resps []proto.Message
  52. }
  53. func (s *mockVideoIntelligenceServer) AnnotateVideo(ctx context.Context, req *videointelligencepb.AnnotateVideoRequest) (*longrunningpb.Operation, error) {
  54. md, _ := metadata.FromIncomingContext(ctx)
  55. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  56. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  57. }
  58. s.reqs = append(s.reqs, req)
  59. if s.err != nil {
  60. return nil, s.err
  61. }
  62. return s.resps[0].(*longrunningpb.Operation), nil
  63. }
  64. // clientOpt is the option tests should use to connect to the test server.
  65. // It is initialized by TestMain.
  66. var clientOpt option.ClientOption
  67. var (
  68. mockVideoIntelligence mockVideoIntelligenceServer
  69. )
  70. func TestMain(m *testing.M) {
  71. flag.Parse()
  72. serv := grpc.NewServer()
  73. videointelligencepb.RegisterVideoIntelligenceServiceServer(serv, &mockVideoIntelligence)
  74. lis, err := net.Listen("tcp", "localhost:0")
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. go serv.Serve(lis)
  79. conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. clientOpt = option.WithGRPCConn(conn)
  84. os.Exit(m.Run())
  85. }
  86. func TestVideoIntelligenceServiceAnnotateVideo(t *testing.T) {
  87. var expectedResponse *videointelligencepb.AnnotateVideoResponse = &videointelligencepb.AnnotateVideoResponse{}
  88. mockVideoIntelligence.err = nil
  89. mockVideoIntelligence.reqs = nil
  90. any, err := ptypes.MarshalAny(expectedResponse)
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. mockVideoIntelligence.resps = append(mockVideoIntelligence.resps[:0], &longrunningpb.Operation{
  95. Name: "longrunning-test",
  96. Done: true,
  97. Result: &longrunningpb.Operation_Response{Response: any},
  98. })
  99. var request *videointelligencepb.AnnotateVideoRequest = &videointelligencepb.AnnotateVideoRequest{}
  100. c, err := NewClient(context.Background(), clientOpt)
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. respLRO, err := c.AnnotateVideo(context.Background(), request)
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. resp, err := respLRO.Wait(context.Background())
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. if want, got := request, mockVideoIntelligence.reqs[0]; !proto.Equal(want, got) {
  113. t.Errorf("wrong request %q, want %q", got, want)
  114. }
  115. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  116. t.Errorf("wrong response %q, want %q)", got, want)
  117. }
  118. }
  119. func TestVideoIntelligenceServiceAnnotateVideoError(t *testing.T) {
  120. errCode := codes.PermissionDenied
  121. mockVideoIntelligence.err = nil
  122. mockVideoIntelligence.resps = append(mockVideoIntelligence.resps[:0], &longrunningpb.Operation{
  123. Name: "longrunning-test",
  124. Done: true,
  125. Result: &longrunningpb.Operation_Error{
  126. Error: &status.Status{
  127. Code: int32(errCode),
  128. Message: "test error",
  129. },
  130. },
  131. })
  132. var request *videointelligencepb.AnnotateVideoRequest = &videointelligencepb.AnnotateVideoRequest{}
  133. c, err := NewClient(context.Background(), clientOpt)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. respLRO, err := c.AnnotateVideo(context.Background(), request)
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. resp, err := respLRO.Wait(context.Background())
  142. if st, ok := gstatus.FromError(err); !ok {
  143. t.Errorf("got error %v, expected grpc error", err)
  144. } else if c := st.Code(); c != errCode {
  145. t.Errorf("got error code %q, want %q", c, errCode)
  146. }
  147. _ = resp
  148. }