db_test.go 3.4 KB

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