github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/datatypes/datetime_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 TestDatetime(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 := Datetime(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  
    20  	t.Run("Marshal & Unmarshal", func(t *testing.T) {
    21  		t0, _ := time.Parse(time.RFC3339, "2017-03-27T23:58:59+08:00")
    22  		dt := Datetime(t0)
    23  
    24  		dateString, err := dt.MarshalText()
    25  		testutil.Expect(t, err, testutil.Be[error](nil))
    26  		testutil.Expect(t, string(dateString), testutil.Equal("2017-03-27T23:58:59+08:00"))
    27  
    28  		dt2 := DatetimeZero
    29  		testutil.Expect(t, dt2.IsZero(), testutil.Be(true))
    30  
    31  		err = dt2.UnmarshalText(dateString)
    32  		testutil.Expect(t, err, testutil.Be[error](nil))
    33  		testutil.Expect(t, dt2, testutil.Equal(dt))
    34  		testutil.Expect(t, dt2.IsZero(), testutil.Be(false))
    35  
    36  		dt3 := TimestampZero
    37  		err = dt3.UnmarshalText([]byte(""))
    38  		testutil.Expect(t, err, testutil.Be[error](nil))
    39  	})
    40  
    41  	t.Run("Scan & Value", func(t *testing.T) {
    42  		t0, _ := time.Parse(time.RFC3339, "2017-03-27T23:58:59+08:00")
    43  		dt := Datetime(t0)
    44  
    45  		value, err := dt.Value()
    46  		testutil.Expect(t, err, testutil.Be[error](nil))
    47  		testutil.Expect(t, value.(time.Time).In(CST).Format(time.RFC3339), testutil.Equal("2017-03-27T23:58:59+08:00"))
    48  
    49  		dt2 := DatetimeZero
    50  		testutil.Expect(t, dt2.IsZero(), testutil.Be(true))
    51  
    52  		err = dt2.Scan(value)
    53  		testutil.Expect(t, err, testutil.Be[error](nil))
    54  		testutil.Expect(t, dt2.In(CST), testutil.Equal(dt.In(CST)))
    55  		testutil.Expect(t, dt2.IsZero(), testutil.Be(false))
    56  	})
    57  }