Skip to content

Commit e51ccae

Browse files
authored
Add database integration tests (#2)
1 parent b068af5 commit e51ccae

File tree

9 files changed

+338
-54
lines changed

9 files changed

+338
-54
lines changed

.semaphore/semaphore.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@ agent:
77
type: e1-standard-2
88
os_image: ubuntu1804
99
blocks:
10-
- name: Golang example
10+
- name: DinoSQL
1111
task:
1212
jobs:
13-
- name: Run Go
13+
- name: Test dinosql
1414
commands:
1515
- checkout
1616
- sem-version go 1.12
1717
- go test -v ./...
18+
- name: Test dinosql/ondeck
19+
commands:
20+
- sem-service start postgres
21+
- sudo apt-get install -y -qq postgresql-client
22+
- createdb -U postgres -h 0.0.0.0 dinotest
23+
- checkout
24+
- sem-version go 1.12
25+
- cd testdata/ondeck
26+
- go test -v ./...

parser.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ func ParseQueries(s *postgres.Schema, dir string) (*Result, error) {
246246
}
247247
var q []Query
248248
for _, f := range files {
249+
if !strings.HasSuffix(f.Name(), ".sql") {
250+
continue
251+
}
249252
blob, err := ioutil.ReadFile(filepath.Join(dir, f.Name()))
250253
if err != nil {
251254
return nil, err
@@ -609,22 +612,12 @@ var hh = `package {{.Package}}
609612
import (
610613
"context"
611614
"database/sql"
612-
{{with .Schema.Enums}}"database/sql/driver"{{end}}
613615
{{if .ImportTime}}"time"{{end}}
614616
)
615617
616618
{{range .Schema.Enums}}
617619
type {{.GoName}} string
618620
619-
func (e *{{.GoName}}) Scan(v interface{}) error {
620-
*e = {{.GoName}}(string(v.([]byte)))
621-
return nil
622-
}
623-
624-
func (e {{.GoName}}) Value() (driver.Value, error) {
625-
return []byte(string(e)), nil
626-
}
627-
628621
const (
629622
{{- range $i, $c := .Constants}}
630623
{{- if eq $i 0}}

parser_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ func TestParseSchema(t *testing.T) {
102102
})
103103

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

107-
blob, err := ioutil.ReadFile(filepath.Join("testdata", "ondeck", "prepared.go"))
107+
blob, err := ioutil.ReadFile(filepath.Join("testdata", "ondeck", "prepared", "prepared.go"))
108108
if err != nil {
109109
log.Fatal(err)
110110
}
@@ -119,7 +119,7 @@ func TestParseSchema(t *testing.T) {
119119
func TestCompile(t *testing.T) {
120120
files := []string{
121121
filepath.Join("testdata", "ondeck", "db.go"),
122-
filepath.Join("testdata", "ondeck", "prepared.go"),
122+
filepath.Join("testdata", "ondeck", "prepared", "prepared.go"),
123123
}
124124
for _, filename := range files {
125125
f := filename

testdata/ondeck/db.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,11 @@ package ondeck
33
import (
44
"context"
55
"database/sql"
6-
"database/sql/driver"
76
"time"
87
)
98

109
type StatusEnum string
1110

12-
func (e *StatusEnum) Scan(v interface{}) error {
13-
*e = StatusEnum(string(v.([]byte)))
14-
return nil
15-
}
16-
17-
func (e StatusEnum) Value() (driver.Value, error) {
18-
return []byte(string(e)), nil
19-
}
20-
2111
const (
2212
StatusOpen StatusEnum = "open"
2313
StatusClosed = "closed"
@@ -79,22 +69,24 @@ func (q *Queries) CreateCity(ctx context.Context, name string, slug string) (Cit
7969

8070
const createVenue = `-- name: CreateVenue :one
8171
INSERT INTO venue (
82-
name,
8372
slug,
73+
name,
74+
city,
8475
created_at,
8576
spotify_playlist,
86-
city
77+
status
8778
) VALUES (
8879
$1,
8980
$2,
90-
NOW(),
9181
$3,
92-
$4
82+
NOW(),
83+
$4,
84+
$5
9385
) RETURNING id
9486
`
9587

96-
func (q *Queries) CreateVenue(ctx context.Context, name string, slug string, spotifyPlaylist string, city string) (int, error) {
97-
row := q.db.QueryRowContext(ctx, createVenue, name, slug, spotifyPlaylist, city)
88+
func (q *Queries) CreateVenue(ctx context.Context, slug string, name string, city string, spotifyPlaylist string, status StatusEnum) (int, error) {
89+
row := q.db.QueryRowContext(ctx, createVenue, slug, name, city, spotifyPlaylist, status)
9890
var i int
9991
err := row.Scan(&i)
10092
return i, err

0 commit comments

Comments
 (0)