drh | 1d37e28 | 2000-05-30 03:12:21 +0000 | [diff] [blame^] | 1 | # Copyright (c) 1999, 2000 D. Richard Hipp |
| 2 | # |
| 3 | # This program is free software; you can redistribute it and/or |
| 4 | # modify it under the terms of the GNU General Public |
| 5 | # License as published by the Free Software Foundation; either |
| 6 | # version 2 of the License, or (at your option) any later version. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | # General Public License for more details. |
| 12 | # |
| 13 | # You should have received a copy of the GNU General Public |
| 14 | # License along with this library; if not, write to the |
| 15 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | # Boston, MA 02111-1307, USA. |
| 17 | # |
| 18 | # Author contact information: |
| 19 | # drh@hwaci.com |
| 20 | # http://www.hwaci.com/drh/ |
| 21 | # |
| 22 | #*********************************************************************** |
| 23 | # This file implements regression tests for SQLite library. The |
| 24 | # focus of this file is testing the UPDATE statement. |
| 25 | # |
| 26 | # $Id: update.test,v 1.1 2000/05/30 03:12:22 drh Exp $ |
| 27 | |
| 28 | set testdir [file dirname $argv0] |
| 29 | source $testdir/tester.tcl |
| 30 | |
| 31 | # Try to update an non-existent table |
| 32 | # |
| 33 | do_test update-1.1 { |
| 34 | set v [catch {execsql {UPDATE test1 SET f2=5 WHERE f1<1}} msg] |
| 35 | lappend v $msg |
| 36 | } {1 {no such table: test1}} |
| 37 | |
| 38 | # Try to update a read-only table |
| 39 | # |
| 40 | do_test update-2.1 { |
| 41 | set v [catch \ |
| 42 | {execsql {UPDATE sqlite_master SET name='xyz' WHERE name='123'}} msg] |
| 43 | lappend v $msg |
| 44 | } {1 {table sqlite_master may not be modified}} |
| 45 | |
| 46 | # Create a table to work with |
| 47 | # |
| 48 | do_test update-3.1 { |
| 49 | execsql {CREATE TABLE test1(f1 int,f2 int)} |
| 50 | for {set i 1} {$i<=10} {incr i} { |
| 51 | set sql "INSERT INTO test1 VALUES($i,[expr {int(pow(2,$i))}])" |
| 52 | execsql $sql |
| 53 | } |
| 54 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 55 | } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024} |
| 56 | |
| 57 | # Unknown field name in an expression |
| 58 | # |
| 59 | do_test update-3.2 { |
| 60 | set v [catch {execsql {UPDATE test1 SET f1=f3*2 WHERE f2==32}} msg] |
| 61 | lappend v $msg |
| 62 | } {1 {no such field: f3}} |
| 63 | do_test update-3.3 { |
| 64 | set v [catch {execsql {UPDATE test1 SET f1=test2.f1*2 WHERE f2==32}} msg] |
| 65 | lappend v $msg |
| 66 | } {1 {no such field: test2.f1}} |
| 67 | do_test update-3.4 { |
| 68 | set v [catch {execsql {UPDATE test1 SET f3=f1*2 WHERE f2==32}} msg] |
| 69 | lappend v $msg |
| 70 | } {1 {no such field: f3}} |
| 71 | |
| 72 | # Actually do some updates |
| 73 | # |
| 74 | do_test update-3.5 { |
| 75 | execsql {UPDATE test1 SET f2=f2*3} |
| 76 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 77 | } {1 6 2 12 3 24 4 48 5 96 6 192 7 384 8 768 9 1536 10 3072} |
| 78 | do_test update-3.6 { |
| 79 | execsql {UPDATE test1 SET f2=f2/3 WHERE f1<=5} |
| 80 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 81 | } {1 2 2 4 3 8 4 16 5 32 6 192 7 384 8 768 9 1536 10 3072} |
| 82 | do_test update-3.7 { |
| 83 | execsql {UPDATE test1 SET f2=f2/3 WHERE f1>5} |
| 84 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 85 | } {1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512 10 1024} |
| 86 | |
| 87 | # Swap the values of f1 and f2 for all elements |
| 88 | # |
| 89 | do_test update-3.8 { |
| 90 | execsql {UPDATE test1 SET f2=f1, f1=f2} |
| 91 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 92 | } {2 1 4 2 8 3 16 4 32 5 64 6 128 7 256 8 512 9 1024 10} |
| 93 | |
| 94 | # Create an index and make sure updating works with an index. |
| 95 | # |
| 96 | do_test update-3.9 { |
| 97 | execsql {CREATE INDEX index1 ON test1(f1)} |
| 98 | execsql {CREATE INDEX index2 ON test1(f1)} |
| 99 | execsql {UPDATE test1 SET f1=1025 WHERE f2==10} |
| 100 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 101 | } {2 1 4 2 8 3 16 4 32 5 64 6 128 7 256 8 512 9 1025 10} |
| 102 | do_test update-3.10 { |
| 103 | execsql {SELECT * FROM test1 WHERE f1=1025} |
| 104 | } {1025 10} |
| 105 | do_test update-3.11 { |
| 106 | execsql {UPDATE test1 SET f2=11 WHERE f1==1025} |
| 107 | execsql {SELECT * FROM test1 ORDER BY f1} |
| 108 | } {2 1 4 2 8 3 16 4 32 5 64 6 128 7 256 8 512 9 1025 11} |
| 109 | do_test update-3.12 { |
| 110 | execsql {SELECT * FROM test1 WHERE f1=1025} |
| 111 | } {1025 11} |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | finish_test |