25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

233 satır
6.4 KiB

  1. // Copyright 2019 Google LLC
  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. // https://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. // Code generated by gapic-generator. DO NOT EDIT.
  15. package texttospeech
  16. import (
  17. texttospeechpb "google.golang.org/genproto/googleapis/cloud/texttospeech/v1"
  18. )
  19. import (
  20. "context"
  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. "google.golang.org/api/option"
  32. status "google.golang.org/genproto/googleapis/rpc/status"
  33. "google.golang.org/grpc"
  34. "google.golang.org/grpc/codes"
  35. "google.golang.org/grpc/metadata"
  36. gstatus "google.golang.org/grpc/status"
  37. )
  38. var _ = io.EOF
  39. var _ = ptypes.MarshalAny
  40. var _ status.Status
  41. type mockTextToSpeechServer struct {
  42. // Embed for forward compatibility.
  43. // Tests will keep working if more methods are added
  44. // in the future.
  45. texttospeechpb.TextToSpeechServer
  46. reqs []proto.Message
  47. // If set, all calls return this error.
  48. err error
  49. // responses to return if err == nil
  50. resps []proto.Message
  51. }
  52. func (s *mockTextToSpeechServer) ListVoices(ctx context.Context, req *texttospeechpb.ListVoicesRequest) (*texttospeechpb.ListVoicesResponse, error) {
  53. md, _ := metadata.FromIncomingContext(ctx)
  54. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  55. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  56. }
  57. s.reqs = append(s.reqs, req)
  58. if s.err != nil {
  59. return nil, s.err
  60. }
  61. return s.resps[0].(*texttospeechpb.ListVoicesResponse), nil
  62. }
  63. func (s *mockTextToSpeechServer) SynthesizeSpeech(ctx context.Context, req *texttospeechpb.SynthesizeSpeechRequest) (*texttospeechpb.SynthesizeSpeechResponse, error) {
  64. md, _ := metadata.FromIncomingContext(ctx)
  65. if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
  66. return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
  67. }
  68. s.reqs = append(s.reqs, req)
  69. if s.err != nil {
  70. return nil, s.err
  71. }
  72. return s.resps[0].(*texttospeechpb.SynthesizeSpeechResponse), nil
  73. }
  74. // clientOpt is the option tests should use to connect to the test server.
  75. // It is initialized by TestMain.
  76. var clientOpt option.ClientOption
  77. var (
  78. mockTextToSpeech mockTextToSpeechServer
  79. )
  80. func TestMain(m *testing.M) {
  81. flag.Parse()
  82. serv := grpc.NewServer()
  83. texttospeechpb.RegisterTextToSpeechServer(serv, &mockTextToSpeech)
  84. lis, err := net.Listen("tcp", "localhost:0")
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. go serv.Serve(lis)
  89. conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
  90. if err != nil {
  91. log.Fatal(err)
  92. }
  93. clientOpt = option.WithGRPCConn(conn)
  94. os.Exit(m.Run())
  95. }
  96. func TestTextToSpeechListVoices(t *testing.T) {
  97. var expectedResponse *texttospeechpb.ListVoicesResponse = &texttospeechpb.ListVoicesResponse{}
  98. mockTextToSpeech.err = nil
  99. mockTextToSpeech.reqs = nil
  100. mockTextToSpeech.resps = append(mockTextToSpeech.resps[:0], expectedResponse)
  101. var request *texttospeechpb.ListVoicesRequest = &texttospeechpb.ListVoicesRequest{}
  102. c, err := NewClient(context.Background(), clientOpt)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. resp, err := c.ListVoices(context.Background(), request)
  107. if err != nil {
  108. t.Fatal(err)
  109. }
  110. if want, got := request, mockTextToSpeech.reqs[0]; !proto.Equal(want, got) {
  111. t.Errorf("wrong request %q, want %q", got, want)
  112. }
  113. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  114. t.Errorf("wrong response %q, want %q)", got, want)
  115. }
  116. }
  117. func TestTextToSpeechListVoicesError(t *testing.T) {
  118. errCode := codes.PermissionDenied
  119. mockTextToSpeech.err = gstatus.Error(errCode, "test error")
  120. var request *texttospeechpb.ListVoicesRequest = &texttospeechpb.ListVoicesRequest{}
  121. c, err := NewClient(context.Background(), clientOpt)
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. resp, err := c.ListVoices(context.Background(), request)
  126. if st, ok := gstatus.FromError(err); !ok {
  127. t.Errorf("got error %v, expected grpc error", err)
  128. } else if c := st.Code(); c != errCode {
  129. t.Errorf("got error code %q, want %q", c, errCode)
  130. }
  131. _ = resp
  132. }
  133. func TestTextToSpeechSynthesizeSpeech(t *testing.T) {
  134. var audioContent []byte = []byte("16")
  135. var expectedResponse = &texttospeechpb.SynthesizeSpeechResponse{
  136. AudioContent: audioContent,
  137. }
  138. mockTextToSpeech.err = nil
  139. mockTextToSpeech.reqs = nil
  140. mockTextToSpeech.resps = append(mockTextToSpeech.resps[:0], expectedResponse)
  141. var input *texttospeechpb.SynthesisInput = &texttospeechpb.SynthesisInput{}
  142. var voice *texttospeechpb.VoiceSelectionParams = &texttospeechpb.VoiceSelectionParams{}
  143. var audioConfig *texttospeechpb.AudioConfig = &texttospeechpb.AudioConfig{}
  144. var request = &texttospeechpb.SynthesizeSpeechRequest{
  145. Input: input,
  146. Voice: voice,
  147. AudioConfig: audioConfig,
  148. }
  149. c, err := NewClient(context.Background(), clientOpt)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. resp, err := c.SynthesizeSpeech(context.Background(), request)
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. if want, got := request, mockTextToSpeech.reqs[0]; !proto.Equal(want, got) {
  158. t.Errorf("wrong request %q, want %q", got, want)
  159. }
  160. if want, got := expectedResponse, resp; !proto.Equal(want, got) {
  161. t.Errorf("wrong response %q, want %q)", got, want)
  162. }
  163. }
  164. func TestTextToSpeechSynthesizeSpeechError(t *testing.T) {
  165. errCode := codes.PermissionDenied
  166. mockTextToSpeech.err = gstatus.Error(errCode, "test error")
  167. var input *texttospeechpb.SynthesisInput = &texttospeechpb.SynthesisInput{}
  168. var voice *texttospeechpb.VoiceSelectionParams = &texttospeechpb.VoiceSelectionParams{}
  169. var audioConfig *texttospeechpb.AudioConfig = &texttospeechpb.AudioConfig{}
  170. var request = &texttospeechpb.SynthesizeSpeechRequest{
  171. Input: input,
  172. Voice: voice,
  173. AudioConfig: audioConfig,
  174. }
  175. c, err := NewClient(context.Background(), clientOpt)
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. resp, err := c.SynthesizeSpeech(context.Background(), request)
  180. if st, ok := gstatus.FromError(err); !ok {
  181. t.Errorf("got error %v, expected grpc error", err)
  182. } else if c := st.Code(); c != errCode {
  183. t.Errorf("got error code %q, want %q", c, errCode)
  184. }
  185. _ = resp
  186. }