postgresql_only_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package engine
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. )
  7. // TestPostgreSQLOnlySupport ensures that non-PostgreSQL syntax is properly rejected
  8. func TestPostgreSQLOnlySupport(t *testing.T) {
  9. engine := NewTestSQLEngine()
  10. testCases := []struct {
  11. name string
  12. sql string
  13. shouldError bool
  14. errorMsg string
  15. desc string
  16. }{
  17. // Test that MySQL backticks are not supported for identifiers
  18. {
  19. name: "MySQL_Backticks_Table",
  20. sql: "SELECT * FROM `user_events` LIMIT 1",
  21. shouldError: true,
  22. desc: "MySQL backticks for table names should be rejected",
  23. },
  24. {
  25. name: "MySQL_Backticks_Column",
  26. sql: "SELECT `column_name` FROM user_events LIMIT 1",
  27. shouldError: true,
  28. desc: "MySQL backticks for column names should be rejected",
  29. },
  30. // Test that PostgreSQL double quotes work (should NOT error)
  31. {
  32. name: "PostgreSQL_Double_Quotes_OK",
  33. sql: `SELECT "user_id" FROM user_events LIMIT 1`,
  34. shouldError: false,
  35. desc: "PostgreSQL double quotes for identifiers should work",
  36. },
  37. // Note: MySQL functions like YEAR(), MONTH() may parse but won't have proper implementations
  38. // They're removed from the engine so they won't work correctly, but we don't explicitly reject them
  39. // Test that PostgreSQL EXTRACT works (should NOT error)
  40. {
  41. name: "PostgreSQL_EXTRACT_OK",
  42. sql: "SELECT EXTRACT(YEAR FROM CURRENT_DATE) FROM user_events LIMIT 1",
  43. shouldError: false,
  44. desc: "PostgreSQL EXTRACT function should work",
  45. },
  46. // Test that single quotes work for string literals but not identifiers
  47. {
  48. name: "Single_Quotes_String_Literal_OK",
  49. sql: "SELECT 'hello world' FROM user_events LIMIT 1",
  50. shouldError: false,
  51. desc: "Single quotes for string literals should work",
  52. },
  53. }
  54. passCount := 0
  55. for _, tc := range testCases {
  56. t.Run(tc.name, func(t *testing.T) {
  57. result, err := engine.ExecuteSQL(context.Background(), tc.sql)
  58. if tc.shouldError {
  59. // We expect this query to fail
  60. if err == nil && result.Error == nil {
  61. t.Errorf("❌ Expected error for %s, but query succeeded", tc.desc)
  62. return
  63. }
  64. // Check for specific error message if provided
  65. if tc.errorMsg != "" {
  66. errorText := ""
  67. if err != nil {
  68. errorText = err.Error()
  69. } else if result.Error != nil {
  70. errorText = result.Error.Error()
  71. }
  72. if !strings.Contains(errorText, tc.errorMsg) {
  73. t.Errorf("❌ Expected error containing '%s', got: %s", tc.errorMsg, errorText)
  74. return
  75. }
  76. }
  77. t.Logf("CORRECTLY REJECTED: %s", tc.desc)
  78. passCount++
  79. } else {
  80. // We expect this query to succeed
  81. if err != nil {
  82. t.Errorf("Unexpected error for %s: %v", tc.desc, err)
  83. return
  84. }
  85. if result.Error != nil {
  86. t.Errorf("Unexpected result error for %s: %v", tc.desc, result.Error)
  87. return
  88. }
  89. t.Logf("CORRECTLY ACCEPTED: %s", tc.desc)
  90. passCount++
  91. }
  92. })
  93. }
  94. t.Logf("PostgreSQL-only compliance: %d/%d tests passed", passCount, len(testCases))
  95. }