1 package pgtype_test
2
3 import (
4 "context"
5 "reflect"
6 "testing"
7
8 "github.com/jackc/pgx/v5/pgtype"
9 "github.com/jackc/pgx/v5/pgxtest"
10 "github.com/stretchr/testify/require"
11 )
12
13 func TestUUIDCodec(t *testing.T) {
14 pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "uuid", []pgxtest.ValueRoundTripTest{
15 {
16 pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
17 new(pgtype.UUID),
18 isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
19 },
20 {
21 "00010203-0405-0607-0809-0a0b0c0d0e0f",
22 new(pgtype.UUID),
23 isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
24 },
25 {
26 "000102030405060708090a0b0c0d0e0f",
27 new(pgtype.UUID),
28 isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
29 },
30 {
31 pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true},
32 new(string),
33 isExpectedEq("00010203-0405-0607-0809-0a0b0c0d0e0f"),
34 },
35 {pgtype.UUID{}, new([]byte), isExpectedEqBytes([]byte(nil))},
36 {pgtype.UUID{}, new(pgtype.UUID), isExpectedEq(pgtype.UUID{})},
37 {nil, new(pgtype.UUID), isExpectedEq(pgtype.UUID{})},
38 })
39
40 pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, pgxtest.KnownOIDQueryExecModes, "uuid", []pgxtest.ValueRoundTripTest{
41 {
42 [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
43 new(pgtype.UUID),
44 isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
45 },
46 {
47 []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
48 new(pgtype.UUID),
49 isExpectedEq(pgtype.UUID{Bytes: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, Valid: true}),
50 },
51 })
52 }
53
54 func TestUUID_MarshalJSON(t *testing.T) {
55 tests := []struct {
56 name string
57 src pgtype.UUID
58 want []byte
59 }{
60 {
61 name: "first",
62 src: pgtype.UUID{
63 Bytes: [16]byte{29, 72, 90, 122, 109, 24, 69, 153, 140, 108, 52, 66, 86, 22, 136, 122},
64 Valid: true,
65 },
66 want: []byte(`"1d485a7a-6d18-4599-8c6c-34425616887a"`),
67 },
68 {
69 name: "third",
70 src: pgtype.UUID{
71 Bytes: [16]byte{},
72 },
73 want: []byte("null"),
74 },
75 }
76 for _, tt := range tests {
77 t.Run(tt.name, func(t *testing.T) {
78 got, err := tt.src.MarshalJSON()
79 require.NoError(t, err)
80 if !reflect.DeepEqual(got, tt.want) {
81 t.Errorf("MarshalJSON() got = %v, want %v", got, tt.want)
82 }
83 })
84 }
85 }
86
87 func TestUUID_UnmarshalJSON(t *testing.T) {
88 tests := []struct {
89 name string
90 want *pgtype.UUID
91 src []byte
92 wantErr bool
93 }{
94 {
95 name: "first",
96 want: &pgtype.UUID{
97 Bytes: [16]byte{29, 72, 90, 122, 109, 24, 69, 153, 140, 108, 52, 66, 86, 22, 136, 122},
98 Valid: true,
99 },
100 src: []byte(`"1d485a7a-6d18-4599-8c6c-34425616887a"`),
101 wantErr: false,
102 },
103 {
104 name: "second",
105 want: &pgtype.UUID{
106 Bytes: [16]byte{},
107 },
108 src: []byte("null"),
109 wantErr: false,
110 },
111 {
112 name: "third",
113 want: &pgtype.UUID{
114 Bytes: [16]byte{},
115 Valid: false,
116 },
117 src: []byte("1d485a7a-6d18-4599-8c6c-34425616887a"),
118 wantErr: true,
119 },
120 }
121 for _, tt := range tests {
122 t.Run(tt.name, func(t *testing.T) {
123 got := &pgtype.UUID{}
124 if err := got.UnmarshalJSON(tt.src); (err != nil) != tt.wantErr {
125 t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
126 }
127 if !reflect.DeepEqual(got, tt.want) {
128 t.Errorf("UnmarshalJSON() got = %v, want %v", got, tt.want)
129 }
130 })
131 }
132 }
133
View as plain text