Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
test: Update example tests to work correctly with pgx v5
  • Loading branch information
andrewmbenton committed Oct 16, 2023
commit 9adb266ef389034bb074f70ad4ec442210e8d03b
12 changes: 6 additions & 6 deletions examples/authors/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ package authors

import (
"context"
"database/sql"
"testing"

_ "github.com/lib/pq"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"

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

func TestAuthors(t *testing.T) {
ctx := context.Background()
uri := hosted.PostgreSQL(t, []string{"schema.sql"})
db, err := sql.Open("postgres", uri)
db, err := pgx.Connect(ctx, uri)
if err != nil {
t.Fatal(err)
}
defer db.Close()
defer db.Close(ctx)

ctx := context.Background()
q := New(db)

// list all authors
Expand All @@ -34,7 +34,7 @@ func TestAuthors(t *testing.T) {
// create an author
insertedAuthor, err := q.CreateAuthor(ctx, CreateAuthorParams{
Name: "Brian Kernighan",
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
Bio: pgtype.Text{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
})
if err != nil {
t.Fatal(err)
Expand Down
7 changes: 4 additions & 3 deletions examples/batch/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"testing"
"time"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
)

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

now := time.Now()
now := pgtype.Timestamptz{Time: time.Now()}

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

for _, book := range books0 {
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Time.Format(time.RFC822Z))
author, err := dq.GetAuthor(ctx, book.AuthorID)
if err != nil {
t.Fatal(err)
Expand Down
20 changes: 10 additions & 10 deletions examples/booktest/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ package booktest

import (
"context"
"database/sql"
"testing"
"time"

_ "github.com/lib/pq"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"

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

func TestBooks(t *testing.T) {
uri := hosted.PostgreSQL(t, []string{"schema.sql"})
db, err := sql.Open("postgres", uri)
ctx := context.Background()
uri := hosted.PostgreSQL(t, []string{"schema"})
db, err := pgx.Connect(ctx, uri)
if err != nil {
t.Fatal(err)
}
defer db.Close()
defer db.Close(ctx)

ctx := context.Background()
dq := New(db)

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

// create transaction
tx, err := db.Begin()
tx, err := db.Begin(ctx)
if err != nil {
t.Fatal(err)
}

tq := dq.WithTx(tx)

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

// tx commit
err = tx.Commit()
err = tx.Commit(ctx)
if err != nil {
t.Fatal(err)
}
Expand All @@ -132,7 +132,7 @@ func TestBooks(t *testing.T) {
t.Fatal(err)
}
for _, book := range books0 {
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Time.Format(time.RFC822Z))
author, err := dq.GetAuthor(ctx, book.AuthorID)
if err != nil {
t.Fatal(err)
Expand Down
26 changes: 4 additions & 22 deletions examples/ondeck/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ package ondeck

import (
"context"
"database/sql"
"testing"

"github.com/google/go-cmp/cmp"
_ "github.com/lib/pq"
"github.com/jackc/pgx/v5"

"github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
)
Expand Down Expand Up @@ -123,33 +122,16 @@ func runOnDeckQueries(t *testing.T, q *Queries) {
}
}

func TestPrepared(t *testing.T) {
t.Parallel()

uri := hosted.PostgreSQL(t, []string{"schema"})
db, err := sql.Open("postgres", uri)
if err != nil {
t.Fatal(err)
}
defer db.Close()

q, err := Prepare(context.Background(), db)
if err != nil {
t.Fatal(err)
}

runOnDeckQueries(t, q)
}

func TestQueries(t *testing.T) {
t.Parallel()

ctx := context.Background()
uri := hosted.PostgreSQL(t, []string{"schema"})
db, err := sql.Open("postgres", uri)
db, err := pgx.Connect(ctx, uri)
if err != nil {
t.Fatal(err)
}
defer db.Close()
defer db.Close(ctx)

runOnDeckQueries(t, New(db))
}