MCPcopy
hub / github.com/sqlc-dev/sqlc / TestBooks

Function TestBooks

examples/booktest/postgresql/db_test.go:16–174  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

14)
15
16func TestBooks(t *testing.T) {
17 ctx := context.Background()
18 uri := local.PostgreSQL(t, []string{"schema.sql"})
19 db, err := pgx.Connect(ctx, uri)
20 if err != nil {
21 t.Fatal(err)
22 }
23 defer db.Close(ctx)
24
25 dq := New(db)
26
27 // create an author
28 a, err := dq.CreateAuthor(ctx, "Unknown Master")
29 if err != nil {
30 t.Fatal(err)
31 }
32
33 // create transaction
34 tx, err := db.Begin(ctx)
35 if err != nil {
36 t.Fatal(err)
37 }
38
39 tq := dq.WithTx(tx)
40
41 // save first book
42 now := pgtype.Timestamptz{Time: time.Now(), Valid: true}
43 _, err = tq.CreateBook(ctx, CreateBookParams{
44 AuthorID: a.AuthorID,
45 Isbn: "1",
46 Title: "my book title",
47 BookType: BookTypeFICTION,
48 Year: 2016,
49 Available: now,
50 Tags: []string{},
51 })
52 if err != nil {
53 t.Fatal(err)
54 }
55
56 // save second book
57 b1, err := tq.CreateBook(ctx, CreateBookParams{
58 AuthorID: a.AuthorID,
59 Isbn: "2",
60 Title: "the second book",
61 BookType: BookTypeFICTION,
62 Year: 2016,
63 Available: now,
64 Tags: []string{"cool", "unique"},
65 })
66 if err != nil {
67 t.Fatal(err)
68 }
69
70 // update the title and tags
71 err = tq.UpdateBook(ctx, UpdateBookParams{
72 BookID: b1.BookID,
73 Title: "changed second title",

Callers

nothing calls this directly

Calls 15

PostgreSQLFunction · 0.92
SayHelloMethod · 0.80
NewFunction · 0.70
CloseMethod · 0.65
CreateAuthorMethod · 0.65
CreateBookMethod · 0.65
UpdateBookMethod · 0.65
FormatMethod · 0.65
GetAuthorMethod · 0.65
DeleteBookMethod · 0.65
WithTxMethod · 0.45
UpdateBookISBNMethod · 0.45

Tested by

no test coverage detected