capabilities_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package capabilities
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. "github.com/danfragoso/pizzasql-next/pkg/pgerror"
  7. "github.com/danfragoso/pizzasql-next/pkg/version"
  8. )
  9. func TestCurrentDocumentIsVersionedAndMarshalable(t *testing.T) {
  10. doc := Current()
  11. if doc.SchemaVersion != SchemaVersion {
  12. t.Fatalf("schema version = %d, want %d", doc.SchemaVersion, SchemaVersion)
  13. }
  14. if doc.Engine != "pizzasql" || doc.EngineVersion != version.String() {
  15. t.Fatalf("engine identity = %q/%q", doc.Engine, doc.EngineVersion)
  16. }
  17. body, err := json.Marshal(doc)
  18. if err != nil {
  19. t.Fatalf("marshal: %v", err)
  20. }
  21. var round Document
  22. if err := json.Unmarshal(body, &round); err != nil {
  23. t.Fatalf("unmarshal: %v", err)
  24. }
  25. if round.Constraints.Unique.SQLState != "23505" {
  26. t.Fatalf("unique SQLSTATE = %q, want 23505", round.Constraints.Unique.SQLState)
  27. }
  28. if round.Transactions.SerializationCode != "40001" {
  29. t.Fatalf("serialization SQLSTATE = %q, want 40001", round.Transactions.SerializationCode)
  30. }
  31. if round.ErrorCodes["0A000"] != "feature_not_supported" {
  32. t.Fatalf("0A000 mapping = %q", round.ErrorCodes["0A000"])
  33. }
  34. if !round.Notifications.Listen.Supported || !round.Notifications.Notify.Supported {
  35. t.Fatal("LISTEN/NOTIFY should be advertised as supported")
  36. }
  37. if round.Notifications.PayloadLimit != 8000 || round.Notifications.DurableReplay {
  38. t.Fatalf("unexpected notification limits: %+v", round.Notifications)
  39. }
  40. if !round.DDL.RenameColumnIndexes.Supported {
  41. t.Fatal("RENAME COLUMN must advertise index metadata rewriting")
  42. }
  43. }
  44. func TestJSONArrowOperatorsAreAdvertised(t *testing.T) {
  45. arrow := Current().JSON.ArrowOperators
  46. if !arrow.Supported {
  47. t.Fatal("-> and ->> should be advertised as supported")
  48. }
  49. if arrow.SQLState != "" {
  50. t.Fatalf("arrow operator SQLSTATE = %q, want none", arrow.SQLState)
  51. }
  52. if arrow.Status == "rejected" {
  53. t.Fatalf("arrow operator status = %q, want a supported status", arrow.Status)
  54. }
  55. }
  56. func TestTransactionalDDLIsAdvertised(t *testing.T) {
  57. ddl := Current().DDL
  58. if !ddl.Transactional.Supported {
  59. t.Fatalf("transactional DDL = %+v, want supported", ddl.Transactional)
  60. }
  61. if ddl.Transactional.Status != "staged_until_commit" {
  62. t.Fatalf("transactional DDL status = %q, want staged_until_commit", ddl.Transactional.Status)
  63. }
  64. }
  65. func TestAddColumnBackfillIsAdvertised(t *testing.T) {
  66. doc := Current()
  67. if !doc.DDL.AddColumnBackfill.Supported {
  68. t.Fatalf("add_column_backfill = %+v, want supported", doc.DDL.AddColumnBackfill)
  69. }
  70. if doc.DDL.AddColumnBackfill.Status != "rewrites_rows" {
  71. t.Fatalf("add_column_backfill status = %q, want rewrites_rows", doc.DDL.AddColumnBackfill.Status)
  72. }
  73. if doc.DDL.AddColumn.Status == "metadata_only" {
  74. t.Fatalf("add_column status = %q, but a DEFAULT rewrites rows", doc.DDL.AddColumn.Status)
  75. }
  76. }
  77. // The capability document must describe the current default semantics: constant
  78. // and whitelisted clock defaults are supported, the clock ones are evaluated on
  79. // write, and only the remaining non-constant forms are rejected.
  80. func TestNonConstantDefaultsAreAdvertisedAsEvaluatedAtWriteTime(t *testing.T) {
  81. defaults := Current().Constraints.DefaultValues
  82. if !defaults.Supported {
  83. t.Fatalf("default_values = %+v, want supported", defaults)
  84. }
  85. if defaults.Status != "evaluated_at_write_time" {
  86. t.Fatalf("default_values status = %q, want evaluated_at_write_time", defaults.Status)
  87. }
  88. if defaults.SQLState != "0A000" {
  89. t.Fatalf("default_values SQLSTATE = %q, want 0A000 for the remaining rejected forms", defaults.SQLState)
  90. }
  91. }
  92. func TestCheckConstraintIsAdvertised(t *testing.T) {
  93. doc := Current()
  94. check := doc.Constraints.Check
  95. if !check.Supported {
  96. t.Fatalf("check = %+v, want supported", check)
  97. }
  98. if check.Status != "enforced" {
  99. t.Fatalf("check status = %q, want enforced", check.Status)
  100. }
  101. if check.SQLState != pgerror.CodeCheckViolation {
  102. t.Fatalf("check SQLSTATE = %q, want %q", check.SQLState, pgerror.CodeCheckViolation)
  103. }
  104. if check.Notes == "" {
  105. t.Fatal("check notes must describe the enforcement surface")
  106. }
  107. if got := doc.ErrorCodes[pgerror.CodeCheckViolation]; got != "check_violation" {
  108. t.Fatalf("23514 mapping = %q, want check_violation", got)
  109. }
  110. }
  111. func TestDDLNotesDescribeCheckDependencies(t *testing.T) {
  112. doc := Current()
  113. if !strings.Contains(doc.DDL.DropColumn.Notes, "CHECK") || !strings.Contains(doc.DDL.DropColumn.Notes, "0A000") {
  114. t.Fatalf("drop_column notes = %q, want the persisted CHECK restriction", doc.DDL.DropColumn.Notes)
  115. }
  116. if !strings.Contains(doc.DDL.RenameColumn.Notes, "CHECK") || !strings.Contains(doc.DDL.RenameColumn.Notes, "0A000") {
  117. t.Fatalf("rename_column notes = %q, want the persisted CHECK restriction", doc.DDL.RenameColumn.Notes)
  118. }
  119. if !strings.Contains(doc.DDL.AddColumnBackfill.Notes, "23514") {
  120. t.Fatalf("add_column_backfill notes = %q, want the CHECK backfill failure", doc.DDL.AddColumnBackfill.Notes)
  121. }
  122. }
  123. // Composite primary keys are advertised as supported, with the SQLSTATE callers
  124. // should expect for a duplicate key.
  125. func TestCompositePrimaryKeyIsAdvertised(t *testing.T) {
  126. doc := Current()
  127. composite := doc.Constraints.CompositePK
  128. if !composite.Supported {
  129. t.Fatalf("composite_primary_key = %+v, want supported", composite)
  130. }
  131. if composite.Status == "rejected" {
  132. t.Fatalf("composite_primary_key status = %q, want a supported status", composite.Status)
  133. }
  134. if composite.SQLState != "23505" {
  135. t.Fatalf("composite_primary_key SQLSTATE = %q, want 23505", composite.SQLState)
  136. }
  137. if !doc.Constraints.PrimaryKey.Supported {
  138. t.Fatalf("primary_key = %+v, want supported", doc.Constraints.PrimaryKey)
  139. }
  140. body, err := json.Marshal(doc)
  141. if err != nil {
  142. t.Fatalf("marshal: %v", err)
  143. }
  144. if !strings.Contains(string(body), `"composite_primary_key":{"supported":true`) {
  145. t.Fatalf("document does not advertise composite_primary_key as supported: %s", body)
  146. }
  147. }
  148. // Foreign keys are advertised as enforced, with the SQLSTATE callers should
  149. // expect for a dangling reference, the referential actions that execute
  150. // immediately, and the explicit exclusions for deferred enforcement.
  151. func TestForeignKeyIsAdvertised(t *testing.T) {
  152. doc := Current()
  153. fk := doc.Constraints.ForeignKey
  154. if !fk.Supported {
  155. t.Fatalf("foreign_key = %+v, want supported", fk)
  156. }
  157. if fk.Status != "enforced" {
  158. t.Fatalf("foreign_key status = %q, want enforced", fk.Status)
  159. }
  160. if fk.SQLState != pgerror.CodeForeignKeyViolation {
  161. t.Fatalf("foreign_key SQLSTATE = %q, want %q", fk.SQLState, pgerror.CodeForeignKeyViolation)
  162. }
  163. for _, want := range []string{
  164. "UNIQUE", "COMMIT", "MATCH SIMPLE", "MATCH keyword", "self",
  165. "CASCADE", "SET NULL", "SET DEFAULT", "NOT NULL", "transitively",
  166. "autocommit", "no later than COMMIT", "statement savepoint", "hand-authored catalog",
  167. "O(FK component)", "DEFERRABLE", "0A000",
  168. "PRAGMA foreign_keys", "rejects OFF", "foreign_key_list", "database-wide foreign-key write gate",
  169. "DROP/RENAME COLUMN", "DROP TABLE", "pg_constraint",
  170. } {
  171. if !strings.Contains(fk.Notes, want) {
  172. t.Fatalf("foreign_key notes = %q, want it to mention %q", fk.Notes, want)
  173. }
  174. }
  175. if got := doc.ErrorCodes[pgerror.CodeForeignKeyViolation]; got != "foreign_key_violation" {
  176. t.Fatalf("23503 mapping = %q, want foreign_key_violation", got)
  177. }
  178. }