Skip to content

Commit eb4e518

Browse files
author
Viktor Pentyukhov
committed
Rewrote examples test logic + local.YDB func to work with native driver instead of database/sql
1 parent df80efa commit eb4e518

File tree

4 files changed

+129
-76
lines changed

4 files changed

+129
-76
lines changed

examples/authors/ydb/db_test.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
88
_ "github.com/ydb-platform/ydb-go-sdk/v3"
9+
"github.com/ydb-platform/ydb-go-sdk/v3/query"
910
)
1011

1112
func ptr(s string) *string {
@@ -15,10 +16,10 @@ func ptr(s string) *string {
1516
func TestAuthors(t *testing.T) {
1617
ctx := context.Background()
1718

18-
test := local.YDB(t, []string{"schema.sql"})
19-
defer test.DB.Close()
19+
db := local.YDB(t, []string{"schema.sql"})
20+
defer db.Close(ctx)
2021

21-
q := New(test.DB)
22+
q := New(db.Query())
2223

2324
t.Run("InsertAuthors", func(t *testing.T) {
2425
authorsToInsert := []CreateOrUpdateAuthorParams{
@@ -38,7 +39,7 @@ func TestAuthors(t *testing.T) {
3839
}
3940

4041
for _, author := range authorsToInsert {
41-
if _, err := q.CreateOrUpdateAuthor(ctx, author); err != nil {
42+
if err := q.CreateOrUpdateAuthor(ctx, author, query.WithIdempotent()); err != nil {
4243
t.Fatalf("failed to insert author %q: %v", author.P1, err)
4344
}
4445
}
@@ -52,7 +53,7 @@ func TestAuthors(t *testing.T) {
5253
P2: &newBio,
5354
}
5455

55-
returnedBio, err := q.CreateOrUpdateAuthorReturningBio(ctx, arg)
56+
returnedBio, err := q.CreateOrUpdateAuthorReturningBio(ctx, arg, query.WithIdempotent())
5657
if err != nil {
5758
t.Fatalf("failed to create or update author: %v", err)
5859
}
@@ -74,15 +75,17 @@ func TestAuthors(t *testing.T) {
7475
P2: 10,
7576
}
7677

77-
singleAuthor, err := q.UpdateAuthorByID(ctx, arg)
78+
authors, err := q.UpdateAuthorByID(ctx, arg, query.WithIdempotent(), query.WithIdempotent())
7879
if err != nil {
7980
t.Fatal(err)
8081
}
81-
bio := "Null"
82-
if singleAuthor.Bio != nil {
83-
bio = *singleAuthor.Bio
82+
for _, a := range authors {
83+
bio := "Null"
84+
if a.Bio != nil {
85+
bio = *a.Bio
86+
}
87+
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
8488
}
85-
t.Logf("- ID: %d | Name: %s | Bio: %s", singleAuthor.ID, singleAuthor.Name, bio)
8689
})
8790

8891
t.Run("ListAuthors", func(t *testing.T) {
@@ -154,7 +157,7 @@ func TestAuthors(t *testing.T) {
154157
t.Run("Delete All Authors", func(t *testing.T) {
155158
var i uint64
156159
for i = 1; i <= 13; i++ {
157-
if err := q.DeleteAuthor(ctx, i); err != nil {
160+
if err := q.DeleteAuthor(ctx, i, query.WithIdempotent()); err != nil {
158161
t.Fatalf("failed to delete authors: %v", err)
159162
}
160163
}

examples/authors/ydb/query.sql

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ WHERE bio IS NULL;
1616
-- name: Count :one
1717
SELECT COUNT(*) FROM authors;
1818

19-
-- name: UpsertAuthor :queryrows
20-
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2) RETURNING *;
19+
-- name: Coalesce :many
20+
SELECT id, name, COALESCE(bio, 'Null value!') FROM authors;
21+
22+
-- name: CreateOrUpdateAuthor :exec
23+
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2);
24+
25+
-- name: CreateOrUpdateAuthorReturningBio :one
26+
UPSERT INTO authors (id, name, bio) VALUES ($p0, $p1, $p2) RETURNING bio;
2127

2228
-- name: DeleteAuthor :exec
2329
DELETE FROM authors WHERE id = $p0;
2430

25-
-- name: UpdateAuthorByID :one
26-
UPDATE authors SET name = $p0, bio = $p1 WHERE id = $p2 RETURNING *;
31+
-- name: UpdateAuthorByID :queryrows
32+
UPDATE authors SET name = $p0, bio = $p1 WHERE id = $p2 RETURNING *;

examples/authors/ydb/query.sql.go

Lines changed: 85 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/sqltest/local/ydb.go

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package local
22

33
import (
44
"context"
5-
"database/sql"
65
"fmt"
76
"hash/fnv"
87
"math/rand"
@@ -14,30 +13,25 @@ import (
1413
migrate "github.com/sqlc-dev/sqlc/internal/migrations"
1514
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
1615
"github.com/ydb-platform/ydb-go-sdk/v3"
16+
"github.com/ydb-platform/ydb-go-sdk/v3/query"
17+
"github.com/ydb-platform/ydb-go-sdk/v3/sugar"
1718
)
1819

1920
func init() {
2021
rand.Seed(time.Now().UnixNano())
2122
}
2223

23-
func YDB(t *testing.T, migrations []string) TestYDB {
24+
func YDB(t *testing.T, migrations []string) *ydb.Driver {
2425
return link_YDB(t, migrations, true)
2526
}
2627

27-
func ReadOnlyYDB(t *testing.T, migrations []string) TestYDB {
28+
func ReadOnlyYDB(t *testing.T, migrations []string) *ydb.Driver {
2829
return link_YDB(t, migrations, false)
2930
}
3031

31-
type TestYDB struct {
32-
DB *sql.DB
33-
Prefix string
34-
}
35-
36-
func link_YDB(t *testing.T, migrations []string, rw bool) TestYDB {
32+
func link_YDB(t *testing.T, migrations []string, rw bool) *ydb.Driver {
3733
t.Helper()
3834

39-
time.Sleep(1 * time.Second) // wait for YDB to start
40-
4135
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
4236
defer cancel()
4337

@@ -72,46 +66,43 @@ func link_YDB(t *testing.T, migrations []string, rw bool) TestYDB {
7266

7367
var name string
7468
if rw {
75-
// name = fmt.Sprintf("sqlc_test_%s", id())
7669
name = fmt.Sprintf("sqlc_test_%s", "test_new")
7770
} else {
7871
name = fmt.Sprintf("sqlc_test_%x", h.Sum(nil))
7972
}
80-
prefix := fmt.Sprintf("%s/%s", baseDB, name)
8173

82-
rootDSN := fmt.Sprintf("grpc://%s?database=%s", dbuiri, baseDB)
83-
t.Logf("→ Opening root driver: %s", rootDSN)
84-
driver, err := ydb.Open(ctx, rootDSN,
74+
connectionString := fmt.Sprintf("grpc://%s%s", dbuiri, baseDB)
75+
t.Logf("→ Opening YDB connection: %s", connectionString)
76+
77+
db, err := ydb.Open(ctx, connectionString,
8578
ydb.WithInsecure(),
8679
ydb.WithDiscoveryInterval(time.Hour),
8780
ydb.WithNodeAddressMutator(func(_ string) string {
8881
return host
8982
}),
9083
)
9184
if err != nil {
92-
t.Fatalf("failed to open root YDB connection: %s", err)
85+
t.Fatalf("failed to open YDB connection: %s", err)
9386
}
9487

95-
connector, err := ydb.Connector(
96-
driver,
97-
ydb.WithTablePathPrefix(prefix),
98-
ydb.WithAutoDeclare(),
99-
ydb.WithNumericArgs(),
100-
)
88+
prefix := fmt.Sprintf("%s/%s", baseDB, name)
89+
t.Logf("→ Using prefix: %s", prefix)
90+
91+
err = sugar.RemoveRecursive(ctx, db, prefix)
10192
if err != nil {
102-
t.Fatalf("failed to create connector: %s", err)
93+
t.Logf("Warning: failed to remove old data: %s", err)
10394
}
10495

105-
db := sql.OpenDB(connector)
106-
10796
t.Log("→ Applying migrations to prefix: ", prefix)
10897

109-
schemeCtx := ydb.WithQueryMode(ctx, ydb.SchemeQueryMode)
11098
for _, stmt := range seed {
111-
_, err := db.ExecContext(schemeCtx, stmt)
99+
err := db.Query().Exec(ctx, stmt,
100+
query.WithTxControl(query.EmptyTxControl()),
101+
)
112102
if err != nil {
113103
t.Fatalf("failed to apply migration: %s\nSQL: %s", err, stmt)
114104
}
115105
}
116-
return TestYDB{DB: db, Prefix: prefix}
106+
107+
return db
117108
}

0 commit comments

Comments
 (0)