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.
 
 
 

66 lines
2.5 KiB

  1. /*
  2. Copyright 2018 Google LLC
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testutil
  14. import (
  15. "context"
  16. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  17. "google.golang.org/grpc"
  18. )
  19. // FuncMock overloads some of MockCloudSpannerClient's methods with pluggable
  20. // functions.
  21. //
  22. // Note: if you overload a method, you're in charge of making sure
  23. // MockCloudSpannerClient.ReceivedRequests receives the request appropriately.
  24. type FuncMock struct {
  25. CommitFn func(ctx context.Context, r *sppb.CommitRequest, opts ...grpc.CallOption) (*sppb.CommitResponse, error)
  26. BeginTransactionFn func(ctx context.Context, r *sppb.BeginTransactionRequest, opts ...grpc.CallOption) (*sppb.Transaction, error)
  27. GetSessionFn func(ctx context.Context, r *sppb.GetSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error)
  28. CreateSessionFn func(ctx context.Context, r *sppb.CreateSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error)
  29. *MockCloudSpannerClient
  30. }
  31. func (s FuncMock) Commit(ctx context.Context, r *sppb.CommitRequest, opts ...grpc.CallOption) (*sppb.CommitResponse, error) {
  32. if s.CommitFn == nil {
  33. return s.MockCloudSpannerClient.Commit(ctx, r, opts...)
  34. }
  35. return s.CommitFn(ctx, r, opts...)
  36. }
  37. func (s FuncMock) BeginTransaction(ctx context.Context, r *sppb.BeginTransactionRequest, opts ...grpc.CallOption) (*sppb.Transaction, error) {
  38. if s.BeginTransactionFn == nil {
  39. return s.MockCloudSpannerClient.BeginTransaction(ctx, r, opts...)
  40. }
  41. return s.BeginTransactionFn(ctx, r, opts...)
  42. }
  43. func (s *FuncMock) GetSession(ctx context.Context, r *sppb.GetSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error) {
  44. if s.GetSessionFn == nil {
  45. return s.MockCloudSpannerClient.GetSession(ctx, r, opts...)
  46. }
  47. return s.GetSessionFn(ctx, r, opts...)
  48. }
  49. func (s *FuncMock) CreateSession(c context.Context, r *sppb.CreateSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error) {
  50. if s.CreateSessionFn == nil {
  51. return s.MockCloudSpannerClient.CreateSession(c, r, opts...)
  52. }
  53. return s.CreateSessionFn(c, r, opts...)
  54. }