-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathdb_test.go
More file actions
171 lines (146 loc) · 3.1 KB
/
db_test.go
File metadata and controls
171 lines (146 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//go:build examples
package ondeck
import (
"context"
"database/sql"
"strings"
"testing"
"github.com/sqlc-dev/sqlc/internal/sqltest"
"github.com/google/go-cmp/cmp"
)
// TODO: Enum is not yet supported
const (
VenuesStatusOpen string = "open"
VenuesStatusClosed string = "closed"
)
func join(vals ...string) sql.NullString {
if len(vals) == 0 {
return sql.NullString{}
}
return sql.NullString{
Valid: true,
String: strings.Join(vals, ","),
}
}
func runOnDeckQueries(t *testing.T, q *Queries) {
ctx := context.Background()
err := q.CreateCity(ctx, CreateCityParams{
Slug: "san-francisco",
Name: "San Francisco",
})
if err != nil {
t.Fatal(err)
}
city, err := q.GetCity(ctx, "san-francisco")
if err != nil {
t.Fatal(err)
}
venueResult, err := q.CreateVenue(ctx, CreateVenueParams{
Slug: "the-fillmore",
Name: "The Fillmore",
City: city.Slug,
SpotifyPlaylist: "spotify:uri",
Status: VenuesStatusOpen,
Statuses: join(string(VenuesStatusOpen), string(VenuesStatusClosed)),
Tags: join("rock", "punk"),
})
if err != nil {
t.Fatal(err)
}
venueID, err := venueResult.LastInsertId()
if err != nil {
t.Fatal(err)
}
venue, err := q.GetVenue(ctx, GetVenueParams{
Slug: "the-fillmore",
City: city.Slug,
})
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(venue.ID, venueID); diff != "" {
t.Errorf("venue ID mismatch:\n%s", diff)
}
{
actual, err := q.VenueCountByCity(ctx)
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(actual, []VenueCountByCityRow{
{city.Slug, int64(1)},
}); diff != "" {
t.Errorf("venue count mismatch:\n%s", diff)
}
}
{
actual, err := q.ListCities(ctx)
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(actual, []City{city}); diff != "" {
t.Errorf("list city mismatch:\n%s", diff)
}
}
{
actual, err := q.ListVenues(ctx, city.Slug)
if err != nil {
t.Error(err)
}
if diff := cmp.Diff(actual, []Venue{venue}); diff != "" {
t.Errorf("list venue mismatch:\n%s", diff)
}
}
{
err := q.UpdateCityName(ctx, UpdateCityNameParams{
Slug: city.Slug,
Name: "SF",
})
if err != nil {
t.Error(err)
}
}
{
expected := "Fillmore"
err := q.UpdateVenueName(ctx, UpdateVenueNameParams{
Slug: venue.Slug,
Name: expected,
})
if err != nil {
t.Error(err)
}
fresh, err := q.GetVenue(ctx, GetVenueParams{
Slug: venue.Slug,
City: city.Slug,
})
if diff := cmp.Diff(expected, fresh.Name); diff != "" {
t.Errorf("update venue mismatch:\n%s", diff)
}
}
{
err := q.DeleteVenue(ctx, DeleteVenueParams{
Slug: venue.Slug,
Slug_2: venue.Slug,
})
if err != nil {
t.Error(err)
}
}
}
func TestPrepared(t *testing.T) {
t.Parallel()
sdb, cleanup := sqltest.SQLite(t, []string{"schema"})
defer sdb.Close()
defer cleanup()
q, err := Prepare(context.Background(), sdb)
if err != nil {
t.Fatal(err)
}
runOnDeckQueries(t, q)
}
func TestQueries(t *testing.T) {
t.Parallel()
sdb, cleanup := sqltest.SQLite(t, []string{"schema"})
defer sdb.Close()
defer cleanup()
runOnDeckQueries(t, New(sdb))
}