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
Next Next commit
test(examples): Use a hosted database for example testing
  • Loading branch information
kyleconroy committed Sep 20, 2023
commit 4dcca7769f5fb202b2908d2923231c5c70b8ac8d
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ jobs:
MYSQL_HOST: localhost
MYSQL_PORT: ${{ job.services.mysql.ports['3306'] }}
MYSQL_ROOT_PASSWORD: mysecretpassword
DDL_SQLC_PROJECT_ID: ${{ secrets.DDL_SQLC_PROJECT_ID }}
DDL_SQLC_AUTH_TOKEN: ${{ secrets.DDL_SQLC_AUTH_TOKEN }}
CI_SQLC_PROJECT_ID: ${{ secrets.CI_SQLC_PROJECT_ID }}
CI_SQLC_AUTH_TOKEN: ${{ secrets.CI_SQLC_AUTH_TOKEN }}

- name: build internal/endtoend
run: go build ./...
Expand Down
20 changes: 13 additions & 7 deletions examples/authors/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,31 @@ import (
"database/sql"
"testing"

"github.com/sqlc-dev/sqlc/internal/sqltest"
_ "github.com/lib/pq"

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

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

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

// list all authors
authors, err := db.ListAuthors(ctx)
authors, err := q.ListAuthors(ctx)
if err != nil {
t.Fatal(err)
}
t.Log(authors)

// create an author
insertedAuthor, err := db.CreateAuthor(ctx, CreateAuthorParams{
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},
})
Expand All @@ -36,7 +42,7 @@ func TestAuthors(t *testing.T) {
t.Log(insertedAuthor)

// get the author we just inserted
fetchedAuthor, err := db.GetAuthor(ctx, insertedAuthor.ID)
fetchedAuthor, err := q.GetAuthor(ctx, insertedAuthor.ID)
if err != nil {
t.Fatal(err)
}
Expand Down
13 changes: 10 additions & 3 deletions examples/batch/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ import (
"testing"
"time"

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

func TestBatchBooks(t *testing.T) {
db, cleanup := sqltest.PostgreSQLPgx(t, []string{"schema.sql"})
defer cleanup()
uri := hosted.PostgreSQL(t, []string{"schema.sql"})

ctx := context.Background()

db, err := pgx.Connect(ctx, uri)
if err != nil {
t.Fatal(err)
}
defer db.Close(ctx)

dq := New(db)

// create an author
Expand Down
13 changes: 10 additions & 3 deletions examples/booktest/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ package booktest

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

"github.com/sqlc-dev/sqlc/internal/sqltest"
_ "github.com/lib/pq"

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

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

ctx := context.Background()
dq := New(db)
Expand Down
26 changes: 18 additions & 8 deletions examples/ondeck/postgresql/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ package ondeck

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

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

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

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

func runOnDeckQueries(t *testing.T, q *Queries) {
Expand Down Expand Up @@ -124,10 +126,14 @@ func runOnDeckQueries(t *testing.T, q *Queries) {
func TestPrepared(t *testing.T) {
t.Parallel()

sdb, cleanup := sqltest.PostgreSQL(t, []string{"schema"})
defer cleanup()
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(), sdb)
q, err := Prepare(context.Background(), db)
if err != nil {
t.Fatal(err)
}
Expand All @@ -138,8 +144,12 @@ func TestPrepared(t *testing.T) {
func TestQueries(t *testing.T) {
t.Parallel()

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

runOnDeckQueries(t, New(sdb))
runOnDeckQueries(t, New(db))
}
4 changes: 2 additions & 2 deletions internal/endtoend/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
func TestValidSchema(t *testing.T) {
ctx := context.Background()

projectID := os.Getenv("DDL_SQLC_PROJECT_ID")
authToken := os.Getenv("DDL_SQLC_AUTH_TOKEN")
projectID := os.Getenv("CI_SQLC_PROJECT_ID")
authToken := os.Getenv("CI_SQLC_AUTH_TOKEN")

if projectID == "" || authToken == "" {
if os.Getenv("CI") == "" {
Expand Down
78 changes: 78 additions & 0 deletions internal/sqltest/hosted/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package hosted

import (
"context"
"fmt"
"os"
"sync"
"testing"

"github.com/sqlc-dev/sqlc/internal/quickdb"
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
)

var client pb.QuickClient
var once sync.Once

func initClient() error {
projectID := os.Getenv("CI_SQLC_PROJECT_ID")
authToken := os.Getenv("CI_SQLC_AUTH_TOKEN")
if projectID == "" || authToken == "" {
return fmt.Errorf("missing project id or auth token")
}
c, err := quickdb.NewClient(projectID, authToken)
if err != nil {
return err
}
client = c
return nil
}

func PostgreSQL(t *testing.T, migrations []string) string {
ctx := context.Background()
t.Helper()

once.Do(func() {
if err := initClient(); err != nil {
t.Fatal(err)
}
})

if client == nil {
t.Fatalf("client init failed")
}

var seed []string
files, err := sqlpath.Glob(migrations)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
blob, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
seed = append(seed, string(blob))
}

resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{
Engine: "postgresql",
Region: "iad",
Migrations: seed,
})
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
_, err = client.DropEphemeralDatabase(ctx, &pb.DropEphemeralDatabaseRequest{
DatabaseId: resp.DatabaseId,
})
if err != nil {
t.Fatal(err)
}
})

return resp.Uri
}