Skip to content

Commit 84a2b9b

Browse files
authored
Merge pull request #1505 from ydb-platform/add-test-old-uuid-serialization
canonization of current uuid behavior
2 parents 848edf6 + 2f4905c commit 84a2b9b

File tree

2 files changed

+281
-0
lines changed

2 files changed

+281
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//go:build integration
2+
// +build integration
3+
4+
package integration
5+
6+
import (
7+
"testing"
8+
9+
"github.com/google/uuid"
10+
"github.com/stretchr/testify/require"
11+
12+
"github.com/ydb-platform/ydb-go-sdk/v3"
13+
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/tx"
14+
"github.com/ydb-platform/ydb-go-sdk/v3/query"
15+
)
16+
17+
func TestUUIDSerializationQueryServiceIssue1501(t *testing.T) {
18+
// https://github.com/ydb-platform/ydb-go-sdk/issues/1501
19+
// test with special uuid - all bytes are different for check any byte swaps
20+
21+
t.Run("old-send", func(t *testing.T) {
22+
// test old behavior - for test way of safe work with data, written with bagged API version
23+
var (
24+
scope = newScope(t)
25+
ctx = scope.Ctx
26+
db = scope.Driver()
27+
)
28+
29+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
30+
expectedResultWithBug := "2d9e498b-b746-9cfb-084d-de4e1cb4736e"
31+
id := uuid.MustParse(idString)
32+
row, err := db.Query().QueryRow(ctx, `
33+
DECLARE $val AS UUID;
34+
35+
SELECT CAST($val AS Utf8)`,
36+
query.WithIdempotent(),
37+
query.WithParameters(ydb.ParamsBuilder().Param("$val").UUID(id).Build()),
38+
query.WithTxControl(tx.SerializableReadWriteTxControl()),
39+
)
40+
41+
require.NoError(t, err)
42+
43+
var res string
44+
45+
err = row.Scan(&res)
46+
require.NoError(t, err)
47+
require.Equal(t, expectedResultWithBug, res)
48+
})
49+
t.Run("old-receive-to-bytes", func(t *testing.T) {
50+
// test old behavior - for test way of safe work with data, written with bagged API version
51+
var (
52+
scope = newScope(t)
53+
ctx = scope.Ctx
54+
db = scope.Driver()
55+
)
56+
57+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
58+
expectedResultWithBug := "8b499e2d-46b7-fb9c-4d08-4ede6e73b41c"
59+
row, err := db.Query().QueryRow(ctx, `
60+
DECLARE $val AS Text;
61+
62+
SELECT CAST($val AS UUID)`,
63+
query.WithIdempotent(),
64+
query.WithParameters(ydb.ParamsBuilder().Param("$val").Text(idString).Build()),
65+
query.WithTxControl(tx.SerializableReadWriteTxControl()),
66+
)
67+
68+
require.NoError(t, err)
69+
70+
var res [16]byte
71+
72+
err = row.Scan(&res)
73+
require.NoError(t, err)
74+
75+
resUUID := uuid.UUID(res)
76+
require.Equal(t, expectedResultWithBug, resUUID.String())
77+
})
78+
t.Run("old-receive-to-string", func(t *testing.T) {
79+
// test old behavior - for test way of safe work with data, written with bagged API version
80+
var (
81+
scope = newScope(t)
82+
ctx = scope.Ctx
83+
db = scope.Driver()
84+
)
85+
86+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
87+
expectedResultWithBug := []byte{0x8b, 0x49, 0x9e, 0x2d, 0x46, 0xb7, 0xfb, 0x9c, 0x4d, 0x8, 0x4e, 0xde, 0x6e, 0x73, 0xb4, 0x1c}
88+
row, err := db.Query().QueryRow(ctx, `
89+
DECLARE $val AS Text;
90+
91+
SELECT CAST($val AS UUID)`,
92+
query.WithIdempotent(),
93+
query.WithParameters(ydb.ParamsBuilder().Param("$val").Text(idString).Build()),
94+
query.WithTxControl(tx.SerializableReadWriteTxControl()),
95+
)
96+
97+
require.NoError(t, err)
98+
99+
var res string
100+
101+
err = row.Scan(&res)
102+
require.NoError(t, err)
103+
104+
require.Equal(t, expectedResultWithBug, []byte(res))
105+
})
106+
t.Run("send-receive", func(t *testing.T) {
107+
// test old behavior - for test way of safe work with data, written with bagged API version
108+
var (
109+
scope = newScope(t)
110+
ctx = scope.Ctx
111+
db = scope.Driver()
112+
)
113+
114+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
115+
id := uuid.MustParse(idString)
116+
row, err := db.Query().QueryRow(ctx, `
117+
DECLARE $val AS UUID;
118+
119+
SELECT $val`,
120+
query.WithIdempotent(),
121+
query.WithParameters(ydb.ParamsBuilder().Param("$val").UUID(id).Build()),
122+
query.WithTxControl(tx.SerializableReadWriteTxControl()),
123+
)
124+
125+
require.NoError(t, err)
126+
127+
var resBytes [16]byte
128+
err = row.Scan(&resBytes)
129+
require.NoError(t, err)
130+
131+
resUUID := uuid.UUID(resBytes)
132+
133+
require.Equal(t, id, resUUID)
134+
})
135+
}

tests/integration/table_regression_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"github.com/google/uuid"
1314
"github.com/stretchr/testify/require"
1415
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1516

@@ -113,3 +114,148 @@ func TestRegressionIssue1227RetryBadSession(t *testing.T) {
113114
require.NoError(t, err)
114115
require.EqualValues(t, 100, cnt)
115116
}
117+
118+
func TestUUIDSerializationTableServiceServiceIssue1501(t *testing.T) {
119+
// https://github.com/ydb-platform/ydb-go-sdk/issues/1501
120+
// test with special uuid - all bytes are different for check any byte swaps
121+
122+
t.Run("old-send", func(t *testing.T) {
123+
// test old behavior - for test way of safe work with data, written with bagged API version
124+
var (
125+
scope = newScope(t)
126+
ctx = scope.Ctx
127+
db = scope.Driver()
128+
)
129+
130+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
131+
expectedResultWithBug := "2d9e498b-b746-9cfb-084d-de4e1cb4736e"
132+
id := uuid.MustParse(idString)
133+
134+
var idFromDB string
135+
err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
136+
res, err := tx.Execute(ctx, `
137+
DECLARE $val AS UUID;
138+
139+
SELECT CAST($val AS Utf8)
140+
`, table.NewQueryParameters(table.ValueParam("$val", types.UUIDValue(id))))
141+
if err != nil {
142+
return err
143+
}
144+
res.NextResultSet(ctx)
145+
res.NextRow()
146+
147+
err = res.Scan(&idFromDB)
148+
return err
149+
})
150+
require.NoError(t, err)
151+
require.Equal(t, expectedResultWithBug, idFromDB)
152+
})
153+
t.Run("old-receive-to-bytes", func(t *testing.T) {
154+
// test old behavior - for test way of safe work with data, written with bagged API version
155+
var (
156+
scope = newScope(t)
157+
ctx = scope.Ctx
158+
db = scope.Driver()
159+
)
160+
161+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
162+
expectedResultWithBug := "8b499e2d-46b7-fb9c-4d08-4ede6e73b41c"
163+
var resultFromDb uuid.UUID
164+
err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
165+
res, err := tx.Execute(ctx, `
166+
DECLARE $val AS Text;
167+
168+
SELECT CAST($val AS UUID)
169+
`, table.NewQueryParameters(table.ValueParam("$val", types.TextValue(idString))))
170+
if err != nil {
171+
return err
172+
}
173+
174+
res.NextResultSet(ctx)
175+
res.NextRow()
176+
177+
var resBytes [16]byte
178+
err = res.Scan(&resBytes)
179+
if err != nil {
180+
return err
181+
}
182+
183+
resultFromDb = resBytes
184+
return nil
185+
})
186+
187+
require.NoError(t, err)
188+
require.Equal(t, expectedResultWithBug, resultFromDb.String())
189+
})
190+
t.Run("old-receive-to-string", func(t *testing.T) {
191+
// test old behavior - for test way of safe work with data, written with bagged API version
192+
var (
193+
scope = newScope(t)
194+
ctx = scope.Ctx
195+
db = scope.Driver()
196+
)
197+
198+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
199+
expectedResultWithBug := []byte{0x8b, 0x49, 0x9e, 0x2d, 0x46, 0xb7, 0xfb, 0x9c, 0x4d, 0x8, 0x4e, 0xde, 0x6e, 0x73, 0xb4, 0x1c}
200+
var resultFromDb string
201+
err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
202+
res, err := tx.Execute(ctx, `
203+
DECLARE $val AS Text;
204+
205+
SELECT CAST($val AS UUID)
206+
`, table.NewQueryParameters(table.ValueParam("$val", types.TextValue(idString))))
207+
if err != nil {
208+
return err
209+
}
210+
211+
res.NextResultSet(ctx)
212+
res.NextRow()
213+
214+
err = res.ScanWithDefaults(&resultFromDb)
215+
if err != nil {
216+
return err
217+
}
218+
219+
return nil
220+
})
221+
222+
require.NoError(t, err)
223+
resultBytes := []byte(resultFromDb)
224+
require.Equal(t, expectedResultWithBug, resultBytes)
225+
})
226+
t.Run("old-send-receive", func(t *testing.T) {
227+
// test old behavior - for test way of safe work with data, written with bagged API version
228+
var (
229+
scope = newScope(t)
230+
ctx = scope.Ctx
231+
db = scope.Driver()
232+
)
233+
234+
idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
235+
id := uuid.MustParse(idString)
236+
237+
var idFromDB uuid.UUID
238+
err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
239+
res, err := tx.Execute(ctx, `
240+
DECLARE $val AS UUID;
241+
242+
SELECT $val
243+
`, table.NewQueryParameters(table.ValueParam("$val", types.UUIDValue(id))))
244+
if err != nil {
245+
return err
246+
}
247+
res.NextResultSet(ctx)
248+
res.NextRow()
249+
250+
var resBytes [16]byte
251+
err = res.Scan(&resBytes)
252+
if err != nil {
253+
return err
254+
}
255+
idFromDB = resBytes
256+
return nil
257+
})
258+
require.NoError(t, err)
259+
require.Equal(t, id, idFromDB)
260+
})
261+
}

0 commit comments

Comments
 (0)