github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/datatypes/timestamp_test.go (about)

     1  package datatypes
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/octohelm/storage/internal/testutil"
     8  )
     9  
    10  func TestTimestamp(t *testing.T) {
    11  	t.Run("Parse", func(t *testing.T) {
    12  		t0, _ := time.Parse(time.RFC3339, "2017-03-27T23:58:59+08:00")
    13  		dt := Timestamp(t0)
    14  
    15  		testutil.Expect(t, dt.String(), testutil.Equal("2017-03-27T23:58:59+08:00"))
    16  		testutil.Expect(t, dt.Format(time.RFC3339), testutil.Equal("2017-03-27T23:58:59+08:00"))
    17  		testutil.Expect(t, dt.Unix(), testutil.Equal(int64(1490630339)))
    18  	})
    19  	t.Run("Marshal & Unmarshal", func(t *testing.T) {
    20  		t0, _ := time.Parse(time.RFC3339, "2017-03-27T23:58:59+08:00")
    21  		dt := Timestamp(t0)
    22  
    23  		dateString, err := dt.MarshalText()
    24  		testutil.Expect(t, err, testutil.Be[error](nil))
    25  		testutil.Expect(t, string(dateString), testutil.Equal("2017-03-27T23:58:59+08:00"))
    26  
    27  		dt2 := TimestampZero
    28  		testutil.Expect(t, dt2.IsZero(), testutil.Be(true))
    29  
    30  		err = dt2.UnmarshalText(dateString)
    31  		testutil.Expect(t, err, testutil.Be[error](nil))
    32  		testutil.Expect(t, dt2, testutil.Equal(dt))
    33  		testutil.Expect(t, dt2.IsZero(), testutil.Be(false))
    34  
    35  		dt3 := TimestampZero
    36  		err = dt3.UnmarshalText([]byte(""))
    37  		testutil.Expect(t, err, testutil.Be[error](nil))
    38  	})
    39  }