db_test.go 3.5 KB

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