view.test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # 2002 February 26
  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 VIEW statements.
  13. #
  14. # $Id: view.test,v 1.5 2002/05/08 21:30:16 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. do_test view-1.0 {
  18. execsql {
  19. CREATE TABLE t1(a,b,c);
  20. INSERT INTO t1 VALUES(1,2,3);
  21. INSERT INTO t1 VALUES(4,5,6);
  22. INSERT INTO t1 VALUES(7,8,9);
  23. SELECT * FROM t1;
  24. }
  25. } {1 2 3 4 5 6 7 8 9}
  26. do_test view-1.1 {
  27. execsql {
  28. BEGIN;
  29. CREATE VIEW v1 AS SELECT a,b FROM t1;
  30. SELECT * FROM v1 ORDER BY a;
  31. }
  32. } {1 2 4 5 7 8}
  33. do_test view-1.2 {
  34. catchsql {
  35. ROLLBACK;
  36. SELECT * FROM v1 ORDER BY a;
  37. }
  38. } {1 {no such table: v1}}
  39. do_test view-1.3 {
  40. execsql {
  41. CREATE VIEW v1 AS SELECT a,b FROM t1;
  42. SELECT * FROM v1 ORDER BY a;
  43. }
  44. } {1 2 4 5 7 8}
  45. do_test view-1.3.1 {
  46. db close
  47. sqlite db test.db
  48. execsql {
  49. SELECT * FROM v1 ORDER BY a;
  50. }
  51. } {1 2 4 5 7 8}
  52. do_test view-1.4 {
  53. catchsql {
  54. DROP VIEW v1;
  55. SELECT * FROM v1 ORDER BY a;
  56. }
  57. } {1 {no such table: v1}}
  58. do_test view-1.5 {
  59. execsql {
  60. CREATE VIEW v1 AS SELECT a,b FROM t1;
  61. SELECT * FROM v1 ORDER BY a;
  62. }
  63. } {1 2 4 5 7 8}
  64. do_test view-1.6 {
  65. catchsql {
  66. DROP TABLE t1;
  67. SELECT * FROM v1 ORDER BY a;
  68. }
  69. } {1 {no such table: t1}}
  70. do_test view-1.7 {
  71. execsql {
  72. CREATE TABLE t1(x,a,b,c);
  73. INSERT INTO t1 VALUES(1,2,3,4);
  74. INSERT INTO t1 VALUES(4,5,6,7);
  75. INSERT INTO t1 VALUES(7,8,9,10);
  76. SELECT * FROM v1 ORDER BY a;
  77. }
  78. } {2 3 5 6 8 9}
  79. do_test view-1.8 {
  80. db close
  81. sqlite db test.db
  82. execsql {
  83. SELECT * FROM v1 ORDER BY a;
  84. }
  85. } {2 3 5 6 8 9}
  86. do_test view-2.1 {
  87. execsql {
  88. CREATE VIEW v2 AS SELECT * FROM t1 WHERE a>5
  89. }; # No semicolon
  90. execsql2 {
  91. SELECT * FROM v2;
  92. }
  93. } {x 7 a 8 b 9 c 10}
  94. do_test view-2.2 {
  95. catchsql {
  96. INSERT INTO v2 VALUES(1,2,3,4);
  97. }
  98. } {1 {view v2 may not be modified}}
  99. do_test view-2.3 {
  100. catchsql {
  101. UPDATE v2 SET a=10 WHERE a=5;
  102. }
  103. } {1 {view v2 may not be modified}}
  104. do_test view-2.4 {
  105. catchsql {
  106. DELETE FROM v2;
  107. }
  108. } {1 {view v2 may not be modified}}
  109. do_test view-2.5 {
  110. execsql {
  111. INSERT INTO t1 VALUES(11,12,13,14);
  112. SELECT * FROM v2 ORDER BY x;
  113. }
  114. } {7 8 9 10 11 12 13 14}
  115. do_test view-2.6 {
  116. execsql {
  117. SELECT x FROM v2 WHERE a>10
  118. }
  119. } {11}
  120. # Test that column name of views are generated correctly.
  121. #
  122. do_test view-3.1 {
  123. execsql2 {
  124. SELECT * FROM v1 LIMIT 1
  125. }
  126. } {a 2 b 3}
  127. do_test view-3.2 {
  128. execsql2 {
  129. SELECT * FROM v2 LIMIT 1
  130. }
  131. } {x 7 a 8 b 9 c 10}
  132. do_test view-3.3 {
  133. execsql2 {
  134. DROP VIEW v1;
  135. CREATE VIEW v1 AS SELECT a AS 'xyz', b+c AS 'pqr', c-b FROM t1;
  136. SELECT * FROM v1 LIMIT 1
  137. }
  138. } {xyz 2 pqr 7 c-b 1}
  139. do_test view-3.4 {
  140. execsql2 {
  141. CREATE VIEW v3 AS SELECT a FROM t1 UNION SELECT b FROM t1 ORDER BY b;
  142. SELECT * FROM v3 LIMIT 4;
  143. }
  144. } {b 2 b 3 b 5 b 6}
  145. do_test view-3.5 {
  146. execsql2 {
  147. CREATE VIEW v4 AS
  148. SELECT a, b FROM t1
  149. UNION
  150. SELECT b AS 'x', a AS 'y' FROM t1
  151. ORDER BY x, y;
  152. SELECT y FROM v4 ORDER BY y LIMIT 4;
  153. }
  154. } {y 2 y 3 y 5 y 6}
  155. finish_test