go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/pbutil/query_test.go (about)

     1  // Copyright 2020 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 pbutil
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	pb "go.chromium.org/luci/resultdb/proto/v1"
    22  
    23  	"github.com/golang/mock/gomock"
    24  	"github.com/golang/protobuf/proto"
    25  	"go.chromium.org/luci/common/testing/mock"
    26  
    27  	. "github.com/smartystreets/goconvey/convey"
    28  	. "go.chromium.org/luci/common/testing/assertions"
    29  )
    30  
    31  func TestQuery(t *testing.T) {
    32  	t.Parallel()
    33  	Convey(`TestQuery`, t, func() {
    34  		ctx := context.Background()
    35  
    36  		ctl := gomock.NewController(t)
    37  		defer ctl.Finish()
    38  		client := pb.NewMockResultDBClient(ctl)
    39  
    40  		fetch := func(req *pb.QueryTestResultsRequest) ([]*pb.TestResult, error) {
    41  			itemC := make(chan proto.Message)
    42  			errC := make(chan error)
    43  			go func() {
    44  				err := Query(ctx, itemC, client, req)
    45  				close(itemC)
    46  				errC <- err
    47  			}()
    48  
    49  			var results []*pb.TestResult
    50  			for r := range itemC {
    51  				results = append(results, r.(*pb.TestResult))
    52  			}
    53  			return results, <-errC
    54  		}
    55  
    56  		Convey("One page", func() {
    57  			expected := []*pb.TestResult{{Name: "a"}, {Name: "b"}}
    58  			client.EXPECT().
    59  				QueryTestResults(gomock.Any(), mock.EqProto(&pb.QueryTestResultsRequest{})).
    60  				Return(&pb.QueryTestResultsResponse{TestResults: expected}, nil)
    61  
    62  			actual, err := fetch(&pb.QueryTestResultsRequest{})
    63  			So(err, ShouldBeNil)
    64  			So(actual, ShouldResembleProto, expected)
    65  		})
    66  
    67  		Convey("Two pages", func() {
    68  			firstPage := client.EXPECT().
    69  				QueryTestResults(gomock.Any(), mock.EqProto(&pb.QueryTestResultsRequest{})).
    70  				Return(&pb.QueryTestResultsResponse{
    71  					TestResults:   []*pb.TestResult{{Name: "a"}, {Name: "b"}},
    72  					NextPageToken: "token",
    73  				}, nil)
    74  			client.EXPECT().
    75  				QueryTestResults(gomock.Any(), mock.EqProto(&pb.QueryTestResultsRequest{
    76  					PageToken: "token",
    77  				})).
    78  				After(firstPage).
    79  				Return(&pb.QueryTestResultsResponse{
    80  					TestResults: []*pb.TestResult{{Name: "c"}},
    81  				}, nil)
    82  
    83  			actual, err := fetch(&pb.QueryTestResultsRequest{})
    84  			So(err, ShouldBeNil)
    85  			So(actual, ShouldResembleProto, []*pb.TestResult{{Name: "a"}, {Name: "b"}, {Name: "c"}})
    86  		})
    87  	})
    88  }