From 0b5d763253e8d346d40a1bdf94b2ab902e91f34b Mon Sep 17 00:00:00 2001 From: lhitchon Date: Sun, 14 Oct 2018 18:23:56 -0700 Subject: [PATCH] add value_type age --- assertion/compare.go | 18 ++++++++++++++++++ assertion/compare_test.go | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 assertion/compare_test.go diff --git a/assertion/compare.go b/assertion/compare.go index a5ca3f6..8f304a9 100644 --- a/assertion/compare.go +++ b/assertion/compare.go @@ -2,6 +2,7 @@ package assertion import ( "strconv" + "time" ) func intCompare(n1 int, n2 int) int { @@ -14,6 +15,20 @@ func intCompare(n1 int, n2 int) int { return 0 } +func daysOld(data interface{}) int { + if stringValue, ok := data.(string); ok { + layout := "2006-01-02T15:04:05Z" + t, err := time.Parse(layout, stringValue) + if err != nil { + return 0 + } + days := int(time.Since(t).Hours() / 24.0) + Debugf("Date: %v Days ago: %d\n", data, days) + return days + } + return 0 +} + func compare(data interface{}, value string, valueType string) int { switch valueType { case "size": @@ -41,6 +56,9 @@ func compare(data interface{}, value string, valueType string) int { return intCompare(n1, n2) } return 0 + case "age": + n, _ := strconv.Atoi(value) + return intCompare(daysOld(data), n) default: tmp, _ := JSONStringify(data) s := unquoted(tmp) diff --git a/assertion/compare_test.go b/assertion/compare_test.go new file mode 100644 index 0000000..43890f2 --- /dev/null +++ b/assertion/compare_test.go @@ -0,0 +1,17 @@ +package assertion + +import ( + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestDaysOldForToday(t *testing.T) { + now := time.Now().Format("2006-01-02T15:04:05Z") + assert.Equal(t, 0, daysOld(now), "Expecting daysOld to return 0") +} + +func TestDaysOldFor90DaysAgo(t *testing.T) { + then := time.Now().Add(-time.Duration(90) * time.Hour * 24).Format("2006-01-02T15:04:05Z") + assert.Equal(t, 90, daysOld(then), "Expecting daysOld to return 90") +}