notify_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package parser
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/danfragoso/pizzasql-next/pkg/lexer"
  6. )
  7. func parseNotifySQL(t *testing.T, sql string) (Statement, error) {
  8. t.Helper()
  9. return New(lexer.New(sql)).Parse()
  10. }
  11. func TestParseListen(t *testing.T) {
  12. stmt, err := parseNotifySQL(t, `LISTEN orders`)
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. listen, ok := stmt.(*ListenStmt)
  17. if !ok || listen.Channel != "orders" {
  18. t.Fatalf("unexpected statement: %#v", stmt)
  19. }
  20. quoted, err := parseNotifySQL(t, `LISTEN "Mixed Case"`)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. if quoted.(*ListenStmt).Channel != "Mixed Case" {
  25. t.Fatalf("quoted channel = %q", quoted.(*ListenStmt).Channel)
  26. }
  27. }
  28. func TestParseUnlisten(t *testing.T) {
  29. stmt, err := parseNotifySQL(t, `UNLISTEN orders`)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. unlisten := stmt.(*UnlistenStmt)
  34. if unlisten.All || unlisten.Channel != "orders" {
  35. t.Fatalf("unexpected unlisten: %#v", unlisten)
  36. }
  37. all, err := parseNotifySQL(t, `UNLISTEN *`)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if !all.(*UnlistenStmt).All {
  42. t.Fatal("expected UNLISTEN *")
  43. }
  44. }
  45. func TestParseNotify(t *testing.T) {
  46. stmt, err := parseNotifySQL(t, `NOTIFY orders, 'created'`)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. notify := stmt.(*NotifyStmt)
  51. if notify.Channel != "orders" || notify.Payload != "created" {
  52. t.Fatalf("unexpected notify: %#v", notify)
  53. }
  54. empty, err := parseNotifySQL(t, `NOTIFY orders`)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if empty.(*NotifyStmt).Payload != "" {
  59. t.Fatalf("payload = %q, want empty", empty.(*NotifyStmt).Payload)
  60. }
  61. escaped, err := parseNotifySQL(t, `NOTIFY orders, 'it''s here'`)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if escaped.(*NotifyStmt).Payload != "it's here" {
  66. t.Fatalf("payload = %q", escaped.(*NotifyStmt).Payload)
  67. }
  68. }
  69. func TestParseNotifyPayloadLimit(t *testing.T) {
  70. payload := strings.Repeat("a", 8000)
  71. if _, err := parseNotifySQL(t, `NOTIFY ch, '`+payload+`'`); err != nil {
  72. t.Fatalf("8000-byte payload rejected: %v", err)
  73. }
  74. if _, err := parseNotifySQL(t, `NOTIFY ch, '`+payload+`a'`); err == nil {
  75. t.Fatal("expected 8001-byte payload to be rejected")
  76. }
  77. }
  78. func TestParseChannelNameLimit(t *testing.T) {
  79. channel := strings.Repeat("a", 63)
  80. if _, err := parseNotifySQL(t, `LISTEN `+channel); err != nil {
  81. t.Fatalf("63-byte channel rejected: %v", err)
  82. }
  83. if _, err := parseNotifySQL(t, `LISTEN `+channel+`a`); err == nil {
  84. t.Fatal("expected 64-byte channel to be rejected")
  85. }
  86. }
  87. func TestParseNotifyInvalid(t *testing.T) {
  88. for _, sql := range []string{
  89. `LISTEN`,
  90. `UNLISTEN`,
  91. `NOTIFY ch,`,
  92. `NOTIFY ch, 42`,
  93. `NOTIFY ch 'payload'`,
  94. `LISTEN 42`,
  95. } {
  96. if _, err := parseNotifySQL(t, sql); err == nil {
  97. t.Errorf("expected %q to fail", sql)
  98. }
  99. }
  100. }
  101. func TestNotifyIsNotReservedAsColumnName(t *testing.T) {
  102. stmt, err := parseNotifySQL(t, `SELECT notify, listen FROM events`)
  103. if err != nil {
  104. t.Fatalf("contextual keywords should remain usable as columns: %v", err)
  105. }
  106. sel := stmt.(*SelectStmt)
  107. if len(sel.Columns) != 2 {
  108. t.Fatalf("columns = %d, want 2", len(sel.Columns))
  109. }
  110. }