From e114019d6905fe41bb94e02db155f88ddd9ba9fb Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Tue, 26 Jul 2022 13:08:00 +0300 Subject: [PATCH] test(proto): add tests for ToDate, ToDate32 --- proto/date.go | 2 +- proto/date32.go | 2 +- proto/date32_test.go | 21 +++++++++++++++++++++ proto/date_test.go | 22 ++++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/proto/date.go b/proto/date.go index b031761e..3f850327 100644 --- a/proto/date.go +++ b/proto/date.go @@ -31,7 +31,7 @@ func (d Date) String() string { return d.Time().UTC().Format(DateLayout) } -// ToDate returns Date of time.Time in UTC. +// ToDate returns Date of time.Time. func ToDate(t time.Time) Date { _, offset := t.Zone() return Date((t.Unix() + int64(offset)) / secInDay) diff --git a/proto/date32.go b/proto/date32.go index 751c6d76..00ec39ce 100644 --- a/proto/date32.go +++ b/proto/date32.go @@ -26,7 +26,7 @@ func (d Date32) String() string { return d.Time().Format(DateLayout) } -// ToDate32 returns Date32 of time.Time in UTC. +// ToDate32 returns Date32 of time.Time. func ToDate32(t time.Time) Date32 { _, offset := t.Zone() return Date32((t.Unix() + int64(offset) - date32Epoch) / secInDay) diff --git a/proto/date32_test.go b/proto/date32_test.go index 27630c4a..d7a37ca2 100644 --- a/proto/date32_test.go +++ b/proto/date32_test.go @@ -35,6 +35,27 @@ func TestDate32_Time(t *testing.T) { }) } +func TestToDate32(t *testing.T) { + for _, tc := range []struct { + Value string + }{ + {Value: "2006-01-02T06:04:03+07:00"}, + {Value: "2008-01-02T06:44:15+03:00"}, + {Value: "2009-01-01T06:03:31+12:00"}, + {Value: "2006-12-31T22:04:41-06:30"}, + } { + t.Run(tc.Value, func(t *testing.T) { + v, err := time.Parse(time.RFC3339, tc.Value) + require.NoError(t, err) + d := ToDate32(v) + expected := NewDate32(v.Year(), v.Month(), v.Day()) + assert.Equal(t, v.Format(DateLayout), d.String()) + assert.Equal(t, expected.String(), d.String()) + assert.Equal(t, expected, d) + }) + } +} + func BenchmarkDate32_Time(b *testing.B) { b.ReportAllocs() diff --git a/proto/date_test.go b/proto/date_test.go index a92ce2d3..965c38f3 100644 --- a/proto/date_test.go +++ b/proto/date_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestDate_Time(t *testing.T) { @@ -34,6 +35,27 @@ func TestDate_Time(t *testing.T) { }) } +func TestToDate(t *testing.T) { + for _, tc := range []struct { + Value string + }{ + {Value: "2006-01-02T06:04:03+07:00"}, + {Value: "2008-01-02T06:44:15+03:00"}, + {Value: "2009-01-01T06:03:31+12:00"}, + {Value: "2006-12-31T22:04:41-06:30"}, + } { + t.Run(tc.Value, func(t *testing.T) { + v, err := time.Parse(time.RFC3339, tc.Value) + require.NoError(t, err) + d := ToDate(v) + expected := NewDate(v.Year(), v.Month(), v.Day()) + assert.Equal(t, v.Format(DateLayout), d.String()) + assert.Equal(t, expected.String(), d.String()) + assert.Equal(t, expected, d) + }) + } +} + func BenchmarkDate_Time(b *testing.B) { b.ReportAllocs()