sqlite/test/capi2.test
drh 37ed48ed2f Provide a more informative error message when a uniqueness constraint
fails.  Ticket #419. (CVS 1068)

FossilOrigin-Name: 086aa1c9922b7bf399b3ee8b73ba7353d126b119
2003-08-05 13:13:38 +00:00

476 lines
13 KiB
Plaintext

# 2003 January 29
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this script testing the callback-free C/C++ API.
#
# $Id: capi2.test,v 1.10 2003/08/05 13:13:38 drh Exp $
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Check basic functionality
#
do_test capi2-1.1 {
db close
set DB [sqlite db test.db]
execsql {CREATE TABLE t1(a,b,c)}
set VM [sqlite_compile $DB {SELECT name, rowid FROM sqlite_master} TAIL]
set TAIL
} {}
do_test capi2-1.2 {
sqlite_step $VM N VALUES COLNAMES
} {SQLITE_ROW}
do_test capi2-1.3 {
set N
} {2}
do_test capi2-1.4 {
set VALUES
} {t1 1}
do_test capi2-1.5 {
set COLNAMES
} {name rowid text INTEGER}
do_test capi2-1.6 {
set N x
set VALUES y
set COLNAMES z
sqlite_step $VM N VALUES COLNAMES
} {SQLITE_DONE}
do_test capi2-1.7 {
list $N $VALUES $COLNAMES
} {2 {} {name rowid text INTEGER}}
do_test capi2-1.8 {
set N x
set VALUES y
set COLNAMES z
sqlite_step $VM N VALUES COLNAMES
} {SQLITE_MISUSE}
do_test capi2-1.9 {
list $N $VALUES $COLNAMES
} {0 {} {}}
do_test capi2-1.10 {
sqlite_finalize $VM
} {}
# Check to make sure that the "tail" of a multi-statement SQL script
# is returned by sqlite_compile.
#
do_test capi2-2.1 {
set SQL {
SELECT name, rowid FROM sqlite_master;
SELECT name, rowid FROM sqlite_temp_master;
-- A comment at the end
}
set VM [sqlite_compile $DB $SQL SQL]
set SQL
} {
SELECT name, rowid FROM sqlite_temp_master;
-- A comment at the end
}
do_test capi2-2.2 {
set r [sqlite_step $VM n val colname]
lappend r $n $val $colname
} {SQLITE_ROW 2 {t1 1} {name rowid text INTEGER}}
do_test capi2-2.3 {
set r [sqlite_step $VM n val colname]
lappend r $n $val $colname
} {SQLITE_DONE 2 {} {name rowid text INTEGER}}
do_test capi2-2.4 {
sqlite_finalize $VM
} {}
do_test capi2-2.5 {
set VM [sqlite_compile $DB $SQL SQL]
set SQL
} {
-- A comment at the end
}
do_test capi2-2.6 {
set r [sqlite_step $VM n val colname]
lappend r $n $val $colname
} {SQLITE_DONE 2 {} {name rowid text INTEGER}}
do_test capi2-2.7 {
sqlite_finalize $VM
} {}
do_test capi2-2.8 {
set VM [sqlite_compile $DB $SQL SQL]
list $SQL $VM
} {{} {}}
# Check the error handling.
#
do_test capi2-3.1 {
set rc [catch {
sqlite_compile $DB {select bogus from sqlite_master} TAIL
} msg]
lappend rc $msg $TAIL
} {1 {(1) no such column: bogus} {}}
do_test capi2-3.2 {
set rc [catch {
sqlite_compile $DB {select bogus from } TAIL
} msg]
lappend rc $msg $TAIL
} {1 {(1) near " ": syntax error} {}}
do_test capi2-3.3 {
set rc [catch {
sqlite_compile $DB {;;;;select bogus from sqlite_master} TAIL
} msg]
lappend rc $msg $TAIL
} {1 {(1) no such column: bogus} {}}
do_test capi2-3.4 {
set rc [catch {
sqlite_compile $DB {select bogus from sqlite_master;x;} TAIL
} msg]
lappend rc $msg $TAIL
} {1 {(1) no such column: bogus} {x;}}
do_test capi2-3.5 {
set rc [catch {
sqlite_compile $DB {select bogus from sqlite_master;;;x;} TAIL
} msg]
lappend rc $msg $TAIL
} {1 {(1) no such column: bogus} {;;x;}}
do_test capi2-3.6 {
set rc [catch {
sqlite_compile $DB {select 5/0} TAIL
} VM]
lappend rc $TAIL
} {0 {}}
do_test capi2-3.7 {
set N {}
set VALUE {}
set COLNAME {}
list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 {{}} {5/0 NUMERIC}}
do_test capi2-3.8 {
sqlite_finalize $VM
} {}
do_test capi2-3.9 {
execsql {CREATE UNIQUE INDEX i1 ON t1(a)}
set VM [sqlite_compile $DB {INSERT INTO t1 VALUES(1,2,3)} TAIL]
set TAIL
} {}
do_test capi2-3.9b {db changes} {0}
do_test capi2-3.10 {
set N {}
set VALUE {}
set COLNAME {}
list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_DONE 0 {} {}}
do_test capi2-3.10b {db changes} {1}
do_test capi2-3.11 {
sqlite_finalize $VM
} {}
do_test capi2-3.11b {db changes} {1}
do_test capi2-3.12 {
list [catch {sqlite_finalize $VM} msg] [set msg]
} {1 {(21) library routine called out of sequence}}
do_test capi2-3.13 {
set VM [sqlite_compile $DB {INSERT INTO t1 VALUES(1,3,4)} TAIL]
list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ERROR 0 {} {}}
do_test capi2-3.13b {db changes} {0}
do_test capi2-3.14 {
list [catch {sqlite_finalize $VM} msg] [set msg]
} {1 {(19) column a is not unique}}
do_test capi2-3.15 {
set VM [sqlite_compile $DB {CREATE TABLE t2(a NOT NULL, b)} TAIL]
set TAIL
} {}
do_test capi2-3.16 {
list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_DONE 0 {} {}}
do_test capi2-3.17 {
list [catch {sqlite_finalize $VM} msg] [set msg]
} {0 {}}
do_test capi2-3.18 {
set VM [sqlite_compile $DB {INSERT INTO t2 VALUES(NULL,2)} TAIL]
list [sqlite_step $VM N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ERROR 0 {} {}}
do_test capi2-3.19 {
list [catch {sqlite_finalize $VM} msg] [set msg]
} {1 {(19) t2.a may not be NULL}}
# Two or more virtual machines exists at the same time.
#
do_test capi2-4.1 {
set VM1 [sqlite_compile $DB {INSERT INTO t2 VALUES(1,2)} TAIL]
set TAIL
} {}
do_test capi2-4.2 {
set VM2 [sqlite_compile $DB {INSERT INTO t2 VALUES(2,3)} TAIL]
set TAIL
} {}
do_test capi2-4.3 {
set VM3 [sqlite_compile $DB {INSERT INTO t2 VALUES(3,4)} TAIL]
set TAIL
} {}
do_test capi2-4.4 {
list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_DONE 0 {} {}}
do_test capi2-4.5 {
execsql {SELECT * FROM t2 ORDER BY a}
} {2 3}
do_test capi2-4.6 {
list [catch {sqlite_finalize $VM2} msg] [set msg]
} {0 {}}
do_test capi2-4.7 {
list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_DONE 0 {} {}}
do_test capi2-4.8 {
execsql {SELECT * FROM t2 ORDER BY a}
} {2 3 3 4}
do_test capi2-4.9 {
list [catch {sqlite_finalize $VM3} msg] [set msg]
} {0 {}}
do_test capi2-4.10 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_DONE 0 {} {}}
do_test capi2-4.11 {
execsql {SELECT * FROM t2 ORDER BY a}
} {1 2 2 3 3 4}
do_test capi2-4.12 {
list [catch {sqlite_finalize $VM1} msg] [set msg]
} {0 {}}
# Interleaved SELECTs
#
do_test capi2-5.1 {
set VM1 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
set VM2 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
set VM3 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 2 {2 3} {a b {} {}}}
do_test capi2-5.2 {
list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 2 {2 3} {a b {} {}}}
do_test capi2-5.3 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 2 {3 4} {a b {} {}}}
do_test capi2-5.4 {
list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 2 {2 3} {a b {} {}}}
do_test capi2-5.5 {
list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 2 {3 4} {a b {} {}}}
do_test capi2-5.6 {
list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 2 {1 2} {a b {} {}}}
do_test capi2-5.7 {
list [sqlite_step $VM3 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_DONE 2 {} {a b {} {}}}
do_test capi2-5.8 {
list [catch {sqlite_finalize $VM3} msg] [set msg]
} {0 {}}
do_test capi2-5.9 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 2 {1 2} {a b {} {}}}
do_test capi2-5.10 {
list [catch {sqlite_finalize $VM1} msg] [set msg]
} {0 {}}
do_test capi2-5.11 {
list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 2 {3 4} {a b {} {}}}
do_test capi2-5.12 {
list [sqlite_step $VM2 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 2 {1 2} {a b {} {}}}
do_test capi2-5.11 {
list [catch {sqlite_finalize $VM2} msg] [set msg]
} {0 {}}
# Check for proper SQLITE_BUSY returns.
#
do_test capi2-6.1 {
execsql {
BEGIN;
CREATE TABLE t3(x counter);
INSERT INTO t3 VALUES(1);
INSERT INTO t3 VALUES(2);
INSERT INTO t3 SELECT x+2 FROM t3;
INSERT INTO t3 SELECT x+4 FROM t3;
INSERT INTO t3 SELECT x+8 FROM t3;
COMMIT;
}
set VM1 [sqlite_compile $DB {SELECT * FROM t3} TAIL]
sqlite db2 test.db
execsql {BEGIN} db2
} {}
do_test capi2-6.2 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_BUSY 0 {} {}}
do_test capi2-6.3 {
execsql {COMMIT} db2
} {}
do_test capi2-6.4 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 1 {x counter}}
do_test capi2-6.5 {
catchsql {BEGIN} db2
} {1 {database is locked}}
do_test capi2-6.6 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 2 {x counter}}
do_test capi2-6.7 {
execsql {SELECT * FROM t2} db2
} {2 3 3 4 1 2}
do_test capi2-6.8 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 3 {x counter}}
do_test capi2-6.9 {
execsql {SELECT * FROM t2}
} {2 3 3 4 1 2}
do_test capi2-6.10 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 4 {x counter}}
do_test capi2-6.11 {
execsql {BEGIN}
} {}
do_test capi2-6.12 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 5 {x counter}}
# execsql {pragma vdbe_trace=on}
do_test capi2-6.13 {
catchsql {UPDATE t3 SET x=x+1}
} {1 {database table is locked}}
do_test capi2-6.14 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 6 {x counter}}
# puts [list [catch {sqlite_finalize $VM1} msg] [set msg]]; exit
do_test capi2-6.15 {
execsql {SELECT * FROM t1}
} {1 2 3}
do_test capi2-6.16 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 7 {x counter}}
do_test capi2-6.17 {
catchsql {UPDATE t1 SET b=b+1}
} {0 {}}
do_test capi2-6.18 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 8 {x counter}}
do_test capi2-6.19 {
execsql {SELECT * FROM t1}
} {1 3 3}
do_test capi2-6.20 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 9 {x counter}}
do_test capi2-6.21 {
execsql {ROLLBACK; SELECT * FROM t1}
} {1 2 3}
do_test capi2-6.22 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 10 {x counter}}
do_test capi2-6.23 {
execsql {BEGIN TRANSACTION ON CONFLICT ROLLBACK;}
} {}
do_test capi2-6.24 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 11 {x counter}}
do_test capi2-6.25 {
execsql {
INSERT INTO t1 VALUES(2,3,4);
SELECT * FROM t1;
}
} {1 2 3 2 3 4}
do_test capi2-6.26 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 12 {x counter}}
do_test capi2-6.27 {
catchsql {
INSERT INTO t1 VALUES(2,4,5);
SELECT * FROM t1;
}
} {1 {column a is not unique}}
do_test capi2-6.28 {
list [sqlite_step $VM1 N VALUE COLNAME] [set N] [set VALUE] [set COLNAME]
} {SQLITE_ROW 1 13 {x counter}}
do_test capi2-6.99 {
list [catch {sqlite_finalize $VM1} msg] [set msg]
} {0 {}}
catchsql {ROLLBACK}
do_test capi2-7.1 {
stepsql $DB {
SELECT * FROM t1
}
} {0 1 2 3}
do_test capi2-7.2 {
stepsql $DB {
PRAGMA count_changes=on
}
} {0}
do_test capi2-7.3 {
stepsql $DB {
UPDATE t1 SET a=a+10;
}
} {0 1}
do_test capi2-7.4 {
stepsql $DB {
INSERT INTO t1 SELECT a+1,b+1,c+1 FROM t1;
}
} {0 1}
do_test capi2-7.4b {db changes} {1}
do_test capi2-7.5 {
stepsql $DB {
UPDATE t1 SET a=a+10;
}
} {0 2}
do_test capi2-7.5b {db changes} {2}
do_test capi2-7.6 {
stepsql $DB {
SELECT * FROM t1;
}
} {0 21 2 3 22 3 4}
do_test capi2-7.7 {
stepsql $DB {
INSERT INTO t1 SELECT a+2,b+2,c+2 FROM t1;
}
} {0 2}
do_test capi2-7.8 {
db changes
} {2}
do_test capi2-7.9 {
stepsql $DB {
SELECT * FROM t1;
}
} {0 21 2 3 22 3 4 23 4 5 24 5 6}
do_test capi2-7.10 {
stepsql $DB {
UPDATE t1 SET a=a-20;
SELECT * FROM t1;
}
} {0 4 1 2 3 2 3 4 3 4 5 4 5 6}
do_test capi2-7.11 {
db changes
} {0}
do_test capi2-7.12 {
set x [stepsql $DB {EXPLAIN SELECT * FROM t1}]
lindex $x 0
} {0}
# Ticket #261 - make sure we can finalize before the end of a query.
#
do_test capi2-8.1 {
set VM1 [sqlite_compile $DB {SELECT * FROM t2} TAIL]
sqlite_finalize $VM1
} {}
# Tickets #384 and #385 - make sure the TAIL argument to sqlite_compile
# and all of the return pointers in sqlite_step can be null.
#
do_test capi2-9.1 {
set VM1 [sqlite_compile $DB {SELECT * FROM t2}]
sqlite_step $VM1
sqlite_finalize $VM1
} {}
db2 close
finish_test