summaryrefslogtreecommitdiff
path: root/test/fts3comp1.test
blob: b5077a35547c0b30f496390dd71c7a17a937b3dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# 2011 January 27
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#*************************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this script is testing the FTS3 module.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
ifcapable !fts3 { finish_test ; return }
set ::testprefix fts3comp1

# Create a pretend compression system. 
#
# Each time the [zip] function is called, an entry is added to the ::strings
# array mapping from an integer key to the string argument to zip. The key
# is returned. Later on, when the key is passed to [unzip], the original
# string is retrieved from the ::strings array and returned.
#
set next_x 0
proc zip {x} {
 incr ::next_x
 set ::strings($::next_x) $x
 return $::next_x
}
proc unzip {x} {
  return $::strings($x)
}

foreach {tn zip unzip} {
  1   zip   unzip
  2   {z.i.p!!}    {un "zip"}
} {

  set next_x 0
  catch {db close}
  forcedelete test.db
  sqlite3 db test.db
  db func $zip zip
  db func $unzip unzip
  
  # Create a table that uses zip/unzip. Check that content inserted into
  # the table can be read back (using a full-scan query). Check that the
  # underlying %_content table contains the compressed (integer) values.
  #
  do_execsql_test 1.$tn.0 "
    CREATE VIRTUAL TABLE t1 USING fts4(
      a, b, 
      compress='$zip', uncompress='$unzip'
    );
  "
  do_execsql_test 1.$tn.1 {
    INSERT INTO t1 VALUES('one two three', 'two four six');
    SELECT a, b FROM t1;
  } {{one two three} {two four six}}
  do_execsql_test 1.$tn.2 {
    SELECT c0a, c1b FROM t1_content;
  } {1 2}
  
  # Insert another row and check that it can be read back. Also that the
  # %_content table still contains all compressed content. This time, try
  # full-text index and by-docid queries too.
  #
  do_execsql_test 1.$tn.3 {
    INSERT INTO t1 VALUES('three six nine', 'four eight twelve');
    SELECT a, b FROM t1;
  } {{one two three} {two four six} {three six nine} {four eight twelve}}
  do_execsql_test 1.$tn.4 {
    SELECT c0a, c1b FROM t1_content;
  } {1 2 3 4}
  
  do_execsql_test 1.$tn.5 {
    SELECT a, b FROM t1 WHERE docid = 2
  } {{three six nine} {four eight twelve}}
  do_execsql_test 1.$tn.6 {
    SELECT a, b FROM t1 WHERE t1 MATCH 'two'
  } {{one two three} {two four six}}
  
  # Delete a row and check that the full-text index is correctly updated.
  # Inspect the full-text index using an fts4aux table.
  #
  do_execsql_test 1.$tn.7 {
    CREATE VIRTUAL TABLE terms USING fts4aux(t1);
    SELECT term, documents, occurrences FROM terms WHERE col = '*';
  } {
    eight 1 1    four 2 2    nine 1 1    one 1 1 
    six 2 2      three 2 2   twelve 1 1  two 1 2
  }
  do_execsql_test 1.$tn.8 {
    DELETE FROM t1 WHERE docid = 1;
    SELECT term, documents, occurrences FROM terms WHERE col = '*';
  } {
    eight 1 1   four 1 1    nine 1 1 
    six 1 1     three 1 1   twelve 1 1
  }
  do_execsql_test 1.$tn.9 { SELECT c0a, c1b FROM t1_content } {3 4}
}

# Test that is an error to specify just one of compress and uncompress.
#
do_catchsql_test 2.1 {
  CREATE VIRTUAL TABLE t2 USING fts4(x, compress=zip)
} {1 {missing uncompress parameter in fts4 constructor}}
do_catchsql_test 2.2 {
  CREATE VIRTUAL TABLE t2 USING fts4(x, uncompress=unzip)
} {1 {missing compress parameter in fts4 constructor}}

#--------------------------------------------------------------------------
reset_db
do_execsql_test 3.0 {
  PRAGMA trusted_schema = OFF;
}

set ::myfunc_invoked 0
proc myfunc {data} {
  incr ::myfunc_invoked
  return $data
}
db func myfunc myfunc

do_execsql_test 3.1 {
  CREATE VIEW v1 AS SELECT myfunc('xyz');
}

do_catchsql_test 3.2 {
  SELECT * FROM v1
} {1 {unsafe use of myfunc()}}

do_execsql_test 3.3 {
  CREATE VIRTUAL TABLE f1 USING fts4(x, compress=myfunc, uncompress=myfunc);
}

do_catchsql_test 3.4 {
  INSERT INTO f1(rowid, x) VALUES(123, 'one two three');
} {1 {SQL logic error}}

do_test 3.5 {
  set ::myfunc_invoked
} {0}

do_execsql_test 3.6.1 {
  CREATE TABLE t1(x);
  CREATE TABLE t2(y);

  CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
    INSERT INTO t2 VALUES( myfunc(new.x) );
  END;
}

do_catchsql_test 3.6.2 {
  INSERT INTO t1 VALUES('hello world');
} {1 {unsafe use of myfunc()}}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 4.0 {
  CREATE VIRTUAL TABLE v1 USING fts4(x, compress=comp, uncompress=uncomp);
}

proc comp {data} { return $data }
proc uncomp {data} { return $data }

db func comp comp
db func uncomp uncomp

do_catchsql_test 4.1 {
  INSERT INTO v1 VALUES('one two three');
} {0 {}}

db close
sqlite3 db test.db
db func comp -directonly comp

do_catchsql_test 4.2 {
  INSERT INTO v1 VALUES('one two three');
} {1 {SQL logic error}}

db func uncomp -directonly uncomp

do_catchsql_test 4.3 {
  SELECT * FROM v1
} {1 {SQL logic error}}
  

finish_test