go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/changelist/external_id_test.go (about)

     1  // Copyright 2021 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 changelist
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  	. "go.chromium.org/luci/common/testing/assertions"
    22  )
    23  
    24  func TestExternalID(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	Convey("ExternalID works", t, func() {
    28  
    29  		Convey("GobID", func() {
    30  			eid, err := GobID("x-review.example.com", 12)
    31  			So(err, ShouldBeNil)
    32  			So(eid, ShouldResemble, ExternalID("gerrit/x-review.example.com/12"))
    33  
    34  			host, change, err := eid.ParseGobID()
    35  			So(err, ShouldBeNil)
    36  			So(host, ShouldResemble, "x-review.example.com")
    37  			So(change, ShouldEqual, 12)
    38  
    39  			So(eid.MustURL(), ShouldResemble, "https://x-review.example.com/c/12")
    40  		})
    41  
    42  		Convey("Invalid GobID", func() {
    43  			_, _, err := ExternalID("meh").ParseGobID()
    44  			So(err, ShouldErrLike, "is not a valid GobID")
    45  
    46  			_, _, err = ExternalID("gerrit/x/y").ParseGobID()
    47  			So(err, ShouldErrLike, "is not a valid GobID")
    48  		})
    49  
    50  	})
    51  }
    52  
    53  func TestJoinExternalURLs(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	Convey("JoinExternalURLs works", t, func() {
    57  		gob := func(num int64) ExternalID {
    58  			eid, err := GobID("example.com", num)
    59  			So(err, ShouldBeNil)
    60  			return eid
    61  		}
    62  		var eids []ExternalID
    63  		Convey("with empty input", func() {
    64  			So(JoinExternalURLs(eids, ", "), ShouldEqual, "")
    65  		})
    66  		Convey("with single input", func() {
    67  			eids = append(eids, gob(1))
    68  			So(JoinExternalURLs(eids, ", "), ShouldEqual, "https://example.com/c/1")
    69  		})
    70  		Convey("with > 1 inputs", func() {
    71  			eids = append(eids, gob(1), gob(2))
    72  			So(JoinExternalURLs(eids, ", "), ShouldEqual,
    73  				"https://example.com/c/1, https://example.com/c/2")
    74  		})
    75  	})
    76  }