go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/protoutil/tag_test.go (about)

     1  // Copyright 2019 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 protoutil
    16  
    17  import (
    18  	"testing"
    19  
    20  	"go.chromium.org/luci/common/data/strpair"
    21  
    22  	pb "go.chromium.org/luci/buildbucket/proto"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  )
    27  
    28  func TestBuildSet(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	Convey("Gerrit", t, func() {
    32  		Convey("ParseBuildSet", func() {
    33  			actual := ParseBuildSet("patch/gerrit/chromium-review.googlesource.com/678507/3")
    34  			So(actual, ShouldResemble, &pb.GerritChange{
    35  				Host:     "chromium-review.googlesource.com",
    36  				Change:   678507,
    37  				Patchset: 3,
    38  			})
    39  		})
    40  		Convey("BuildSet", func() {
    41  			bs := GerritBuildSet(&pb.GerritChange{
    42  				Host:     "chromium-review.googlesource.com",
    43  				Change:   678507,
    44  				Patchset: 3,
    45  			})
    46  			So(bs, ShouldEqual, "patch/gerrit/chromium-review.googlesource.com/678507/3")
    47  		})
    48  	})
    49  
    50  	Convey("Gitiles", t, func() {
    51  		Convey("ParseBuildSet", func() {
    52  			actual := ParseBuildSet("commit/gitiles/chromium.googlesource.com/infra/luci/luci-go/+/b7a757f457487cd5cfe2dae83f65c5bc10e288b7")
    53  			So(actual, ShouldResemble, &pb.GitilesCommit{
    54  				Host:    "chromium.googlesource.com",
    55  				Project: "infra/luci/luci-go",
    56  				Id:      "b7a757f457487cd5cfe2dae83f65c5bc10e288b7",
    57  			})
    58  		})
    59  		Convey("not sha1", func() {
    60  			bs := ParseBuildSet("commit/gitiles/chromium.googlesource.com/infra/luci/luci-go/+/non-sha1")
    61  			So(bs, ShouldBeNil)
    62  		})
    63  
    64  		Convey("no host", func() {
    65  			bs := ParseBuildSet("commit/gitiles//infra/luci/luci-go/+/b7a757f457487cd5cfe2dae83f65c5bc10e288b7")
    66  			So(bs, ShouldBeNil)
    67  		})
    68  		Convey("no plus", func() {
    69  			bs := ParseBuildSet("commit/gitiles//infra/luci/luci-go/b7a757f457487cd5cfe2dae83f65c5bc10e288b7")
    70  			So(bs, ShouldBeNil)
    71  		})
    72  		Convey("BuildSet", func() {
    73  			bs := GitilesBuildSet(&pb.GitilesCommit{
    74  				Host:    "chromium.googlesource.com",
    75  				Project: "infra/luci/luci-go",
    76  				Id:      "b7a757f457487cd5cfe2dae83f65c5bc10e288b7",
    77  			})
    78  			So(bs, ShouldEqual, "commit/gitiles/chromium.googlesource.com/infra/luci/luci-go/+/b7a757f457487cd5cfe2dae83f65c5bc10e288b7")
    79  		})
    80  	})
    81  }
    82  
    83  func TestStringPairs(t *testing.T) {
    84  	t.Parallel()
    85  
    86  	Convey("StringPairs", t, func() {
    87  		m := strpair.Map{}
    88  		m.Add("a", "1")
    89  		m.Add("a", "2")
    90  		m.Add("b", "1")
    91  
    92  		pairs := StringPairs(m)
    93  		So(pairs, ShouldResembleProto, []*pb.StringPair{
    94  			{Key: "a", Value: "1"},
    95  			{Key: "a", Value: "2"},
    96  			{Key: "b", Value: "1"},
    97  		})
    98  	})
    99  
   100  	Convey("StringPairMap", t, func() {
   101  		expected := make(strpair.Map)
   102  		var stringPairs []*pb.StringPair
   103  		So(StringPairMap(stringPairs), ShouldResemble, expected)
   104  
   105  		stringPairs = []*pb.StringPair{}
   106  		So(StringPairMap(stringPairs), ShouldResemble, expected)
   107  
   108  		stringPairs = []*pb.StringPair{
   109  			{Key: "a", Value: "1"},
   110  			{Key: "a", Value: "2"},
   111  			{Key: "b", Value: "1"},
   112  		}
   113  		expected = strpair.ParseMap([]string{"a:1", "a:2", "b:1"})
   114  		So(StringPairMap(stringPairs), ShouldResemble, expected)
   115  	})
   116  }