go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/changelist/util_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  	"sort"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.chromium.org/luci/auth/identity"
    23  
    24  	"go.chromium.org/luci/cv/internal/common"
    25  	"go.chromium.org/luci/cv/internal/cvtesting"
    26  	"go.chromium.org/luci/cv/internal/gerrit/gerritfake"
    27  
    28  	. "github.com/smartystreets/goconvey/convey"
    29  	. "go.chromium.org/luci/common/testing/assertions"
    30  )
    31  
    32  func TestOwnerIdentity(t *testing.T) {
    33  	t.Parallel()
    34  
    35  	Convey("Snapshot.OwnerIdentity works", t, func() {
    36  		s := &Snapshot{}
    37  		_, err := s.OwnerIdentity()
    38  		So(err, ShouldErrLike, "non-Gerrit CL")
    39  
    40  		ci := gerritfake.CI(101, gerritfake.Owner("owner-1"))
    41  		s.Kind = &Snapshot_Gerrit{Gerrit: &Gerrit{
    42  			Host: "x-review.example.com",
    43  			Info: ci,
    44  		}}
    45  		i, err := s.OwnerIdentity()
    46  		So(err, ShouldBeNil)
    47  		So(i, ShouldEqual, identity.Identity("user:owner-1@example.com"))
    48  
    49  		Convey("no preferred email set", func() {
    50  			// Yes, this happens if no preferred email is set. See crbug/1175771.
    51  			ci.Owner.Email = ""
    52  			_, err = s.OwnerIdentity()
    53  			So(err, ShouldErrLike, "CL x-review.example.com/101 owner email of account 1 is unknown")
    54  		})
    55  	})
    56  }
    57  
    58  func TestQueryCLIDsUpdatedBefore(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	Convey("QueryCLIDsUpdatedBefore", t, func() {
    62  		ct := cvtesting.Test{}
    63  		ctx, cancel := ct.SetUp(t)
    64  		defer cancel()
    65  
    66  		nextChangeNumber := 1
    67  		createNCLs := func(n int) []*CL {
    68  			cls := make([]*CL, n)
    69  			for i := range cls {
    70  				eid := MustGobID("example.com", int64(nextChangeNumber))
    71  				nextChangeNumber++
    72  				cls[i] = eid.MustCreateIfNotExists(ctx)
    73  			}
    74  			return cls
    75  		}
    76  
    77  		var allCLs []*CL
    78  		allCLs = append(allCLs, createNCLs(1000)...)
    79  		ct.Clock.Add(1 * time.Minute)
    80  		allCLs = append(allCLs, createNCLs(1000)...)
    81  		ct.Clock.Add(1 * time.Minute)
    82  		allCLs = append(allCLs, createNCLs(1000)...)
    83  
    84  		before := ct.Clock.Now().Add(-30 * time.Second)
    85  		var expected common.CLIDs
    86  		for _, cl := range allCLs {
    87  			if cl.UpdateTime.Before(before) {
    88  				expected = append(expected, cl.ID)
    89  			}
    90  		}
    91  		sort.Sort(expected)
    92  
    93  		actual, err := QueryCLIDsUpdatedBefore(ctx, before)
    94  		So(err, ShouldBeNil)
    95  		So(actual, ShouldResemble, expected)
    96  	})
    97  }