chore: increase test coverage of time pkg

Signed-off-by: Terry Howe <terrylhowe@gmail.com>
pull/30829/head
Terry Howe 5 months ago
parent 31e22b9866
commit f754e6a23b

@ -20,24 +20,141 @@ import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testingTime, _ = Parse(time.RFC3339, "1977-09-02T22:04:05Z")
testingTimeString = `"1977-09-02T22:04:05Z"`
timeParseString = `"1977-09-02T22:04:05Z"`
timeString = "1977-09-02 22:04:05 +0000 UTC"
)
func TestNonZeroValueMarshal(t *testing.T) {
func givenTime(t *testing.T) Time {
result, err := Parse(time.RFC3339, timeParseString)
require.NoError(t, err)
return result
}
func TestDate(t *testing.T) {
got := Date(1977, 9, 2, 22, 04, 05, 0, time.UTC)
assert.Equal(t, timeString, got.String())
}
func TestNow(t *testing.T) {
testingTime := givenTime(t)
got := Now()
assert.Truef(t, testingTime.Before(got), "expected %s before %s", testingTime.String(), got.String())
}
func TestParse(t *testing.T) {
testingTime := givenTime(t)
got, err := Parse(time.RFC3339, timeParseString)
assert.NoError(t, err)
if testingTime.Before(got) {
t.Errorf("expected %s before %s", testingTime.String(), got.String())
}
}
//func TestParseInLocation(t *testing.T) {
//
// got, err := ParseInLocation(tt.args.layout, tt.args.value, tt.args.loc)
// if (err != nil) != tt.wantErr {
// t.Errorf("ParseInLocation() error = %v, wantErr %v", err, tt.wantErr)
// return
// }
// if !reflect.DeepEqual(got, tt.want) {
// t.Errorf("ParseInLocation() got = %v, want %v", got, tt.want)
// }
//}
//func TestTime_Add(t *testing.T) {
//
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.Add(tt.args.d); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("Add() = %v, want %v", got, tt.want)
// }
//}
//
//func TestTime_AddDate(t *testing.T) {
//
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.AddDate(tt.args.years, tt.args.months, tt.args.days); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("AddDate() = %v, want %v", got, tt.want)
// }
//}
//func TestTime_After(t *testing.T) {
//
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.After(tt.args.u); got != tt.want {
// t.Errorf("After() = %v, want %v", got, tt.want)
// }
//
//}
//
//func TestTime_Before(t *testing.T) {
//
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.Before(tt.args.u); got != tt.want {
// t.Errorf("Before() = %v, want %v", got, tt.want)
// }
//
//}
//
//func TestTime_Equal(t *testing.T) {
//
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.Equal(tt.args.u); got != tt.want {
// t.Errorf("Equal() = %v, want %v", got, tt.want)
// }
//
//}
//func TestTime_In(t *testing.T) {
//
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.In(tt.args.loc); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("In() = %v, want %v", got, tt.want)
// }
//
//}
//
//func TestTime_Local(t *testing.T) {
//
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.Local(); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("Local() = %v, want %v", got, tt.want)
// }
//
//}
func TestTime_MarshalJSONNonZero(t *testing.T) {
testingTime := givenTime(t)
res, err := json.Marshal(testingTime)
if err != nil {
t.Fatal(err)
}
if testingTimeString != string(res) {
t.Errorf("expected a marshaled value of %s, got %s", testingTimeString, res)
if timeParseString != string(res) {
t.Errorf("expected a marshaled value of %s, got %s", timeParseString, res)
}
}
func TestZeroValueMarshal(t *testing.T) {
func TestTime_MarshalJSONZeroValue(t *testing.T) {
res, err := json.Marshal(Time{})
if err != nil {
t.Fatal(err)
@ -47,9 +164,47 @@ func TestZeroValueMarshal(t *testing.T) {
}
}
func TestNonZeroValueUnmarshal(t *testing.T) {
//func TestTime_Round(t *testing.T) {
// if got := t.Round(tt.args.d); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("Round() = %v, want %v", got, tt.want)
// }
//}
//func TestTime_Sub(t *testing.T) {
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.Sub(tt.args.u); got != tt.want {
// t.Errorf("Sub() = %v, want %v", got, tt.want)
// }
//}
//func TestTime_Truncate(t *testing.T) {
//
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.Truncate(tt.args.d); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("Truncate() = %v, want %v", got, tt.want)
// }
//
//}
//
//func TestTime_UTC(t *testing.T) {
//
// sut := Time{
// Time: tt.fields.Time,
// }
// if got := t.UTC(); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("UTC() = %v, want %v", got, tt.want)
// }
//
//}
func TestTime_UnmarshalJSONNonZeroValue(t *testing.T) {
testingTime := givenTime(t)
var myTime Time
err := json.Unmarshal([]byte(testingTimeString), &myTime)
err := json.Unmarshal([]byte(timeParseString), &myTime)
if err != nil {
t.Fatal(err)
}
@ -58,7 +213,7 @@ func TestNonZeroValueUnmarshal(t *testing.T) {
}
}
func TestEmptyStringUnmarshal(t *testing.T) {
func TestTime_UnmarshalJSONEmptyString(t *testing.T) {
var myTime Time
err := json.Unmarshal([]byte(emptyString), &myTime)
if err != nil {
@ -69,7 +224,7 @@ func TestEmptyStringUnmarshal(t *testing.T) {
}
}
func TestZeroValueUnmarshal(t *testing.T) {
func TestTime_UnmarshalJSONZeroValue(t *testing.T) {
// This test ensures that we can unmarshal any time value that was output
// with the current go default value of "0001-01-01T00:00:00Z"
var myTime Time
@ -81,3 +236,11 @@ func TestZeroValueUnmarshal(t *testing.T) {
t.Errorf("expected time to be equal to zero value, got %v", myTime)
}
}
//func TestUnix(t *testing.T) {
//
// if got := Unix(tt.args.sec, tt.args.nsec); !reflect.DeepEqual(got, tt.want) {
// t.Errorf("Unix() = %v, want %v", got, tt.want)
// }
//
//}

Loading…
Cancel
Save