Skip to content

Commit

Permalink
add value_type age
Browse files Browse the repository at this point in the history
  • Loading branch information
lhitchon committed Oct 15, 2018
1 parent 1fb6c93 commit 0b5d763
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions assertion/compare.go
Expand Up @@ -2,6 +2,7 @@ package assertion

import (
"strconv"
"time"
)

func intCompare(n1 int, n2 int) int {
Expand All @@ -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":
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions 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")
}

0 comments on commit 0b5d763

Please sign in to comment.