-
Notifications
You must be signed in to change notification settings - Fork 868
/
Copy pathdb_test.go
194 lines (171 loc) · 4.05 KB
/
db_test.go
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//go:build examples
package booktest
import (
"context"
"database/sql"
"slices"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
)
func TestBooks(t *testing.T) {
ctx := context.Background()
uri := local.MySQL(t, []string{"schema.sql"})
db, err := sql.Open("mysql", uri)
if err != nil {
t.Fatal(err)
}
defer db.Close()
dq := New(db)
// create an author
result, err := dq.CreateAuthor(ctx, "Unknown Master")
if err != nil {
t.Fatal(err)
}
authorID, err := result.LastInsertId()
if err != nil {
t.Fatal(err)
}
// create transaction
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
tq := dq.WithTx(tx)
// save first book
now := time.Now()
_, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int32(authorID),
Isbn: "1",
Title: "my book title",
BookType: BooksBookTypeFICTION,
Yr: 2016,
Available: now,
})
if err != nil {
t.Fatal(err)
}
// save second book
result, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int32(authorID),
Isbn: "2",
Title: "the second book",
BookType: BooksBookTypeFICTION,
Yr: 2016,
Available: now,
Tags: "cool,unique",
})
if err != nil {
t.Fatal(err)
}
bookOneID, err := result.LastInsertId()
if err != nil {
t.Fatal(err)
}
// update the title and tags
err = tq.UpdateBook(ctx, UpdateBookParams{
BookID: int32(bookOneID),
Title: "changed second title",
Tags: "cool,disastor",
})
if err != nil {
t.Fatal(err)
}
// save third book
_, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int32(authorID),
Isbn: "3",
Title: "the third book",
BookType: BooksBookTypeFICTION,
Yr: 2001,
Available: now,
Tags: "cool",
})
if err != nil {
t.Fatal(err)
}
// save fourth book
result, err = tq.CreateBook(ctx, CreateBookParams{
AuthorID: int32(authorID),
Isbn: "4",
Title: "4th place finisher",
BookType: BooksBookTypeNONFICTION,
Yr: 2011,
Available: now,
Tags: "other",
})
if err != nil {
t.Fatal(err)
}
bookThreeID, err := result.LastInsertId()
if err != nil {
t.Fatal(err)
}
// tx commit
err = tx.Commit()
if err != nil {
t.Fatal(err)
}
// upsert, changing ISBN and title
err = dq.UpdateBookISBN(ctx, UpdateBookISBNParams{
BookID: int32(bookThreeID),
Isbn: "NEW ISBN",
Title: "never ever gonna finish, a quatrain",
Tags: "someother",
})
if err != nil {
t.Fatal(err)
}
// retrieve first book
books0, err := dq.BooksByTitleYear(ctx, BooksByTitleYearParams{
Title: "my book title",
Yr: 2016,
})
if err != nil {
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))
author, err := dq.GetAuthor(ctx, book.AuthorID)
if err != nil {
t.Fatal(err)
}
t.Logf("Book %d author: %s\n", book.BookID, author.Name)
}
// retrieve first book
rows := dq.IterBooksByTitleYear(ctx, IterBooksByTitleYearParams{
Title: "my book title",
Yr: 2016,
})
// We need to collect the iterator elements to avoid panic when we make other db queries in the loop.
for _, book := range slices.Collect(rows.Iterate()) {
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
author, err := dq.GetAuthor(ctx, book.AuthorID)
if err != nil {
t.Fatal(err)
}
t.Logf("Book %d author: %s\n", book.BookID, author.Name)
}
if err = rows.Err(); err != nil {
t.Fatal(err)
}
// find a book with either "cool" or "other" tag
t.Logf("---------\nTag search results:\n")
res, err := dq.BooksByTags(ctx, "cool")
if err != nil {
t.Fatal(err)
}
for _, ab := range res {
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name.String, ab.Isbn, ab.Tags)
}
// TODO: call say_hello(varchar)
// get book 4 and delete
b5, err := dq.GetBook(ctx, int32(bookThreeID))
if err != nil {
t.Fatal(err)
}
if err := dq.DeleteBook(ctx, b5.BookID); err != nil {
t.Fatal(err)
}
}