Skip to content

Add tests that hit a real database #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ agent:
type: e1-standard-2
os_image: ubuntu1804
blocks:
- name: Golang example
- name: DinoSQL
task:
jobs:
- name: Run Go
- name: Test dinosql
commands:
- checkout
- sem-version go 1.12
- go test -v ./...
- name: Test dinosql/ondeck
commands:
- sem-service start postgres
- sudo apt-get install -y -qq postgresql-client
- createdb -U postgres -h 0.0.0.0 dinotest
- checkout
- sem-version go 1.12
- cd testdata/ondeck
- go test -v ./...
13 changes: 3 additions & 10 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ func ParseQueries(s *postgres.Schema, dir string) (*Result, error) {
}
var q []Query
for _, f := range files {
if !strings.HasSuffix(f.Name(), ".sql") {
continue
}
blob, err := ioutil.ReadFile(filepath.Join(dir, f.Name()))
if err != nil {
return nil, err
Expand Down Expand Up @@ -609,22 +612,12 @@ var hh = `package {{.Package}}
import (
"context"
"database/sql"
{{with .Schema.Enums}}"database/sql/driver"{{end}}
{{if .ImportTime}}"time"{{end}}
)

{{range .Schema.Enums}}
type {{.GoName}} string

func (e *{{.GoName}}) Scan(v interface{}) error {
*e = {{.GoName}}(string(v.([]byte)))
return nil
}

func (e {{.GoName}}) Value() (driver.Value, error) {
return []byte(string(e)), nil
}

const (
{{- range $i, $c := .Constants}}
{{- if eq $i 0}}
Expand Down
6 changes: 3 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ func TestParseSchema(t *testing.T) {
})

t.Run("prepared", func(t *testing.T) {
source := generate(q, "ondeck", true)
source := generate(q, "prepared", true)

blob, err := ioutil.ReadFile(filepath.Join("testdata", "ondeck", "prepared.go"))
blob, err := ioutil.ReadFile(filepath.Join("testdata", "ondeck", "prepared", "prepared.go"))
if err != nil {
log.Fatal(err)
}
Expand All @@ -119,7 +119,7 @@ func TestParseSchema(t *testing.T) {
func TestCompile(t *testing.T) {
files := []string{
filepath.Join("testdata", "ondeck", "db.go"),
filepath.Join("testdata", "ondeck", "prepared.go"),
filepath.Join("testdata", "ondeck", "prepared", "prepared.go"),
}
for _, filename := range files {
f := filename
Expand Down
24 changes: 8 additions & 16 deletions testdata/ondeck/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@ package ondeck
import (
"context"
"database/sql"
"database/sql/driver"
"time"
)

type StatusEnum string

func (e *StatusEnum) Scan(v interface{}) error {
*e = StatusEnum(string(v.([]byte)))
return nil
}

func (e StatusEnum) Value() (driver.Value, error) {
return []byte(string(e)), nil
}

const (
StatusOpen StatusEnum = "open"
StatusClosed = "closed"
Expand Down Expand Up @@ -79,22 +69,24 @@ func (q *Queries) CreateCity(ctx context.Context, name string, slug string) (Cit

const createVenue = `-- name: CreateVenue :one
INSERT INTO venue (
name,
slug,
name,
city,
created_at,
spotify_playlist,
city
status
) VALUES (
$1,
$2,
NOW(),
$3,
$4
NOW(),
$4,
$5
) RETURNING id
`

func (q *Queries) CreateVenue(ctx context.Context, name string, slug string, spotifyPlaylist string, city string) (int, error) {
row := q.db.QueryRowContext(ctx, createVenue, name, slug, spotifyPlaylist, city)
func (q *Queries) CreateVenue(ctx context.Context, slug string, name string, city string, spotifyPlaylist string, status StatusEnum) (int, error) {
row := q.db.QueryRowContext(ctx, createVenue, slug, name, city, spotifyPlaylist, status)
var i int
err := row.Scan(&i)
return i, err
Expand Down
Loading