xref: /sqlite-3.40.0/test/e_dropview.test (revision 00bd55e1)
14a819825Sdan# 2010 November 30
24a819825Sdan#
34a819825Sdan# The author disclaims copyright to this source code.  In place of
44a819825Sdan# a legal notice, here is a blessing:
54a819825Sdan#
64a819825Sdan#    May you do good and not evil.
74a819825Sdan#    May you find forgiveness for yourself and forgive others.
84a819825Sdan#    May you share freely, never taking more than you give.
94a819825Sdan#
104a819825Sdan#***********************************************************************
114a819825Sdan#
124a819825Sdan# This file implements tests to verify that the "testable statements" in
134a819825Sdan# the lang_dropview.html document are correct.
144a819825Sdan#
154a819825Sdan
164a819825Sdanset testdir [file dirname $argv0]
174a819825Sdansource $testdir/tester.tcl
184a819825Sdanset ::testprefix e_dropview
194a819825Sdan
204a819825Sdanproc dropview_reopen_db {} {
214a819825Sdan  db close
224a819825Sdan  forcedelete test.db test.db2
234a819825Sdan  sqlite3 db test.db
244a819825Sdan
254a819825Sdan  db eval {
264a819825Sdan    ATTACH 'test.db2' AS aux;
274a819825Sdan    CREATE TABLE t1(a, b);
284a819825Sdan    INSERT INTO t1 VALUES('a main', 'b main');
294a819825Sdan    CREATE VIEW v1 AS SELECT * FROM t1;
304a819825Sdan    CREATE VIEW v2 AS SELECT * FROM t1;
314a819825Sdan
324a819825Sdan    CREATE TEMP TABLE t1(a, b);
334a819825Sdan    INSERT INTO temp.t1 VALUES('a temp', 'b temp');
344a819825Sdan    CREATE VIEW temp.v1 AS SELECT * FROM t1;
354a819825Sdan
364a819825Sdan    CREATE TABLE aux.t1(a, b);
374a819825Sdan    INSERT INTO aux.t1 VALUES('a aux', 'b aux');
384a819825Sdan    CREATE VIEW aux.v1 AS SELECT * FROM t1;
394a819825Sdan    CREATE VIEW aux.v2 AS SELECT * FROM t1;
404a819825Sdan    CREATE VIEW aux.v3 AS SELECT * FROM t1;
414a819825Sdan  }
424a819825Sdan}
434a819825Sdan
444a819825Sdanproc list_all_views {{db db}} {
454a819825Sdan  set res [list]
464a819825Sdan  $db eval { PRAGMA database_list } {
474a819825Sdan    set tbl "$name.sqlite_master"
48e0a04a36Sdrh    if {$name == "temp"} { set tbl temp.sqlite_master }
494a819825Sdan
504a819825Sdan    set sql "SELECT '$name.' || name FROM $tbl WHERE type = 'view'"
514a819825Sdan    lappend res {*}[$db eval $sql]
524a819825Sdan  }
534a819825Sdan  set res
544a819825Sdan}
554a819825Sdan
564a819825Sdanproc list_all_data {{db db}} {
574a819825Sdan  set res [list]
584a819825Sdan  $db eval { PRAGMA database_list } {
594a819825Sdan    set tbl "$name.sqlite_master"
604a819825Sdan    if {$name == "temp"} { set tbl sqlite_temp_master }
614a819825Sdan
624a819825Sdan    db eval "SELECT '$name.' || name AS x FROM $tbl WHERE type = 'table'" {
634a819825Sdan      lappend res [list $x [db eval "SELECT * FROM $x"]]
644a819825Sdan    }
654a819825Sdan  }
664a819825Sdan  set res
674a819825Sdan}
684a819825Sdan
694a819825Sdanproc do_dropview_tests {nm args} {
704a819825Sdan  uplevel do_select_tests $nm $args
714a819825Sdan}
724a819825Sdan
7339759747Sdrh# -- syntax diagram drop-view-stmt
744a819825Sdan#
754a819825Sdan# All paths in the syntax diagram for DROP VIEW are tested by tests 1.*.
764a819825Sdan#
774a819825Sdando_dropview_tests 1 -repair {
784a819825Sdan  dropview_reopen_db
794a819825Sdan} -tclquery {
804a819825Sdan  list_all_views
814a819825Sdan} {
824a819825Sdan  1   "DROP VIEW v1"                  {main.v1 main.v2 aux.v1 aux.v2 aux.v3}
834a819825Sdan  2   "DROP VIEW v2"                  {main.v1 temp.v1 aux.v1 aux.v2 aux.v3}
844a819825Sdan  3   "DROP VIEW main.v1"             {main.v2 temp.v1 aux.v1 aux.v2 aux.v3}
854a819825Sdan  4   "DROP VIEW main.v2"             {main.v1 temp.v1 aux.v1 aux.v2 aux.v3}
864a819825Sdan  5   "DROP VIEW IF EXISTS v1"        {main.v1 main.v2 aux.v1 aux.v2 aux.v3}
874a819825Sdan  6   "DROP VIEW IF EXISTS v2"        {main.v1 temp.v1 aux.v1 aux.v2 aux.v3}
884a819825Sdan  7   "DROP VIEW IF EXISTS main.v1"   {main.v2 temp.v1 aux.v1 aux.v2 aux.v3}
894a819825Sdan  8   "DROP VIEW IF EXISTS main.v2"   {main.v1 temp.v1 aux.v1 aux.v2 aux.v3}
904a819825Sdan}
914a819825Sdan
924a819825Sdan# EVIDENCE-OF: R-27002-52307 The DROP VIEW statement removes a view
934a819825Sdan# created by the CREATE VIEW statement.
944a819825Sdan#
954a819825Sdandropview_reopen_db
964a819825Sdando_execsql_test 2.1 {
974a819825Sdan  CREATE VIEW "new view" AS SELECT * FROM t1 AS x, t1 AS y;
984a819825Sdan  SELECT * FROM "new view";
994a819825Sdan} {{a main} {b main} {a main} {b main}}
1004a819825Sdando_execsql_test 2.2 {;
1014a819825Sdan  SELECT * FROM sqlite_master WHERE name = 'new view';
1024a819825Sdan} {
1034a819825Sdan  view {new view} {new view} 0
1044a819825Sdan  {CREATE VIEW "new view" AS SELECT * FROM t1 AS x, t1 AS y}
1054a819825Sdan}
1064a819825Sdando_execsql_test 2.3 {
1074a819825Sdan  DROP VIEW "new view";
1084a819825Sdan  SELECT * FROM sqlite_master WHERE name = 'new view';
1094a819825Sdan} {}
1104a819825Sdando_catchsql_test 2.4 {
1114a819825Sdan  SELECT * FROM "new view"
1124a819825Sdan} {1 {no such table: new view}}
1134a819825Sdan
1144a819825Sdan# EVIDENCE-OF: R-00359-41639 The view definition is removed from the
1154a819825Sdan# database schema, but no actual data in the underlying base tables is
1164a819825Sdan# modified.
1174a819825Sdan#
1184a819825Sdan#     For each view in the database, check that it can be queried. Then drop
1194a819825Sdan#     it. Check that it can no longer be queried and is no longer listed
1204a819825Sdan#     in any schema table. Then check that the contents of the db tables have
1214a819825Sdan#     not changed
1224a819825Sdan#
1234a819825Sdanset databasedata [list_all_data]
1244a819825Sdan
1254a819825Sdando_execsql_test  3.1.0 { SELECT * FROM temp.v1 } {{a temp} {b temp}}
1264a819825Sdando_execsql_test  3.1.1 { DROP VIEW temp.v1 } {}
1274a819825Sdando_catchsql_test 3.1.2 { SELECT * FROM temp.v1 } {1 {no such table: temp.v1}}
1284a819825Sdando_test          3.1.3 { list_all_views } {main.v1 main.v2 aux.v1 aux.v2 aux.v3}
129*00bd55e1Sdando_test          3.1.4 { string compare [list_all_data] $databasedata } 0
1304a819825Sdan
1314a819825Sdando_execsql_test  3.2.0 { SELECT * FROM v1 } {{a main} {b main}}
1324a819825Sdando_execsql_test  3.2.1 { DROP VIEW v1 } {}
1334a819825Sdando_catchsql_test 3.2.2 { SELECT * FROM main.v1 } {1 {no such table: main.v1}}
1344a819825Sdando_test          3.2.3 { list_all_views } {main.v2 aux.v1 aux.v2 aux.v3}
135*00bd55e1Sdando_test          3.2.4 { string compare [list_all_data] $databasedata } 0
1364a819825Sdan
1374a819825Sdando_execsql_test  3.3.0 { SELECT * FROM v2 } {{a main} {b main}}
1384a819825Sdando_execsql_test  3.3.1 { DROP VIEW v2 } {}
1394a819825Sdando_catchsql_test 3.3.2 { SELECT * FROM main.v2 } {1 {no such table: main.v2}}
1404a819825Sdando_test          3.3.3 { list_all_views } {aux.v1 aux.v2 aux.v3}
141*00bd55e1Sdando_test          3.3.4 { string compare [list_all_data] $databasedata } 0
1424a819825Sdan
1434a819825Sdando_execsql_test  3.4.0 { SELECT * FROM v1 } {{a aux} {b aux}}
1444a819825Sdando_execsql_test  3.4.1 { DROP VIEW v1 } {}
1454a819825Sdando_catchsql_test 3.4.2 { SELECT * FROM v1 } {1 {no such table: v1}}
1464a819825Sdando_test          3.4.3 { list_all_views } {aux.v2 aux.v3}
147*00bd55e1Sdando_test          3.4.4 { string compare [list_all_data] $databasedata } 0
1484a819825Sdan
149*00bd55e1Sdando_execsql_test  3.5.0 { SELECT * FROM aux.v2 } {{a aux} {b aux}}
150*00bd55e1Sdando_execsql_test  3.5.1 { DROP VIEW aux.v2 } {}
151*00bd55e1Sdando_catchsql_test 3.5.2 { SELECT * FROM aux.v2 } {1 {no such table: aux.v2}}
152*00bd55e1Sdando_test          3.5.3 { list_all_views } {aux.v3}
153*00bd55e1Sdando_test          3.5.4 { string compare [list_all_data] $databasedata } 0
1544a819825Sdan
155*00bd55e1Sdando_execsql_test  3.6.0 { SELECT * FROM v3 } {{a aux} {b aux}}
156*00bd55e1Sdando_execsql_test  3.6.1 { DROP VIEW v3 } {}
157*00bd55e1Sdando_catchsql_test 3.6.2 { SELECT * FROM v3 } {1 {no such table: v3}}
158*00bd55e1Sdando_test          3.6.3 { list_all_views } {}
159*00bd55e1Sdando_test          3.6.4 { string compare [list_all_data] $databasedata } 0
1604a819825Sdan
1614a819825Sdan# EVIDENCE-OF: R-25558-37487 If the specified view cannot be found and
1624a819825Sdan# the IF EXISTS clause is not present, it is an error.
1634a819825Sdan#
1644a819825Sdando_dropview_tests 4 -repair {
1654a819825Sdan  dropview_reopen_db
1664a819825Sdan} -errorformat {
1674a819825Sdan  no such view: %s
1684a819825Sdan} {
1694a819825Sdan  1   "DROP VIEW xx"                  xx
1704a819825Sdan  2   "DROP VIEW main.xx"             main.xx
1714a819825Sdan  3   "DROP VIEW temp.v2"             temp.v2
1724a819825Sdan}
1734a819825Sdan
1744a819825Sdan# EVIDENCE-OF: R-07490-32536 If the specified view cannot be found and
1754a819825Sdan# an IF EXISTS clause is present in the DROP VIEW statement, then the
1764a819825Sdan# statement is a no-op.
1774a819825Sdan#
1784a819825Sdando_dropview_tests 5 -repair {
1794a819825Sdan  dropview_reopen_db
1804a819825Sdan} -tclquery {
1814a819825Sdan  list_all_views
182*00bd55e1Sdan  #expr {[list_all_views] == "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3"}
1834a819825Sdan} {
184*00bd55e1Sdan  1    "DROP VIEW IF EXISTS xx" "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3"
185*00bd55e1Sdan  2    "DROP VIEW IF EXISTS main.xx" "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3"
186*00bd55e1Sdan  3    "DROP VIEW IF EXISTS temp.v2" "main.v1 main.v2 temp.v1 aux.v1 aux.v2 aux.v3"
1874a819825Sdan}
1884a819825Sdan
1894a819825Sdan
1904a819825Sdan
1914a819825Sdan
1924a819825Sdanfinish_test
193