where_validation_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package engine
  2. import (
  3. "context"
  4. "strconv"
  5. "testing"
  6. )
  7. // TestWhereClauseValidation tests WHERE clause functionality with various conditions
  8. func TestWhereClauseValidation(t *testing.T) {
  9. engine := NewTestSQLEngine()
  10. t.Log("WHERE CLAUSE VALIDATION TESTS")
  11. t.Log("==============================")
  12. // Test 1: Baseline - get all rows to understand the data
  13. baselineResult, err := engine.ExecuteSQL(context.Background(), "SELECT id FROM user_events")
  14. if err != nil {
  15. t.Fatalf("Baseline query failed: %v", err)
  16. }
  17. t.Logf("Baseline data - Total rows: %d", len(baselineResult.Rows))
  18. if len(baselineResult.Rows) > 0 {
  19. t.Logf("Sample IDs: %s, %s, %s",
  20. baselineResult.Rows[0][0].ToString(),
  21. baselineResult.Rows[1][0].ToString(),
  22. baselineResult.Rows[2][0].ToString())
  23. }
  24. // Test 2: Specific ID match (should return 1 row)
  25. firstId := baselineResult.Rows[0][0].ToString()
  26. specificResult, err := engine.ExecuteSQL(context.Background(),
  27. "SELECT id FROM user_events WHERE id = "+firstId)
  28. if err != nil {
  29. t.Fatalf("Specific ID query failed: %v", err)
  30. }
  31. t.Logf("WHERE id = %s: %d rows", firstId, len(specificResult.Rows))
  32. if len(specificResult.Rows) == 1 {
  33. t.Logf("✅ Specific ID filtering works correctly")
  34. } else {
  35. t.Errorf("❌ Expected 1 row, got %d rows", len(specificResult.Rows))
  36. }
  37. // Test 3: Range filtering (find actual data ranges)
  38. // First, find the min and max IDs in our data
  39. var minId, maxId int64 = 999999999, 0
  40. for _, row := range baselineResult.Rows {
  41. if idVal, err := strconv.ParseInt(row[0].ToString(), 10, 64); err == nil {
  42. if idVal < minId {
  43. minId = idVal
  44. }
  45. if idVal > maxId {
  46. maxId = idVal
  47. }
  48. }
  49. }
  50. t.Logf("Data range: min ID = %d, max ID = %d", minId, maxId)
  51. // Test with a threshold between min and max
  52. threshold := (minId + maxId) / 2
  53. rangeResult, err := engine.ExecuteSQL(context.Background(),
  54. "SELECT id FROM user_events WHERE id > "+strconv.FormatInt(threshold, 10))
  55. if err != nil {
  56. t.Fatalf("Range query failed: %v", err)
  57. }
  58. t.Logf("WHERE id > %d: %d rows", threshold, len(rangeResult.Rows))
  59. // Verify all returned IDs are > threshold
  60. allCorrect := true
  61. for _, row := range rangeResult.Rows {
  62. if idVal, err := strconv.ParseInt(row[0].ToString(), 10, 64); err == nil {
  63. if idVal <= threshold {
  64. t.Errorf("❌ Found ID %d which should be filtered out (≤ %d)", idVal, threshold)
  65. allCorrect = false
  66. }
  67. }
  68. }
  69. if allCorrect && len(rangeResult.Rows) > 0 {
  70. t.Logf("✅ Range filtering works correctly - all returned IDs > %d", threshold)
  71. } else if len(rangeResult.Rows) == 0 {
  72. t.Logf("✅ Range filtering works correctly - no IDs > %d in data", threshold)
  73. }
  74. // Test 4: String filtering
  75. statusResult, err := engine.ExecuteSQL(context.Background(),
  76. "SELECT id, status FROM user_events WHERE status = 'active'")
  77. if err != nil {
  78. t.Fatalf("Status query failed: %v", err)
  79. }
  80. t.Logf("WHERE status = 'active': %d rows", len(statusResult.Rows))
  81. // Verify all returned rows have status = 'active'
  82. statusCorrect := true
  83. for _, row := range statusResult.Rows {
  84. if len(row) > 1 && row[1].ToString() != "active" {
  85. t.Errorf("❌ Found status '%s' which should be filtered out", row[1].ToString())
  86. statusCorrect = false
  87. }
  88. }
  89. if statusCorrect {
  90. t.Logf("✅ String filtering works correctly")
  91. }
  92. // Test 5: Comparison with actual real-world case
  93. t.Log("\n🎯 TESTING REAL-WORLD CASE:")
  94. realWorldResult, err := engine.ExecuteSQL(context.Background(),
  95. "SELECT id FROM user_events WHERE id > 10000000 LIMIT 10 OFFSET 5")
  96. if err != nil {
  97. t.Fatalf("Real-world query failed: %v", err)
  98. }
  99. t.Logf("Real-world query returned: %d rows", len(realWorldResult.Rows))
  100. // Check if any IDs are <= 10,000,000 (should be 0)
  101. violationCount := 0
  102. for _, row := range realWorldResult.Rows {
  103. if idVal, err := strconv.ParseInt(row[0].ToString(), 10, 64); err == nil {
  104. if idVal <= 10000000 {
  105. violationCount++
  106. }
  107. }
  108. }
  109. if violationCount == 0 {
  110. t.Logf("✅ Real-world case FIXED: No violations found")
  111. } else {
  112. t.Errorf("❌ Real-world case FAILED: %d violations found", violationCount)
  113. }
  114. }
  115. // TestWhereClauseComparisonOperators tests all comparison operators
  116. func TestWhereClauseComparisonOperators(t *testing.T) {
  117. engine := NewTestSQLEngine()
  118. // Get baseline data
  119. baselineResult, _ := engine.ExecuteSQL(context.Background(), "SELECT id FROM user_events")
  120. if len(baselineResult.Rows) == 0 {
  121. t.Skip("No test data available")
  122. return
  123. }
  124. // Use the second ID as our test value
  125. testId := baselineResult.Rows[1][0].ToString()
  126. operators := []struct {
  127. op string
  128. desc string
  129. expectRows bool
  130. }{
  131. {"=", "equals", true},
  132. {"!=", "not equals", true},
  133. {">", "greater than", false}, // Depends on data
  134. {"<", "less than", true}, // Should have some results
  135. {">=", "greater or equal", true},
  136. {"<=", "less or equal", true},
  137. }
  138. t.Logf("Testing comparison operators with ID = %s", testId)
  139. for _, op := range operators {
  140. sql := "SELECT id FROM user_events WHERE id " + op.op + " " + testId
  141. result, err := engine.ExecuteSQL(context.Background(), sql)
  142. if err != nil {
  143. t.Errorf("❌ Operator %s failed: %v", op.op, err)
  144. continue
  145. }
  146. t.Logf("WHERE id %s %s: %d rows (%s)", op.op, testId, len(result.Rows), op.desc)
  147. // Basic validation - should not return more rows than baseline
  148. if len(result.Rows) > len(baselineResult.Rows) {
  149. t.Errorf("❌ Operator %s returned more rows than baseline", op.op)
  150. }
  151. }
  152. }