forked from tee8z/nullable
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bool_test.go
112 lines (87 loc) · 2.75 KB
/
bool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package nullable_test
import (
"testing"
"gorm.io/gorm/utils/tests"
"github.com/tee8z/nullable"
)
func TestScanBool(t *testing.T) {
nullableBool := nullable.NewBool(nil)
nullableBool.Scan(true)
tests.AssertEqual(t, nullableBool.Get(), true)
nullableBool.Scan(false)
tests.AssertEqual(t, nullableBool.Get(), false)
nullableBool.Scan(nil)
tests.AssertEqual(t, nullableBool.Get(), nil)
}
func TestNewBool(t *testing.T) {
basicBool1 := true
nullableBool1 := nullable.NewBool(&basicBool1)
tests.AssertEqual(t, nullableBool1.Get(), true)
basicBool2 := false
nullableBool2 := nullable.NewBool(&basicBool2)
tests.AssertEqual(t, nullableBool2.Get(), false)
nullableBool3 := nullable.NewBool(nil)
tests.AssertEqual(t, nullableBool3.Get(), nil)
}
func TestSetBool(t *testing.T) {
nullableBool := nullable.NewBool(nil)
tests.AssertEqual(t, nullableBool.Get(), nil)
trueBool := true
nullableBool.Set(&trueBool)
tests.AssertEqual(t, nullableBool.Get(), true)
falseBool := false
nullableBool.Set(&falseBool)
tests.AssertEqual(t, nullableBool.Get(), false)
nullableBool.Set(nil)
tests.AssertEqual(t, nullableBool.Get(), nil)
}
func TestJSONBool(t *testing.T) {
trueBool := true
marshalUnmarshalJSON(t, nullable.NewBool(&trueBool))
falseBool := false
marshalUnmarshalJSON(t, nullable.NewBool(&falseBool))
marshalUnmarshalJSON(t, nullable.NewBool(nil))
}
func TestBool(t *testing.T) {
type TestNullableBool struct {
ID uint
Name string
Checked nullable.Bool
}
DB.Migrator().DropTable(&TestNullableBool{})
if err := DB.Migrator().AutoMigrate(&TestNullableBool{}); err != nil {
t.Errorf("failed to migrate nullable boolean, got error: %v", err)
}
checked := true
checkedUser := TestNullableBool{
Name: "Athaariq Ardhiansyah",
Checked: nullable.NewBool(&checked),
}
DB.Create(&checkedUser)
unchecked := false
uncheckedUser := TestNullableBool{
Name: "Bad Cat",
Checked: nullable.NewBool(&unchecked),
}
DB.Create(&uncheckedUser)
unknownUser := TestNullableBool{
Name: "Charles Robinson",
Checked: nullable.NewBool(nil),
}
DB.Create(&unknownUser)
var result1 TestNullableBool
if err := DB.First(&result1, "name = ?", "Athaariq Ardhiansyah").Error; err != nil {
t.Fatal("Cannot read boolean test record of \"Athaariq Ardhiansyah\"")
}
tests.AssertEqual(t, result1, checkedUser)
var result2 TestNullableBool
if err := DB.First(&result2, "name = ?", "Bad Cat").Error; err != nil {
t.Fatal("Cannot read boolean test record of \"Bad Cat\"")
}
tests.AssertEqual(t, result2, uncheckedUser)
var result3 TestNullableBool
if err := DB.First(&result3, "name = ?", "Charles Robinson").Error; err != nil {
t.Fatal("Cannot read boolean test record of \"Charles Robinson\"")
}
tests.AssertEqual(t, result3, unknownUser)
}