From 883e940d7f932ecefb153d526cba9eef3d2b7cbf Mon Sep 17 00:00:00 2001 From: Hoang Pham Date: Tue, 1 Mar 2022 16:48:03 -0600 Subject: [PATCH] FB-1188 - Fixed units tests for TTL --- client/client.go | 39 ++++++++++++----------------------- client/orm_test.go | 2 +- http_handler.go | 6 +++++- http_handler_internal_test.go | 2 ++ 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/client/client.go b/client/client.go index 30e11e4bd..a023f9c29 100644 --- a/client/client.go +++ b/client/client.go @@ -1674,18 +1674,18 @@ type SchemaField struct { // SchemaOptions contains options for a field or an index. type SchemaOptions struct { - FieldType FieldType `json:"type"` - CacheType string `json:"cacheType"` - CacheSize uint `json:"cacheSize"` - TimeQuantum string `json:"timeQuantum"` - Ttl string `json:"ttl"` - Min pql.Decimal `json:"min"` - Max pql.Decimal `json:"max"` - Scale int64 `json:"scale"` - Keys bool `json:"keys"` - NoStandardView bool `json:"noStandardView"` - TrackExistence bool `json:"trackExistence"` - TimeUnit string `json:"timeUnit"` + FieldType FieldType `json:"type"` + CacheType string `json:"cacheType"` + CacheSize uint `json:"cacheSize"` + TimeQuantum string `json:"timeQuantum"` + Ttl time.Duration `json:"ttl"` + Min pql.Decimal `json:"min"` + Max pql.Decimal `json:"max"` + Scale int64 `json:"scale"` + Keys bool `json:"keys"` + NoStandardView bool `json:"noStandardView"` + TrackExistence bool `json:"trackExistence"` + TimeUnit string `json:"timeUnit"` } func (so SchemaOptions) asIndexOptions() *IndexOptions { @@ -1698,25 +1698,12 @@ func (so SchemaOptions) asIndexOptions() *IndexOptions { } func (so SchemaOptions) asFieldOptions() *FieldOptions { - var ttlValue time.Duration - if so.Ttl != "" { - ttlVal, err := time.ParseDuration(so.Ttl) - if err != nil { - ttlValue = 0 - fmt.Println(fmt.Errorf("ParseDuration error: %v", err)) - } else { - ttlValue = ttlVal - } - } else { - ttlValue = 0 - } - return &FieldOptions{ fieldType: so.FieldType, cacheSize: int(so.CacheSize), cacheType: CacheType(so.CacheType), timeQuantum: TimeQuantum(so.TimeQuantum), - ttl: ttlValue, + ttl: so.Ttl, min: so.Min, max: so.Max, scale: so.Scale, diff --git a/client/orm_test.go b/client/orm_test.go index 650d113c3..1a37bc18b 100644 --- a/client/orm_test.go +++ b/client/orm_test.go @@ -1004,7 +1004,7 @@ func TestORM(t *testing.T) { t.Fatalf("field noStandardView %v != %v", true, field.Opts().NoStandardView()) } jsonString := field.options.String() - targetString := `{"options":{"noStandardView":true,"type":"time","timeQuantum":"DH"}}` + targetString := `{"options":{"noStandardView":true,"type":"time","timeQuantum":"DH","ttl":"0s"}}` if sortedString(targetString) != sortedString(jsonString) { t.Fatalf("`%s` != `%s`", targetString, jsonString) } diff --git a/http_handler.go b/http_handler.go index a771d7245..549afa926 100644 --- a/http_handler.go +++ b/http_handler.go @@ -1615,7 +1615,11 @@ func fieldOptionsToFunctionalOpts(opt fieldOptions) []FieldOption { } fos = append(fos, OptFieldTypeTimestamp(opt.Epoch.UTC(), *opt.TimeUnit)) case FieldTypeTime: - fos = append(fos, OptFieldTypeTime(*opt.TimeQuantum, *opt.Ttl, opt.NoStandardView)) + if opt.Ttl != nil { + fos = append(fos, OptFieldTypeTime(*opt.TimeQuantum, *opt.Ttl, opt.NoStandardView)) + } else { + fos = append(fos, OptFieldTypeTime(*opt.TimeQuantum, "0", opt.NoStandardView)) + } case FieldTypeMutex: fos = append(fos, OptFieldTypeMutex(*opt.CacheType, *opt.CacheSize)) case FieldTypeBool: diff --git a/http_handler_internal_test.go b/http_handler_internal_test.go index 455a40c64..de337cde0 100644 --- a/http_handler_internal_test.go +++ b/http_handler_internal_test.go @@ -131,6 +131,7 @@ func TestFieldOptionValidation(t *testing.T) { {json: `{"options": {"type": "set", "min": 0}}`, err: "min does not apply to field type set"}, {json: `{"options": {"type": "set", "max": 100}}`, err: "max does not apply to field type set"}, {json: `{"options": {"type": "set", "timeQuantum": "YMD"}}`, err: "timeQuantum does not apply to field type set"}, + {json: `{"options": {"type": "set", "ttl": "1h"}}`, err: "ttl does not apply to field type set"}, // FieldType: Int {json: `{"options": {"type": "int"}}`, err: "min is required for field type int"}, @@ -143,6 +144,7 @@ func TestFieldOptionValidation(t *testing.T) { {json: `{"options": {"type": "int", "min": 0, "max": 1000, "cacheType": "ranked"}}`, err: "cacheType does not apply to field type int"}, {json: `{"options": {"type": "int", "min": 0, "max": 1000, "cacheSize": 1000}}`, err: "cacheSize does not apply to field type int"}, {json: `{"options": {"type": "int", "min": 0, "max": 1000, "timeQuantum": "YMD"}}`, err: "timeQuantum does not apply to field type int"}, + {json: `{"options": {"type": "int", "min": 0, "max": 1000, "ttl": "1h"}}`, err: "ttl does not apply to field type int"}, // FieldType: Time {json: `{"options": {"type": "time"}}`, err: "timeQuantum is required for field type time"},