go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/resultdb/testutil.go (about)

     1  // Copyright 2022 The LUCI Authors.
     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  
    15  package resultdb
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/golang/mock/gomock"
    21  
    22  	"go.chromium.org/luci/common/proto"
    23  	rdbpb "go.chromium.org/luci/resultdb/proto/v1"
    24  )
    25  
    26  // MockedClient is a mocked ResultDB client for testing.
    27  // It wraps a rdbpb.MockResultDBClient and a context with the mocked client.
    28  type MockedClient struct {
    29  	Client *rdbpb.MockResultDBClient
    30  	Ctx    context.Context
    31  }
    32  
    33  // NewMockedClient creates a MockedClient for testing.
    34  func NewMockedClient(ctx context.Context, ctl *gomock.Controller) *MockedClient {
    35  	mockClient := rdbpb.NewMockResultDBClient(ctl)
    36  	return &MockedClient{
    37  		Client: mockClient,
    38  		Ctx:    context.WithValue(ctx, &mockResultDBClientKey, mockClient),
    39  	}
    40  }
    41  
    42  // QueryTestVariants mocks the QueryTestVariants RPC.
    43  func (mc *MockedClient) QueryTestVariants(req *rdbpb.QueryTestVariantsRequest, res *rdbpb.QueryTestVariantsResponse) {
    44  	mc.Client.EXPECT().QueryTestVariants(gomock.Any(), proto.MatcherEqual(req),
    45  		gomock.Any()).Return(res, nil)
    46  }
    47  
    48  // GetInvocation mocks the GetInvocation RPC.
    49  func (mc *MockedClient) GetInvocation(req *rdbpb.GetInvocationRequest, res *rdbpb.Invocation) {
    50  	mc.Client.EXPECT().GetInvocation(gomock.Any(), proto.MatcherEqual(req),
    51  		gomock.Any()).Return(res, nil)
    52  }
    53  
    54  // GetRealm is a shortcut of GetInvocation to get realm of the invocation.
    55  func (mc *MockedClient) GetRealm(inv, realm string) {
    56  	req := &rdbpb.GetInvocationRequest{
    57  		Name: inv,
    58  	}
    59  	mc.GetInvocation(req, &rdbpb.Invocation{
    60  		Name:  inv,
    61  		Realm: realm,
    62  	})
    63  }
    64  
    65  // BatchGetTestVariants mocks the BatchGetTestVariants RPC.
    66  func (mc *MockedClient) BatchGetTestVariants(req *rdbpb.BatchGetTestVariantsRequest, res *rdbpb.BatchGetTestVariantsResponse) {
    67  	mc.Client.EXPECT().BatchGetTestVariants(gomock.Any(), proto.MatcherEqual(req),
    68  		gomock.Any()).Return(res, nil)
    69  }