db_test.go 903 B

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