Skip to content

Commit

Permalink
sdk: Fix fmt + add FieldType test (#19493)
Browse files Browse the repository at this point in the history
* sdk: Fix fmt + add FieldType test

* Add test comment
  • Loading branch information
tomhjp authored and raymonstah committed Mar 17, 2023
1 parent 0e8ecd0 commit 0fb9b15
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sdk/framework/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ func (t FieldType) Zero() interface{} {
case TypeInt:
return 0
case TypeInt64:
return int64(0)
return int64(0)
case TypeBool:
return false
case TypeMap:
Expand Down
21 changes: 21 additions & 0 deletions sdk/framework/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package framework

import (
"context"
"fmt"
"net/http"
"reflect"
"strings"
Expand Down Expand Up @@ -811,3 +812,23 @@ func TestInitializeBackend(t *testing.T) {
t.Fatal("backend should be open")
}
}

// TestFieldTypeMethods tries to ensure our switch-case statements for the
// FieldType "enum" are complete.
func TestFieldTypeMethods(t *testing.T) {
unknownFormat := convertType(TypeInvalid).format

for i := TypeInvalid + 1; i < typeInvalidMax; i++ {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if i.String() == TypeInvalid.String() {
t.Errorf("unknown type string for %d", i)
}

if convertType(i).format == unknownFormat {
t.Errorf("unknown schema for %d", i)
}

_ = i.Zero()
})
}
}
6 changes: 6 additions & 0 deletions sdk/framework/field_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ const (
// formatted as a string or a number. The resulting time.Time
// is converted to UTC.
TypeTime

// DO NOT USE. Any new values must be inserted before this value.
// Used to write tests that ensure type methods handle all possible values.
typeInvalidMax
)

func (t FieldType) String() string {
Expand All @@ -75,6 +79,8 @@ func (t FieldType) String() string {
return "name string"
case TypeInt:
return "int"
case TypeInt64:
return "int64"
case TypeBool:
return "bool"
case TypeMap:
Expand Down

0 comments on commit 0fb9b15

Please sign in to comment.