From c3bd1215ccd2dfb952c8d9d133815edc3c2fa0e2 Mon Sep 17 00:00:00 2001 From: Timofey Koolin Date: Thu, 17 Oct 2024 19:31:00 +0300 Subject: [PATCH 1/2] add database uuid tests --- .../database_sql_regression_test.go | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/tests/integration/database_sql_regression_test.go b/tests/integration/database_sql_regression_test.go index 9abacdfb2..b310f254d 100644 --- a/tests/integration/database_sql_regression_test.go +++ b/tests/integration/database_sql_regression_test.go @@ -12,6 +12,7 @@ import ( "testing" "time" + "github.com/google/uuid" "github.com/stretchr/testify/require" "github.com/ydb-platform/ydb-go-genproto/protos/Ydb" @@ -167,3 +168,108 @@ func TestRegressionKikimr17104(t *testing.T) { }) }) } + +func TestUUIDSerializationDatabaseSQLIssue1501(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) + db = scope.SQLDriver() + ) + + idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B" + expectedResultWithBug := "2d9e498b-b746-9cfb-084d-de4e1cb4736e" + id := [16]byte(uuid.MustParse(idString)) + row := db.QueryRow(` +DECLARE $val AS UUID; + +SELECT CAST($val AS Utf8)`, sql.Named("val", id), + ) + + require.NoError(t, row.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) + db = scope.SQLDriver() + ) + + idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B" + expectedResultWithBug := "8b499e2d-46b7-fb9c-4d08-4ede6e73b41c" + row := db.QueryRow(` +DECLARE $val AS Text; + +SELECT CAST($val AS UUID)`, + sql.Named("val", idString), + ) + + require.NoError(t, row.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) + db = scope.SQLDriver() + ) + + idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B" + row := db.QueryRow(` +DECLARE $val AS Text; + +SELECT CAST($val AS UUID)`, + sql.Named("val", idString), + ) + + require.NoError(t, row.Err()) + + var res string + + err := row.Scan(&res) + require.Error(t, err) + }) + 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) + db = scope.SQLDriver() + ) + + idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B" + id := uuid.MustParse(idString) + idParam := [16]byte(id) + row := db.QueryRow(` +DECLARE $val AS UUID; + +SELECT $val`, + sql.Named("val", idParam), + ) + + require.NoError(t, row.Err()) + + var resBytes [16]byte + err := row.Scan(&resBytes) + require.NoError(t, err) + + resUUID := uuid.UUID(resBytes) + + require.Equal(t, id, resUUID) + }) +} From 29f6adbb9882f88255162e253c5551148678f657 Mon Sep 17 00:00:00 2001 From: Timofey Koolin Date: Thu, 17 Oct 2024 20:29:47 +0300 Subject: [PATCH 2/2] add test for old code can't scan to uuid type --- .../database_sql_regression_test.go | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/integration/database_sql_regression_test.go b/tests/integration/database_sql_regression_test.go index b310f254d..465ae16d6 100644 --- a/tests/integration/database_sql_regression_test.go +++ b/tests/integration/database_sql_regression_test.go @@ -245,6 +245,28 @@ SELECT CAST($val AS UUID)`, err := row.Scan(&res) require.Error(t, err) }) + t.Run("old-receive-to-uuid", func(t *testing.T) { + // test old behavior - for test way of safe work with data, written with bagged API version + var ( + scope = newScope(t) + db = scope.SQLDriver() + ) + + idString := "6E73B41C-4EDE-4D08-9CFB-B7462D9E498B" + row := db.QueryRow(` +DECLARE $val AS Text; + +SELECT CAST($val AS UUID)`, + sql.Named("val", idString), + ) + + require.NoError(t, row.Err()) + + var res uuid.UUID + + err := row.Scan(&res) + require.Error(t, err) + }) 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 (