2
0

parser_test.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. package parser
  2. import (
  3. "testing"
  4. "github.com/danfragoso/pizzasql-next/pkg/lexer"
  5. )
  6. func parse(t *testing.T, input string) Statement {
  7. t.Helper()
  8. l := lexer.New(input)
  9. p := New(l)
  10. stmt, err := p.Parse()
  11. if err != nil {
  12. t.Fatalf("parse error: %v", err)
  13. }
  14. return stmt
  15. }
  16. func parseExpr(t *testing.T, input string) Expr {
  17. t.Helper()
  18. // Wrap in SELECT to parse as expression
  19. l := lexer.New("SELECT " + input)
  20. p := New(l)
  21. stmt, err := p.Parse()
  22. if err != nil {
  23. t.Fatalf("parse error: %v", err)
  24. }
  25. sel := stmt.(*SelectStmt)
  26. return sel.Columns[0].Expr
  27. }
  28. // SELECT statement tests
  29. func TestParseSelectStar(t *testing.T) {
  30. stmt := parse(t, "SELECT * FROM users")
  31. sel, ok := stmt.(*SelectStmt)
  32. if !ok {
  33. t.Fatalf("expected SelectStmt, got %T", stmt)
  34. }
  35. if len(sel.Columns) != 1 || !sel.Columns[0].Star {
  36. t.Error("expected SELECT *")
  37. }
  38. if len(sel.From) != 1 || sel.From[0].Name != "users" {
  39. t.Error("expected FROM users")
  40. }
  41. }
  42. func TestParseSelectColumns(t *testing.T) {
  43. stmt := parse(t, "SELECT id, name, email FROM users")
  44. sel := stmt.(*SelectStmt)
  45. if len(sel.Columns) != 3 {
  46. t.Fatalf("expected 3 columns, got %d", len(sel.Columns))
  47. }
  48. cols := []string{"id", "name", "email"}
  49. for i, col := range sel.Columns {
  50. ref, ok := col.Expr.(*ColumnRef)
  51. if !ok {
  52. t.Errorf("column %d: expected ColumnRef", i)
  53. continue
  54. }
  55. if ref.Column != cols[i] {
  56. t.Errorf("column %d: expected %s, got %s", i, cols[i], ref.Column)
  57. }
  58. }
  59. }
  60. func TestParseSelectWithAlias(t *testing.T) {
  61. stmt := parse(t, "SELECT id AS user_id, name AS full_name FROM users u")
  62. sel := stmt.(*SelectStmt)
  63. if sel.Columns[0].Alias != "user_id" {
  64. t.Errorf("expected alias user_id, got %s", sel.Columns[0].Alias)
  65. }
  66. if sel.Columns[1].Alias != "full_name" {
  67. t.Errorf("expected alias full_name, got %s", sel.Columns[1].Alias)
  68. }
  69. if sel.From[0].Alias != "u" {
  70. t.Errorf("expected table alias u, got %s", sel.From[0].Alias)
  71. }
  72. }
  73. func TestParseSelectDistinct(t *testing.T) {
  74. stmt := parse(t, "SELECT DISTINCT name FROM users")
  75. sel := stmt.(*SelectStmt)
  76. if !sel.Distinct {
  77. t.Error("expected DISTINCT")
  78. }
  79. }
  80. func TestParseSelectWhere(t *testing.T) {
  81. stmt := parse(t, "SELECT * FROM users WHERE id = 1")
  82. sel := stmt.(*SelectStmt)
  83. if sel.Where == nil {
  84. t.Fatal("expected WHERE clause")
  85. }
  86. binary, ok := sel.Where.(*BinaryExpr)
  87. if !ok {
  88. t.Fatalf("expected BinaryExpr, got %T", sel.Where)
  89. }
  90. if binary.Op != lexer.TokenEq {
  91. t.Errorf("expected =, got %v", binary.Op)
  92. }
  93. }
  94. func TestParseSelectWhereComplex(t *testing.T) {
  95. stmt := parse(t, "SELECT * FROM users WHERE id = 1 AND name = 'John' OR active = TRUE")
  96. sel := stmt.(*SelectStmt)
  97. if sel.Where == nil {
  98. t.Fatal("expected WHERE clause")
  99. }
  100. // Should be: (id = 1 AND name = 'John') OR active = TRUE
  101. or, ok := sel.Where.(*BinaryExpr)
  102. if !ok || or.Op != lexer.TokenOR {
  103. t.Fatal("expected OR at top level")
  104. }
  105. }
  106. func TestParseSelectOrderBy(t *testing.T) {
  107. stmt := parse(t, "SELECT * FROM users ORDER BY name ASC, id DESC")
  108. sel := stmt.(*SelectStmt)
  109. if len(sel.OrderBy) != 2 {
  110. t.Fatalf("expected 2 ORDER BY items, got %d", len(sel.OrderBy))
  111. }
  112. if sel.OrderBy[0].Desc {
  113. t.Error("first item should be ASC")
  114. }
  115. if !sel.OrderBy[1].Desc {
  116. t.Error("second item should be DESC")
  117. }
  118. }
  119. func TestParseSelectLimitOffset(t *testing.T) {
  120. stmt := parse(t, "SELECT * FROM users LIMIT 10 OFFSET 20")
  121. sel := stmt.(*SelectStmt)
  122. if sel.Limit == nil {
  123. t.Error("expected LIMIT")
  124. }
  125. if sel.Offset == nil {
  126. t.Error("expected OFFSET")
  127. }
  128. limit := sel.Limit.(*LiteralExpr)
  129. if limit.Value != "10" {
  130. t.Errorf("expected LIMIT 10, got %s", limit.Value)
  131. }
  132. offset := sel.Offset.(*LiteralExpr)
  133. if offset.Value != "20" {
  134. t.Errorf("expected OFFSET 20, got %s", offset.Value)
  135. }
  136. }
  137. func TestParseSelectGroupBy(t *testing.T) {
  138. stmt := parse(t, "SELECT name, COUNT(*) FROM users GROUP BY name")
  139. sel := stmt.(*SelectStmt)
  140. if len(sel.GroupBy) != 1 {
  141. t.Fatalf("expected 1 GROUP BY column, got %d", len(sel.GroupBy))
  142. }
  143. }
  144. func TestParseSelectHaving(t *testing.T) {
  145. stmt := parse(t, "SELECT name, COUNT(*) as cnt FROM users GROUP BY name HAVING COUNT(*) > 5")
  146. sel := stmt.(*SelectStmt)
  147. if sel.Having == nil {
  148. t.Fatal("expected HAVING clause")
  149. }
  150. }
  151. func TestParseSelectJoin(t *testing.T) {
  152. tests := []struct {
  153. input string
  154. joinType JoinType
  155. }{
  156. {"SELECT * FROM a JOIN b ON a.id = b.id", JoinInner},
  157. {"SELECT * FROM a INNER JOIN b ON a.id = b.id", JoinInner},
  158. {"SELECT * FROM a LEFT JOIN b ON a.id = b.id", JoinLeft},
  159. {"SELECT * FROM a LEFT OUTER JOIN b ON a.id = b.id", JoinLeft},
  160. {"SELECT * FROM a RIGHT JOIN b ON a.id = b.id", JoinRight},
  161. {"SELECT * FROM a CROSS JOIN b", JoinCross},
  162. }
  163. for _, tt := range tests {
  164. t.Run(tt.input, func(t *testing.T) {
  165. stmt := parse(t, tt.input)
  166. sel := stmt.(*SelectStmt)
  167. if sel.From[0].Join == nil {
  168. t.Fatal("expected JOIN")
  169. }
  170. if sel.From[0].Join.Type != tt.joinType {
  171. t.Errorf("expected join type %v, got %v", tt.joinType, sel.From[0].Join.Type)
  172. }
  173. })
  174. }
  175. }
  176. // INSERT statement tests
  177. func TestParseInsertValues(t *testing.T) {
  178. stmt := parse(t, "INSERT INTO users (name, age) VALUES ('John', 30)")
  179. ins, ok := stmt.(*InsertStmt)
  180. if !ok {
  181. t.Fatalf("expected InsertStmt, got %T", stmt)
  182. }
  183. if ins.Table.Name != "users" {
  184. t.Errorf("expected table users, got %s", ins.Table.Name)
  185. }
  186. if len(ins.Columns) != 2 {
  187. t.Fatalf("expected 2 columns, got %d", len(ins.Columns))
  188. }
  189. if len(ins.Values) != 1 || len(ins.Values[0]) != 2 {
  190. t.Error("expected 1 row with 2 values")
  191. }
  192. }
  193. func TestParseInsertMultipleRows(t *testing.T) {
  194. stmt := parse(t, "INSERT INTO users VALUES (1, 'John'), (2, 'Jane')")
  195. ins := stmt.(*InsertStmt)
  196. if len(ins.Values) != 2 {
  197. t.Fatalf("expected 2 rows, got %d", len(ins.Values))
  198. }
  199. }
  200. func TestParseInsertOrReplace(t *testing.T) {
  201. stmt := parse(t, "INSERT OR REPLACE INTO users (id, name) VALUES (1, 'John')")
  202. ins := stmt.(*InsertStmt)
  203. if ins.OnConflict != ConflictReplace {
  204. t.Errorf("expected ConflictReplace, got %v", ins.OnConflict)
  205. }
  206. if ins.Table.Name != "users" {
  207. t.Errorf("expected table users, got %s", ins.Table.Name)
  208. }
  209. }
  210. func TestParseInsertOrIgnore(t *testing.T) {
  211. stmt := parse(t, "INSERT OR IGNORE INTO users (id, name) VALUES (1, 'John')")
  212. ins := stmt.(*InsertStmt)
  213. if ins.OnConflict != ConflictIgnore {
  214. t.Errorf("expected ConflictIgnore, got %v", ins.OnConflict)
  215. }
  216. }
  217. func TestParseInsertOrFail(t *testing.T) {
  218. stmt := parse(t, "INSERT OR FAIL INTO users (id, name) VALUES (1, 'John')")
  219. ins := stmt.(*InsertStmt)
  220. if ins.OnConflict != ConflictFail {
  221. t.Errorf("expected ConflictFail, got %v", ins.OnConflict)
  222. }
  223. }
  224. func TestParseInsertOrAbort(t *testing.T) {
  225. stmt := parse(t, "INSERT OR ABORT INTO users (id, name) VALUES (1, 'John')")
  226. ins := stmt.(*InsertStmt)
  227. if ins.OnConflict != ConflictAbort {
  228. t.Errorf("expected ConflictAbort, got %v", ins.OnConflict)
  229. }
  230. }
  231. // UPDATE statement tests
  232. func TestParseUpdate(t *testing.T) {
  233. stmt := parse(t, "UPDATE users SET name = 'John', age = 30 WHERE id = 1")
  234. upd, ok := stmt.(*UpdateStmt)
  235. if !ok {
  236. t.Fatalf("expected UpdateStmt, got %T", stmt)
  237. }
  238. if upd.Table.Name != "users" {
  239. t.Errorf("expected table users, got %s", upd.Table.Name)
  240. }
  241. if len(upd.Set) != 2 {
  242. t.Fatalf("expected 2 assignments, got %d", len(upd.Set))
  243. }
  244. if upd.Where == nil {
  245. t.Error("expected WHERE clause")
  246. }
  247. }
  248. // DELETE statement tests
  249. func TestParseDelete(t *testing.T) {
  250. stmt := parse(t, "DELETE FROM users WHERE id = 1")
  251. del, ok := stmt.(*DeleteStmt)
  252. if !ok {
  253. t.Fatalf("expected DeleteStmt, got %T", stmt)
  254. }
  255. if del.Table.Name != "users" {
  256. t.Errorf("expected table users, got %s", del.Table.Name)
  257. }
  258. if del.Where == nil {
  259. t.Error("expected WHERE clause")
  260. }
  261. }
  262. func TestParseDeleteAll(t *testing.T) {
  263. stmt := parse(t, "DELETE FROM users")
  264. del := stmt.(*DeleteStmt)
  265. if del.Where != nil {
  266. t.Error("expected no WHERE clause")
  267. }
  268. }
  269. // CREATE TABLE tests
  270. func TestParseCreateTable(t *testing.T) {
  271. stmt := parse(t, `CREATE TABLE users (
  272. id INTEGER PRIMARY KEY,
  273. name TEXT NOT NULL,
  274. email VARCHAR(255) UNIQUE,
  275. age INTEGER DEFAULT 0
  276. )`)
  277. create, ok := stmt.(*CreateTableStmt)
  278. if !ok {
  279. t.Fatalf("expected CreateTableStmt, got %T", stmt)
  280. }
  281. if create.Table.Name != "users" {
  282. t.Errorf("expected table users, got %s", create.Table.Name)
  283. }
  284. if len(create.Columns) != 4 {
  285. t.Fatalf("expected 4 columns, got %d", len(create.Columns))
  286. }
  287. // Check id column
  288. if create.Columns[0].Name != "id" {
  289. t.Error("expected first column to be id")
  290. }
  291. if create.Columns[0].Type.Name != "INTEGER" {
  292. t.Error("expected INTEGER type")
  293. }
  294. // Check name column has NOT NULL
  295. found := false
  296. for _, c := range create.Columns[1].Constraints {
  297. if c.Type == ConstraintNotNull {
  298. found = true
  299. }
  300. }
  301. if !found {
  302. t.Error("expected NOT NULL constraint on name")
  303. }
  304. // Check email has VARCHAR(255)
  305. if create.Columns[2].Type.Name != "VARCHAR" || create.Columns[2].Type.Precision != 255 {
  306. t.Error("expected VARCHAR(255) for email")
  307. }
  308. }
  309. func TestParseCreateTableIfNotExists(t *testing.T) {
  310. stmt := parse(t, "CREATE TABLE IF NOT EXISTS users (id INTEGER)")
  311. create := stmt.(*CreateTableStmt)
  312. if !create.IfNotExists {
  313. t.Error("expected IF NOT EXISTS")
  314. }
  315. }
  316. func TestParseCreateTableWithConstraints(t *testing.T) {
  317. stmt := parse(t, `CREATE TABLE orders (
  318. id INTEGER,
  319. user_id INTEGER,
  320. PRIMARY KEY (id),
  321. FOREIGN KEY (user_id) REFERENCES users(id)
  322. )`)
  323. create := stmt.(*CreateTableStmt)
  324. if len(create.Constraints) != 2 {
  325. t.Fatalf("expected 2 table constraints, got %d", len(create.Constraints))
  326. }
  327. // Check PRIMARY KEY
  328. if create.Constraints[0].Type != ConstraintPrimaryKey {
  329. t.Error("expected PRIMARY KEY constraint")
  330. }
  331. // Check FOREIGN KEY
  332. if create.Constraints[1].Type != ConstraintForeignKey {
  333. t.Error("expected FOREIGN KEY constraint")
  334. }
  335. if create.Constraints[1].RefTable != "users" {
  336. t.Errorf("expected reference to users, got %s", create.Constraints[1].RefTable)
  337. }
  338. }
  339. // DROP TABLE tests
  340. func TestParseDropTable(t *testing.T) {
  341. stmt := parse(t, "DROP TABLE users")
  342. drop, ok := stmt.(*DropTableStmt)
  343. if !ok {
  344. t.Fatalf("expected DropTableStmt, got %T", stmt)
  345. }
  346. if len(drop.Tables) != 1 || drop.Tables[0].Name != "users" {
  347. t.Error("expected DROP TABLE users")
  348. }
  349. }
  350. func TestParseDropTableIfExists(t *testing.T) {
  351. stmt := parse(t, "DROP TABLE IF EXISTS users")
  352. drop := stmt.(*DropTableStmt)
  353. if !drop.IfExists {
  354. t.Error("expected IF EXISTS")
  355. }
  356. }
  357. // Expression tests
  358. func TestParseExprArithmetic(t *testing.T) {
  359. expr := parseExpr(t, "1 + 2 * 3")
  360. // Should be: 1 + (2 * 3) due to precedence
  361. add, ok := expr.(*BinaryExpr)
  362. if !ok || add.Op != lexer.TokenPlus {
  363. t.Fatal("expected + at top level")
  364. }
  365. mul, ok := add.Right.(*BinaryExpr)
  366. if !ok || mul.Op != lexer.TokenStar {
  367. t.Fatal("expected * on right side")
  368. }
  369. }
  370. func TestParseExprParens(t *testing.T) {
  371. expr := parseExpr(t, "(1 + 2) * 3")
  372. // Should be: (1 + 2) * 3
  373. mul, ok := expr.(*BinaryExpr)
  374. if !ok || mul.Op != lexer.TokenStar {
  375. t.Fatal("expected * at top level")
  376. }
  377. paren, ok := mul.Left.(*ParenExpr)
  378. if !ok {
  379. t.Fatal("expected ParenExpr on left")
  380. }
  381. add, ok := paren.Expr.(*BinaryExpr)
  382. if !ok || add.Op != lexer.TokenPlus {
  383. t.Fatal("expected + inside parens")
  384. }
  385. }
  386. func TestParseExprComparison(t *testing.T) {
  387. tests := []struct {
  388. input string
  389. op lexer.TokenType
  390. }{
  391. {"a = b", lexer.TokenEq},
  392. {"a <> b", lexer.TokenNeq},
  393. {"a != b", lexer.TokenNeq},
  394. {"a < b", lexer.TokenLt},
  395. {"a <= b", lexer.TokenLte},
  396. {"a > b", lexer.TokenGt},
  397. {"a >= b", lexer.TokenGte},
  398. }
  399. for _, tt := range tests {
  400. t.Run(tt.input, func(t *testing.T) {
  401. expr := parseExpr(t, tt.input)
  402. binary, ok := expr.(*BinaryExpr)
  403. if !ok {
  404. t.Fatalf("expected BinaryExpr, got %T", expr)
  405. }
  406. if binary.Op != tt.op {
  407. t.Errorf("expected %v, got %v", tt.op, binary.Op)
  408. }
  409. })
  410. }
  411. }
  412. func TestParseExprIsNull(t *testing.T) {
  413. tests := []struct {
  414. input string
  415. not bool
  416. }{
  417. {"a IS NULL", false},
  418. {"a IS NOT NULL", true},
  419. }
  420. for _, tt := range tests {
  421. t.Run(tt.input, func(t *testing.T) {
  422. expr := parseExpr(t, tt.input)
  423. isNull, ok := expr.(*IsNullExpr)
  424. if !ok {
  425. t.Fatalf("expected IsNullExpr, got %T", expr)
  426. }
  427. if isNull.Not != tt.not {
  428. t.Errorf("expected Not=%v, got %v", tt.not, isNull.Not)
  429. }
  430. })
  431. }
  432. }
  433. func TestParseExprIn(t *testing.T) {
  434. tests := []struct {
  435. input string
  436. not bool
  437. }{
  438. {"a IN (1, 2, 3)", false},
  439. {"a NOT IN (1, 2, 3)", true},
  440. }
  441. for _, tt := range tests {
  442. t.Run(tt.input, func(t *testing.T) {
  443. expr := parseExpr(t, tt.input)
  444. in, ok := expr.(*InExpr)
  445. if !ok {
  446. t.Fatalf("expected InExpr, got %T", expr)
  447. }
  448. if in.Not != tt.not {
  449. t.Errorf("expected Not=%v, got %v", tt.not, in.Not)
  450. }
  451. if len(in.Values) != 3 {
  452. t.Errorf("expected 3 values, got %d", len(in.Values))
  453. }
  454. })
  455. }
  456. }
  457. func TestParseExprBetween(t *testing.T) {
  458. tests := []struct {
  459. input string
  460. not bool
  461. }{
  462. {"a BETWEEN 1 AND 10", false},
  463. {"a NOT BETWEEN 1 AND 10", true},
  464. }
  465. for _, tt := range tests {
  466. t.Run(tt.input, func(t *testing.T) {
  467. expr := parseExpr(t, tt.input)
  468. between, ok := expr.(*BetweenExpr)
  469. if !ok {
  470. t.Fatalf("expected BetweenExpr, got %T", expr)
  471. }
  472. if between.Not != tt.not {
  473. t.Errorf("expected Not=%v, got %v", tt.not, between.Not)
  474. }
  475. })
  476. }
  477. }
  478. func TestParseExprLike(t *testing.T) {
  479. tests := []struct {
  480. input string
  481. not bool
  482. }{
  483. {"name LIKE '%test%'", false},
  484. {"name NOT LIKE '%test%'", true},
  485. }
  486. for _, tt := range tests {
  487. t.Run(tt.input, func(t *testing.T) {
  488. expr := parseExpr(t, tt.input)
  489. like, ok := expr.(*LikeExpr)
  490. if !ok {
  491. t.Fatalf("expected LikeExpr, got %T", expr)
  492. }
  493. if like.Not != tt.not {
  494. t.Errorf("expected Not=%v, got %v", tt.not, like.Not)
  495. }
  496. })
  497. }
  498. }
  499. func TestParseExprCase(t *testing.T) {
  500. expr := parseExpr(t, "CASE WHEN x = 1 THEN 'one' WHEN x = 2 THEN 'two' ELSE 'other' END")
  501. caseExpr, ok := expr.(*CaseExpr)
  502. if !ok {
  503. t.Fatalf("expected CaseExpr, got %T", expr)
  504. }
  505. if len(caseExpr.Whens) != 2 {
  506. t.Errorf("expected 2 WHEN clauses, got %d", len(caseExpr.Whens))
  507. }
  508. if caseExpr.Else == nil {
  509. t.Error("expected ELSE clause")
  510. }
  511. }
  512. func TestParseExprCast(t *testing.T) {
  513. expr := parseExpr(t, "CAST(x AS INTEGER)")
  514. cast, ok := expr.(*CastExpr)
  515. if !ok {
  516. t.Fatalf("expected CastExpr, got %T", expr)
  517. }
  518. if cast.Type.Name != "INTEGER" {
  519. t.Errorf("expected INTEGER type, got %s", cast.Type.Name)
  520. }
  521. }
  522. func TestParseExprFunction(t *testing.T) {
  523. tests := []struct {
  524. input string
  525. name string
  526. argCount int
  527. star bool
  528. distinct bool
  529. }{
  530. {"COUNT(*)", "COUNT", 0, true, false},
  531. {"COUNT(id)", "COUNT", 1, false, false},
  532. {"COUNT(DISTINCT id)", "COUNT", 1, false, true},
  533. {"SUM(amount)", "SUM", 1, false, false},
  534. {"UPPER(name)", "UPPER", 1, false, false},
  535. {"COALESCE(a, b, c)", "COALESCE", 3, false, false},
  536. }
  537. for _, tt := range tests {
  538. t.Run(tt.input, func(t *testing.T) {
  539. expr := parseExpr(t, tt.input)
  540. fn, ok := expr.(*FunctionCall)
  541. if !ok {
  542. t.Fatalf("expected FunctionCall, got %T", expr)
  543. }
  544. if fn.Name != tt.name {
  545. t.Errorf("expected name %s, got %s", tt.name, fn.Name)
  546. }
  547. if len(fn.Args) != tt.argCount {
  548. t.Errorf("expected %d args, got %d", tt.argCount, len(fn.Args))
  549. }
  550. if fn.Star != tt.star {
  551. t.Errorf("expected Star=%v, got %v", tt.star, fn.Star)
  552. }
  553. if fn.Distinct != tt.distinct {
  554. t.Errorf("expected Distinct=%v, got %v", tt.distinct, fn.Distinct)
  555. }
  556. })
  557. }
  558. }
  559. func TestParseExprSubquery(t *testing.T) {
  560. expr := parseExpr(t, "id IN (SELECT user_id FROM orders)")
  561. in, ok := expr.(*InExpr)
  562. if !ok {
  563. t.Fatalf("expected InExpr, got %T", expr)
  564. }
  565. if in.Subquery == nil {
  566. t.Error("expected subquery")
  567. }
  568. }
  569. func TestParseExprExists(t *testing.T) {
  570. expr := parseExpr(t, "EXISTS (SELECT 1 FROM users WHERE id = 1)")
  571. exists, ok := expr.(*ExistsExpr)
  572. if !ok {
  573. t.Fatalf("expected ExistsExpr, got %T", expr)
  574. }
  575. if exists.Subquery == nil {
  576. t.Error("expected subquery")
  577. }
  578. }
  579. func TestParseExprColumnRef(t *testing.T) {
  580. tests := []struct {
  581. input string
  582. table string
  583. column string
  584. }{
  585. {"id", "", "id"},
  586. {"users.id", "users", "id"},
  587. {"u.name", "u", "name"},
  588. }
  589. for _, tt := range tests {
  590. t.Run(tt.input, func(t *testing.T) {
  591. expr := parseExpr(t, tt.input)
  592. ref, ok := expr.(*ColumnRef)
  593. if !ok {
  594. t.Fatalf("expected ColumnRef, got %T", expr)
  595. }
  596. if ref.Table != tt.table {
  597. t.Errorf("expected table %q, got %q", tt.table, ref.Table)
  598. }
  599. if ref.Column != tt.column {
  600. t.Errorf("expected column %q, got %q", tt.column, ref.Column)
  601. }
  602. })
  603. }
  604. }
  605. // Error cases
  606. func TestParseErrors(t *testing.T) {
  607. tests := []struct {
  608. name string
  609. input string
  610. }{
  611. {"missing columns", "SELECT FROM users"},
  612. {"missing VALUES", "INSERT INTO users"},
  613. {"missing SET", "UPDATE users WHERE id = 1"},
  614. {"missing table name", "DELETE FROM WHERE id = 1"},
  615. {"unclosed paren", "SELECT * FROM users WHERE (id = 1"},
  616. {"invalid token", "SELECT @ FROM users"},
  617. }
  618. for _, tt := range tests {
  619. t.Run(tt.name, func(t *testing.T) {
  620. l := lexer.New(tt.input)
  621. p := New(l)
  622. _, err := p.Parse()
  623. if err == nil {
  624. t.Error("expected parse error")
  625. }
  626. })
  627. }
  628. }
  629. // Multiple statements
  630. func TestParseMultiple(t *testing.T) {
  631. input := `
  632. SELECT * FROM users;
  633. INSERT INTO users VALUES (1, 'John');
  634. DELETE FROM users WHERE id = 1
  635. `
  636. l := lexer.New(input)
  637. p := New(l)
  638. stmts, err := p.ParseMultiple()
  639. if err != nil {
  640. t.Fatalf("parse error: %v", err)
  641. }
  642. if len(stmts) != 3 {
  643. t.Errorf("expected 3 statements, got %d", len(stmts))
  644. }
  645. }
  646. // Phase 4: PRAGMA and EXPLAIN tests
  647. func TestParsePragmaTableInfo(t *testing.T) {
  648. stmt := parse(t, "PRAGMA table_info(users)")
  649. pragma, ok := stmt.(*PragmaStmt)
  650. if !ok {
  651. t.Fatalf("expected PragmaStmt, got %T", stmt)
  652. }
  653. if pragma.Name != "table_info" {
  654. t.Errorf("expected pragma name 'table_info', got %s", pragma.Name)
  655. }
  656. if pragma.Arg != "users" {
  657. t.Errorf("expected arg 'users', got %s", pragma.Arg)
  658. }
  659. }
  660. func TestParsePragmaTableList(t *testing.T) {
  661. stmt := parse(t, "PRAGMA table_list")
  662. pragma, ok := stmt.(*PragmaStmt)
  663. if !ok {
  664. t.Fatalf("expected PragmaStmt, got %T", stmt)
  665. }
  666. if pragma.Name != "table_list" {
  667. t.Errorf("expected pragma name 'table_list', got %s", pragma.Name)
  668. }
  669. }
  670. func TestParsePragmaDatabaseList(t *testing.T) {
  671. stmt := parse(t, "PRAGMA database_list")
  672. pragma := stmt.(*PragmaStmt)
  673. if pragma.Name != "database_list" {
  674. t.Errorf("expected pragma name 'database_list', got %s", pragma.Name)
  675. }
  676. }
  677. func TestParsePragmaVersion(t *testing.T) {
  678. stmt := parse(t, "PRAGMA version")
  679. pragma := stmt.(*PragmaStmt)
  680. if pragma.Name != "version" {
  681. t.Errorf("expected pragma name 'version', got %s", pragma.Name)
  682. }
  683. }
  684. func TestParseExplain(t *testing.T) {
  685. stmt := parse(t, "EXPLAIN SELECT * FROM users")
  686. explain, ok := stmt.(*ExplainStmt)
  687. if !ok {
  688. t.Fatalf("expected ExplainStmt, got %T", stmt)
  689. }
  690. if explain.QueryPlan {
  691. t.Error("expected QueryPlan to be false")
  692. }
  693. _, ok = explain.Statement.(*SelectStmt)
  694. if !ok {
  695. t.Errorf("expected SelectStmt inside EXPLAIN, got %T", explain.Statement)
  696. }
  697. }
  698. func TestParseExplainQueryPlan(t *testing.T) {
  699. stmt := parse(t, "EXPLAIN QUERY PLAN SELECT * FROM users WHERE id = 1")
  700. explain, ok := stmt.(*ExplainStmt)
  701. if !ok {
  702. t.Fatalf("expected ExplainStmt, got %T", stmt)
  703. }
  704. if !explain.QueryPlan {
  705. t.Error("expected QueryPlan to be true")
  706. }
  707. sel, ok := explain.Statement.(*SelectStmt)
  708. if !ok {
  709. t.Errorf("expected SelectStmt inside EXPLAIN, got %T", explain.Statement)
  710. }
  711. if sel.Where == nil {
  712. t.Error("expected WHERE clause in explained statement")
  713. }
  714. }
  715. func TestParseExplainInsert(t *testing.T) {
  716. stmt := parse(t, "EXPLAIN INSERT INTO users (name) VALUES ('John')")
  717. explain := stmt.(*ExplainStmt)
  718. _, ok := explain.Statement.(*InsertStmt)
  719. if !ok {
  720. t.Errorf("expected InsertStmt inside EXPLAIN, got %T", explain.Statement)
  721. }
  722. }
  723. // Phase 5: Transaction statement tests
  724. func TestParseBegin(t *testing.T) {
  725. stmt := parse(t, "BEGIN")
  726. _, ok := stmt.(*BeginStmt)
  727. if !ok {
  728. t.Fatalf("expected BeginStmt, got %T", stmt)
  729. }
  730. }
  731. func TestParseBeginTransaction(t *testing.T) {
  732. stmt := parse(t, "BEGIN TRANSACTION")
  733. _, ok := stmt.(*BeginStmt)
  734. if !ok {
  735. t.Fatalf("expected BeginStmt, got %T", stmt)
  736. }
  737. }
  738. func TestParseCommit(t *testing.T) {
  739. stmt := parse(t, "COMMIT")
  740. _, ok := stmt.(*CommitStmt)
  741. if !ok {
  742. t.Fatalf("expected CommitStmt, got %T", stmt)
  743. }
  744. }
  745. func TestParseCommitTransaction(t *testing.T) {
  746. stmt := parse(t, "COMMIT TRANSACTION")
  747. _, ok := stmt.(*CommitStmt)
  748. if !ok {
  749. t.Fatalf("expected CommitStmt, got %T", stmt)
  750. }
  751. }
  752. func TestParseRollback(t *testing.T) {
  753. stmt := parse(t, "ROLLBACK")
  754. rollback, ok := stmt.(*RollbackStmt)
  755. if !ok {
  756. t.Fatalf("expected RollbackStmt, got %T", stmt)
  757. }
  758. if rollback.Savepoint != "" {
  759. t.Errorf("expected empty savepoint, got %s", rollback.Savepoint)
  760. }
  761. }
  762. func TestParseRollbackToSavepoint(t *testing.T) {
  763. stmt := parse(t, "ROLLBACK TO SAVEPOINT sp1")
  764. rollback, ok := stmt.(*RollbackStmt)
  765. if !ok {
  766. t.Fatalf("expected RollbackStmt, got %T", stmt)
  767. }
  768. if rollback.Savepoint != "sp1" {
  769. t.Errorf("expected savepoint 'sp1', got %s", rollback.Savepoint)
  770. }
  771. }
  772. func TestParseRollbackTo(t *testing.T) {
  773. stmt := parse(t, "ROLLBACK TO sp1")
  774. rollback := stmt.(*RollbackStmt)
  775. if rollback.Savepoint != "sp1" {
  776. t.Errorf("expected savepoint 'sp1', got %s", rollback.Savepoint)
  777. }
  778. }
  779. func TestParseSavepoint(t *testing.T) {
  780. stmt := parse(t, "SAVEPOINT my_savepoint")
  781. sp, ok := stmt.(*SavepointStmt)
  782. if !ok {
  783. t.Fatalf("expected SavepointStmt, got %T", stmt)
  784. }
  785. if sp.Name != "my_savepoint" {
  786. t.Errorf("expected savepoint name 'my_savepoint', got %s", sp.Name)
  787. }
  788. }
  789. func TestParseReleaseSavepoint(t *testing.T) {
  790. stmt := parse(t, "RELEASE SAVEPOINT sp1")
  791. rel, ok := stmt.(*ReleaseStmt)
  792. if !ok {
  793. t.Fatalf("expected ReleaseStmt, got %T", stmt)
  794. }
  795. if rel.Name != "sp1" {
  796. t.Errorf("expected savepoint name 'sp1', got %s", rel.Name)
  797. }
  798. }
  799. func TestParseRelease(t *testing.T) {
  800. stmt := parse(t, "RELEASE sp1")
  801. rel := stmt.(*ReleaseStmt)
  802. if rel.Name != "sp1" {
  803. t.Errorf("expected savepoint name 'sp1', got %s", rel.Name)
  804. }
  805. }
  806. // Benchmark
  807. func BenchmarkParseSelect(b *testing.B) {
  808. input := `SELECT u.id, u.name, u.email, COUNT(o.id) as order_count
  809. FROM users u
  810. LEFT JOIN orders o ON u.id = o.user_id
  811. WHERE u.active = TRUE AND u.created_at >= '2024-01-01'
  812. GROUP BY u.id, u.name, u.email
  813. HAVING COUNT(o.id) > 5
  814. ORDER BY order_count DESC
  815. LIMIT 100 OFFSET 0`
  816. b.ResetTimer()
  817. for i := 0; i < b.N; i++ {
  818. l := lexer.New(input)
  819. p := New(l)
  820. _, _ = p.Parse()
  821. }
  822. }
  823. func BenchmarkParseCreateTable(b *testing.B) {
  824. input := `CREATE TABLE users (
  825. id INTEGER PRIMARY KEY AUTOINCREMENT,
  826. name TEXT NOT NULL,
  827. email VARCHAR(255) UNIQUE,
  828. age INTEGER DEFAULT 0,
  829. active BOOLEAN DEFAULT TRUE,
  830. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  831. )`
  832. b.ResetTimer()
  833. for i := 0; i < b.N; i++ {
  834. l := lexer.New(input)
  835. p := New(l)
  836. _, _ = p.Parse()
  837. }
  838. }
  839. // CREATE INDEX tests
  840. func TestParseCreateIndex(t *testing.T) {
  841. stmt := parse(t, "CREATE INDEX idx_email ON users (email)")
  842. idx, ok := stmt.(*CreateIndexStmt)
  843. if !ok {
  844. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  845. }
  846. if idx.Name != "idx_email" {
  847. t.Errorf("expected index name idx_email, got %s", idx.Name)
  848. }
  849. if idx.Table != "users" {
  850. t.Errorf("expected table users, got %s", idx.Table)
  851. }
  852. if len(idx.Columns) != 1 || idx.Columns[0].Name != "email" {
  853. t.Error("expected column email")
  854. }
  855. if idx.Unique {
  856. t.Error("expected non-unique index")
  857. }
  858. if idx.IfNotExists {
  859. t.Error("expected IfNotExists to be false")
  860. }
  861. }
  862. func TestParseCreateUniqueIndex(t *testing.T) {
  863. stmt := parse(t, "CREATE UNIQUE INDEX idx_email ON users (email)")
  864. idx, ok := stmt.(*CreateIndexStmt)
  865. if !ok {
  866. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  867. }
  868. if !idx.Unique {
  869. t.Error("expected unique index")
  870. }
  871. }
  872. func TestParseCreateIndexIfNotExists(t *testing.T) {
  873. stmt := parse(t, "CREATE INDEX IF NOT EXISTS idx_email ON users (email)")
  874. idx, ok := stmt.(*CreateIndexStmt)
  875. if !ok {
  876. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  877. }
  878. if !idx.IfNotExists {
  879. t.Error("expected IfNotExists to be true")
  880. }
  881. }
  882. func TestParseCreateIndexMultiColumn(t *testing.T) {
  883. stmt := parse(t, "CREATE INDEX idx_name_email ON users (name, email)")
  884. idx, ok := stmt.(*CreateIndexStmt)
  885. if !ok {
  886. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  887. }
  888. if len(idx.Columns) != 2 {
  889. t.Fatalf("expected 2 columns, got %d", len(idx.Columns))
  890. }
  891. if idx.Columns[0].Name != "name" {
  892. t.Errorf("expected first column name, got %s", idx.Columns[0].Name)
  893. }
  894. if idx.Columns[1].Name != "email" {
  895. t.Errorf("expected second column email, got %s", idx.Columns[1].Name)
  896. }
  897. }
  898. func TestParseCreateIndexWithDesc(t *testing.T) {
  899. stmt := parse(t, "CREATE INDEX idx_created ON users (created_at DESC)")
  900. idx, ok := stmt.(*CreateIndexStmt)
  901. if !ok {
  902. t.Fatalf("expected CreateIndexStmt, got %T", stmt)
  903. }
  904. if len(idx.Columns) != 1 {
  905. t.Fatalf("expected 1 column, got %d", len(idx.Columns))
  906. }
  907. if !idx.Columns[0].Desc {
  908. t.Error("expected DESC ordering")
  909. }
  910. }
  911. // DROP INDEX tests
  912. func TestParseDropIndex(t *testing.T) {
  913. stmt := parse(t, "DROP INDEX idx_email")
  914. drop, ok := stmt.(*DropIndexStmt)
  915. if !ok {
  916. t.Fatalf("expected DropIndexStmt, got %T", stmt)
  917. }
  918. if drop.Name != "idx_email" {
  919. t.Errorf("expected index name idx_email, got %s", drop.Name)
  920. }
  921. if drop.IfExists {
  922. t.Error("expected IfExists to be false")
  923. }
  924. }
  925. func TestParseDropIndexIfExists(t *testing.T) {
  926. stmt := parse(t, "DROP INDEX IF EXISTS idx_email")
  927. drop, ok := stmt.(*DropIndexStmt)
  928. if !ok {
  929. t.Fatalf("expected DropIndexStmt, got %T", stmt)
  930. }
  931. if !drop.IfExists {
  932. t.Error("expected IfExists to be true")
  933. }
  934. }
  935. // Subquery in FROM clause tests
  936. func TestParseSelectFromSubquery(t *testing.T) {
  937. stmt := parse(t, "SELECT * FROM (SELECT id, name FROM users) AS u")
  938. sel, ok := stmt.(*SelectStmt)
  939. if !ok {
  940. t.Fatalf("expected SelectStmt, got %T", stmt)
  941. }
  942. if len(sel.From) != 1 {
  943. t.Fatalf("expected 1 FROM item, got %d", len(sel.From))
  944. }
  945. if sel.From[0].Subquery == nil {
  946. t.Fatal("expected subquery in FROM")
  947. }
  948. if sel.From[0].Alias != "u" {
  949. t.Errorf("expected alias 'u', got '%s'", sel.From[0].Alias)
  950. }
  951. // Check subquery
  952. subquery := sel.From[0].Subquery
  953. if len(subquery.Columns) != 2 {
  954. t.Errorf("expected 2 columns in subquery, got %d", len(subquery.Columns))
  955. }
  956. if len(subquery.From) != 1 || subquery.From[0].Name != "users" {
  957. t.Error("expected subquery FROM users")
  958. }
  959. }
  960. func TestParseSelectFromSubqueryWithWhere(t *testing.T) {
  961. stmt := parse(t, "SELECT name FROM (SELECT id, name FROM users WHERE active = TRUE) AS active_users WHERE id > 10")
  962. sel, ok := stmt.(*SelectStmt)
  963. if !ok {
  964. t.Fatalf("expected SelectStmt, got %T", stmt)
  965. }
  966. if sel.From[0].Subquery == nil {
  967. t.Fatal("expected subquery in FROM")
  968. }
  969. // Check outer WHERE clause
  970. if sel.Where == nil {
  971. t.Error("expected outer WHERE clause")
  972. }
  973. // Check subquery WHERE clause
  974. if sel.From[0].Subquery.Where == nil {
  975. t.Error("expected subquery WHERE clause")
  976. }
  977. }
  978. func TestParseSelectFromSubqueryComplex(t *testing.T) {
  979. stmt := parse(t, "SELECT u.name, u.total FROM (SELECT user_id, SUM(amount) AS total FROM orders GROUP BY user_id) AS u")
  980. sel, ok := stmt.(*SelectStmt)
  981. if !ok {
  982. t.Fatalf("expected SelectStmt, got %T", stmt)
  983. }
  984. if sel.From[0].Subquery == nil {
  985. t.Fatal("expected subquery in FROM")
  986. }
  987. subquery := sel.From[0].Subquery
  988. if len(subquery.GroupBy) == 0 {
  989. t.Error("expected GROUP BY in subquery")
  990. }
  991. // Check that columns reference the alias
  992. if len(sel.Columns) != 2 {
  993. t.Fatalf("expected 2 columns, got %d", len(sel.Columns))
  994. }
  995. }
  996. func TestParseSelectFromNestedSubquery(t *testing.T) {
  997. stmt := parse(t, "SELECT * FROM (SELECT * FROM (SELECT id FROM users) AS inner_q) AS outer_q")
  998. sel, ok := stmt.(*SelectStmt)
  999. if !ok {
  1000. t.Fatalf("expected SelectStmt, got %T", stmt)
  1001. }
  1002. if sel.From[0].Subquery == nil {
  1003. t.Fatal("expected subquery in FROM")
  1004. }
  1005. // Check nested subquery
  1006. outerSubquery := sel.From[0].Subquery
  1007. if len(outerSubquery.From) == 0 || outerSubquery.From[0].Subquery == nil {
  1008. t.Error("expected nested subquery")
  1009. }
  1010. }
  1011. // ALTER TABLE tests
  1012. func TestParseAlterTableAddColumn(t *testing.T) {
  1013. stmt := parse(t, "ALTER TABLE users ADD COLUMN age INTEGER")
  1014. alter, ok := stmt.(*AlterTableStmt)
  1015. if !ok {
  1016. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1017. }
  1018. if alter.Table != "users" {
  1019. t.Errorf("expected table users, got %s", alter.Table)
  1020. }
  1021. action, ok := alter.Action.(*AddColumnAction)
  1022. if !ok {
  1023. t.Fatalf("expected AddColumnAction, got %T", alter.Action)
  1024. }
  1025. if action.Column.Name != "age" {
  1026. t.Errorf("expected column name age, got %s", action.Column.Name)
  1027. }
  1028. if action.Column.Type.Name != "INTEGER" {
  1029. t.Errorf("expected column type INTEGER, got %s", action.Column.Type.Name)
  1030. }
  1031. }
  1032. func TestParseAlterTableAddColumnOptional(t *testing.T) {
  1033. stmt := parse(t, "ALTER TABLE users ADD age INTEGER")
  1034. alter, ok := stmt.(*AlterTableStmt)
  1035. if !ok {
  1036. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1037. }
  1038. action, ok := alter.Action.(*AddColumnAction)
  1039. if !ok {
  1040. t.Fatalf("expected AddColumnAction, got %T", alter.Action)
  1041. }
  1042. if action.Column.Name != "age" {
  1043. t.Errorf("expected column name age, got %s", action.Column.Name)
  1044. }
  1045. }
  1046. func TestParseAlterTableDropColumn(t *testing.T) {
  1047. stmt := parse(t, "ALTER TABLE users DROP COLUMN email")
  1048. alter, ok := stmt.(*AlterTableStmt)
  1049. if !ok {
  1050. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1051. }
  1052. action, ok := alter.Action.(*DropColumnAction)
  1053. if !ok {
  1054. t.Fatalf("expected DropColumnAction, got %T", alter.Action)
  1055. }
  1056. if action.Column != "email" {
  1057. t.Errorf("expected column email, got %s", action.Column)
  1058. }
  1059. }
  1060. func TestParseAlterTableRename(t *testing.T) {
  1061. stmt := parse(t, "ALTER TABLE users RENAME TO customers")
  1062. alter, ok := stmt.(*AlterTableStmt)
  1063. if !ok {
  1064. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1065. }
  1066. action, ok := alter.Action.(*RenameTableAction)
  1067. if !ok {
  1068. t.Fatalf("expected RenameTableAction, got %T", alter.Action)
  1069. }
  1070. if action.NewName != "customers" {
  1071. t.Errorf("expected new name customers, got %s", action.NewName)
  1072. }
  1073. }
  1074. func TestParseAlterTableRenameColumn(t *testing.T) {
  1075. stmt := parse(t, "ALTER TABLE users RENAME COLUMN name TO full_name")
  1076. alter, ok := stmt.(*AlterTableStmt)
  1077. if !ok {
  1078. t.Fatalf("expected AlterTableStmt, got %T", stmt)
  1079. }
  1080. action, ok := alter.Action.(*RenameColumnAction)
  1081. if !ok {
  1082. t.Fatalf("expected RenameColumnAction, got %T", alter.Action)
  1083. }
  1084. if action.OldName != "name" {
  1085. t.Errorf("expected old name 'name', got %s", action.OldName)
  1086. }
  1087. if action.NewName != "full_name" {
  1088. t.Errorf("expected new name 'full_name', got %s", action.NewName)
  1089. }
  1090. }
  1091. // ATTACH/DETACH DATABASE tests
  1092. func TestParseAttach(t *testing.T) {
  1093. stmt := parse(t, "ATTACH DATABASE 'test.db' AS testdb")
  1094. attach, ok := stmt.(*AttachStmt)
  1095. if !ok {
  1096. t.Fatalf("expected AttachStmt, got %T", stmt)
  1097. }
  1098. if attach.FilePath != "test.db" {
  1099. t.Errorf("expected file path 'test.db', got '%s'", attach.FilePath)
  1100. }
  1101. if attach.Alias != "testdb" {
  1102. t.Errorf("expected alias 'testdb', got '%s'", attach.Alias)
  1103. }
  1104. }
  1105. func TestParseAttachOptional(t *testing.T) {
  1106. stmt := parse(t, "ATTACH 'another.db' AS other")
  1107. attach, ok := stmt.(*AttachStmt)
  1108. if !ok {
  1109. t.Fatalf("expected AttachStmt, got %T", stmt)
  1110. }
  1111. if attach.FilePath != "another.db" {
  1112. t.Errorf("expected file path 'another.db', got '%s'", attach.FilePath)
  1113. }
  1114. if attach.Alias != "other" {
  1115. t.Errorf("expected alias 'other', got '%s'", attach.Alias)
  1116. }
  1117. }
  1118. func TestParseDetach(t *testing.T) {
  1119. stmt := parse(t, "DETACH DATABASE testdb")
  1120. detach, ok := stmt.(*DetachStmt)
  1121. if !ok {
  1122. t.Fatalf("expected DetachStmt, got %T", stmt)
  1123. }
  1124. if detach.Alias != "testdb" {
  1125. t.Errorf("expected alias 'testdb', got '%s'", detach.Alias)
  1126. }
  1127. }
  1128. func TestParseDetachOptional(t *testing.T) {
  1129. stmt := parse(t, "DETACH other")
  1130. detach, ok := stmt.(*DetachStmt)
  1131. if !ok {
  1132. t.Fatalf("expected DetachStmt, got %T", stmt)
  1133. }
  1134. if detach.Alias != "other" {
  1135. t.Errorf("expected alias 'other', got '%s'", detach.Alias)
  1136. }
  1137. }