forked from tee8z/nullable
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uint64_test.go
140 lines (111 loc) · 3.41 KB
/
uint64_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
package nullable_test
import (
"testing"
"github.com/tee8z/nullable"
"gorm.io/gorm/utils/tests"
)
func TestScanUint64(t *testing.T) {
nullableInt := nullable.NewUint64(nil)
// uint8
nullableInt.Scan(37)
tests.AssertEqual(t, nullableInt.Get(), 37)
// uint16
nullableInt.Scan(1234)
tests.AssertEqual(t, nullableInt.Get(), 1234)
// uint32
nullableInt.Scan(654321)
tests.AssertEqual(t, nullableInt.Get(), 654321)
// uint64
nullableInt.Scan(50000000000)
tests.AssertEqual(t, nullableInt.Get(), 50000000000)
nullableInt.Scan(nil)
tests.AssertEqual(t, nullableInt.Get(), nil)
}
func TestNewUint64(t *testing.T) {
// uint8
var basicUint1 uint64 = 37
nullableUint1 := nullable.NewUint64(&basicUint1)
tests.AssertEqual(t, nullableUint1.Get(), 37)
// uint16
var basicUint2 uint64 = 1234
nullableUint2 := nullable.NewUint64(&basicUint2)
tests.AssertEqual(t, nullableUint2.Get(), 1234)
// uint32
var basicUint3 uint64 = 654321
nullableUint3 := nullable.NewUint64(&basicUint3)
tests.AssertEqual(t, nullableUint3.Get(), 654321)
// uint64
var basicUint4 uint64 = 50000000000
nullableUint4 := nullable.NewUint64(&basicUint4)
tests.AssertEqual(t, nullableUint4.Get(), 50000000000)
nullableUint5 := nullable.NewUint64(nil)
tests.AssertEqual(t, nullableUint5.Get(), nil)
}
func TestSetUint64(t *testing.T) {
nullableUint := nullable.NewUint64(nil)
tests.AssertEqual(t, nullableUint.Get(), nil)
// uint8
var basicUint1 uint64 = 37
nullableUint.Set(&basicUint1)
tests.AssertEqual(t, nullableUint.Get(), 37)
// uint16
var basicUint2 uint64 = 1234
nullableUint.Set(&basicUint2)
tests.AssertEqual(t, nullableUint.Get(), 1234)
// uint32
var basicUint3 uint64 = 654321
nullableUint.Set(&basicUint3)
tests.AssertEqual(t, nullableUint.Get(), 654321)
// uint64
var basicUint4 uint64 = 50000000000
nullableUint.Set(&basicUint4)
tests.AssertEqual(t, nullableUint.Get(), 50000000000)
nullableUint.Set(nil)
tests.AssertEqual(t, nullableUint.Get(), nil)
}
func TestJSONUint64(t *testing.T) {
var basicInt1 uint64 = 37
marshalUnmarshalJSON(t, nullable.NewUint64(&basicInt1))
var basicInt2 uint64 = 1234
marshalUnmarshalJSON(t, nullable.NewUint64(&basicInt2))
var basicInt3 uint64 = 654321
marshalUnmarshalJSON(t, nullable.NewUint64(&basicInt3))
var basicInt4 uint64 = 50000000000
marshalUnmarshalJSON(t, nullable.NewUint64(&basicInt4))
marshalUnmarshalJSON(t, nullable.NewUint64(nil))
}
func TestUint64(t *testing.T) {
type TestNullableUint64 struct {
ID uint64
Name string
Value nullable.Uint64
Unit string
}
DB.Migrator().DropTable(&TestNullableUint64{})
if err := DB.Migrator().AutoMigrate(&TestNullableUint64{}); err != nil {
t.Errorf("failed to migrate nullable uint64, got error: %v", err)
}
var protonEnergy uint64 = 50000000000
proton := TestNullableUint64{
Name: "proton",
Value: nullable.NewUint64(&protonEnergy),
Unit: "Joule",
}
DB.Create(&proton)
neutron := TestNullableUint64{
Name: "neutron",
Value: nullable.NewUint64(nil),
Unit: "Joule",
}
DB.Create(&neutron)
var result1 TestNullableUint64
if err := DB.First(&result1, "name = ?", "proton").Error; err != nil {
t.Fatal("Cannot read uint64 test record of \"proton\"")
}
tests.AssertEqual(t, result1, proton)
var result2 TestNullableUint64
if err := DB.First(&result2, "name = ?", "neutron").Error; err != nil {
t.Fatal("Cannot read uint64 test record of \"neutron\"")
}
tests.AssertEqual(t, result2, neutron)
}