MCPcopy
hub / github.com/sqlc-dev/sqlc / TestBooks

Function TestBooks

examples/booktest/mysql/db_test.go:16–174  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestBooks(t *testing.T) {
17 ctx := context.Background()
18 uri := local.MySQL(t, []string{"schema.sql"})
19 db, err := sql.Open("mysql", uri)
20 if err != nil {
21 t.Fatal(err)
22 }
23 defer db.Close()
24
25 dq := New(db)
26
27 // create an author
28 result, err := dq.CreateAuthor(ctx, "Unknown Master")
29 if err != nil {
30 t.Fatal(err)
31 }
32 authorID, err := result.LastInsertId()
33 if err != nil {
34 t.Fatal(err)
35 }
36
37 // create transaction
38 tx, err := db.Begin()
39 if err != nil {
40 t.Fatal(err)
41 }
42
43 tq := dq.WithTx(tx)
44
45 // save first book
46 now := time.Now()
47 _, err = tq.CreateBook(ctx, CreateBookParams{
48 AuthorID: int32(authorID),
49 Isbn: "1",
50 Title: "my book title",
51 BookType: BooksBookTypeFICTION,
52 Yr: 2016,
53 Available: now,
54 })
55 if err != nil {
56 t.Fatal(err)
57 }
58
59 // save second book
60 result, err = tq.CreateBook(ctx, CreateBookParams{
61 AuthorID: int32(authorID),
62 Isbn: "2",
63 Title: "the second book",
64 BookType: BooksBookTypeFICTION,
65 Yr: 2016,
66 Available: now,
67 Tags: "cool,unique",
68 })
69 if err != nil {
70 t.Fatal(err)
71 }
72 bookOneID, err := result.LastInsertId()
73 if err != nil {

Callers

nothing calls this directly

Calls 15

MySQLFunction · 0.92
OpenMethod · 0.80
NewFunction · 0.70
CloseMethod · 0.65
CreateAuthorMethod · 0.65
CreateBookMethod · 0.65
UpdateBookMethod · 0.65
FormatMethod · 0.65
GetAuthorMethod · 0.65
DeleteBookMethod · 0.65
WithTxMethod · 0.45
UpdateBookISBNMethod · 0.45

Tested by

no test coverage detected