db_test.go 3.2 KB

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