github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/go/util/datetime/date_time_test.go (about)

     1  // Copyright 2017 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package datetime
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/attic-labs/noms/go/chunks"
    13  	"github.com/attic-labs/noms/go/marshal"
    14  	"github.com/attic-labs/noms/go/types"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestBasics(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	vs := newTestValueStore()
    22  	defer vs.Close()
    23  
    24  	// Since we are using float64 in noms we cannot represent all possible times.
    25  	dt := DateTime{time.Unix(1234567, 1234567)}
    26  
    27  	nomsValue, err := marshal.Marshal(vs, dt)
    28  	assert.NoError(err)
    29  
    30  	var dt2 DateTime
    31  	err = marshal.Unmarshal(nomsValue, &dt2)
    32  	assert.NoError(err)
    33  
    34  	assert.True(dt.Equal(dt2.Time))
    35  }
    36  
    37  func TestUnmarshal(t *testing.T) {
    38  	assert := assert.New(t)
    39  
    40  	test := func(v types.Struct, t time.Time) {
    41  		var dt DateTime
    42  		err := marshal.Unmarshal(v, &dt)
    43  		assert.NoError(err)
    44  		assert.True(dt.Equal(t))
    45  	}
    46  
    47  	for _, name := range []string{"DateTime", "Date", "xxx", ""} {
    48  		test(types.NewStruct(name, types.StructData{
    49  			"secSinceEpoch": types.Number(42),
    50  		}), time.Unix(42, 0))
    51  	}
    52  
    53  	test(types.NewStruct("", types.StructData{
    54  		"secSinceEpoch": types.Number(42),
    55  		"extra":         types.String("field"),
    56  	}), time.Unix(42, 0))
    57  }
    58  
    59  func TestUnmarshalInvalid(t *testing.T) {
    60  	assert := assert.New(t)
    61  
    62  	test := func(v types.Value) {
    63  		var dt DateTime
    64  		err := marshal.Unmarshal(v, &dt)
    65  		assert.Error(err)
    66  	}
    67  
    68  	test(types.Number(42))
    69  	test(types.NewStruct("DateTime", types.StructData{}))
    70  	test(types.NewStruct("DateTime", types.StructData{
    71  		"secSinceEpoch": types.String(42),
    72  	}))
    73  	test(types.NewStruct("DateTime", types.StructData{
    74  		"SecSinceEpoch": types.Number(42),
    75  	}))
    76  	test(types.NewStruct("DateTime", types.StructData{
    77  		"msSinceEpoch": types.Number(42),
    78  	}))
    79  }
    80  
    81  func TestMarshal(t *testing.T) {
    82  	assert := assert.New(t)
    83  
    84  	vs := newTestValueStore()
    85  	defer vs.Close()
    86  
    87  	test := func(dt DateTime, expected float64) {
    88  		v, err := marshal.Marshal(vs, dt)
    89  		assert.NoError(err)
    90  
    91  		assert.True(types.NewStruct("DateTime", types.StructData{
    92  			"secSinceEpoch": types.Number(expected),
    93  		}).Equals(v))
    94  	}
    95  
    96  	test(DateTime{time.Unix(0, 0)}, 0)
    97  	test(DateTime{time.Unix(42, 0)}, 42)
    98  	test(DateTime{time.Unix(42, 123456789)}, 42.123456789)
    99  	test(DateTime{time.Unix(123456789, 123456789)}, 123456789.123456789)
   100  	test(DateTime{time.Unix(-42, 0)}, -42)
   101  	test(DateTime{time.Unix(-42, -123456789)}, -42.123456789)
   102  	test(DateTime{time.Unix(-123456789, -123456789)}, -123456789.123456789)
   103  }
   104  
   105  func TestMarshalType(t *testing.T) {
   106  	assert := assert.New(t)
   107  
   108  	vs := newTestValueStore()
   109  	defer vs.Close()
   110  
   111  	dt := DateTime{time.Unix(0, 0)}
   112  	typ := marshal.MustMarshalType(dt)
   113  	assert.Equal(DateTimeType, typ)
   114  
   115  	v := marshal.MustMarshal(vs, dt)
   116  	assert.Equal(typ, types.TypeOf(v))
   117  }
   118  
   119  func newTestValueStore() *types.ValueStore {
   120  	st := &chunks.TestStorage{}
   121  	return types.NewValueStore(st.NewView())
   122  }
   123  
   124  func TestZeroValues(t *testing.T) {
   125  	assert := assert.New(t)
   126  
   127  	vs := newTestValueStore()
   128  	defer vs.Close()
   129  
   130  	dt1 := DateTime{}
   131  	assert.True(dt1.IsZero())
   132  
   133  	nomsDate, _ := dt1.MarshalNoms(vs)
   134  
   135  	dt2 := DateTime{}
   136  	marshal.Unmarshal(nomsDate, &dt2)
   137  	assert.True(dt2.IsZero())
   138  
   139  	dt3 := DateTime{}
   140  	dt3.UnmarshalNoms(nomsDate)
   141  	assert.True(dt3.IsZero())
   142  }
   143  
   144  func TestString(t *testing.T) {
   145  	assert := assert.New(t)
   146  	dt := DateTime{time.Unix(1234567, 1234567)}
   147  	// Don't test the actual output since that
   148  	assert.IsType(dt.String(), "s")
   149  }
   150  
   151  func TestEpoch(t *testing.T) {
   152  	assert := assert.New(t)
   153  	assert.Equal(Epoch, DateTime{time.Unix(0, 0)})
   154  }
   155  
   156  func TestHRSComment(t *testing.T) {
   157  	a := assert.New(t)
   158  	vs := newTestValueStore()
   159  
   160  	dt := Now()
   161  	mdt := marshal.MustMarshal(vs, dt)
   162  
   163  	exp := dt.Format(time.RFC3339)
   164  	s1 := types.EncodedValue(mdt)
   165  	a.True(strings.Contains(s1, "{ // "+exp))
   166  
   167  	RegisterHRSCommenter(time.UTC)
   168  	exp = dt.In(time.UTC).Format((time.RFC3339))
   169  	s1 = types.EncodedValue(mdt)
   170  	a.True(strings.Contains(s1, "{ // "+exp))
   171  
   172  	types.UnregisterHRSCommenter(datetypename, hrsEncodingName)
   173  	s1 = types.EncodedValue(mdt)
   174  	a.False(strings.Contains(s1, "{ // 20"))
   175  }