sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / test / fts3comp1.test
| 1 | # 2011 January 27 |
| 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 script is testing the FTS3 module. |
| 13 | # |
| 14 | |
| 15 | set testdir [file dirname $argv0] |
| 16 | source $testdir/tester.tcl |
| 17 | ifcapable !fts3 { finish_test ; return } |
| 18 | set ::testprefix fts3comp1 |
| 19 | |
| 20 | # Create a pretend compression system. |
| 21 | # |
| 22 | # Each time the [zip] function is called, an entry is added to the ::strings |
| 23 | # array mapping from an integer key to the string argument to zip. The key |
| 24 | # is returned. Later on, when the key is passed to [unzip], the original |
| 25 | # string is retrieved from the ::strings array and returned. |
| 26 | # |
| 27 | set next_x 0 |
| 28 | proc zip {x} { |
| 29 | incr ::next_x |
| 30 | set ::strings($::next_x) $x |
| 31 | return $::next_x |
| 32 | } |
| 33 | proc unzip {x} { |
| 34 | return $::strings($x) |
| 35 | } |
| 36 | |
| 37 | foreach {tn zip unzip} { |
| 38 | 1 zip unzip |
| 39 | 2 {z.i.p!!} {un "zip"} |
| 40 | } { |
| 41 | |
| 42 | set next_x 0 |
| 43 | catch {db close} |
| 44 | forcedelete test.db |
| 45 | sqlite3 db test.db |
| 46 | db func $zip zip |
| 47 | db func $unzip unzip |
| 48 | |
| 49 | # Create a table that uses zip/unzip. Check that content inserted into |
| 50 | # the table can be read back (using a full-scan query). Check that the |
| 51 | # underlying %_content table contains the compressed (integer) values. |
| 52 | # |
| 53 | do_execsql_test 1.$tn.0 " |
| 54 | CREATE VIRTUAL TABLE t1 USING fts4( |
| 55 | a, b, |
| 56 | compress='$zip', uncompress='$unzip' |
| 57 | ); |
| 58 | " |
| 59 | do_execsql_test 1.$tn.1 { |
| 60 | INSERT INTO t1 VALUES('one two three', 'two four six'); |
| 61 | SELECT a, b FROM t1; |
| 62 | } {{one two three} {two four six}} |
| 63 | do_execsql_test 1.$tn.2 { |
| 64 | SELECT c0a, c1b FROM t1_content; |
| 65 | } {1 2} |
| 66 | |
| 67 | # Insert another row and check that it can be read back. Also that the |
| 68 | # %_content table still contains all compressed content. This time, try |
| 69 | # full-text index and by-docid queries too. |
| 70 | # |
| 71 | do_execsql_test 1.$tn.3 { |
| 72 | INSERT INTO t1 VALUES('three six nine', 'four eight twelve'); |
| 73 | SELECT a, b FROM t1; |
| 74 | } {{one two three} {two four six} {three six nine} {four eight twelve}} |
| 75 | do_execsql_test 1.$tn.4 { |
| 76 | SELECT c0a, c1b FROM t1_content; |
| 77 | } {1 2 3 4} |
| 78 | |
| 79 | do_execsql_test 1.$tn.5 { |
| 80 | SELECT a, b FROM t1 WHERE docid = 2 |
| 81 | } {{three six nine} {four eight twelve}} |
| 82 | do_execsql_test 1.$tn.6 { |
| 83 | SELECT a, b FROM t1 WHERE t1 MATCH 'two' |
| 84 | } {{one two three} {two four six}} |
| 85 | |
| 86 | # Delete a row and check that the full-text index is correctly updated. |
| 87 | # Inspect the full-text index using an fts4aux table. |
| 88 | # |
| 89 | do_execsql_test 1.$tn.7 { |
| 90 | CREATE VIRTUAL TABLE terms USING fts4aux(t1); |
| 91 | SELECT term, documents, occurrences FROM terms WHERE col = '*'; |
| 92 | } { |
| 93 | eight 1 1 four 2 2 nine 1 1 one 1 1 |
| 94 | six 2 2 three 2 2 twelve 1 1 two 1 2 |
| 95 | } |
| 96 | do_execsql_test 1.$tn.8 { |
| 97 | DELETE FROM t1 WHERE docid = 1; |
| 98 | SELECT term, documents, occurrences FROM terms WHERE col = '*'; |
| 99 | } { |
| 100 | eight 1 1 four 1 1 nine 1 1 |
| 101 | six 1 1 three 1 1 twelve 1 1 |
| 102 | } |
| 103 | do_execsql_test 1.$tn.9 { SELECT c0a, c1b FROM t1_content } {3 4} |
| 104 | } |
| 105 | |
| 106 | # Test that is an error to specify just one of compress and uncompress. |
| 107 | # |
| 108 | do_catchsql_test 2.1 { |
| 109 | CREATE VIRTUAL TABLE t2 USING fts4(x, compress=zip) |
| 110 | } {1 {missing uncompress parameter in fts4 constructor}} |
| 111 | do_catchsql_test 2.2 { |
| 112 | CREATE VIRTUAL TABLE t2 USING fts4(x, uncompress=unzip) |
| 113 | } {1 {missing compress parameter in fts4 constructor}} |
| 114 | |
| 115 | #-------------------------------------------------------------------------- |
| 116 | reset_db |
| 117 | do_execsql_test 3.0 { |
| 118 | PRAGMA trusted_schema = OFF; |
| 119 | } |
| 120 | |
| 121 | set ::myfunc_invoked 0 |
| 122 | proc myfunc {data} { |
| 123 | incr ::myfunc_invoked |
| 124 | return $data |
| 125 | } |
| 126 | db func myfunc myfunc |
| 127 | |
| 128 | do_execsql_test 3.1 { |
| 129 | CREATE VIEW v1 AS SELECT myfunc('xyz'); |
| 130 | } |
| 131 | |
| 132 | do_catchsql_test 3.2 { |
| 133 | SELECT * FROM v1 |
| 134 | } {1 {unsafe use of myfunc()}} |
| 135 | |
| 136 | do_execsql_test 3.3 { |
| 137 | CREATE VIRTUAL TABLE f1 USING fts4(x, compress=myfunc, uncompress=myfunc); |
| 138 | } |
| 139 | |
| 140 | do_catchsql_test 3.4 { |
| 141 | INSERT INTO f1(rowid, x) VALUES(123, 'one two three'); |
| 142 | } {1 {SQL logic error}} |
| 143 | |
| 144 | do_test 3.5 { |
| 145 | set ::myfunc_invoked |
| 146 | } {0} |
| 147 | |
| 148 | do_execsql_test 3.6.1 { |
| 149 | CREATE TABLE t1(x); |
| 150 | CREATE TABLE t2(y); |
| 151 | |
| 152 | CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN |
| 153 | INSERT INTO t2 VALUES( myfunc(new.x) ); |
| 154 | END; |
| 155 | } |
| 156 | |
| 157 | do_catchsql_test 3.6.2 { |
| 158 | INSERT INTO t1 VALUES('hello world'); |
| 159 | } {1 {unsafe use of myfunc()}} |
| 160 | |
| 161 | #------------------------------------------------------------------------- |
| 162 | reset_db |
| 163 | do_execsql_test 4.0 { |
| 164 | CREATE VIRTUAL TABLE v1 USING fts4(x, compress=comp, uncompress=uncomp); |
| 165 | } |
| 166 | |
| 167 | proc comp {data} { return $data } |
| 168 | proc uncomp {data} { return $data } |
| 169 | |
| 170 | db func comp comp |
| 171 | db func uncomp uncomp |
| 172 | |
| 173 | do_catchsql_test 4.1 { |
| 174 | INSERT INTO v1 VALUES('one two three'); |
| 175 | } {0 {}} |
| 176 | |
| 177 | db close |
| 178 | sqlite3 db test.db |
| 179 | db func comp -directonly comp |
| 180 | |
| 181 | do_catchsql_test 4.2 { |
| 182 | INSERT INTO v1 VALUES('one two three'); |
| 183 | } {1 {SQL logic error}} |
| 184 | |
| 185 | db func uncomp -directonly uncomp |
| 186 | |
| 187 | do_catchsql_test 4.3 { |
| 188 | SELECT * FROM v1 |
| 189 | } {1 {SQL logic error}} |
| 190 | |
| 191 | |
| 192 | finish_test |