github.com/avenga/couper@v1.12.2/eval/lib/time_test.go (about) 1 package lib_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/zclconf/go-cty/cty" 8 9 "github.com/avenga/couper/config/configload" 10 "github.com/avenga/couper/config/request" 11 "github.com/avenga/couper/eval" 12 "github.com/avenga/couper/internal/test" 13 ) 14 15 func TestUnixtime(t *testing.T) { 16 helper := test.New(t) 17 18 cf, err := configload.LoadBytes([]byte(`server "test" {}`), "couper.hcl") 19 helper.Must(err) 20 21 hclContext := cf.Context.Value(request.ContextType).(*eval.Context).HCLContext() 22 23 expectedNow := time.Now().Unix() 24 now, err := hclContext.Functions["unixtime"].Call([]cty.Value{}) 25 helper.Must(err) 26 27 if !cty.Number.Equals(now.Type()) { 28 t.Errorf("Wrong return type; expected %s, got: %s", cty.Number.FriendlyName(), now.Type().FriendlyName()) 29 } 30 31 bfnow := now.AsBigFloat() 32 inow, _ := bfnow.Int64() 33 if !fuzzyEqual(expectedNow, inow, 2) { 34 t.Errorf("Wrong return value; expected %d, got: %d", expectedNow, inow) 35 } 36 } 37 38 func fuzzyEqual(a, b, fuzz int64) bool { 39 return b <= a+fuzz && 40 b >= a-fuzz 41 }