go.temporal.io/server@v1.23.0/common/primitives/timestamp/time_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package timestamp
    26  
    27  import (
    28  	"testing"
    29  	"time"
    30  
    31  	"github.com/stretchr/testify/require"
    32  	"github.com/stretchr/testify/suite"
    33  	"google.golang.org/protobuf/proto"
    34  	"google.golang.org/protobuf/types/known/timestamppb"
    35  
    36  	"go.temporal.io/server/common/testing/protorequire"
    37  )
    38  
    39  // Timestamp suite tests time
    40  type TimestampSuite struct {
    41  	protorequire.ProtoAssertions
    42  	*require.Assertions
    43  	suite.Suite
    44  }
    45  
    46  func TestTimestampSuite(t *testing.T) {
    47  	suite.Run(t, new(TimestampSuite))
    48  }
    49  
    50  func (s *TimestampSuite) SetupTest() {
    51  	s.Assertions = require.New(s.T())
    52  	s.ProtoAssertions = protorequire.New(s.T())
    53  }
    54  
    55  func (s *TimestampSuite) TestCreateTimestampFromGoToProto() {
    56  	t := time.Now().UTC()
    57  	ts := TimestampFromTimePtr(&t)
    58  	tp := ts.ToProto()
    59  	t2 := tp.AsTime()
    60  	s.EqualValues(t.UTC(), t2)
    61  }
    62  
    63  func (s *TimestampSuite) TestCreateTimestampFromProtoToGo() {
    64  	t := timestamppb.New(time.Now())
    65  	ts := TimestampFromProto(t)
    66  	tt := ts.ToTime()
    67  	t2 := timestamppb.New(tt)
    68  	s.ProtoEqual(t, t2)
    69  }
    70  
    71  func (s *TimestampSuite) TestTimestampBeforeAfterProtoProto() {
    72  	t := timestamppb.New(time.Now())
    73  	a := proto.Clone(t).(*timestamppb.Timestamp)
    74  	a.Nanos += 1
    75  	s.NotProtoEqual(t, a)
    76  
    77  	before := TimestampFromProto(t)
    78  	after := TimestampFromProto(a)
    79  	s.beforeAfterValidation(before, after)
    80  }
    81  
    82  func (s *TimestampSuite) TestTimestampBeforeAfterProtoGo() {
    83  	t := timestamppb.New(time.Now())
    84  	a := time.Now().UTC().Add(time.Nanosecond)
    85  
    86  	before := TimestampFromProto(t)
    87  	after := TimestampFromTimePtr(&a)
    88  	s.beforeAfterValidation(before, after)
    89  }
    90  
    91  func (s *TimestampSuite) TestTimestampBeforeAfterGoProto() {
    92  	t := time.Now().UTC()
    93  	a := timestamppb.New(time.Now())
    94  	a.Nanos += 1
    95  
    96  	before := TimestampFromTimePtr(&t)
    97  	after := TimestampFromProto(a)
    98  	s.beforeAfterValidation(before, after)
    99  }
   100  
   101  func (s *TimestampSuite) TestTimestampBeforeAfterProto() {
   102  	t := time.Now().UTC()
   103  	a := t.Add(time.Nanosecond)
   104  	s.NotEqual(t.UnixNano(), a.UnixNano())
   105  
   106  	before := TimestampFromTimePtr(&t)
   107  	after := TimestampFromTimePtr(&a)
   108  	s.beforeAfterValidation(before, after)
   109  }
   110  
   111  func (s *TimestampSuite) beforeAfterValidation(before *Timestamp, after *Timestamp) {
   112  	s.True(before.Before(after))
   113  	s.True(after.After(before))
   114  
   115  	s.False(after.Before(before))
   116  	s.False(before.After(after))
   117  
   118  	s.False(after.SameAs(before))
   119  	s.False(before.SameAs(after))
   120  }