This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Table driven tests for SimpleType, EnumType and ListType. (#115)
* table drive tests for SimpleType. WIP * conflict fix added pkg govalidator to glide * added KindList and updated related tests. * Basic Enum tests added. * variable declaration factored into blocks. * Test cases added for available Types. #115 These test cases are testing ConvertToModel function of the Type. Also added fixes in existing ConvertToModel as needed. Few validations are added in Conversion logic. govalidator lib is added for the validations like IsURL. Fixes: #77
- Loading branch information
1 parent
829fdae
commit 72a5560
Showing
5 changed files
with
170 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package models | ||
|
||
import "testing" | ||
|
||
var ( | ||
stString = SimpleType{KindString} | ||
stInt = SimpleType{KindInteger} | ||
stFloat = SimpleType{KindFloat} | ||
stDuration = SimpleType{KindDuration} | ||
stURL = SimpleType{KindURL} | ||
stList = SimpleType{KindList} | ||
) | ||
|
||
type input struct { | ||
t FieldType | ||
value interface{} | ||
expectedValue interface{} | ||
errorExpected bool | ||
} | ||
|
||
func TestSimpleTypeConversion(t *testing.T) { | ||
test_data := []input{ | ||
{stString, "hello world", "hello world", false}, | ||
{stString, "", "", false}, | ||
{stString, 100, nil, true}, | ||
{stString, 1.90, nil, true}, | ||
|
||
{stInt, 100.0, nil, true}, | ||
{stInt, 100, 100, false}, | ||
{stInt, "100", nil, true}, | ||
{stInt, true, nil, true}, | ||
|
||
{stFloat, 1.1, 1.1, false}, | ||
{stFloat, 1, nil, true}, | ||
{stFloat, "a", nil, true}, | ||
|
||
{stDuration, 0, 0, false}, | ||
{stDuration, 1.1, nil, true}, | ||
{stDuration, "duration", nil, true}, | ||
|
||
{stURL, "http://www.google.com", "http://www.google.com", false}, | ||
{stURL, "", nil, true}, | ||
{stURL, "google", nil, true}, | ||
{stURL, "http://google.com", "http://google.com", false}, | ||
|
||
{stList, [4]int{1, 2, 3, 4}, [4]int{1, 2, 3, 4}, false}, | ||
{stList, [2]string{"1", "2"}, [2]string{"1", "2"}, false}, | ||
{stList, "", nil, true}, | ||
// {stList, []int{}, []int{}, false}, need to find out the way for empty array. | ||
// because slices do not have equality operator. | ||
} | ||
for _, inp := range test_data { | ||
retVal, err := inp.t.ConvertToModel(inp.value) | ||
if retVal == inp.expectedValue && (err != nil) == inp.errorExpected { | ||
t.Log("test pass for input: ", inp) | ||
} else { | ||
t.Error(retVal, err) | ||
t.Fail() | ||
} | ||
} | ||
} | ||
|
||
var ( | ||
stEnum = SimpleType{KindEnum} | ||
enum = EnumType{ | ||
BaseType: stEnum, | ||
// ENUM with same type values | ||
Values: []interface{}{"new", "triaged", "WIP", "QA", "done"}, | ||
} | ||
|
||
multipleTypeEnum = EnumType{ | ||
BaseType: stEnum, | ||
// ENUM with different type values. | ||
Values: []interface{}{100, 1.1, "hello"}, | ||
} | ||
) | ||
|
||
func TestEnumTypeConversion(t *testing.T) { | ||
data := []input{ | ||
{enum, "string", nil, true}, | ||
{enum, "triaged", "triaged", false}, | ||
{enum, "done", "done", false}, | ||
{enum, "", nil, true}, | ||
{enum, 100, nil, true}, | ||
|
||
{multipleTypeEnum, "abcd", nil, true}, | ||
{multipleTypeEnum, 100, 100, false}, | ||
{multipleTypeEnum, "hello", "hello", false}, | ||
} | ||
for _, inp := range data { | ||
retVal, err := inp.t.ConvertToModel(inp.value) | ||
if retVal == inp.expectedValue && (err != nil) == inp.errorExpected { | ||
t.Log("test pass for input: ", inp) | ||
} else { | ||
t.Error(retVal, err) | ||
t.Fail() | ||
} | ||
} | ||
} | ||
|
||
var ( | ||
intList = ListType{ | ||
SimpleType: SimpleType{KindList}, | ||
ComponentType: SimpleType{KindInteger}, | ||
} | ||
strList = ListType{ | ||
SimpleType: SimpleType{KindList}, | ||
ComponentType: SimpleType{KindString}, | ||
} | ||
) | ||
|
||
func TestListTypeConversion(t *testing.T) { | ||
data := []input{ | ||
{intList, [2]int{11, 2}, "array/slice", false}, | ||
{intList, [2]string{"11", "2"}, nil, true}, | ||
|
||
{strList, [2]string{"11", "2"}, "array/slice", false}, | ||
{strList, [2]int{11, 2}, nil, true}, | ||
} | ||
|
||
for _, inp := range data { | ||
// Ignore expectedValue for now. | ||
// slices can be compared only with nil. | ||
// Because we will need to iterate and match the output. | ||
retVal, err := inp.t.ConvertToModel(inp.value) | ||
if (err != nil) == inp.errorExpected { | ||
t.Log("test pass for input: ", inp) | ||
} else { | ||
t.Error("failed for input=", inp) | ||
t.Error(retVal, err) | ||
t.Fail() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters