vitess.io/vitess@v0.16.2/go/protoutil/time_test.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package protoutil
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"vitess.io/vitess/go/test/utils"
    26  	"vitess.io/vitess/go/vt/proto/vttime"
    27  )
    28  
    29  func TestTimeFromProto(t *testing.T) {
    30  	now := time.Date(2021, time.June, 12, 13, 14, 15, 0 /* nanos */, time.UTC)
    31  	vtt := TimeToProto(now)
    32  
    33  	utils.MustMatch(t, now, TimeFromProto(vtt))
    34  
    35  	vtt.Nanoseconds = 100
    36  	utils.MustMatch(t, now.Add(100*time.Nanosecond), TimeFromProto(vtt))
    37  
    38  	vtt.Nanoseconds = 1e9
    39  	utils.MustMatch(t, now.Add(time.Second), TimeFromProto(vtt))
    40  
    41  	assert.True(t, TimeFromProto(nil).IsZero(), "expected Go time from nil vttime to be Zero")
    42  }
    43  
    44  func TestTimeToProto(t *testing.T) {
    45  	now := time.Date(2021, time.June, 12, 13, 14, 15, 0 /* nanos */, time.UTC)
    46  	secs := now.Unix()
    47  	utils.MustMatch(t, &vttime.Time{Seconds: secs}, TimeToProto(now))
    48  
    49  	// Testing secs/nanos conversions
    50  	utils.MustMatch(t, &vttime.Time{Seconds: secs, Nanoseconds: 100}, TimeToProto(now.Add(100*time.Nanosecond)))
    51  	utils.MustMatch(t, &vttime.Time{Seconds: secs + 1}, TimeToProto(now.Add(1e9*time.Nanosecond))) // this should rollover to a full second
    52  }