Skip to content

Commit

Permalink
Use time.Equal in tests to assert equality
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewpage authored and TylerBrock committed Jan 8, 2019
1 parent 0269905 commit 7156549
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,48 @@ import (

var testTimeNow = time.Date(2018, 12, 01, 15, 30, 0, 0, time.UTC)

/*
* Helper method to assert equality of time objects in our tests
*/
func assertTimeEquality(t *testing.T, expected *time.Time, result *time.Time, err error) bool {
if err != nil || !result.Equal(*expected) {
t.Errorf("Expected to parse absolute time from simple string. Error: %s. Expected Time: %s, Absolute Time: %s", err, expected.String(), result.String())

return false;
}

return true;
}

// test the relative parsing functionality of getTime
func TestGetTimeRelative(t *testing.T) {
relativeTest1, err := getTime("-2h", testTimeNow)

if err != nil || relativeTest1.Hour() != 13 {
t.Errorf("Expected relativeTest1 to set time back by two hours. Error: %s", err)
}
expectedTime1 := time.Date(2018, 12, 01, 13, 30, 0, 0, time.UTC)
relativeTime1, err := getTime("-2h", testTimeNow)
assertTimeEquality(t, &expectedTime1, &relativeTime1, err)

relativeTest2, err := getTime("-1h15m", testTimeNow)
if err != nil || !(relativeTest2.Hour() == 14 && relativeTest2.Minute() == 15) {
t.Errorf("Expected relativeTest2 to set time back by 1 hour and 15 minutes. Error: %s", err)
}
expectedTime2 := time.Date(2018, 12, 01, 14, 15, 0, 0, time.UTC)
relativeTime2, err := getTime("-1h15m", testTimeNow)
assertTimeEquality(t, &expectedTime2, &relativeTime2, err)
}

// test the absolute RFC3339 parsing functionality of getTime
func TestGetTimeAbsoluteRFC3339(t *testing.T) {
absoluteTime1, err := getTime("2006-01-02T15:04:05+07:00", testTimeNow)
expectedTime := time.Date(2006, 1, 2, 23, 4, 5, 0, time.UTC)
absoluteTime1, err := getTime("2006-01-02T15:04:05-08:00", testTimeNow)

if err != nil && absoluteTime1.Hour() == 15 && absoluteTime1.Minute() == 4 {
t.Errorf("Expected to parse absolute time. Error: %s", err)
}
assertTimeEquality(t, &expectedTime, &absoluteTime1, err)
}

func TestGetTimeAbsoluteSimpleDate(t *testing.T) {
expectedTime := time.Date(2018, 6, 26, 0, 0, 0, 0, time.UTC)
absoluteTime1, err := getTime("2018-06-26", testTimeNow)

if err != nil &&
absoluteTime1.Year() == 2018 &&
absoluteTime1.Month() == 6 &&
absoluteTime1.Day() == 26 &&
absoluteTime1.Hour() == 0 &&
absoluteTime1.Minute() == 0 &&
absoluteTime1.Second() == 0 {
t.Errorf("Expected to parse absolute time from simple string. Error: %s", err)
}
assertTimeEquality(t, &expectedTime, &absoluteTime1, err)
}

func TestGetTimeAbsoluteSimpleDateAndTime(t *testing.T) {
expectedTime := time.Date(2018, 6, 26, 12, 43, 30, 0, time.UTC)
absoluteTime1, err := getTime("2018-06-26 12:43:30", testTimeNow)

if err != nil &&
absoluteTime1.Year() == 2018 &&
absoluteTime1.Month() == 6 &&
absoluteTime1.Day() == 26 &&
absoluteTime1.Hour() == 12 &&
absoluteTime1.Minute() == 43 &&
absoluteTime1.Second() == 30 {
t.Errorf("Expected to parse absolute time from simple string. Error: %s", err)
}
assertTimeEquality(t, &expectedTime, &absoluteTime1, err)
}

0 comments on commit 7156549

Please sign in to comment.