Skip to content

Commit 5fbb224

Browse files
authored
examples: Run tests for MySQL booktest (#627)
* examples: Run tests for MySQL booktest * include protocol * rm drop * multiStatements=true * rm defaults * parseTime=true
1 parent 917b6db commit 5fbb224

File tree

14 files changed

+404
-99
lines changed

14 files changed

+404
-99
lines changed

.github/workflows/ci.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ jobs:
1717
- 5432:5432
1818
# needed because the postgres container does not provide a healthcheck
1919
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
20+
mysql:
21+
image: mysql:8
22+
env:
23+
MYSQL_ROOT_PASSWORD: mysecretpassword
24+
MYSQL_DATABASE: mysql
25+
ports:
26+
- 3306:3306
2027

2128
steps:
2229
- uses: actions/checkout@v2
@@ -37,3 +44,8 @@ jobs:
3744
PG_DATABASE: postgres
3845
PG_PASSWORD: postgres
3946
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
47+
MYSQL_DATABASE: mysql
48+
MYSQL_HOST: localhost
49+
MYSQL_PORT: ${{ job.services.mysql.ports['5432'] }}
50+
MYSQL_ROOT_PASSWORD: mysecretpassword
51+

examples/booktest/mysql/db_test.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// +build examples
2+
3+
package booktest
4+
5+
import (
6+
"context"
7+
"testing"
8+
"time"
9+
10+
"github.com/kyleconroy/sqlc/internal/sqltest"
11+
)
12+
13+
func TestBooks(t *testing.T) {
14+
db, cleanup := sqltest.MySQL(t, []string{"schema.sql"})
15+
defer cleanup()
16+
17+
ctx := context.Background()
18+
dq := New(db)
19+
20+
// create an author
21+
result, err := dq.CreateAuthor(ctx, "Unknown Master")
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
authorID, err := result.LastInsertId()
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
30+
// create transaction
31+
tx, err := db.Begin()
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
36+
tq := dq.WithTx(tx)
37+
38+
// save first book
39+
now := time.Now()
40+
_, err = tq.CreateBook(ctx, CreateBookParams{
41+
AuthorID: int32(authorID),
42+
Isbn: "1",
43+
Title: "my book title",
44+
BookType: "FICTION",
45+
Yr: 2016,
46+
Available: now,
47+
})
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
52+
// save second book
53+
result, err = tq.CreateBook(ctx, CreateBookParams{
54+
AuthorID: int32(authorID),
55+
Isbn: "2",
56+
Title: "the second book",
57+
BookType: "FICTION",
58+
Yr: 2016,
59+
Available: now,
60+
Tags: "cool,unique",
61+
})
62+
if err != nil {
63+
t.Fatal(err)
64+
}
65+
bookOneID, err := result.LastInsertId()
66+
if err != nil {
67+
t.Fatal(err)
68+
}
69+
70+
// update the title and tags
71+
err = tq.UpdateBook(ctx, UpdateBookParams{
72+
BookID: int32(bookOneID),
73+
Title: "changed second title",
74+
Tags: "cool,disastor",
75+
})
76+
if err != nil {
77+
t.Fatal(err)
78+
}
79+
80+
// save third book
81+
_, err = tq.CreateBook(ctx, CreateBookParams{
82+
AuthorID: int32(authorID),
83+
Isbn: "3",
84+
Title: "the third book",
85+
BookType: "FICTION",
86+
Yr: 2001,
87+
Available: now,
88+
Tags: "cool",
89+
})
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
94+
// save fourth book
95+
result, err = tq.CreateBook(ctx, CreateBookParams{
96+
AuthorID: int32(authorID),
97+
Isbn: "4",
98+
Title: "4th place finisher",
99+
BookType: "NONFICTION",
100+
Yr: 2011,
101+
Available: now,
102+
Tags: "other",
103+
})
104+
if err != nil {
105+
t.Fatal(err)
106+
}
107+
bookThreeID, err := result.LastInsertId()
108+
if err != nil {
109+
t.Fatal(err)
110+
}
111+
112+
// tx commit
113+
err = tx.Commit()
114+
if err != nil {
115+
t.Fatal(err)
116+
}
117+
118+
// upsert, changing ISBN and title
119+
err = dq.UpdateBookISBN(ctx, UpdateBookISBNParams{
120+
BookID: int32(bookThreeID),
121+
Isbn: "NEW ISBN",
122+
Title: "never ever gonna finish, a quatrain",
123+
Tags: "someother",
124+
})
125+
if err != nil {
126+
t.Fatal(err)
127+
}
128+
129+
// retrieve first book
130+
books0, err := dq.BooksByTitleYear(ctx, BooksByTitleYearParams{
131+
Title: "my book title",
132+
Yr: 2016,
133+
})
134+
if err != nil {
135+
t.Fatal(err)
136+
}
137+
for _, book := range books0 {
138+
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
139+
author, err := dq.GetAuthor(ctx, book.AuthorID)
140+
if err != nil {
141+
t.Fatal(err)
142+
}
143+
t.Logf("Book %d author: %s\n", book.BookID, author.Name)
144+
}
145+
146+
// find a book with either "cool" or "other" tag
147+
t.Logf("---------\nTag search results:\n")
148+
res, err := dq.BooksByTags(ctx, "cool")
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
for _, ab := range res {
153+
t.Logf("Book %d: '%s', Author: '%s', ISBN: '%s' Tags: '%v'\n", ab.BookID, ab.Title, ab.Name, ab.Isbn, ab.Tags)
154+
}
155+
156+
// TODO: call say_hello(varchar)
157+
158+
// get book 4 and delete
159+
b5, err := dq.GetBook(ctx, int32(bookThreeID))
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
if err := dq.DeleteBook(ctx, b5.BookID); err != nil {
164+
t.Fatal(err)
165+
}
166+
167+
}

examples/booktest/mysql/models.go

Lines changed: 5 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/booktest/mysql/query.sql

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,25 @@ WHERE book_id = ?;
1414
SELECT * FROM books
1515
WHERE title = ? AND yr = ?;
1616

17-
/* name: CreateAuthor :exec */
17+
/* name: BooksByTags :many */
18+
SELECT
19+
book_id,
20+
title,
21+
name,
22+
isbn,
23+
tags
24+
FROM books
25+
LEFT JOIN authors ON books.author_id = authors.author_id
26+
WHERE tags = ?;
27+
28+
/* name: CreateAuthor :execresult */
1829
INSERT INTO authors (name) VALUES (?);
1930

20-
/* name: CreateBook :exec */
31+
/* name: CreateBook :execresult */
2132
INSERT INTO books (
2233
author_id,
2334
isbn,
24-
booktype,
35+
book_type,
2536
title,
2637
yr,
2738
available,
@@ -32,6 +43,7 @@ INSERT INTO books (
3243
?,
3344
?,
3445
?,
46+
?,
3547
?
3648
);
3749

@@ -42,9 +54,10 @@ WHERE book_id = ?;
4254

4355
/* name: UpdateBookISBN :exec */
4456
UPDATE books
45-
SET title = ?, tags = :book_tags, isbn = ?
57+
SET title = ?, tags = ?, isbn = ?
4658
WHERE book_id = ?;
4759

4860
/* name: DeleteAuthorBeforeYear :exec */
4961
DELETE FROM books
50-
WHERE yr < sqlc.arg(min_publish_year) AND author_id = ?;
62+
WHERE yr < ? AND author_id = ?;
63+
-- WHERE yr < sqlc.arg(min_publish_year) AND author_id = ?;

0 commit comments

Comments
 (0)