sq/libsq/core/sqlz/nullbool_test.go

60 lines
1.3 KiB
Go
Raw Normal View History

2020-08-06 20:58:47 +03:00
package sqlz_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/neilotoole/sq/libsq/core/sqlz"
2020-08-06 20:58:47 +03:00
)
func TestNullBool_Scan(t *testing.T) {
tests := []struct {
2022-12-17 02:34:33 +03:00
input any
2020-08-06 20:58:47 +03:00
expectValid bool
expectBool bool
}{
{"yes", true, true},
{"Yes", true, true},
{"YES", true, true},
{"Y", true, true},
{" Yes ", true, true},
{"no", true, false},
{"No", true, false},
{"NO", true, false},
{"N", true, false},
{" No ", true, false},
// check that the pre-existing sql.NullBool stuff works
{"true", true, true},
{true, true, true},
{1, true, true},
{"1", true, true},
{"false", true, false},
{false, true, false},
{0, true, false},
{"0", true, false},
{"garbage", false, false},
}
for i, tt := range tests {
var nb sqlz.NullBool
err := nb.Scan(tt.input)
if err != nil {
if !tt.expectValid {
2020-08-06 20:58:47 +03:00
continue
}
t.Errorf("[%d] {%s}: did not expect error: %v", i, tt.input, err)
2020-08-06 20:58:47 +03:00
continue
}
require.Nil(t, err, "[%d] {%s}: did not expect error: %v", i, tt.input, err)
require.Equal(t, tt.expectValid, nb.Valid, "[%d] {%s}: expected Valid to be %v but got %v", i, tt.input,
tt.expectValid, nb.Valid)
require.Equal(t, tt.expectBool, nb.Bool, "[%d] {%s}: expected Bool to be %v but got %v", i, tt.input,
tt.expectBool, nb.Bool)
2020-08-06 20:58:47 +03:00
}
}