interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / if.sql

858 lines · 13159 bytes Raw Blame History
1#!sqlite3
2#
3# 2026-10-24
4#
5# The author disclaims copyright to this source code. In place of
6# a legal notice, here is a blessing:
7#
8# May you do good and not evil.
9# May you find forgiveness for yourself and forgive others.
10# May you share freely, never taking more than you give.
11#
12#***********************************************************************
13#
14# Test cases for the ".if", ".elseif", ".else", and ".endif" commands
15# and the capable() SQL function of the CLI.
16#
17# ./sqlite3 test/if.sql
18#
19# Many tests use ".read <<EOF" to run a script contained in a heredoc,
20# so that a failure is confined to that script. Heredocs within such
21# scripts use a different end mark, usually END.
22#
23# Some tests deliberately leave the .if stack in a bad state when they
24# fail. Such tests are followed by ".if 1" and ".endif" before the
25# ".check", so that a failure does not cause the rest of this script
26# to be skipped.
27#
28.open :memory:
29
30# The following tests must come before any SQL statement, so that
31# the database connection has not yet been opened.
32#
33.testcase 100
34.if 0
35.print 100-BUG
36.endif
37.print 100-end
38.check 100-end
39
40.testcase 110
41.if capable('abs')
42.print 110-yes
43.else
44.print 110-BUG
45.endif
46.check 110-yes
47
48.testcase setup
49.open :memory:
50.mode list
51.headers off
52CREATE TABLE t1(a);
53INSERT INTO t1 VALUES(1),(2),(3);
54.check ''
55
56#############################################################################
57# Basic .if, .else, .endif
58#
59.testcase 200
60.if 1
61.print 200-then
62.endif
63.if 0
64.print 200-BUG
65.endif
66.print 200-end
67.check <<END
68200-then
69200-end
70END
71
72.testcase 210
73.if 1
74.print 210-then
75.else
76.print 210-BUG
77.endif
78.if 0
79.print 210-BUG
80.else
81.print 210-else
82.endif
83.check <<END
84210-then
85210-else
86END
87
88# SQL statements are skipped as well as dot-commands
89#
90.testcase 220
91.if 0
92SELECT '220-BUG';
93INSERT INTO t1 VALUES(99);
94.endif
95SELECT count(*) FROM t1;
96.check 3
97
98# SQL errors in skipped regions are not reported
99#
100.testcase 230
101.if 0
102SELECT * FROM no_such_table;
103THIS IS NOT VALID SQL;
104.endif
105.print 230-end
106.check 230-end
107
108# A multi-line SQL statement in a skipped region is skipped in its
109# entirety, even if it contains text that looks like a dot-command.
110#
111.testcase 240
112.if 0
113SELECT '240-BUG
114.endif
115.print 240-BUG
116';
117.print 240-BUG
118.endif
119.print 240-end
120.check 240-end
121
122# .exit within a skipped region is not run
123#
124.testcase 250
125.if 0
126.exit
127.endif
128.print 250-end
129.check 250-end
130
131# Skipped .testcase/.check pairs
132#
133.testcase 260
134.if 0
135.testcase 260-inner
136SELECT 'x';
137.check 'y'
138.endif
139.print 260-end
140.check 260-end
141
142# A ".check <<END" heredoc within a skipped region is skipped.
143# The text of the heredoc is not treated as SQL or as dot-commands.
144#
145.testcase 270
146.read <<EOF
147.if 0
148.testcase 270-inner
149SELECT 1;
150.check <<END
1511
152.print 270-BUG
153END
154.endif
155.print 270-inner-end
156EOF
157.if 1
158.endif
159.check 270-inner-end
160
161#############################################################################
162# Truth values of the expression
163#
164.testcase 300
165.if 1+1=2
166.print 300-a
167.endif
168.if 1+1=3
169.print 300-BUG
170.endif
171.if NULL
172.print 300-BUG
173.endif
174.if (SELECT count(*) FROM t1)=3
175.print 300-b
176.endif
177.if (SELECT a FROM t1 WHERE a>100)
178.print 300-BUG
179.endif
180.if EXISTS(SELECT 1 FROM t1 WHERE a=2)
181.print 300-c
182.endif
183.if -1
184.print 300-d
185.endif
186.if 'abc'
187.print 300-BUG
188.endif
189.if '5'
190.print 300-e
191.endif
192.if 0.0
193.print 300-BUG
194.endif
195.if 0.5
196.print 300-f
197.endif
198.if 9223372036854775807
199.print 300-g
200.endif
201.check <<END
202300-a
203300-b
204300-c
205300-d
206300-e
207300-f
208300-g
209END
210
211# A trailing semicolon is allowed
212#
213.testcase 310
214.if 1;
215.print 310-a
216.endif
217.if 0;
218.print 310-BUG
219.endif
220.check 310-a
221
222# The expression may use parameters set by .parameter
223#
224.testcase 320
225.parameter set $x 5
226.if $x>3
227.print 320-a
228.else
229.print 320-BUG
230.endif
231.if $x>10
232.print 320-BUG
233.endif
234.parameter unset $x
235.check 320-a
236
237# An expression that has a syntax error or a run-time error is false.
238# No error message is issued.
239#
240.testcase 330
241.if 1 +
242.print 330-BUG
243.else
244.print 330-else
245.endif
246.check 330-else
247
248.testcase 331
249.if
250.print 331-BUG
251.else
252.print 331-else
253.endif
254.check 331-else
255
256.testcase 332
257.if (SELECT x FROM no_such_table)
258.print 332-BUG
259.else
260.print 332-else
261.endif
262.check 332-else
263
264.testcase 333
265.if NOT (SELECT x FROM no_such_table)
266.print 333-BUG
267.endif
268.print 333-end
269.check 333-end
270
271.testcase 340
272.if abs(-9223372036854775808)
273.print 340-BUG
274.else
275.print 340-else
276.endif
277.check 340-else
278
279.testcase 341
280.if 0
281.print 341-BUG
282.elseif abs(-9223372036854775808)
283.print 341-BUG
284.elseif no_such_function(1)
285.print 341-BUG
286.else
287.print 341-else
288.endif
289.check 341-else
290
291# A capable() test on a table that might not exist. This is the
292# intended use for the error-is-false rule.
293#
294.testcase 350
295.if (SELECT count(*) FROM pragma_module_list WHERE name='no_such_module')
296.print 350-BUG
297.elseif (SELECT count(*) FROM no_such_vtab)>=0
298.print 350-BUG
299.else
300.print 350-else
301.endif
302.check 350-else
303
304#############################################################################
305# .elseif
306#
307.testcase 400
308.if 0
309.print 400-BUG
310.elseif 1
311.print 400-a
312.else
313.print 400-BUG
314.endif
315.check 400-a
316
317.testcase 410
318.if 0
319.print 410-BUG
320.elseif 0
321.print 410-BUG
322.else
323.print 410-a
324.endif
325.check 410-a
326
327# At most one branch of an .if/.elseif/.else chain is run
328#
329.testcase 420
330.if 1
331.print 420-a
332.elseif 1
333.print 420-BUG
334.endif
335.check 420-a
336
337.testcase 421
338.if 1
339.print 421-a
340.elseif 0
341.print 421-BUG
342.else
343.print 421-BUG
344.endif
345.check 421-a
346
347.testcase 422
348.if 0
349.print 422-BUG
350.elseif 0
351.print 422-BUG
352.elseif 1
353.print 422-a
354.elseif 1
355.print 422-BUG
356.else
357.print 422-BUG
358.endif
359.check 422-a
360
361.testcase 423
362.if 0
363.print 423-BUG
364.elseif 1
365.print 423-a
366.elseif 0
367.print 423-BUG
368.else
369.print 423-BUG
370.endif
371.check 423-a
372
373# The chain state of one .if does not carry over to the next .if at
374# the same level.
375#
376.testcase 424
377.if 1
378.endif
379.if 0
380.print 424-BUG
381.elseif 1
382.print 424-a
383.endif
384.if 1
385.endif
386.if 0
387.print 424-BUG
388.else
389.print 424-b
390.endif
391.check <<END
392424-a
393424-b
394END
395
396#############################################################################
397# Nesting
398#
399.testcase 500
400.if 1
401.print 500-a
402.if 0
403.print 500-BUG
404.else
405.print 500-b
406.endif
407.print 500-c
408.else
409.print 500-BUG
410.endif
411.check <<END
412500-a
413500-b
414500-c
415END
416
417# An inner .else or .elseif within a skipped region does not cause
418# anything to run
419#
420.testcase 510
421.if 0
422.if 0
423.print 510-BUG
424.else
425.print 510-BUG
426.endif
427.if 0
428.print 510-BUG
429.elseif 1
430.print 510-BUG
431.endif
432.if 1
433.print 510-BUG
434.endif
435.else
436.print 510-a
437.endif
438.check 510-a
439
440# Nested .if/.elseif/.else chains within the branches of an outer chain
441#
442.testcase 511
443.if 0
444.print 511-BUG
445.elseif 1
446.if 0
447.print 511-BUG
448.elseif 1
449.print 511-a
450.else
451.print 511-BUG
452.endif
453.else
454.if 1
455.print 511-BUG
456.else
457.print 511-BUG
458.endif
459.endif
460.check 511-a
461
462# Unknown dot-commands within a skipped region are not reported, even
463# if they begin with "if", "else", "read", or "check".
464#
465.testcase 530
466.if 0
467.no_such_command
468.iffy
469.elsewhere
470.endiff
471.readx
472.checkmate
473.endif
474.print 530-end
475.check 530-end
476
477# Ten levels of nesting are allowed. The error message says the
478# maximum is ten levels.
479#
480.testcase 540
481.read <<EOF
482.if 1
483.if 1
484.if 1
485.if 1
486.if 1
487.if 1
488.if 1
489.if 1
490.if 1
491.if 1
492.print 540-depth-10
493.endif
494.endif
495.endif
496.endif
497.endif
498.endif
499.endif
500.endif
501.endif
502.endif
503.print 540-inner-end
504EOF
505.check <<END
506540-depth-10
507540-inner-end
508END
509
510# Nesting too deep is an error. The outer script continues, and
511# it is left in a sane state.
512#
513.testcase 550
514.read <<EOF
515.if 0
516.if 1
517.if 1
518.if 1
519.if 1
520.if 1
521.if 1
522.if 1
523.if 1
524.if 1
525.if 1
526.print 550-BUG
527EOF
528.print 550-a
529.if 1
530.endif
531.check --keep --any "nested too deep"
532.check --keep --nowhere 550-BUG
533.check --any 550-a
534
535#############################################################################
536# Errors
537#
538.testcase 600
539.else
540.check --any ".else without a .if"
541
542.testcase 610
543.endif
544.check --any ".endif without a .if"
545
546# .elseif without .if is an error, but it does not stop the script
547#
548.testcase 620
549.read <<EOF
550.elseif 1
551.print 620-inner-end
552EOF
553.check --keep --any ".elseif with no .if"
554.check --any 620-inner-end
555
556.testcase 630
557.if 1
558.print 630-a
559.else
560.else
561.print 630-BUG
562.endif
563.check --keep --any "already saw a .else"
564.check --nowhere 630-BUG
565
566# .elseif after .else is an error. It does not cause the body that
567# follows it to run.
568#
569.testcase 640
570.if 1
571.print 640-a
572.else
573.print 640-BUG
574.elseif 1
575.print 640-BUG
576.endif
577.check --keep --any ".elseif follows a .else"
578.check --nowhere 640-BUG
579
580.testcase 641
581.if 0
582.print 641-BUG
583.else
584.print 641-a
585.elseif 1
586.print 641-BUG
587.endif
588.check --keep --any ".elseif follows a .else"
589.check --nowhere 641-BUG
590
591#############################################################################
592# Interaction with .read
593#
594# A .read script that ends with an unterminated .if reports an error,
595# and the caller is not affected.
596#
597.testcase 700
598.read <<EOF
599.print 700-inner
600.if 0
601.print 700-BUG
602EOF
603.print 700-after-read
604.if 1
605.endif
606.check --keep --any "<heredoc>:2: unterminated .if"
607.check --keep --nowhere 700-BUG
608.check --glob "*700-inner*700-after-read*"
609
610# Also with nested unterminated .ifs. The .else at nesting level 2
611# must not be remembered after the .read finishes.
612#
613.testcase 701
614.read <<EOF
615.if 1
616.if 0
617.else
618.print 701-inner
619EOF
620.print 701-after-read
621.if 1
622.if 0
623.else
624.print 701-nested-else
625.endif
626.endif
627.if 1
628.if 1
629.endif
630.endif
631.check --keep --any "unterminated .if"
632.check --keep --nowhere "already saw a .else"
633.check --glob "*701-inner*701-after-read*701-nested-else*"
634
635# A true branch taken in an unterminated .if of a .read script must
636# not be remembered after the .read finishes.
637#
638.testcase 702
639.read <<EOF
640.if 1
641EOF
642.if 0
643.print 702-BUG
644.elseif 1
645.print 702-a
646.endif
647.if 1
648.endif
649.check --keep --any "unterminated .if"
650.check --any 702-a
651
652# A .read script cannot close, invert, or change a .if of its caller
653#
654.testcase 710
655.read <<OUTER
656.if 1
657.read <<EOF
658.endif
659EOF
660.read <<EOF
661.else
662EOF
663.read <<EOF
664.elseif 1
665EOF
666.read <<EOF
667.elseif 0
668EOF
669.print 710-a
670.else
671.print 710-BUG
672.endif
673.print 710-end
674OUTER
675.if 1
676.endif
677.check --keep --any ".endif without a .if"
678.check --keep --any ".else without a .if"
679.check --keep --any ".elseif with no .if"
680.check --keep --nowhere 710-BUG
681.check --glob "*710-a*710-end*"
682
683.testcase 711
684.read <<OUTER
685.if 0
686.print 711-BUG
687.else
688.read <<EOF
689.elseif 1
690EOF
691.print 711-a
692.endif
693OUTER
694.if 1
695.endif
696.check --keep --any ".elseif with no .if"
697.check --any 711-a
698
699# .if and .endif within a .read script work normally when the
700# caller is within a .if
701#
702.testcase 720
703.if 1
704.read <<EOF
705.if 0
706.print 720-BUG
707.else
708.print 720-inner
709.endif
710EOF
711.print 720-a
712.endif
713.check <<END
714720-inner
715720-a
716END
717
718# A .read heredoc within a skipped region is not run, and its text is
719# skipped in its entirety. It is not treated as SQL or as dot-commands
720# of the outer script.
721#
722.testcase 730
723.read <<OUTER
724.if 0
725.read <<EOF
726.print 730-BUG
727.endif
728.print 730-BUG
729EOF
730.print 730-a
731.endif
732.print 730-inner-end
733OUTER
734.if 1
735.endif
736.check <<END
737730-inner-end
738END
739
740.testcase 731
741.read <<OUTER
742.if 0
743.read <<EOF
744.check <<END
745.print 731-BUG
746END
747.print 731-BUG
748EOF
749.endif
750.print 731-inner-end
751OUTER
752.if 1
753.endif
754.check 731-inner-end
755
756# .read heredocs nest
757#
758.testcase 740
759.read <<OUTER
760.print 740-a
761.read <<INNER
762.if 1
763.print 740-b
764.endif
765INNER
766.print 740-c
767OUTER
768.check <<END
769740-a
770740-b
771740-c
772END
773
774# A .read heredoc may be the last thing in an .if branch, and
775# an .else that follows it is processed by the outer script.
776#
777.testcase 750
778.read <<OUTER
779.if 0
780.read <<EOF
781.print 750-BUG
782EOF
783.else
784.read <<EOF
785.print 750-a
786EOF
787.endif
788OUTER
789.if 1
790.endif
791.check 750-a
792
793# Line numbers in error messages within the outer script are correct
794# after a .read heredoc. The .else on the line after the EOF mark is
795# on line 802.
796#
797.testcase 760
798.read <<EOF
799.print 760-a
800.print 760-b
801.print 760-c
802EOF
803.else
804.check --keep --glob "*if.sql:#: .else without a .if"
805.check --any "760-c"
806
807#############################################################################
808# The capable() function
809#
810.testcase 800
811SELECT capable('abs'), capable('ABS'), capable('capable'),
812 capable('no_such_capability');
813.check 1|1|1|0
814
815.if capable('vtab')
816.testcase 810
817SELECT capable('generate_series')
818 = EXISTS(SELECT 1 FROM pragma_module_list WHERE name='generate_series');
819.check 1
820
821# Every compile-time option, other than OMIT and DISABLE options, is
822# a capability.
823#
824.testcase 820
825SELECT compile_options FROM pragma_compile_options
826 WHERE compile_options NOT GLOB 'OMIT_*'
827 AND compile_options NOT GLOB 'DISABLE_*'
828 AND NOT capable(compile_options);
829.check ''
830
831# The ENABLE_ prefix is optional and case does not matter
832#
833.testcase 830
834SELECT capable('fts5')=capable('ENABLE_FTS5'),
835 capable('fts5')=capable('enable_fts5'),
836 capable('fts5')=EXISTS(SELECT 1 FROM pragma_compile_options
837 WHERE compile_options='ENABLE_FTS5');
838.check 1|1|1
839.endif /* capable(virtualtable) */
840
841.testcase 850
842.null NULL
843SELECT capable(NULL);
844.check NULL
845
846# Self-reference of capable()
847#
848.testcase 860
849SELECT capable('capable')
850.check 1
851
852.testcase 870
853.if capable('no_such_capability')
854.print 870-BUG
855.elseif capable('abs')
856.print 870-a
857.endif
858.check 870-a