Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

canonization of current uuid behavior #1505

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions tests/integration/query_regression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//go:build integration
// +build integration

package integration

import (
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/ydb-platform/ydb-go-sdk/v3"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/tx"
"github.com/ydb-platform/ydb-go-sdk/v3/query"
)

func TestUUIDSerializationQueryServiceIssue1501(t *testing.T) {
// https://github.com/ydb-platform/ydb-go-sdk/issues/1501
// test with special uuid - all bytes are different for check any byte swaps

t.Run("old-send", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
scope = newScope(t)
ctx = scope.Ctx
db = scope.Driver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
expectedResultWithBug := "2d9e498b-b746-9cfb-084d-de4e1cb4736e"
id := uuid.MustParse(idString)
row, err := db.Query().QueryRow(ctx, `
DECLARE $val AS UUID;

SELECT CAST($val AS Utf8)`,
query.WithIdempotent(),
query.WithParameters(ydb.ParamsBuilder().Param("$val").UUID(id).Build()),
query.WithTxControl(tx.SerializableReadWriteTxControl()),
)

require.NoError(t, err)

var res string

err = row.Scan(&res)
require.NoError(t, err)
require.Equal(t, expectedResultWithBug, res)
})
t.Run("old-receive-to-bytes", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
scope = newScope(t)
ctx = scope.Ctx
db = scope.Driver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
expectedResultWithBug := "8b499e2d-46b7-fb9c-4d08-4ede6e73b41c"
row, err := db.Query().QueryRow(ctx, `
DECLARE $val AS Text;

SELECT CAST($val AS UUID)`,
query.WithIdempotent(),
query.WithParameters(ydb.ParamsBuilder().Param("$val").Text(idString).Build()),
query.WithTxControl(tx.SerializableReadWriteTxControl()),
)

require.NoError(t, err)

var res [16]byte

err = row.Scan(&res)
require.NoError(t, err)

resUUID := uuid.UUID(res)
require.Equal(t, expectedResultWithBug, resUUID.String())
})
t.Run("old-receive-to-string", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
scope = newScope(t)
ctx = scope.Ctx
db = scope.Driver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
expectedResultWithBug := []byte{0x8b, 0x49, 0x9e, 0x2d, 0x46, 0xb7, 0xfb, 0x9c, 0x4d, 0x8, 0x4e, 0xde, 0x6e, 0x73, 0xb4, 0x1c}
row, err := db.Query().QueryRow(ctx, `
DECLARE $val AS Text;

SELECT CAST($val AS UUID)`,
query.WithIdempotent(),
query.WithParameters(ydb.ParamsBuilder().Param("$val").Text(idString).Build()),
query.WithTxControl(tx.SerializableReadWriteTxControl()),
)

require.NoError(t, err)

var res string

err = row.Scan(&res)
require.NoError(t, err)

require.Equal(t, expectedResultWithBug, []byte(res))
})
t.Run("send-receive", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
scope = newScope(t)
ctx = scope.Ctx
db = scope.Driver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
id := uuid.MustParse(idString)
row, err := db.Query().QueryRow(ctx, `
DECLARE $val AS UUID;

SELECT $val`,
query.WithIdempotent(),
query.WithParameters(ydb.ParamsBuilder().Param("$val").UUID(id).Build()),
query.WithTxControl(tx.SerializableReadWriteTxControl()),
)

require.NoError(t, err)

var resBytes [16]byte
err = row.Scan(&resBytes)
require.NoError(t, err)

resUUID := uuid.UUID(resBytes)

require.Equal(t, id, resUUID)
})
}
146 changes: 146 additions & 0 deletions tests/integration/table_regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"

Expand Down Expand Up @@ -113,3 +114,148 @@ func TestRegressionIssue1227RetryBadSession(t *testing.T) {
require.NoError(t, err)
require.EqualValues(t, 100, cnt)
}

func TestUUIDSerializationTableServiceServiceIssue1501(t *testing.T) {
// https://github.com/ydb-platform/ydb-go-sdk/issues/1501
// test with special uuid - all bytes are different for check any byte swaps

t.Run("old-send", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
scope = newScope(t)
ctx = scope.Ctx
db = scope.Driver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
expectedResultWithBug := "2d9e498b-b746-9cfb-084d-de4e1cb4736e"
id := uuid.MustParse(idString)

var idFromDB string
err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
res, err := tx.Execute(ctx, `
DECLARE $val AS UUID;

SELECT CAST($val AS Utf8)
`, table.NewQueryParameters(table.ValueParam("$val", types.UUIDValue(id))))
if err != nil {
return err
}
res.NextResultSet(ctx)
res.NextRow()

err = res.Scan(&idFromDB)
return err
})
require.NoError(t, err)
require.Equal(t, expectedResultWithBug, idFromDB)
})
t.Run("old-receive-to-bytes", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
scope = newScope(t)
ctx = scope.Ctx
db = scope.Driver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
expectedResultWithBug := "8b499e2d-46b7-fb9c-4d08-4ede6e73b41c"
var resultFromDb uuid.UUID
err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
res, err := tx.Execute(ctx, `
DECLARE $val AS Text;

SELECT CAST($val AS UUID)
`, table.NewQueryParameters(table.ValueParam("$val", types.TextValue(idString))))
if err != nil {
return err
}

res.NextResultSet(ctx)
res.NextRow()

var resBytes [16]byte
err = res.Scan(&resBytes)
if err != nil {
return err
}

resultFromDb = resBytes
return nil
})

require.NoError(t, err)
require.Equal(t, expectedResultWithBug, resultFromDb.String())
})
t.Run("old-receive-to-string", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
scope = newScope(t)
ctx = scope.Ctx
db = scope.Driver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
expectedResultWithBug := []byte{0x8b, 0x49, 0x9e, 0x2d, 0x46, 0xb7, 0xfb, 0x9c, 0x4d, 0x8, 0x4e, 0xde, 0x6e, 0x73, 0xb4, 0x1c}
var resultFromDb string
err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
res, err := tx.Execute(ctx, `
DECLARE $val AS Text;

SELECT CAST($val AS UUID)
`, table.NewQueryParameters(table.ValueParam("$val", types.TextValue(idString))))
if err != nil {
return err
}

res.NextResultSet(ctx)
res.NextRow()

err = res.ScanWithDefaults(&resultFromDb)
if err != nil {
return err
}

return nil
})

require.NoError(t, err)
resultBytes := []byte(resultFromDb)
require.Equal(t, expectedResultWithBug, resultBytes)
})
t.Run("old-send-receive", func(t *testing.T) {
// test old behavior - for test way of safe work with data, written with bagged API version
var (
scope = newScope(t)
ctx = scope.Ctx
db = scope.Driver()
)

idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B"
id := uuid.MustParse(idString)

var idFromDB uuid.UUID
err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
res, err := tx.Execute(ctx, `
DECLARE $val AS UUID;

SELECT $val
`, table.NewQueryParameters(table.ValueParam("$val", types.UUIDValue(id))))
if err != nil {
return err
}
res.NextResultSet(ctx)
res.NextRow()

var resBytes [16]byte
err = res.Scan(&resBytes)
if err != nil {
return err
}
idFromDB = resBytes
return nil
})
require.NoError(t, err)
require.Equal(t, id, idFromDB)
})
}
Loading