Skip to content

Commit 9adb266

Browse files
committed
test: Update example tests to work correctly with pgx v5
1 parent 97be417 commit 9adb266

File tree

4 files changed

+24
-41
lines changed

4 files changed

+24
-41
lines changed

examples/authors/postgresql/db_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ package authors
55

66
import (
77
"context"
8-
"database/sql"
98
"testing"
109

11-
_ "github.com/lib/pq"
10+
"github.com/jackc/pgx/v5"
11+
"github.com/jackc/pgx/v5/pgtype"
1212

1313
"github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
1414
)
1515

1616
func TestAuthors(t *testing.T) {
17+
ctx := context.Background()
1718
uri := hosted.PostgreSQL(t, []string{"schema.sql"})
18-
db, err := sql.Open("postgres", uri)
19+
db, err := pgx.Connect(ctx, uri)
1920
if err != nil {
2021
t.Fatal(err)
2122
}
22-
defer db.Close()
23+
defer db.Close(ctx)
2324

24-
ctx := context.Background()
2525
q := New(db)
2626

2727
// list all authors
@@ -34,7 +34,7 @@ func TestAuthors(t *testing.T) {
3434
// create an author
3535
insertedAuthor, err := q.CreateAuthor(ctx, CreateAuthorParams{
3636
Name: "Brian Kernighan",
37-
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
37+
Bio: pgtype.Text{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
3838
})
3939
if err != nil {
4040
t.Fatal(err)

examples/batch/postgresql/db_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/jackc/pgx/v4"
11+
"github.com/jackc/pgx/v5"
12+
"github.com/jackc/pgx/v5/pgtype"
1213
"github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
1314
)
1415

@@ -31,7 +32,7 @@ func TestBatchBooks(t *testing.T) {
3132
t.Fatal(err)
3233
}
3334

34-
now := time.Now()
35+
now := pgtype.Timestamptz{Time: time.Now()}
3536

3637
// batch insert new books
3738
newBooksParams := []CreateBookParams{
@@ -114,7 +115,7 @@ func TestBatchBooks(t *testing.T) {
114115
})
115116

116117
for _, book := range books0 {
117-
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
118+
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Time.Format(time.RFC822Z))
118119
author, err := dq.GetAuthor(ctx, book.AuthorID)
119120
if err != nil {
120121
t.Fatal(err)

examples/booktest/postgresql/db_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ package booktest
55

66
import (
77
"context"
8-
"database/sql"
98
"testing"
109
"time"
1110

12-
_ "github.com/lib/pq"
11+
"github.com/jackc/pgx/v5"
12+
"github.com/jackc/pgx/v5/pgtype"
1313

1414
"github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
1515
)
1616

1717
func TestBooks(t *testing.T) {
18-
uri := hosted.PostgreSQL(t, []string{"schema.sql"})
19-
db, err := sql.Open("postgres", uri)
18+
ctx := context.Background()
19+
uri := hosted.PostgreSQL(t, []string{"schema"})
20+
db, err := pgx.Connect(ctx, uri)
2021
if err != nil {
2122
t.Fatal(err)
2223
}
23-
defer db.Close()
24+
defer db.Close(ctx)
2425

25-
ctx := context.Background()
2626
dq := New(db)
2727

2828
// create an author
@@ -32,15 +32,15 @@ func TestBooks(t *testing.T) {
3232
}
3333

3434
// create transaction
35-
tx, err := db.Begin()
35+
tx, err := db.Begin(ctx)
3636
if err != nil {
3737
t.Fatal(err)
3838
}
3939

4040
tq := dq.WithTx(tx)
4141

4242
// save first book
43-
now := time.Now()
43+
now := pgtype.Timestamptz{Time: time.Now()}
4444
_, err = tq.CreateBook(ctx, CreateBookParams{
4545
AuthorID: a.AuthorID,
4646
Isbn: "1",
@@ -107,7 +107,7 @@ func TestBooks(t *testing.T) {
107107
}
108108

109109
// tx commit
110-
err = tx.Commit()
110+
err = tx.Commit(ctx)
111111
if err != nil {
112112
t.Fatal(err)
113113
}
@@ -132,7 +132,7 @@ func TestBooks(t *testing.T) {
132132
t.Fatal(err)
133133
}
134134
for _, book := range books0 {
135-
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
135+
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Time.Format(time.RFC822Z))
136136
author, err := dq.GetAuthor(ctx, book.AuthorID)
137137
if err != nil {
138138
t.Fatal(err)

examples/ondeck/postgresql/db_test.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ package ondeck
55

66
import (
77
"context"
8-
"database/sql"
98
"testing"
109

1110
"github.com/google/go-cmp/cmp"
12-
_ "github.com/lib/pq"
11+
"github.com/jackc/pgx/v5"
1312

1413
"github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
1514
)
@@ -123,33 +122,16 @@ func runOnDeckQueries(t *testing.T, q *Queries) {
123122
}
124123
}
125124

126-
func TestPrepared(t *testing.T) {
127-
t.Parallel()
128-
129-
uri := hosted.PostgreSQL(t, []string{"schema"})
130-
db, err := sql.Open("postgres", uri)
131-
if err != nil {
132-
t.Fatal(err)
133-
}
134-
defer db.Close()
135-
136-
q, err := Prepare(context.Background(), db)
137-
if err != nil {
138-
t.Fatal(err)
139-
}
140-
141-
runOnDeckQueries(t, q)
142-
}
143-
144125
func TestQueries(t *testing.T) {
145126
t.Parallel()
146127

128+
ctx := context.Background()
147129
uri := hosted.PostgreSQL(t, []string{"schema"})
148-
db, err := sql.Open("postgres", uri)
130+
db, err := pgx.Connect(ctx, uri)
149131
if err != nil {
150132
t.Fatal(err)
151133
}
152-
defer db.Close()
134+
defer db.Close(ctx)
153135

154136
runOnDeckQueries(t, New(db))
155137
}

0 commit comments

Comments
 (0)