1
0

db_test.go 3.2 KB

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