1
0

db_test.go 876 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //go:build examples
  2. // +build examples
  3. package authors
  4. import (
  5. "context"
  6. "database/sql"
  7. "testing"
  8. "github.com/kyleconroy/sqlc/internal/sqltest"
  9. )
  10. func TestAuthors(t *testing.T) {
  11. sdb, cleanup := sqltest.PostgreSQL(t, []string{"schema.sql"})
  12. defer cleanup()
  13. ctx := context.Background()
  14. db := New(sdb)
  15. // list all authors
  16. authors, err := db.ListAuthors(ctx)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. t.Log(authors)
  21. // create an author
  22. insertedAuthor, err := db.CreateAuthor(ctx, CreateAuthorParams{
  23. Name: "Brian Kernighan",
  24. Bio: sql.NullString{String: "Co-author of The C Programming Language and The Go Programming Language", Valid: true},
  25. })
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. t.Log(insertedAuthor)
  30. // get the author we just inserted
  31. fetchedAuthor, err := db.GetAuthor(ctx, insertedAuthor.ID)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. t.Log(fetchedAuthor)
  36. }