-
Notifications
You must be signed in to change notification settings - Fork 6
/
id_test.go
144 lines (112 loc) · 2.63 KB
/
id_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
package hide
import (
"database/sql/driver"
"encoding/json"
"testing"
)
type testStruct struct {
Id ID `json:"id"`
Test string `json:"test"`
}
func TestMarshalID(t *testing.T) {
id := ID(123)
expected := `"beJarVNaQM"`
out, err := json.Marshal(id)
if err != nil {
t.Fatal(err)
}
if string(out) != expected {
t.Fatalf("Expected marshalled ID to be %v, but was: %v", expected, string(out))
}
}
func TestUnmarshalID(t *testing.T) {
in := `"beJarVNaQM"`
var id ID
expected := ID(123)
err := id.UnmarshalJSON([]byte(in))
if err != nil {
t.Fatal(err)
}
if id != expected {
t.Fatalf("Expected unmarshalled ID to be %v, but was: %v", expected, id)
}
}
func TestMarshalUnmarshalMatch(t *testing.T) {
id := ID(123)
marshalled, err := id.MarshalJSON()
if err != nil {
t.Fatal(err)
}
id = ID(0)
if err := id.UnmarshalJSON(marshalled); err != nil {
t.Fatal(err)
}
if id != ID(123) {
t.Fatalf("Marshal unmarshal mismatch, expected %v but was: %v", ID(123), id)
}
}
func TestMarshalIDStruct(t *testing.T) {
in := testStruct{ID(123), "struct"}
expected := `{"id":"beJarVNaQM","test":"struct"}`
out, err := json.Marshal(in)
if err != nil {
t.Fatal(err)
}
if string(out) != expected {
t.Fatalf("Expected marshalled struct to be %v, but was: %v", expected, string(out))
}
}
func TestUnmarshalIDStruct(t *testing.T) {
in := `{"id":"beJarVNaQM","test":"struct"}`
var out testStruct
expected := testStruct{123, "struct"}
err := json.Unmarshal([]byte(in), &out)
if err != nil {
t.Fatal(err)
}
if out != expected {
t.Fatalf("Expected unmarshalled struct to be %v, but was: %v", expected, out)
}
}
func TestScan(t *testing.T) {
var id ID
value := int64(123)
if err := id.Scan(value); err != nil {
t.Fatal(err)
}
if id != 123 {
t.Fatalf("ID must have been set to value by scan, but was: %v", id)
}
}
func TestValue(t *testing.T) {
id := ID(123)
driverValue, err := id.Value()
if err != nil {
t.Fatal(err)
}
_, ok := driverValue.(int64)
if !ok {
t.Fatal("Driver value must be of type int64")
}
}
func TestNull(t *testing.T) {
var id ID
out, _ := id.MarshalJSON()
expected := "null"
if string(out) != expected {
t.Fatalf("Expected null ID to be '%v', but was: %v", expected, string(out))
}
value, _ := id.Value()
if value != driver.Value(nil) {
t.Fatalf("Expected null ID to be driver.Value nil, but was: %v", value)
}
}
func TestUnmarshalNull(t *testing.T) {
in := `{"id":null}`
out := &struct {
Id ID `json:"id"`
}{}
if err := json.Unmarshal([]byte(in), out); err != nil || out.Id != 0 {
t.Fatalf("Expected null to be unmarshalled to 0, but was: %v %v", err, out.Id)
}
}