CoolFace
Modelpublic

AryaWu/sqlite

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
e_insert.test432 linesDownload Raw Back to test
1# 2010 September 182#3# The author disclaims copyright to this source code.  In place of4# a legal notice, here is a blessing:5#6#    May you do good and not evil.7#    May you find forgiveness for yourself and forgive others.8#    May you share freely, never taking more than you give.9#10#***********************************************************************11#12# The majority of this file implements tests to verify that the "testable13# statements" in the lang_insert.html document are correct.14#15# Also, it contains tests to verify the statements in (the very short)16# lang_replace.html.17#18set testdir [file dirname $argv0]19source $testdir/tester.tcl20 21ifcapable !compound {22  finish_test23  return24}25 26# Organization of tests:27#28#   e_insert-0.*: Test the syntax diagram.29#30#   e_insert-1.*: Test statements of the form "INSERT ... VALUES(...)".31#   32#   e_insert-2.*: Test statements of the form "INSERT ... SELECT ...".33#34#   e_insert-3.*: Test statements of the form "INSERT ... DEFAULT VALUES".35#36#   e_insert-4.*: Test statements regarding the conflict clause.37#38#   e_insert-5.*: Test that the qualified table name and "DEFAULT VALUES"39#                 syntaxes do not work in trigger bodies.40#41 42do_execsql_test e_insert-0.0 {43  CREATE TABLE a1(a, b);44  CREATE TABLE a2(a, b, c DEFAULT 'xyz');45  CREATE TABLE a3(x DEFAULT 1.0, y DEFAULT 'string', z);46  CREATE TABLE a4(c UNIQUE, d);47} {}48 49proc do_insert_tests {args} {50  uplevel do_select_tests $args51}52 53# -- syntax diagram insert-stmt54#55do_insert_tests e_insert-0 {56     1  "INSERT             INTO a1 DEFAULT VALUES"                   {}57     2  "INSERT             INTO main.a1 DEFAULT VALUES"              {}58     3  "INSERT OR ROLLBACK INTO main.a1 DEFAULT VALUES"              {}59     4  "INSERT OR ROLLBACK INTO a1 DEFAULT VALUES"                   {}60     5  "INSERT OR ABORT    INTO main.a1 DEFAULT VALUES"              {}61     6  "INSERT OR ABORT    INTO a1 DEFAULT VALUES"                   {}62     7  "INSERT OR REPLACE  INTO main.a1 DEFAULT VALUES"              {}63     8  "INSERT OR REPLACE  INTO a1 DEFAULT VALUES"                   {}64     9  "INSERT OR FAIL     INTO main.a1 DEFAULT VALUES"              {}65    10  "INSERT OR FAIL     INTO a1 DEFAULT VALUES"                   {}66    11  "INSERT OR FAIL     INTO main.a1 DEFAULT VALUES"              {}67    12  "INSERT OR IGNORE   INTO a1 DEFAULT VALUES"                   {}68    13  "REPLACE            INTO a1 DEFAULT VALUES"                   {}69    14  "REPLACE            INTO main.a1 DEFAULT VALUES"              {}70    15  "INSERT             INTO a1      VALUES(1, 2)"                {}71    16  "INSERT             INTO main.a1 VALUES(1, 2)"                {}72    17  "INSERT OR ROLLBACK INTO main.a1 VALUES(1, 2)"                {}73    18  "INSERT OR ROLLBACK INTO a1      VALUES(1, 2)"                {}74    19  "INSERT OR ABORT    INTO main.a1 VALUES(1, 2)"                {}75    20  "INSERT OR ABORT    INTO a1      VALUES(1, 2)"                {}76    21  "INSERT OR REPLACE  INTO main.a1 VALUES(1, 2)"                {}77    22  "INSERT OR REPLACE  INTO a1      VALUES(1, 2)"                {}78    23  "INSERT OR FAIL     INTO main.a1 VALUES(1, 2)"                {}79    24  "INSERT OR FAIL     INTO a1      VALUES(1, 2)"                {}80    25  "INSERT OR FAIL     INTO main.a1 VALUES(1, 2)"                {}81    26  "INSERT OR IGNORE   INTO a1      VALUES(1, 2)"                {}82    27  "REPLACE            INTO a1      VALUES(1, 2)"                {}83    28  "REPLACE            INTO main.a1 VALUES(1, 2)"                {}84    29  "INSERT             INTO a1      (b, a) VALUES(1, 2)"         {}85    30  "INSERT             INTO main.a1 (b, a) VALUES(1, 2)"         {}86    31  "INSERT OR ROLLBACK INTO main.a1 (b, a) VALUES(1, 2)"         {}87    32  "INSERT OR ROLLBACK INTO a1      (b, a) VALUES(1, 2)"         {}88    33  "INSERT OR ABORT    INTO main.a1 (b, a) VALUES(1, 2)"         {}89    34  "INSERT OR ABORT    INTO a1      (b, a) VALUES(1, 2)"         {}90    35  "INSERT OR REPLACE  INTO main.a1 (b, a) VALUES(1, 2)"         {}91    36  "INSERT OR REPLACE  INTO a1      (b, a) VALUES(1, 2)"         {}92    37  "INSERT OR FAIL     INTO main.a1 (b, a) VALUES(1, 2)"         {}93    38  "INSERT OR FAIL     INTO a1      (b, a) VALUES(1, 2)"         {}94    39  "INSERT OR FAIL     INTO main.a1 (b, a) VALUES(1, 2)"         {}95    40  "INSERT OR IGNORE   INTO a1      (b, a) VALUES(1, 2)"         {}96    41  "REPLACE            INTO a1      (b, a) VALUES(1, 2)"         {}97    42  "REPLACE            INTO main.a1 (b, a) VALUES(1, 2)"         {}98    43  "INSERT             INTO a1      SELECT c, b FROM a2"         {}99    44  "INSERT             INTO main.a1 SELECT c, b FROM a2"         {}100    45  "INSERT OR ROLLBACK INTO main.a1 SELECT c, b FROM a2"         {}101    46  "INSERT OR ROLLBACK INTO a1      SELECT c, b FROM a2"         {}102    47  "INSERT OR ABORT    INTO main.a1 SELECT c, b FROM a2"         {}103    48  "INSERT OR ABORT    INTO a1      SELECT c, b FROM a2"         {}104    49  "INSERT OR REPLACE  INTO main.a1 SELECT c, b FROM a2"         {}105    50  "INSERT OR REPLACE  INTO a1      SELECT c, b FROM a2"         {}106    51  "INSERT OR FAIL     INTO main.a1 SELECT c, b FROM a2"         {}107    52  "INSERT OR FAIL     INTO a1      SELECT c, b FROM a2"         {}108    53  "INSERT OR FAIL     INTO main.a1 SELECT c, b FROM a2"         {}109    54  "INSERT OR IGNORE   INTO a1      SELECT c, b FROM a2"         {}110    55  "REPLACE            INTO a1      SELECT c, b FROM a2"         {}111    56  "REPLACE            INTO main.a1 SELECT c, b FROM a2"         {}112    57  "INSERT             INTO a1      (b, a) SELECT c, b FROM a2"  {}113    58  "INSERT             INTO main.a1 (b, a) SELECT c, b FROM a2"  {}114    59  "INSERT OR ROLLBACK INTO main.a1 (b, a) SELECT c, b FROM a2"  {}115    60  "INSERT OR ROLLBACK INTO a1      (b, a) SELECT c, b FROM a2"  {}116    61  "INSERT OR ABORT    INTO main.a1 (b, a) SELECT c, b FROM a2"  {}117    62  "INSERT OR ABORT    INTO a1      (b, a) SELECT c, b FROM a2"  {}118    63  "INSERT OR REPLACE  INTO main.a1 (b, a) SELECT c, b FROM a2"  {}119    64  "INSERT OR REPLACE  INTO a1      (b, a) SELECT c, b FROM a2"  {}120    65  "INSERT OR FAIL     INTO main.a1 (b, a) SELECT c, b FROM a2"  {}121    66  "INSERT OR FAIL     INTO a1      (b, a) SELECT c, b FROM a2"  {}122    67  "INSERT OR FAIL     INTO main.a1 (b, a) SELECT c, b FROM a2"  {}123    68  "INSERT OR IGNORE   INTO a1      (b, a) SELECT c, b FROM a2"  {}124    69  "REPLACE            INTO a1      (b, a) SELECT c, b FROM a2"  {}125    70  "REPLACE            INTO main.a1 (b, a) SELECT c, b FROM a2"  {}126    71  "INSERT             INTO a1      (b, a) VALUES(1, 2),(3,4)"   {}127    72  "INSERT             INTO main.a1 (b, a) VALUES(1, 2),(3,4)"   {}128    73  "INSERT OR ROLLBACK INTO main.a1 (b, a) VALUES(1, 2),(3,4)"   {}129    74  "INSERT OR ROLLBACK INTO a1      (b, a) VALUES(1, 2),(3,4)"   {}130    75  "INSERT OR ABORT    INTO main.a1 (b, a) VALUES(1, 2),(3,4)"   {}131    76  "INSERT OR ABORT    INTO a1      (b, a) VALUES(1, 2),(3,4)"   {}132    77  "INSERT OR REPLACE  INTO main.a1 (b, a) VALUES(1, 2),(3,4)"   {}133    78  "INSERT OR REPLACE  INTO a1      (b, a) VALUES(1, 2),(3,4)"   {}134    79  "INSERT OR FAIL     INTO main.a1 (b, a) VALUES(1, 2),(3,4)"   {}135    80  "INSERT OR FAIL     INTO a1      (b, a) VALUES(1, 2),(3,4)"   {}136    81  "INSERT OR FAIL     INTO main.a1 (b, a) VALUES(1, 2),(3,4)"   {}137    82  "INSERT OR IGNORE   INTO a1      (b, a) VALUES(1, 2),(3,4)"   {}138    83  "REPLACE            INTO a1      (b, a) VALUES(1, 2),(3,4)"   {}139    84  "REPLACE            INTO main.a1 (b, a) VALUES(1, 2),(3,4)"   {}140}141 142delete_all_data143 144# EVIDENCE-OF: R-21490-41092 The first form (with the "VALUES" keyword)145# creates one or more new rows in an existing table.146#147do_insert_tests e_insert-1.1 {148    0    "SELECT count(*) FROM a2"           {0}149 150    1a   "INSERT INTO a2 VALUES(1, 2, 3)"    {}151    1b   "SELECT count(*) FROM a2"           {1}152 153    2a   "INSERT INTO a2(a, b) VALUES(1, 2)" {}154    2b   "SELECT count(*) FROM a2"           {2}155 156    3a   "INSERT INTO a2(a) VALUES(3),(4)"   {}157    3b   "SELECT count(*) FROM a2"           {4}158}159 160# EVIDENCE-OF: R-19218-01018 If the column-name list after table-name is161# omitted then the number of values inserted into each row must be the162# same as the number of columns in the table.163#164#   A test in the block above verifies that if the VALUES list has the165#   correct number of columns (for table a2, 3 columns) works. So these166#   tests just show that other values cause an error.167#168do_insert_tests e_insert-1.2 -error { 169  table %s has %d columns but %d values were supplied170} {171    1    "INSERT INTO a2 VALUES(1)"         {a2 3 1}172    2    "INSERT INTO a2 VALUES(1,2)"       {a2 3 2}173    3    "INSERT INTO a2 VALUES(1,2,3,4)"   {a2 3 4}174    4    "INSERT INTO a2 VALUES(1,2,3,4,5)" {a2 3 5}175}176 177# EVIDENCE-OF: R-29730-42609 In this case the result of evaluating the178# left-most expression from each term of the VALUES list is inserted179# into the left-most column of each new row, and so forth for each180# subsequent expression.181#182delete_all_data183do_insert_tests e_insert-1.3 {184    1a   "INSERT INTO a2 VALUES(1, 2, 3)"    {}185    1b   "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {1 2 3}186 187    2a   "INSERT INTO a2 VALUES('abc', NULL, 3*3+1)"      {}188    2b   "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {abc {} 10}189 190    3a   "INSERT INTO a2 VALUES((SELECT count(*) FROM a2), 'x', 'y')" {}191    3b   "SELECT * FROM a2 WHERE oid=last_insert_rowid()" {2 x y}192}193 194# EVIDENCE-OF: R-21115-58321 If a column-name list is specified, then195# the number of values in each term of the VALUE list must match the196# number of specified columns.197#198do_insert_tests e_insert-1.4 -error { 199  %d values for %d columns200} {201    1    "INSERT INTO a2(a, b, c) VALUES(1)"         {1 3}202    2    "INSERT INTO a2(a, b, c) VALUES(1,2)"       {2 3}203    3    "INSERT INTO a2(a, b, c) VALUES(1,2,3,4)"   {4 3}204    4    "INSERT INTO a2(a, b, c) VALUES(1,2,3,4,5)" {5 3}205 206    5    "INSERT INTO a2(c, a) VALUES(1)"            {1 2}207    6    "INSERT INTO a2(c, a) VALUES(1,2,3)"        {3 2}208    7    "INSERT INTO a2(c, a) VALUES(1,2,3,4)"      {4 2}209    8    "INSERT INTO a2(c, a) VALUES(1,2,3,4,5)"    {5 2}210}211 212# EVIDENCE-OF: R-07016-26442 Each of the named columns of the new row is213# populated with the results of evaluating the corresponding VALUES214# expression.215#216# EVIDENCE-OF: R-12183-43719 Table columns that do not appear in the217# column list are populated with the default column value (specified as218# part of the CREATE TABLE statement), or with NULL if no default value219# is specified.220#221delete_all_data222do_insert_tests e_insert-1.5 {223    1a   "INSERT INTO a2(b, c) VALUES('b', 'c')"     {}224    1b   "SELECT * FROM a2"                          {{} b c}225 226    2a   "INSERT INTO a2(a, b) VALUES('a', 'b')"     {}227    2b   "SELECT * FROM a2"                          {{} b c  a b xyz}228}229 230# EVIDENCE-OF: R-52173-30215 A new entry is inserted into the table for231# each row of data returned by executing the SELECT statement.232#233delete_all_data234do_insert_tests e_insert-2.1 {235    0    "SELECT count(*) FROM a1"            {0}236 237    1a   "SELECT count(*) FROM (SELECT 1, 2)" {1}238    1b   "INSERT INTO a1 SELECT 1, 2"         {}239    1c   "SELECT count(*) FROM a1"            {1}240 241    2a   "SELECT count(*) FROM (SELECT b, a FROM a1)"           {1}242    2b   "INSERT INTO a1 SELECT b, a FROM a1"                   {}243    2c   "SELECT count(*) FROM a1"                              {2}244 245    3a   "SELECT count(*) FROM (SELECT b, a FROM a1)"           {2}246    3b   "INSERT INTO a1 SELECT b, a FROM a1"                   {}247    3c   "SELECT count(*) FROM a1"                              {4}248 249    4a   "SELECT count(*) FROM (SELECT b, a FROM a1)"           {4}250    4b   "INSERT INTO a1 SELECT b, a FROM a1"                   {}251    4c   "SELECT count(*) FROM a1"                              {8}252 253    4a   "SELECT count(*) FROM (SELECT min(b), min(a) FROM a1)" {1}254    4b   "INSERT INTO a1 SELECT min(b), min(a) FROM a1"         {}255    4c   "SELECT count(*) FROM a1"                              {9}256}257 258 259# EVIDENCE-OF: R-63614-47421 If a column-list is specified, the number260# of columns in the result of the SELECT must be the same as the number261# of items in the column-list.262#263do_insert_tests e_insert-2.2 -error {264  %d values for %d columns265} {266    1    "INSERT INTO a3(x, y) SELECT a, b, c FROM a2"            {3 2}267    2    "INSERT INTO a3(x, y) SELECT * FROM a2"                  {3 2}268    3    "INSERT INTO a3(x, y) SELECT * FROM a2 CROSS JOIN a1"    {5 2}269    4    "INSERT INTO a3(x, y) SELECT * FROM a2 NATURAL JOIN a1"  {3 2}270    5    "INSERT INTO a3(x, y) SELECT a2.a FROM a2,a1"            {1 2}271 272    6    "INSERT INTO a3(z) SELECT a, b, c FROM a2"               {3 1}273    7    "INSERT INTO a3(z) SELECT * FROM a2"                     {3 1}274    8    "INSERT INTO a3(z) SELECT * FROM a2 CROSS JOIN a1"       {5 1}275    9    "INSERT INTO a3(z) SELECT * FROM a2 NATURAL JOIN a1"     {3 1}276    10   "INSERT INTO a3(z) SELECT a1.* FROM a2,a1"               {2 1}277}278 279# EVIDENCE-OF: R-58951-07798 Otherwise, if no column-list is specified,280# the number of columns in the result of the SELECT must be the same as281# the number of columns in the table.282#283do_insert_tests e_insert-2.3 -error {284  table %s has %d columns but %d values were supplied285} {286    1    "INSERT INTO a1 SELECT a, b, c FROM a2"            {a1 2 3}287    2    "INSERT INTO a1 SELECT * FROM a2"                  {a1 2 3}288    3    "INSERT INTO a1 SELECT * FROM a2 CROSS JOIN a1"    {a1 2 5}289    4    "INSERT INTO a1 SELECT * FROM a2 NATURAL JOIN a1"  {a1 2 3}290    5    "INSERT INTO a1 SELECT a2.a FROM a2,a1"            {a1 2 1}291}292 293# EVIDENCE-OF: R-31074-37730 Any SELECT statement, including compound294# SELECTs and SELECT statements with ORDER BY and/or LIMIT clauses, may295# be used in an INSERT statement of this form.296#297delete_all_data298do_execsql_test e_insert-2.3.0 {299  INSERT INTO a1 VALUES('x', 'y');300} {}301do_insert_tests e_insert-2.3 {302  1  "INSERT INTO a1 SELECT a,b FROM a1 UNION SELECT b,a FROM a1 ORDER BY 1" {}303  2  "INSERT INTO a1(b, a) SELECT * FROM a1 LIMIT 1"                         {}304  3  "INSERT INTO a1 SELECT 'a'||a, 'b'||b FROM a1 LIMIT 2 OFFSET 1"         {}305  4  "INSERT INTO a1 SELECT * FROM a1 ORDER BY b, a"                         {}306  S  "SELECT * FROM a1" {307      x y 308      x y y x309      y x310      ax by ay bx 311      ay bx ax by y x y x x y x y312  }313}314 315# EVIDENCE-OF: R-25149-22012 The INSERT ... DEFAULT VALUES statement316# inserts a single new row into the named table.317#318delete_all_data319do_insert_tests e_insert-3.1 {320    1    "SELECT count(*) FROM a3"           {0}321    2a   "INSERT INTO a3 DEFAULT VALUES"     {}322    2b   "SELECT count(*) FROM a3"           {1}323}324 325# EVIDENCE-OF: R-18927-01951 Each column of the new row is populated326# with its default value, or with a NULL if no default value is327# specified as part of the column definition in the CREATE TABLE328# statement.329#330delete_all_data331do_insert_tests e_insert-3.2 {332    1.1    "INSERT INTO a3 DEFAULT VALUES"     {}333    1.2    "SELECT * FROM a3"                  {1.0 string {}}334 335    2.1    "INSERT INTO a3 DEFAULT VALUES"     {}336    2.2    "SELECT * FROM a3"                  {1.0 string {} 1.0 string {}}337 338    3.1    "INSERT INTO a2 DEFAULT VALUES"     {}339    3.2    "SELECT * FROM a2"                  {{} {} xyz}340 341    4.1    "INSERT INTO a2 DEFAULT VALUES"     {}342    4.2    "SELECT * FROM a2"                  {{} {} xyz {} {} xyz}343 344    5.1    "INSERT INTO a1 DEFAULT VALUES"     {}345    5.2    "SELECT * FROM a1"                  {{} {}}346 347    6.1    "INSERT INTO a1 DEFAULT VALUES"     {}348    6.2    "SELECT * FROM a1"                  {{} {} {} {}}349}350 351# EVIDENCE-OF: R-00267-47727 The initial "INSERT" keyword can be352# replaced by "REPLACE" or "INSERT OR action" to specify an alternative353# constraint conflict resolution algorithm to use during that one INSERT354# command.355#356# EVIDENCE-OF: R-23110-47146 the parser allows the use of the single357# keyword REPLACE as an alias for "INSERT OR REPLACE".358#359#    The two requirements above are tested by e_select-4.1.* and360#    e_select-4.2.*, respectively.361#362# EVIDENCE-OF: R-03421-22330 The REPLACE command is an alias for the363# "INSERT OR REPLACE" variant of the INSERT command.364#365#    This is a dup of R-23110-47146. Therefore it is also verified 366#    by e_select-4.2.*. This requirement is the only one from367#    lang_replace.html.368#369do_execsql_test e_insert-4.1.0 {370  INSERT INTO a4 VALUES(1, 'a');371  INSERT INTO a4 VALUES(2, 'a');372  INSERT INTO a4 VALUES(3, 'a');373} {}374foreach {tn sql error ac data } {375  1.1  "INSERT INTO a4 VALUES(2,'b')"  {UNIQUE constraint failed: a4.c}  1 {1 a 2 a 3 a}376  1.2  "INSERT OR REPLACE INTO a4 VALUES(2, 'b')"            {}  1 {1 a 3 a 2 b}377  1.3  "INSERT OR IGNORE INTO a4 VALUES(3, 'c')"             {}  1 {1 a 3 a 2 b}378  1.4  "BEGIN" {} 0 {1 a 3 a 2 b}379  1.5  "INSERT INTO a4 VALUES(1, 'd')" {UNIQUE constraint failed: a4.c}  0 {1 a 3 a 2 b}380  1.6  "INSERT OR ABORT INTO a4 VALUES(1, 'd')" 381        {UNIQUE constraint failed: a4.c}  0 {1 a 3 a 2 b}382  1.7  "INSERT OR ROLLBACK INTO a4 VALUES(1, 'd')" 383        {UNIQUE constraint failed: a4.c}  1 {1 a 3 a 2 b}384  1.8  "INSERT INTO a4 SELECT 4, 'e' UNION ALL SELECT 3, 'e'"385        {UNIQUE constraint failed: a4.c}  1 {1 a 3 a 2 b}386  1.9  "INSERT OR FAIL INTO a4 SELECT 4, 'e' UNION ALL SELECT 3, 'e'"387        {UNIQUE constraint failed: a4.c}  1 {1 a 3 a 2 b 4 e}388 389  2.1  "INSERT INTO a4 VALUES(2,'f')"  390        {UNIQUE constraint failed: a4.c}  1 {1 a 3 a 2 b 4 e}391  2.2  "REPLACE INTO a4 VALUES(2, 'f')" {}  1 {1 a 3 a 4 e 2 f}392} {393  do_catchsql_test e_insert-4.1.$tn.1 $sql [list [expr {$error!=""}] $error]394  do_execsql_test  e_insert-4.1.$tn.2 {SELECT * FROM a4} [list {*}$data]395  do_test          e_insert-4.1.$tn.3 {sqlite3_get_autocommit db} $ac396}397 398# EVIDENCE-OF: R-59829-49719 The optional "schema-name." prefix on the399# table-name is supported for top-level INSERT statements only.400#401# EVIDENCE-OF: R-05731-00924 The table name must be unqualified for402# INSERT statements that occur within CREATE TRIGGER statements.403#404set err {1 {qualified table names are not allowed on INSERT, UPDATE, and DELETE statements within triggers}}405 406do_catchsql_test e_insert-5.1.1 {407  CREATE TRIGGER AFTER UPDATE ON a1 BEGIN408    INSERT INTO main.a4 VALUES(new.a, new.b);409  END;410} $err411do_catchsql_test e_insert-5.1.2 {412  CREATE TEMP TABLE IF NOT EXISTS tmptable(a, b);413  CREATE TRIGGER AFTER DELETE ON a3 BEGIN414    INSERT INTO temp.tmptable VALUES(1, 2);415  END;416} $err417 418# EVIDENCE-OF: R-15888-36326 Similarly, the "DEFAULT VALUES" form of the419# INSERT statement is supported for top-level INSERT statements only and420# not for INSERT statements within triggers.421#422do_catchsql_test e_insert-5.2.1 {423  CREATE TRIGGER AFTER UPDATE ON a1 BEGIN424    INSERT INTO a4 DEFAULT VALUES;425  END;426} {1 {near "DEFAULT": syntax error}}427 428 429delete_all_data430 431finish_test432