go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/common/protoutil_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 common
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"go.chromium.org/luci/common/clock/testclock"
    22  	"google.golang.org/protobuf/types/known/timestamppb"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  )
    27  
    28  func TestPB2Time(t *testing.T) {
    29  	t.Parallel()
    30  
    31  	Convey("RoundTrip", t, func() {
    32  		Convey("Specified", func() {
    33  			t := testclock.TestRecentTimeUTC
    34  			pb := Time2PBNillable(t)
    35  			So(pb, ShouldResembleProto, Time2PBNillable(t))
    36  			So(pb, ShouldResembleProto, timestamppb.New(t))
    37  			So(PB2TimeNillable(pb), ShouldEqual, t)
    38  		})
    39  		Convey("Zero / nil", func() {
    40  			So(PB2TimeNillable(nil), ShouldEqual, time.Time{})
    41  			So(Time2PBNillable(time.Time{}), ShouldBeNil)
    42  		})
    43  	})
    44  }