-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathdb_test.go
More file actions
48 lines (40 loc) · 922 Bytes
/
db_test.go
File metadata and controls
48 lines (40 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//go:build examples
package authors
import (
"context"
"database/sql"
"testing"
"github.com/sqlc-dev/sqlc/internal/sqltest"
)
func TestAuthors(t *testing.T) {
sdb, cleanup := sqltest.SQLite(t, []string{"schema.sql"})
defer sdb.Close()
defer cleanup()
ctx := context.Background()
db := New(sdb)
// list all authors
authors, err := db.ListAuthors(ctx)
if err != nil {
t.Fatal(err)
}
t.Log(authors)
// create an author
result, err := db.CreateAuthor(ctx, CreateAuthorParams{
Name: "Brian Kernighan",
Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
})
if err != nil {
t.Fatal(err)
}
authorID, err := result.LastInsertId()
if err != nil {
t.Fatal(err)
}
t.Log(authorID)
// get the author we just inserted
fetchedAuthor, err := db.GetAuthor(ctx, authorID)
if err != nil {
t.Fatal(err)
}
t.Log(fetchedAuthor)
}