catalog_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package postgresql
  2. import (
  3. "errors"
  4. "strconv"
  5. "strings"
  6. "testing"
  7. "github.com/kyleconroy/sqlc/internal/sql/sqlerr"
  8. "github.com/google/go-cmp/cmp"
  9. )
  10. func TestUpdateErrors(t *testing.T) {
  11. p := NewParser()
  12. for i, tc := range []struct {
  13. stmt string
  14. err *sqlerr.Error
  15. }{
  16. {
  17. `
  18. CREATE TABLE foo ();
  19. CREATE TABLE foo ();
  20. `,
  21. sqlerr.RelationExists("foo"),
  22. },
  23. {
  24. `
  25. CREATE TYPE foo AS ENUM ('bar');
  26. CREATE TYPE foo AS ENUM ('bar');
  27. `,
  28. sqlerr.TypeExists("foo"),
  29. },
  30. {
  31. `
  32. DROP TABLE foo;
  33. `,
  34. sqlerr.RelationNotFound("foo"),
  35. },
  36. {
  37. `
  38. DROP TYPE foo;
  39. `,
  40. sqlerr.TypeNotFound("foo"),
  41. },
  42. {
  43. `
  44. CREATE TABLE foo ();
  45. CREATE TABLE bar ();
  46. ALTER TABLE foo RENAME TO bar;
  47. `,
  48. sqlerr.RelationExists("bar"),
  49. },
  50. {
  51. `
  52. ALTER TABLE foo RENAME TO bar;
  53. `,
  54. sqlerr.RelationNotFound("foo"),
  55. },
  56. {
  57. `
  58. CREATE TABLE foo ();
  59. ALTER TABLE foo ADD COLUMN bar text;
  60. ALTER TABLE foo ADD COLUMN bar text;
  61. `,
  62. sqlerr.ColumnExists("foo", "bar"),
  63. },
  64. {
  65. `
  66. CREATE TABLE foo ();
  67. ALTER TABLE foo DROP COLUMN bar;
  68. `,
  69. sqlerr.ColumnNotFound("foo", "bar"),
  70. },
  71. {
  72. `
  73. CREATE TABLE foo ();
  74. ALTER TABLE foo ALTER COLUMN bar SET NOT NULL;
  75. `,
  76. sqlerr.ColumnNotFound("foo", "bar"),
  77. },
  78. {
  79. `
  80. CREATE TABLE foo ();
  81. ALTER TABLE foo ALTER COLUMN bar DROP NOT NULL;
  82. `,
  83. sqlerr.ColumnNotFound("foo", "bar"),
  84. },
  85. {
  86. `
  87. CREATE SCHEMA foo;
  88. CREATE SCHEMA foo;
  89. `,
  90. sqlerr.SchemaExists("foo"),
  91. },
  92. {
  93. `
  94. ALTER TABLE foo.baz SET SCHEMA bar;
  95. `,
  96. sqlerr.SchemaNotFound("foo"),
  97. },
  98. {
  99. `
  100. CREATE SCHEMA foo;
  101. ALTER TABLE foo.baz SET SCHEMA bar;
  102. `,
  103. sqlerr.RelationNotFound("baz"),
  104. },
  105. {
  106. `
  107. CREATE SCHEMA foo;
  108. CREATE TABLE foo.baz ();
  109. ALTER TABLE foo.baz SET SCHEMA bar;
  110. `,
  111. sqlerr.SchemaNotFound("bar"),
  112. },
  113. {
  114. `
  115. DROP SCHEMA bar;
  116. `,
  117. sqlerr.SchemaNotFound("bar"),
  118. },
  119. {
  120. `
  121. ALTER TABLE foo RENAME bar TO baz;
  122. `,
  123. sqlerr.RelationNotFound("foo"),
  124. },
  125. {
  126. `
  127. CREATE TABLE foo ();
  128. ALTER TABLE foo RENAME bar TO baz;
  129. `,
  130. sqlerr.ColumnNotFound("foo", "bar"),
  131. },
  132. {
  133. `
  134. CREATE TABLE foo (bar text, baz text);
  135. ALTER TABLE foo RENAME bar TO baz;
  136. `,
  137. sqlerr.ColumnExists("foo", "baz"),
  138. },
  139. } {
  140. test := tc
  141. t.Run(strconv.Itoa(i), func(t *testing.T) {
  142. stmts, err := p.Parse(strings.NewReader(test.stmt))
  143. if err != nil {
  144. t.Log(test.stmt)
  145. t.Fatal(err)
  146. }
  147. c := NewCatalog()
  148. err = c.Build(stmts)
  149. if err == nil {
  150. t.Log(test.stmt)
  151. t.Fatal("err was nil")
  152. }
  153. var actual *sqlerr.Error
  154. if !errors.As(err, &actual) {
  155. t.Fatalf("err is not *sqlerr.Error: %#v", err)
  156. }
  157. if diff := cmp.Diff(test.err.Error(), actual.Error()); diff != "" {
  158. t.Log(test.stmt)
  159. t.Errorf("error mismatch: \n%s", diff)
  160. }
  161. })
  162. }
  163. }