db_test.go 3.3 KB

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