TestData: allow true and false strings as bool (#33889)

This commit is contained in:
Ryan McKinley 2021-05-10 22:29:46 -07:00 committed by GitHub
parent f1b2c750e5
commit a469fa8416
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -334,14 +334,15 @@ func csvToFieldValues(stringInput string) (*data.Field, error) {
return nil, fmt.Errorf("csv must have at least one value")
}
first := parts[0]
if first == "T" || first == "F" {
first := strings.ToUpper(parts[0])
if first == "T" || first == "F" || first == "TRUE" || first == "FALSE" {
field := data.NewFieldFromFieldType(data.FieldTypeNullableBool, len(parts))
for idx, strVal := range parts {
if strVal == "null" || strVal == "" {
strVal = strings.ToUpper(strVal)
if strVal == "NULL" || strVal == "" {
continue
}
field.SetConcrete(idx, strVal == "T")
field.SetConcrete(idx, strVal == "T" || strVal == "TRUE")
}
return field, nil
}

View File

@ -268,13 +268,16 @@ func TestReadCSV(t *testing.T) {
fBool, err := csvToFieldValues("T, F,F,T ,")
require.NoError(t, err)
fBool2, err := csvToFieldValues("true,false,T,F,F")
require.NoError(t, err)
fNum, err := csvToFieldValues("1,2,,4,5")
require.NoError(t, err)
fStr, err := csvToFieldValues("a,b,,,c")
require.NoError(t, err)
frame := data.NewFrame("", fBool, fNum, fStr)
frame := data.NewFrame("", fBool, fBool2, fNum, fStr)
out, err := data.FrameToJSON(frame, true, true)
require.NoError(t, err)
@ -282,12 +285,14 @@ func TestReadCSV(t *testing.T) {
require.JSONEq(t, `{"schema":{
"fields":[
{"type":"boolean","typeInfo":{"frame":"bool","nullable":true}},
{"type":"boolean","typeInfo":{"frame":"bool","nullable":true}},
{"type":"number","typeInfo":{"frame":"float64","nullable":true}},
{"type":"string","typeInfo":{"frame":"string","nullable":true}}
]},"data":{
"values":[
[true,false,false,true,null],
[true,false,true,false,false],
[1,2,null,4,5],
["a","b",null,null,"c"]
]}}`, string(out))