delete.test 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # 2001 September 15
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # 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. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the DELETE FROM statement.
  13. #
  14. # $Id: delete.test,v 1.10 2001/10/15 00:44:36 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Try to delete from a non-existant table.
  18. #
  19. do_test delete-1.1 {
  20. set v [catch {execsql {DELETE FROM test1}} msg]
  21. lappend v $msg
  22. } {1 {no such table: test1}}
  23. # Try to delete from sqlite_master
  24. #
  25. do_test delete-2.1 {
  26. set v [catch {execsql {DELETE FROM sqlite_master}} msg]
  27. lappend v $msg
  28. } {1 {table sqlite_master may not be modified}}
  29. # Delete selected entries from a table with and without an index.
  30. #
  31. do_test delete-3.1.1 {
  32. execsql {CREATE TABLE table1(f1 int, f2 int)}
  33. execsql {INSERT INTO table1 VALUES(1,2)}
  34. execsql {INSERT INTO table1 VALUES(2,4)}
  35. execsql {INSERT INTO table1 VALUES(3,8)}
  36. execsql {INSERT INTO table1 VALUES(4,16)}
  37. execsql {SELECT * FROM table1 ORDER BY f1}
  38. } {1 2 2 4 3 8 4 16}
  39. do_test delete-3.1.2 {
  40. execsql {DELETE FROM table1 WHERE f1=3}
  41. } {}
  42. do_test delete-3.1.3 {
  43. execsql {SELECT * FROM table1 ORDER BY f1}
  44. } {1 2 2 4 4 16}
  45. do_test delete-3.1.4 {
  46. execsql {CREATE INDEX index1 ON table1(f1)}
  47. execsql {PRAGMA count_changes=on}
  48. execsql {DELETE FROM 'table1' WHERE f1=3}
  49. } {0}
  50. do_test delete-3.1.5 {
  51. execsql {SELECT * FROM table1 ORDER BY f1}
  52. } {1 2 2 4 4 16}
  53. do_test delete-3.1.6 {
  54. execsql {DELETE FROM table1 WHERE f1=2}
  55. } {1}
  56. do_test delete-3.1.7 {
  57. execsql {SELECT * FROM table1 ORDER BY f1}
  58. } {1 2 4 16}
  59. # Semantic errors in the WHERE clause
  60. #
  61. do_test delete-4.1 {
  62. execsql {CREATE TABLE table2(f1 int, f2 int)}
  63. set v [catch {execsql {DELETE FROM table2 WHERE f3=5}} msg]
  64. lappend v $msg
  65. } {1 {no such column: f3}}
  66. do_test delete-4.2 {
  67. set v [catch {execsql {DELETE FROM table2 WHERE xyzzy(f1+4)}} msg]
  68. lappend v $msg
  69. } {1 {no such function: xyzzy}}
  70. # Lots of deletes
  71. #
  72. do_test delete-5.1.1 {
  73. execsql {DELETE FROM table1}
  74. } {2}
  75. do_test delete-5.1.2 {
  76. execsql {SELECT count(*) FROM table1}
  77. } {0}
  78. do_test delete-5.2.1 {
  79. execsql {BEGIN TRANSACTION}
  80. for {set i 1} {$i<=200} {incr i} {
  81. execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
  82. }
  83. execsql {COMMIT}
  84. execsql {SELECT count(*) FROM table1}
  85. } {200}
  86. do_test delete-5.2.2 {
  87. execsql {DELETE FROM table1}
  88. } {200}
  89. do_test delete-5.2.3 {
  90. execsql {BEGIN TRANSACTION}
  91. for {set i 1} {$i<=200} {incr i} {
  92. execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
  93. }
  94. execsql {COMMIT}
  95. execsql {SELECT count(*) FROM table1}
  96. } {200}
  97. do_test delete-5.2.4 {
  98. execsql {PRAGMA count_changes=off}
  99. execsql {DELETE FROM table1}
  100. } {}
  101. do_test delete-5.2.5 {
  102. execsql {SELECT count(*) FROM table1}
  103. } {0}
  104. do_test delete-5.2.6 {
  105. execsql {BEGIN TRANSACTION}
  106. for {set i 1} {$i<=200} {incr i} {
  107. execsql "INSERT INTO table1 VALUES($i,[expr {$i*$i}])"
  108. }
  109. execsql {COMMIT}
  110. execsql {SELECT count(*) FROM table1}
  111. } {200}
  112. do_test delete-5.3 {
  113. for {set i 1} {$i<=200} {incr i 4} {
  114. execsql "DELETE FROM table1 WHERE f1==$i"
  115. }
  116. execsql {SELECT count(*) FROM table1}
  117. } {150}
  118. do_test delete-5.4 {
  119. execsql "DELETE FROM table1 WHERE f1>50"
  120. execsql {SELECT count(*) FROM table1}
  121. } {37}
  122. do_test delete-5.5 {
  123. for {set i 1} {$i<=70} {incr i 3} {
  124. execsql "DELETE FROM table1 WHERE f1==$i"
  125. }
  126. execsql {SELECT f1 FROM table1 ORDER BY f1}
  127. } {2 3 6 8 11 12 14 15 18 20 23 24 26 27 30 32 35 36 38 39 42 44 47 48 50}
  128. do_test delete-5.6 {
  129. for {set i 1} {$i<40} {incr i} {
  130. execsql "DELETE FROM table1 WHERE f1==$i"
  131. }
  132. execsql {SELECT f1 FROM table1 ORDER BY f1}
  133. } {42 44 47 48 50}
  134. do_test delete-5.7 {
  135. execsql "DELETE FROM table1 WHERE f1!=48"
  136. execsql {SELECT f1 FROM table1 ORDER BY f1}
  137. } {48}
  138. # Delete large quantities of data. We want to test the List overflow
  139. # mechanism in the vdbe.
  140. #
  141. do_test delete-6.1 {
  142. set fd [open data1.txt w]
  143. for {set i 1} {$i<=3000} {incr i} {
  144. puts $fd "[expr {$i}]\t[expr {$i*$i}]"
  145. }
  146. close $fd
  147. execsql {DELETE FROM table1}
  148. execsql {COPY table1 FROM 'data1.txt'}
  149. execsql {DELETE FROM table2}
  150. execsql {COPY table2 FROM 'data1.txt'}
  151. file delete data1.txt
  152. execsql {SELECT count(*) FROM table1}
  153. } {3000}
  154. do_test delete-6.2 {
  155. execsql {SELECT count(*) FROM table2}
  156. } {3000}
  157. do_test delete-6.3 {
  158. execsql {SELECT f1 FROM table1 WHERE f1<10 ORDER BY f1}
  159. } {1 2 3 4 5 6 7 8 9}
  160. do_test delete-6.4 {
  161. execsql {SELECT f1 FROM table2 WHERE f1<10 ORDER BY f1}
  162. } {1 2 3 4 5 6 7 8 9}
  163. do_test delete-6.5 {
  164. execsql {DELETE FROM table1 WHERE f1>7}
  165. execsql {SELECT f1 FROM table1 ORDER BY f1}
  166. } {1 2 3 4 5 6 7}
  167. do_test delete-6.6 {
  168. execsql {DELETE FROM table2 WHERE f1>7}
  169. execsql {SELECT f1 FROM table2 ORDER BY f1}
  170. } {1 2 3 4 5 6 7}
  171. do_test delete-6.7 {
  172. execsql {DELETE FROM table1}
  173. execsql {SELECT f1 FROM table1}
  174. } {}
  175. do_test delete-6.8 {
  176. execsql {INSERT INTO table1 VALUES(2,3)}
  177. execsql {SELECT f1 FROM table1}
  178. } {2}
  179. do_test delete-6.9 {
  180. execsql {DELETE FROM table2}
  181. execsql {SELECT f1 FROM table2}
  182. } {}
  183. do_test delete-6.10 {
  184. execsql {INSERT INTO table2 VALUES(2,3)}
  185. execsql {SELECT f1 FROM table2}
  186. } {2}
  187. finish_test