capabilities_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 TestAddColumnBackfillIsAdvertised(t *testing.T) {
  57. doc := Current()
  58. if !doc.DDL.AddColumnBackfill.Supported {
  59. t.Fatalf("add_column_backfill = %+v, want supported", doc.DDL.AddColumnBackfill)
  60. }
  61. if doc.DDL.AddColumnBackfill.Status != "rewrites_rows" {
  62. t.Fatalf("add_column_backfill status = %q, want rewrites_rows", doc.DDL.AddColumnBackfill.Status)
  63. }
  64. if doc.DDL.AddColumn.Status == "metadata_only" {
  65. t.Fatalf("add_column status = %q, but a constant DEFAULT rewrites rows", doc.DDL.AddColumn.Status)
  66. }
  67. }
  68. func TestCheckConstraintIsAdvertised(t *testing.T) {
  69. doc := Current()
  70. check := doc.Constraints.Check
  71. if !check.Supported {
  72. t.Fatalf("check = %+v, want supported", check)
  73. }
  74. if check.Status != "enforced" {
  75. t.Fatalf("check status = %q, want enforced", check.Status)
  76. }
  77. if check.SQLState != pgerror.CodeCheckViolation {
  78. t.Fatalf("check SQLSTATE = %q, want %q", check.SQLState, pgerror.CodeCheckViolation)
  79. }
  80. if check.Notes == "" {
  81. t.Fatal("check notes must describe the enforcement surface")
  82. }
  83. if got := doc.ErrorCodes[pgerror.CodeCheckViolation]; got != "check_violation" {
  84. t.Fatalf("23514 mapping = %q, want check_violation", got)
  85. }
  86. }
  87. func TestDDLNotesDescribeCheckDependencies(t *testing.T) {
  88. doc := Current()
  89. if !strings.Contains(doc.DDL.DropColumn.Notes, "CHECK") || !strings.Contains(doc.DDL.DropColumn.Notes, "0A000") {
  90. t.Fatalf("drop_column notes = %q, want the persisted CHECK restriction", doc.DDL.DropColumn.Notes)
  91. }
  92. if !strings.Contains(doc.DDL.RenameColumn.Notes, "CHECK") || !strings.Contains(doc.DDL.RenameColumn.Notes, "0A000") {
  93. t.Fatalf("rename_column notes = %q, want the persisted CHECK restriction", doc.DDL.RenameColumn.Notes)
  94. }
  95. if !strings.Contains(doc.DDL.AddColumnBackfill.Notes, "23514") {
  96. t.Fatalf("add_column_backfill notes = %q, want the CHECK backfill failure", doc.DDL.AddColumnBackfill.Notes)
  97. }
  98. }