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