forked from Thor-x86/nullable
-
Notifications
You must be signed in to change notification settings - Fork 2
/
int_test.go
220 lines (173 loc) · 5.19 KB
/
int_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package nullable_test
import (
"testing"
"github.com/tee8z/nullable"
"gorm.io/gorm/utils/tests"
)
func TestScanInt(t *testing.T) {
nullableInt := nullable.NewInt(nil)
// uint8
nullableInt.Scan(37)
tests.AssertEqual(t, nullableInt.Get(), 37)
// int8
nullableInt.Scan(-37)
tests.AssertEqual(t, nullableInt.Get(), -37)
// uint16
nullableInt.Scan(1234)
tests.AssertEqual(t, nullableInt.Get(), 1234)
// int16
nullableInt.Scan(-1234)
tests.AssertEqual(t, nullableInt.Get(), -1234)
// uint32
nullableInt.Scan(654321)
tests.AssertEqual(t, nullableInt.Get(), 654321)
// int32
nullableInt.Scan(-654321)
tests.AssertEqual(t, nullableInt.Get(), -654321)
// uint64
nullableInt.Scan(50000000000)
tests.AssertEqual(t, nullableInt.Get(), 50000000000)
// int64
nullableInt.Scan(-50000000000)
tests.AssertEqual(t, nullableInt.Get(), -50000000000)
nullableInt.Scan(nil)
tests.AssertEqual(t, nullableInt.Get(), nil)
}
func TestNewInt(t *testing.T) {
// uint8
var basicInt1 int = 37
nullableInt1 := nullable.NewInt(&basicInt1)
tests.AssertEqual(t, nullableInt1.Get(), 37)
// int8
var basicInt2 int = -37
nullableInt2 := nullable.NewInt(&basicInt2)
tests.AssertEqual(t, nullableInt2.Get(), -37)
// uint16
var basicInt3 int = 1234
nullableInt3 := nullable.NewInt(&basicInt3)
tests.AssertEqual(t, nullableInt3.Get(), 1234)
// int16
var basicInt4 int = -1234
nullableInt4 := nullable.NewInt(&basicInt4)
tests.AssertEqual(t, nullableInt4.Get(), -1234)
// uint32
var basicInt5 int = 654321
nullableInt5 := nullable.NewInt(&basicInt5)
tests.AssertEqual(t, nullableInt5.Get(), 654321)
// int32
var basicInt6 int = -654321
nullableInt6 := nullable.NewInt(&basicInt6)
tests.AssertEqual(t, nullableInt6.Get(), -654321)
// uint64
var basicInt7 int = 50000000000
nullableInt7 := nullable.NewInt(&basicInt7)
tests.AssertEqual(t, nullableInt7.Get(), 50000000000)
// int64
var basicInt8 int = -50000000000
nullableInt8 := nullable.NewInt(&basicInt8)
tests.AssertEqual(t, nullableInt8.Get(), -50000000000)
}
func TestSetInt(t *testing.T) {
nullableInt := nullable.NewInt(nil)
tests.AssertEqual(t, nullableInt.Get(), nil)
// uint8
var basicInt1 int = 37
nullableInt.Set(&basicInt1)
tests.AssertEqual(t, nullableInt.Get(), 37)
// int8
var basicInt2 int = -37
nullableInt.Set(&basicInt2)
tests.AssertEqual(t, nullableInt.Get(), -37)
// uint16
var basicInt3 int = 1234
nullableInt.Set(&basicInt3)
tests.AssertEqual(t, nullableInt.Get(), 1234)
// int16
var basicInt4 int = -1234
nullableInt.Set(&basicInt4)
tests.AssertEqual(t, nullableInt.Get(), -1234)
// uint32
var basicInt5 int = 654321
nullableInt.Set(&basicInt5)
tests.AssertEqual(t, nullableInt.Get(), 654321)
// int32
var basicInt6 int = -654321
nullableInt.Set(&basicInt6)
tests.AssertEqual(t, nullableInt.Get(), -654321)
// uint64
var basicInt7 int = 50000000000
nullableInt.Set(&basicInt7)
tests.AssertEqual(t, nullableInt.Get(), 50000000000)
// int64
var basicInt8 int = -50000000000
nullableInt.Set(&basicInt8)
tests.AssertEqual(t, nullableInt.Get(), -50000000000)
nullableInt.Set(nil)
tests.AssertEqual(t, nullableInt.Get(), nil)
}
func TestJSONInt(t *testing.T) {
var basicInt1 int = 37
marshalUnmarshalJSON(t, nullable.NewInt(&basicInt1))
var basicInt2 int = -37
marshalUnmarshalJSON(t, nullable.NewInt(&basicInt2))
var basicInt3 int = 1234
marshalUnmarshalJSON(t, nullable.NewInt(&basicInt3))
var basicInt4 int = -1234
marshalUnmarshalJSON(t, nullable.NewInt(&basicInt4))
var basicInt5 int = 654321
marshalUnmarshalJSON(t, nullable.NewInt(&basicInt5))
var basicInt6 int = -654321
marshalUnmarshalJSON(t, nullable.NewInt(&basicInt6))
var basicInt7 int = 50000000000
marshalUnmarshalJSON(t, nullable.NewInt(&basicInt7))
var basicInt8 int = -50000000000
marshalUnmarshalJSON(t, nullable.NewInt(&basicInt8))
marshalUnmarshalJSON(t, nullable.NewInt(nil))
}
func TestInt(t *testing.T) {
type TestNullableInt struct {
ID uint
Name string
Value nullable.Int
Unit string
}
DB.Migrator().DropTable(&TestNullableInt{})
if err := DB.Migrator().AutoMigrate(&TestNullableInt{}); err != nil {
t.Errorf("failed to migrate nullable int, got error: %v", err)
}
matterEnergy := 50000000000
matter := TestNullableInt{
Name: "matter",
Value: nullable.NewInt(&matterEnergy),
Unit: "Joule",
}
DB.Create(&matter)
antimatterEnergy := -50000000000
antimatter := TestNullableInt{
Name: "antimatter",
Value: nullable.NewInt(&antimatterEnergy),
Unit: "Joule",
}
DB.Create(&antimatter)
neutron := TestNullableInt{
Name: "neutron",
Value: nullable.NewInt(nil),
Unit: "Joule",
}
DB.Create(&neutron)
var result1 TestNullableInt
if err := DB.First(&result1, "name = ?", "matter").Error; err != nil {
t.Fatal("Cannot read int test record of \"matter\"")
}
tests.AssertEqual(t, result1, matter)
var result2 TestNullableInt
if err := DB.First(&result2, "name = ?", "antimatter").Error; err != nil {
t.Fatal("Cannot read int test record of \"antimatter\"")
}
tests.AssertEqual(t, result2, antimatter)
var result3 TestNullableInt
if err := DB.First(&result3, "name = ?", "neutron").Error; err != nil {
t.Fatal("Cannot read int test record of \"neutron\"")
}
tests.AssertEqual(t, result3, neutron)
}