misc1.test 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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.
  12. #
  13. # This file implements tests for miscellanous features that were
  14. # left out of other test files.
  15. #
  16. # $Id: misc1.test,v 1.8 2002/06/09 01:16:01 drh Exp $
  17. set testdir [file dirname $argv0]
  18. source $testdir/tester.tcl
  19. # Test the creation and use of tables that have a large number
  20. # of columns.
  21. #
  22. do_test misc1-1.1 {
  23. set cmd "CREATE TABLE manycol(x0 text"
  24. for {set i 1} {$i<=99} {incr i} {
  25. append cmd ",x$i text"
  26. }
  27. append cmd ")";
  28. execsql $cmd
  29. set cmd "INSERT INTO manycol VALUES(0"
  30. for {set i 1} {$i<=99} {incr i} {
  31. append cmd ",$i"
  32. }
  33. append cmd ")";
  34. execsql $cmd
  35. execsql "SELECT x99 FROM manycol"
  36. } 99
  37. do_test misc1-1.2 {
  38. execsql {SELECT x0, x10, x25, x50, x75 FROM manycol}
  39. } {0 10 25 50 75}
  40. do_test misc1-1.3 {
  41. for {set j 100} {$j<=1000} {incr j 100} {
  42. set cmd "INSERT INTO manycol VALUES($j"
  43. for {set i 1} {$i<=99} {incr i} {
  44. append cmd ",[expr {$i+$j}]"
  45. }
  46. append cmd ")"
  47. execsql $cmd
  48. }
  49. execsql {SELECT x50 FROM manycol ORDER BY x80}
  50. } {50 150 250 350 450 550 650 750 850 950 1050}
  51. do_test misc1-1.4 {
  52. execsql {SELECT x75 FROM manycol WHERE x50=350}
  53. } 375
  54. do_test misc1-1.5 {
  55. execsql {SELECT x50 FROM manycol WHERE x99=599}
  56. } 550
  57. do_test misc1-1.6 {
  58. execsql {CREATE INDEX manycol_idx1 ON manycol(x99)}
  59. execsql {SELECT x50 FROM manycol WHERE x99=899}
  60. } 850
  61. do_test misc1-1.7 {
  62. execsql {SELECT count(*) FROM manycol}
  63. } 11
  64. do_test misc1-1.8 {
  65. execsql {DELETE FROM manycol WHERE x98=1234}
  66. execsql {SELECT count(*) FROM manycol}
  67. } 11
  68. do_test misc1-1.9 {
  69. execsql {DELETE FROM manycol WHERE x98=998}
  70. execsql {SELECT count(*) FROM manycol}
  71. } 10
  72. do_test misc1-1.10 {
  73. execsql {DELETE FROM manycol WHERE x99=500}
  74. execsql {SELECT count(*) FROM manycol}
  75. } 10
  76. do_test misc1-1.11 {
  77. execsql {DELETE FROM manycol WHERE x99=599}
  78. execsql {SELECT count(*) FROM manycol}
  79. } 9
  80. # Check GROUP BY expressions that name two or more columns.
  81. #
  82. do_test misc1-2.1 {
  83. execsql {
  84. BEGIN TRANSACTION;
  85. CREATE TABLE agger(one text, two text, three text, four text);
  86. INSERT INTO agger VALUES(1, 'one', 'hello', 'yes');
  87. INSERT INTO agger VALUES(2, 'two', 'howdy', 'no');
  88. INSERT INTO agger VALUES(3, 'thr', 'howareya', 'yes');
  89. INSERT INTO agger VALUES(4, 'two', 'lothere', 'yes');
  90. INSERT INTO agger VALUES(5, 'one', 'atcha', 'yes');
  91. INSERT INTO agger VALUES(6, 'two', 'hello', 'no');
  92. COMMIT
  93. }
  94. execsql {SELECT count(*) FROM agger}
  95. } 6
  96. do_test misc1-2.2 {
  97. execsql {SELECT sum(one), two, four FROM agger
  98. GROUP BY two, four ORDER BY sum(one) desc}
  99. } {8 two no 6 one yes 4 two yes 3 thr yes}
  100. # Here's a test for a bug found by Joel Lucsy. The code below
  101. # was causing an assertion failure.
  102. #
  103. do_test misc1-3.1 {
  104. set r [execsql {
  105. CREATE TABLE t1(a);
  106. INSERT INTO t1 VALUES('hi');
  107. PRAGMA full_column_names=on;
  108. SELECT rowid, * FROM t1;
  109. }]
  110. lindex $r 1
  111. } {hi}
  112. # Here's a test for yet another bug found by Joel Lucsy. The code
  113. # below was causing an assertion failure.
  114. #
  115. do_test misc1-4.1 {
  116. execsql {
  117. BEGIN;
  118. CREATE TABLE t2(a);
  119. INSERT INTO t2 VALUES('This is a long string to use up a lot of disk -');
  120. UPDATE t2 SET a=a||a||a||a;
  121. INSERT INTO t2 SELECT '1 - ' || a FROM t2;
  122. INSERT INTO t2 SELECT '2 - ' || a FROM t2;
  123. INSERT INTO t2 SELECT '3 - ' || a FROM t2;
  124. INSERT INTO t2 SELECT '4 - ' || a FROM t2;
  125. INSERT INTO t2 SELECT '5 - ' || a FROM t2;
  126. INSERT INTO t2 SELECT '6 - ' || a FROM t2;
  127. COMMIT;
  128. SELECT count(*) FROM t2;
  129. }
  130. } {64}
  131. # Make sure we actually see a semicolon or end-of-file in the SQL input
  132. # before executing a command. Thus if "WHERE" is misspelled on an UPDATE,
  133. # the user won't accidently update every record.
  134. #
  135. do_test misc1-5.1 {
  136. catchsql {
  137. CREATE TABLE t3(a,b);
  138. INSERT INTO t3 VALUES(1,2);
  139. INSERT INTO t3 VALUES(3,4);
  140. UPDATE t3 SET a=0 WHEREwww b=2;
  141. }
  142. } {1 {near "WHEREwww": syntax error}}
  143. do_test misc1-5.2 {
  144. execsql {
  145. SELECT * FROM t3 ORDER BY a;
  146. }
  147. } {1 2 3 4}
  148. # Certain keywords (especially non-standard keywords like "REPLACE") can
  149. # also be used as identifiers. The way this works in the parser is that
  150. # the parser first detects a syntax error, the error handling routine
  151. # sees that the special keyword caused the error, then replaces the keyword
  152. # with "ID" and tries again.
  153. #
  154. # Check the operation of this logic.
  155. #
  156. do_test misc1-6.1 {
  157. catchsql {
  158. CREATE TABLE t4(
  159. abort, asc, begin, cluster, conflict, copy, delimiters, desc, end,
  160. explain, fail, ignore, key, offset, pragma, replace, temp,
  161. vacuum, view
  162. );
  163. }
  164. } {0 {}}
  165. do_test misc1-6.2 {
  166. catchsql {
  167. INSERT INTO t4
  168. VALUES(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19);
  169. }
  170. } {0 {}}
  171. do_test misc1-6.3 {
  172. execsql {
  173. SELECT * FROM t4
  174. }
  175. } {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19}
  176. do_test misc1-6.4 {
  177. execsql {
  178. SELECT abort+asc,max(key,pragma,temp) FROM t4
  179. }
  180. } {3 17}
  181. # Test for multi-column primary keys, and for multiple primary keys.
  182. #
  183. do_test misc1-7.1 {
  184. catchsql {
  185. CREATE TABLE error1(
  186. a TYPE PRIMARY KEY,
  187. b TYPE PRIMARY KEY
  188. );
  189. }
  190. } {1 {table "error1" has more than one primary key}}
  191. do_test misc1-7.2 {
  192. catchsql {
  193. CREATE TABLE error1(
  194. a INTEGER PRIMARY KEY,
  195. b TYPE PRIMARY KEY
  196. );
  197. }
  198. } {1 {table "error1" has more than one primary key}}
  199. do_test misc1-7.3 {
  200. execsql {
  201. CREATE TABLE t5(a,b,c,PRIMARY KEY(a,b));
  202. INSERT INTO t5 VALUES(1,2,3);
  203. SELECT * FROM t5 ORDER BY a;
  204. }
  205. } {1 2 3}
  206. do_test misc1-7.4 {
  207. catchsql {
  208. INSERT INTO t5 VALUES(1,2,4);
  209. }
  210. } {1 {constraint failed}}
  211. do_test misc1-7.5 {
  212. catchsql {
  213. INSERT INTO t5 VALUES(0,2,4);
  214. }
  215. } {0 {}}
  216. do_test misc1-7.6 {
  217. execsql {
  218. SELECT * FROM t5 ORDER BY a;
  219. }
  220. } {0 2 4 1 2 3}
  221. do_test misc1-8.1 {
  222. catchsql {
  223. SELECT *;
  224. }
  225. } {1 {no tables specified}}
  226. do_test misc1-8.2 {
  227. catchsql {
  228. SELECT t1.*;
  229. }
  230. } {1 {no such table: t1}}
  231. execsql {
  232. DROP TABLE t1;
  233. DROP TABLE t2;
  234. DROP TABLE t3;
  235. DROP TABLE t4;
  236. }
  237. # If an integer is too big to be represented as a 32-bit machine integer,
  238. # then treat it as a string.
  239. #
  240. do_test misc1-9.1 {
  241. catchsql {
  242. CREATE TABLE t1(a unique not null, b unique not null);
  243. INSERT INTO t1 VALUES('a',12345678901234567890);
  244. INSERT INTO t1 VALUES('b',12345678911234567890);
  245. INSERT INTO t1 VALUES('c',12345678921234567890);
  246. SELECT * FROM t1;
  247. }
  248. } {0 {a 12345678901234567890 b 12345678911234567890 c 12345678921234567890}}
  249. finish_test