db_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //go:build examples
  2. // +build examples
  3. package ondeck
  4. import (
  5. "context"
  6. "database/sql"
  7. "testing"
  8. "github.com/google/go-cmp/cmp"
  9. _ "github.com/lib/pq"
  10. "github.com/sqlc-dev/sqlc/internal/sqltest/hosted"
  11. )
  12. func runOnDeckQueries(t *testing.T, q *Queries) {
  13. ctx := context.Background()
  14. city, err := q.CreateCity(ctx, CreateCityParams{
  15. Slug: "san-francisco",
  16. Name: "San Francisco",
  17. })
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. venueID, err := q.CreateVenue(ctx, CreateVenueParams{
  22. Slug: "the-fillmore",
  23. Name: "The Fillmore",
  24. City: city.Slug,
  25. SpotifyPlaylist: "spotify:uri",
  26. Status: StatusOpen,
  27. Statuses: []Status{StatusOpen, StatusClosed},
  28. Tags: []string{"rock", "punk"},
  29. })
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. venue, err := q.GetVenue(ctx, GetVenueParams{
  34. Slug: "the-fillmore",
  35. City: city.Slug,
  36. })
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if diff := cmp.Diff(venue.ID, venueID); diff != "" {
  41. t.Errorf("venue ID mismatch:\n%s", diff)
  42. }
  43. {
  44. actual, err := q.GetCity(ctx, city.Slug)
  45. if err != nil {
  46. t.Error(err)
  47. }
  48. if diff := cmp.Diff(actual, city); diff != "" {
  49. t.Errorf("get city mismatch:\n%s", diff)
  50. }
  51. }
  52. {
  53. actual, err := q.VenueCountByCity(ctx)
  54. if err != nil {
  55. t.Error(err)
  56. }
  57. if diff := cmp.Diff(actual, []VenueCountByCityRow{
  58. {city.Slug, 1},
  59. }); diff != "" {
  60. t.Errorf("venue county mismatch:\n%s", diff)
  61. }
  62. }
  63. {
  64. actual, err := q.ListCities(ctx)
  65. if err != nil {
  66. t.Error(err)
  67. }
  68. if diff := cmp.Diff(actual, []City{city}); diff != "" {
  69. t.Errorf("list city mismatch:\n%s", diff)
  70. }
  71. }
  72. {
  73. actual, err := q.ListVenues(ctx, city.Slug)
  74. if err != nil {
  75. t.Error(err)
  76. }
  77. if diff := cmp.Diff(actual, []Venue{venue}); diff != "" {
  78. t.Errorf("list venue mismatch:\n%s", diff)
  79. }
  80. }
  81. {
  82. err := q.UpdateCityName(ctx, UpdateCityNameParams{
  83. Slug: city.Slug,
  84. Name: "SF",
  85. })
  86. if err != nil {
  87. t.Error(err)
  88. }
  89. }
  90. {
  91. id, err := q.UpdateVenueName(ctx, UpdateVenueNameParams{
  92. Slug: venue.Slug,
  93. Name: "Fillmore",
  94. })
  95. if err != nil {
  96. t.Error(err)
  97. }
  98. if diff := cmp.Diff(id, venue.ID); diff != "" {
  99. t.Errorf("update venue mismatch:\n%s", diff)
  100. }
  101. }
  102. {
  103. err := q.DeleteVenue(ctx, venue.Slug)
  104. if err != nil {
  105. t.Error(err)
  106. }
  107. }
  108. }
  109. func TestPrepared(t *testing.T) {
  110. t.Parallel()
  111. uri := hosted.PostgreSQL(t, []string{"schema"})
  112. db, err := sql.Open("postgres", uri)
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. defer db.Close()
  117. q, err := Prepare(context.Background(), db)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. runOnDeckQueries(t, q)
  122. }
  123. func TestQueries(t *testing.T) {
  124. t.Parallel()
  125. uri := hosted.PostgreSQL(t, []string{"schema"})
  126. db, err := sql.Open("postgres", uri)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. defer db.Close()
  131. runOnDeckQueries(t, New(db))
  132. }