-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding_test.go
231 lines (196 loc) · 5.88 KB
/
encoding_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
221
222
223
224
225
226
227
228
229
230
231
package typeid
import (
"bytes"
"database/sql"
"encoding/json"
"reflect"
"strings"
"testing"
"github.com/jackc/pgx/v5/pgtype"
)
func TestTypeID_Pgx_Scan(t *testing.T) {
t.Parallel()
userIDStr := "user_01HCJF4N2RER3R6SZHBPFENHVA"
_ = pgtype.Text{Valid: true, String: userIDStr}
pgtypeMap := pgtype.NewMap()
codec := pgtype.TextCodec{}
t.Run("pgx binary scan", func(t *testing.T) {
t.Parallel()
t.Run("valid string bytes", func(t *testing.T) {
var target UserID
err := codec.PlanScan(pgtypeMap, pgtype.TextOID, pgtype.BinaryFormatCode, &target).
Scan([]byte(userIDStr), &target)
if err != nil {
t.Fatalf("can scan id string into typeid.TypeID: unexpected error:\n%+v", err)
}
if userIDStr != target.String() {
t.Errorf("scanned typeid.TypeID has the correct uuid string: expected %s, got %s", userIDStr, target.String())
}
})
t.Run("error on invalid char count", func(t *testing.T) {
var target UserID
err := codec.PlanScan(pgtypeMap, pgtype.UUIDOID, pgtype.BinaryFormatCode, &target).
Scan([]byte{23, 12, 125, 54}, &target)
if err == nil {
t.Error("must error if byte count is not 16")
}
})
})
t.Run("pgx text scan", func(t *testing.T) {
t.Parallel()
var target UserID
err := codec.PlanScan(pgtypeMap, pgtype.TextOID, pgtype.TextFormatCode, &target).
Scan([]byte(userIDStr), &target)
if err != nil {
t.Fatalf("can scan id string into typeid.TypeID: unexpected error:\n%+v", err)
}
if userIDStr != target.String() {
t.Errorf("scanned typeid.TypeID has the correct uuid string: expected %s, got %s", userIDStr, target.String())
}
})
t.Run("error on invalid byte count", func(t *testing.T) {
t.Parallel()
var target UserID
err := codec.PlanScan(pgtypeMap, pgtype.UUIDOID, pgtype.BinaryFormatCode, &target).
Scan([]byte("2345-2222-111-222"), &target)
if err == nil {
t.Error("must error if byte count is not 16")
}
})
t.Run("error on nil scan", func(t *testing.T) {
t.Parallel()
var target UserID
err := codec.PlanScan(pgtypeMap, pgtype.UUIDOID, pgtype.BinaryFormatCode, &target).
Scan(nil, &target)
if err == nil {
t.Error("must error on a nil scan")
}
if !strings.Contains(err.Error(), "cannot scan NULL into *typeid.TypeID") {
t.Error("error must be cannot scan NULL into *typeid.TypeID")
}
})
}
func TestTypeID_Pgx_Value(t *testing.T) {
t.Parallel()
original, err := New[UserID]()
if err != nil {
t.Fatalf("create UserID: unexpected error:\n%+v", err)
}
pgtypeMap := pgtype.NewMap()
codec := pgtype.TextCodec{}
t.Run("binary encoding", func(t *testing.T) {
t.Parallel()
var buf []byte
newBuf, err := codec.PlanEncode(pgtypeMap, pgtype.TextOID, pgtype.BinaryFormatCode, original).
Encode(original, buf)
if err != nil {
t.Fatalf("binary encoding: unexpected error:\n%+v", err)
}
if !bytes.Equal([]byte(original.String()), newBuf) {
t.Errorf("binary encoding should return the uuid bytes: expected %v, got %v", []byte(original.String()), newBuf)
}
})
t.Run("text encoding", func(t *testing.T) {
t.Parallel()
var buf []byte
newBuf, err := codec.PlanEncode(pgtypeMap, pgtype.TextOID, pgtype.TextFormatCode, original).
Encode(original, buf)
if err != nil {
t.Fatalf("text encoding: unexpected error:\n%+v", err)
}
if !bytes.Equal([]byte(original.String()), newBuf) {
t.Errorf("text encoding should return the uuid string representation: expected %s, got %s", original.String(), newBuf)
}
})
}
func TestTypeID_SQL_Scan(t *testing.T) {
t.Parallel()
original, err := New[UserID]()
if err != nil {
t.Fatalf("create UserID: unexpected error:\n%+v", err)
}
scannerType := reflect.TypeOf((*sql.Scanner)(nil)).Elem()
if !reflect.TypeOf(&UserID{}).Implements(scannerType) {
t.Fatalf("typeid.TypeID instantiation implements the `sql.Scanner` interface")
}
str := original.String()
otherPrefixID, err := New[AccountID]()
if err != nil {
t.Fatalf("create AccountID: unexpected error:\n%+v", err)
}
for _, tt := range []struct {
name string
input any
shouldFail bool
}{
{
name: "scan id string type",
input: str,
},
{
name: "fail on invalid type prefix",
input: otherPrefixID.String(),
shouldFail: true,
},
{
name: "fail on non invalid type",
input: 12345,
shouldFail: true,
},
} {
tc := tt
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var scannedID UserID
err := (&scannedID).Scan(tc.input)
if tc.shouldFail {
if err == nil {
t.Fatalf("scan should fail (expected error %+v)", err)
}
return
}
if err != nil {
t.Fatalf("scan should succeed (unexpected error %+v)", err)
}
if original != scannedID {
t.Errorf("scanned TypeId is equal to the original one: expected %v, got %v", original, scannedID)
}
})
}
}
func TestTypeID_SQL_Value(t *testing.T) {
t.Parallel()
id, err := New[UserID]()
if err != nil {
t.Fatalf("create UserID: unexpected error:\n%+v", err)
}
val, err := id.Value()
if err != nil {
t.Fatalf("value should succeed (unexpected error %+v)", err)
}
if id.String() != val {
t.Errorf("value should return the uuid string: expected %s, got %s", id.String(), val)
}
}
func TestJSON(t *testing.T) {
str := "account_00041061050r3gg28a1c60t3gf"
tid := Must(FromString[AccountID](str))
encoded, err := json.Marshal(tid)
if err != nil {
t.Fatalf("unexpected error:\n%+v", err)
}
if `"`+str+`"` != string(encoded) {
t.Fatalf("json encoding should return the uuid string: expected %s, got %s", `"`+str+`"`, string(encoded))
}
var decoded AccountID
err = json.Unmarshal(encoded, &decoded)
if err != nil {
t.Fatalf("unexpected error:\n%+v", err)
}
if tid != decoded {
t.Errorf("json decoding should return the original uuid: expected %v, got %v", tid, decoded)
}
if str != decoded.String() {
t.Errorf("json decoding should return the original uuid string: expected %s, got %s", str, decoded.String())
}
}