|
| 1 | +// |
| 2 | +// QueryClassifierTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import Testing |
| 8 | +@testable import TablePro |
| 9 | + |
| 10 | +@Suite("QueryClassifier isExplainStatement") |
| 11 | +struct QueryClassifierExplainTests { |
| 12 | + @Test("Detects EXPLAIN and EXPLAIN ANALYZE variants") |
| 13 | + func detectsExplainVariants() { |
| 14 | + #expect(QueryClassifier.isExplainStatement("EXPLAIN SELECT * FROM users")) |
| 15 | + #expect(QueryClassifier.isExplainStatement("explain analyze select o.user_id from orders o")) |
| 16 | + #expect(QueryClassifier.isExplainStatement("EXPLAIN ANALYZE SELECT 1")) |
| 17 | + #expect(QueryClassifier.isExplainStatement("EXPLAIN FORMAT=JSON SELECT 1")) |
| 18 | + #expect(QueryClassifier.isExplainStatement("EXPLAIN (ANALYZE, BUFFERS) SELECT 1")) |
| 19 | + #expect(QueryClassifier.isExplainStatement("EXPLAIN(FORMAT JSON) SELECT 1")) |
| 20 | + #expect(QueryClassifier.isExplainStatement("EXPLAIN QUERY PLAN SELECT 1")) |
| 21 | + } |
| 22 | + |
| 23 | + @Test("Detects MariaDB ANALYZE statements") |
| 24 | + func detectsAnalyzeVariants() { |
| 25 | + #expect(QueryClassifier.isExplainStatement("ANALYZE FORMAT=JSON SELECT 1")) |
| 26 | + #expect(QueryClassifier.isExplainStatement("analyze select 1")) |
| 27 | + } |
| 28 | + |
| 29 | + @Test("Ignores leading whitespace, newlines, and comments") |
| 30 | + func handlesWhitespaceAndComments() { |
| 31 | + #expect(QueryClassifier.isExplainStatement(" EXPLAIN SELECT 1")) |
| 32 | + #expect(QueryClassifier.isExplainStatement("\n\tEXPLAIN\nSELECT 1")) |
| 33 | + #expect(QueryClassifier.isExplainStatement("-- plan check\nEXPLAIN SELECT 1")) |
| 34 | + #expect(QueryClassifier.isExplainStatement("/* warm cache */ EXPLAIN ANALYZE SELECT 1")) |
| 35 | + } |
| 36 | + |
| 37 | + @Test("Does not match DESCRIBE, identifiers, or other statements") |
| 38 | + func rejectsNonExplain() { |
| 39 | + #expect(!QueryClassifier.isExplainStatement("DESCRIBE users")) |
| 40 | + #expect(!QueryClassifier.isExplainStatement("DESC users")) |
| 41 | + #expect(!QueryClassifier.isExplainStatement("SELECT * FROM explain_logs")) |
| 42 | + #expect(!QueryClassifier.isExplainStatement("SELECT explain FROM t")) |
| 43 | + #expect(!QueryClassifier.isExplainStatement("EXPLAINING SELECT 1")) |
| 44 | + #expect(!QueryClassifier.isExplainStatement("EXPLAIN")) |
| 45 | + #expect(!QueryClassifier.isExplainStatement("")) |
| 46 | + } |
| 47 | +} |
0 commit comments