1
0

db_test.go 3.1 KB

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