e_dropview.test 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # 2010 November 30
  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. #
  12. # This file implements tests to verify that the "testable statements" in
  13. # the lang_dropview.html document are correct.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. set ::testprefix e_dropview
  18. proc dropview_reopen_db {} {
  19. db close
  20. forcedelete test.db test.db2
  21. sqlite3 db test.db
  22. db eval {
  23. ATTACH 'test.db2' AS aux;
  24. CREATE TABLE t1(a, b);
  25. INSERT INTO t1 VALUES('a main', 'b main');
  26. CREATE VIEW v1 AS SELECT * FROM t1;
  27. CREATE VIEW v2 AS SELECT * FROM t1;
  28. CREATE TEMP TABLE t1(a, b);
  29. INSERT INTO temp.t1 VALUES('a temp', 'b temp');
  30. CREATE VIEW temp.v1 AS SELECT * FROM t1;
  31. CREATE TABLE aux.t1(a, b);
  32. INSERT INTO aux.t1 VALUES('a aux', 'b aux');
  33. CREATE VIEW aux.v1 AS SELECT * FROM t1;
  34. CREATE VIEW aux.v2 AS SELECT * FROM t1;
  35. CREATE VIEW aux.v3 AS SELECT * FROM t1;
  36. }
  37. }
  38. proc list_all_views {{db db}} {
  39. set res [list]
  40. $db eval { PRAGMA database_list } {
  41. set tbl "$name.sqlite_master"
  42. if {$name == "temp"} { set tbl sqlite_temp_master }
  43. set sql "SELECT '$name.' || name FROM $tbl WHERE type = 'view'"
  44. lappend res {*}[$db eval $sql]
  45. }
  46. set res
  47. }
  48. proc list_all_data {{db db}} {
  49. set res [list]
  50. $db eval { PRAGMA database_list } {
  51. set tbl "$name.sqlite_master"
  52. if {$name == "temp"} { set tbl sqlite_temp_master }
  53. db eval "SELECT '$name.' || name AS x FROM $tbl WHERE type = 'table'" {
  54. lappend res [list $x [db eval "SELECT * FROM $x"]]
  55. }
  56. }
  57. set res
  58. }
  59. proc do_dropview_tests {nm args} {
  60. uplevel do_select_tests $nm $args
  61. }
  62. # -- syntax diagram drop-view-stmt
  63. #
  64. # All paths in the syntax diagram for DROP VIEW are tested by tests 1.*.
  65. #
  66. do_dropview_tests 1 -repair {
  67. dropview_reopen_db
  68. } -tclquery {
  69. list_all_views
  70. } {
  71. 1 "DROP VIEW v1" {main.v1 main.v2 aux.v1 aux.v2 aux.v3}
  72. 2 "DROP VIEW v2" {main.v1 temp.v1 aux.v1 aux.v2 aux.v3}
  73. 3 "DROP VIEW main.v1" {main.v2 temp.v1 aux.v1 aux.v2 aux.v3}
  74. 4 "DROP VIEW main.v2" {main.v1 temp.v1 aux.v1 aux.v2 aux.v3}
  75. 5 "DROP VIEW IF EXISTS v1" {main.v1 main.v2 aux.v1 aux.v2 aux.v3}
  76. 6 "DROP VIEW IF EXISTS v2" {main.v1 temp.v1 aux.v1 aux.v2 aux.v3}
  77. 7 "DROP VIEW IF EXISTS main.v1" {main.v2 temp.v1 aux.v1 aux.v2 aux.v3}
  78. 8 "DROP VIEW IF EXISTS main.v2" {main.v1 temp.v1 aux.v1 aux.v2 aux.v3}
  79. }
  80. # EVIDENCE-OF: R-27002-52307 The DROP VIEW statement removes a view
  81. # created by the CREATE VIEW statement.
  82. #
  83. dropview_reopen_db
  84. do_execsql_test 2.1 {
  85. CREATE VIEW "new view" AS SELECT * FROM t1 AS x, t1 AS y;
  86. SELECT * FROM "new view";
  87. } {{a main} {b main} {a main} {b main}}
  88. do_execsql_test 2.2 {;
  89. SELECT * FROM sqlite_master WHERE name = 'new view';
  90. } {
  91. view {new view} {new view} 0
  92. {CREATE VIEW "new view" AS SELECT * FROM t1 AS x, t1 AS y}
  93. }
  94. do_execsql_test 2.3 {
  95. DROP VIEW "new view";
  96. SELECT * FROM sqlite_master WHERE name = 'new view';
  97. } {}
  98. do_catchsql_test 2.4 {
  99. SELECT * FROM "new view"
  100. } {1 {no such table: new view}}
  101. # EVIDENCE-OF: R-00359-41639 The view definition is removed from the
  102. # database schema, but no actual data in the underlying base tables is
  103. # modified.
  104. #
  105. # For each view in the database, check that it can be queried. Then drop
  106. # it. Check that it can no longer be queried and is no longer listed
  107. # in any schema table. Then check that the contents of the db tables have
  108. # not changed
  109. #
  110. set databasedata [list_all_data]
  111. do_execsql_test 3.1.0 { SELECT * FROM temp.v1 } {{a temp} {b temp}}
  112. do_execsql_test 3.1.1 { DROP VIEW temp.v1 } {}
  113. do_catchsql_test 3.1.2 { SELECT * FROM temp.v1 } {1 {no such table: temp.v1}}
  114. do_test 3.1.3 { list_all_views } {main.v1 main.v2 aux.v1 aux.v2 aux.v3}
  115. do_test 3.1.4 { list_all_data } $databasedata
  116. do_execsql_test 3.2.0 { SELECT * FROM v1 } {{a main} {b main}}
  117. do_execsql_test 3.2.1 { DROP VIEW v1 } {}
  118. do_catchsql_test 3.2.2 { SELECT * FROM main.v1 } {1 {no such table: main.v1}}
  119. do_test 3.2.3 { list_all_views } {main.v2 aux.v1 aux.v2 aux.v3}
  120. do_test 3.2.4 { list_all_data } $databasedata
  121. do_execsql_test 3.3.0 { SELECT * FROM v2 } {{a main} {b main}}
  122. do_execsql_test 3.3.1 { DROP VIEW v2 } {}
  123. do_catchsql_test 3.3.2 { SELECT * FROM main.v2 } {1 {no such table: main.v2}}
  124. do_test 3.3.3 { list_all_views } {aux.v1 aux.v2 aux.v3}
  125. do_test 3.3.4 { list_all_data } $databasedata
  126. do_execsql_test 3.4.0 { SELECT * FROM v1 } {{a aux} {b aux}}
  127. do_execsql_test 3.4.1 { DROP VIEW v1 } {}
  128. do_catchsql_test 3.4.2 { SELECT * FROM v1 } {1 {no such table: v1}}
  129. do_test 3.4.3 { list_all_views } {aux.v2 aux.v3}
  130. do_test 3.4.4 { list_all_data } $databasedata
  131. do_execsql_test 3.4.0 { SELECT * FROM aux.v2 } {{a aux} {b aux}}
  132. do_execsql_test 3.4.1 { DROP VIEW aux.v2 } {}
  133. do_catchsql_test 3.4.2 { SELECT * FROM aux.v2 } {1 {no such table: aux.v2}}
  134. do_test 3.4.3 { list_all_views } {aux.v3}
  135. do_test 3.4.4 { list_all_data } $databasedata
  136. do_execsql_test 3.5.0 { SELECT * FROM v3 } {{a aux} {b aux}}
  137. do_execsql_test 3.5.1 { DROP VIEW v3 } {}
  138. do_catchsql_test 3.5.2 { SELECT * FROM v3 } {1 {no such table: v3}}
  139. do_test 3.5.3 { list_all_views } {}
  140. do_test 3.5.4 { list_all_data } $databasedata
  141. # EVIDENCE-OF: R-25558-37487 If the specified view cannot be found and
  142. # the IF EXISTS clause is not present, it is an error.
  143. #
  144. do_dropview_tests 4 -repair {
  145. dropview_reopen_db
  146. } -errorformat {
  147. no such view: %s
  148. } {
  149. 1 "DROP VIEW xx" xx
  150. 2 "DROP VIEW main.xx" main.xx
  151. 3 "DROP VIEW temp.v2" temp.v2
  152. }
  153. # EVIDENCE-OF: R-07490-32536 If the specified view cannot be found and
  154. # an IF EXISTS clause is present in the DROP VIEW statement, then the
  155. # statement is a no-op.
  156. #
  157. do_dropview_tests 5 -repair {
  158. dropview_reopen_db
  159. } -tclquery {
  160. list_all_views
  161. expr {[list_all_views] == "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3"}
  162. } {
  163. 1 "DROP VIEW IF EXISTS xx" 1
  164. 2 "DROP VIEW IF EXISTS main.xx" 1
  165. 3 "DROP VIEW IF EXISTS temp.v2" 1
  166. }
  167. finish_test