1
0

db_test.go 961 B

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