-
Notifications
You must be signed in to change notification settings - Fork 86
Enum.ConvertFromModel should use Basetype.ConvertFromModel method #2224
Changes from all commits
733fd02
3699075
da8831c
92170e5
414395f
5d2b6b4
89e7036
e5965f9
ec7ace0
427afb0
5512f13
377c80e
575bc70
f47eed7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -339,3 +339,86 @@ func TestEnumType_EqualEnclosing(t *testing.T) { | |
}) | ||
}) | ||
} | ||
|
||
func TestEnumType_ConvertFromModel(t *testing.T) { | ||
t.Parallel() | ||
resource.Require(t, resource.UnitTest) | ||
type testCase struct { | ||
subTestName string | ||
input interface{} // contains valid and invalid values | ||
expectedOutput interface{} | ||
wantErr bool | ||
} | ||
tests := []struct { | ||
name string | ||
enum w.EnumType | ||
data []testCase | ||
}{ | ||
{ | ||
"kind string", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We would need similar test data for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will be part of first cleaning up the typesystem. The test data needs to know no only input and output but also how a value is stored inside: JSON -> Go -> DB -> Go -JSON There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're using an enum of type string, why did you call this test "kind string" then? I suggest to have more tests for list, enums and simple types and test every one of them individually. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a test for enum of type string. I can rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never mind. I didn't see the context for this subtest. |
||
w.EnumType{ | ||
SimpleType: w.SimpleType{Kind: w.KindEnum}, | ||
BaseType: w.SimpleType{Kind: w.KindString}, | ||
Values: []interface{}{"first", "second", "third"}, | ||
}, | ||
[]testCase{ | ||
{"ok", "second", "second", false}, | ||
{"ok - nil", nil, nil, false}, | ||
{"fail - invalid string", "fourth", nil, true}, | ||
{"fail - int", 11, nil, true}, | ||
{"fail - float", 1.3, nil, true}, | ||
{"fail - empty string", "", nil, true}, | ||
{"fail - list", []string{"x", "y"}, nil, true}, | ||
}, | ||
}, | ||
{ | ||
"kind int", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, why do you use an enum of ints and call the tests "kind int"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as #2224 (comment) |
||
w.EnumType{ | ||
SimpleType: w.SimpleType{Kind: w.KindEnum}, | ||
BaseType: w.SimpleType{Kind: w.KindInteger}, | ||
Values: []interface{}{4, 5, 6}, | ||
}, | ||
[]testCase{ | ||
{"ok", 4, 4, false}, | ||
{"ok - nil", nil, nil, false}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is important. The method does not return error on |
||
{"fail - invalid int", 2, nil, true}, | ||
{"fail - string", "11", nil, true}, | ||
{"fail - float", 1.3, nil, true}, | ||
{"fail - bool", true, nil, true}, | ||
{"fail - list", []string{"x", "y"}, nil, true}, | ||
}, | ||
}, | ||
{ | ||
"kind float", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as #2224 (comment) |
||
w.EnumType{ | ||
SimpleType: w.SimpleType{Kind: w.KindEnum}, | ||
BaseType: w.SimpleType{Kind: w.KindFloat}, | ||
Values: []interface{}{1.1, 2.2, 3.3}, | ||
}, | ||
[]testCase{ | ||
{"ok", 1.1, 1.1, false}, | ||
{"ok - nil", nil, nil, false}, | ||
{"fail - invalid float", 4.4, nil, true}, | ||
{"fail - int", 1, nil, true}, | ||
{"fail - string", "11", nil, true}, | ||
{"fail - bool", true, nil, true}, | ||
{"fail - list", []string{"x", "y"}, nil, true}, | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
for _, subtt := range test.data { | ||
t.Run(subtt.subTestName, func(tt *testing.T) { | ||
val, err := test.enum.ConvertFromModel(subtt.input) | ||
if subtt.wantErr { | ||
require.Error(tt, err) | ||
} else { | ||
require.NoError(tt, err) | ||
} | ||
require.Equal(tt, subtt.expectedOutput, val) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -595,7 +595,7 @@ func (r *GormWorkItemRepository) Save(ctx context.Context, spaceID uuid.UUID, up | |
} | ||
fieldValue := updatedWorkItem.Fields[fieldName] | ||
var err error | ||
if fieldName == SystemAssignees || fieldName == SystemLabels || fieldName == SystemBoardcolumns { | ||
if fieldDef.Type.GetKind() == KindList { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change wasn't necessary for this PR but since this was a minor change I did it in this PR. |
||
switch fieldValue.(type) { | ||
case []string: | ||
if len(fieldValue.([]string)) == 0 { | ||
|
@@ -735,7 +735,7 @@ func (r *GormWorkItemRepository) Create(ctx context.Context, spaceID uuid.UUID, | |
if err != nil { | ||
return nil, nil, errors.NewBadParameterError(fieldName, fieldValue) // TODO(kwk): Change errors pkg to consume the original error as well | ||
} | ||
if (fieldName == SystemAssignees || fieldName == SystemLabels || fieldName == SystemBoardcolumns) && fieldValue == nil { | ||
if fieldDef.Type.GetKind() == KindList && fieldValue == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
delete(wi.Fields, fieldName) | ||
} | ||
if fieldName == SystemDescription && wi.Fields[fieldName] != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The enum type created for the test uses only
string, int and float
as the base type. I have not addedarea/iteration/user
for base type because it would not add much value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would add much value but currently the type-system treats all of them as strings which is simple wrong. But this is subject of another PR.