go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/prjmanager/triager/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 triager
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/cv/internal/configs/prjcfg"
    22  	"go.chromium.org/luci/cv/internal/prjmanager/prjpb"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  )
    26  
    27  // simplePMState implements itriager.PMState and is used in tests of this
    28  // package.
    29  type simplePMState struct {
    30  	pb  *prjpb.PState
    31  	cgs []*prjcfg.ConfigGroup
    32  }
    33  
    34  func (s *simplePMState) PCL(clid int64) *prjpb.PCL {
    35  	for _, pcl := range s.pb.GetPcls() {
    36  		if pcl.GetClid() == clid {
    37  			return pcl
    38  		}
    39  	}
    40  	return nil
    41  }
    42  
    43  func (s *simplePMState) PurgingCL(clid int64) *prjpb.PurgingCL {
    44  	for _, p := range s.pb.GetPurgingCls() {
    45  		if p.GetClid() == clid {
    46  			return p
    47  		}
    48  	}
    49  	return nil
    50  }
    51  
    52  func (s *simplePMState) TriggeringCLDeps(clid int64) *prjpb.TriggeringCLDeps {
    53  	for _, p := range s.pb.GetTriggeringClDeps() {
    54  		if p.GetOriginClid() == clid {
    55  			return p
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  func (s *simplePMState) ConfigGroup(index int32) *prjcfg.ConfigGroup {
    62  	return s.cgs[index]
    63  }
    64  
    65  func TestEarliest(t *testing.T) {
    66  	t.Parallel()
    67  
    68  	Convey("earliest of two works", t, func() {
    69  		zero := time.Time{}
    70  		epoch := time.Date(2021, time.February, 6, 15, 0, 0, 0, time.UTC)
    71  		after := epoch.Add(time.Hour)
    72  		before := epoch.Add(-time.Hour)
    73  
    74  		So(earliest(), ShouldResemble, zero)
    75  		So(earliest(zero), ShouldResemble, zero)
    76  		So(earliest(epoch), ShouldResemble, epoch)
    77  
    78  		So(earliest(zero, epoch), ShouldResemble, epoch)
    79  		So(earliest(epoch, zero), ShouldResemble, epoch)
    80  
    81  		So(earliest(after, zero, epoch), ShouldResemble, epoch)
    82  		So(earliest(epoch, before, zero, after), ShouldResemble, before)
    83  	})
    84  }