xref: /sqlite-3.40.0/test/fts3comp1.test (revision 4c600ac3)
1a240fd01Sdan# 2011 January 27
2a240fd01Sdan#
3a240fd01Sdan# The author disclaims copyright to this source code.  In place of
4a240fd01Sdan# a legal notice, here is a blessing:
5a240fd01Sdan#
6a240fd01Sdan#    May you do good and not evil.
7a240fd01Sdan#    May you find forgiveness for yourself and forgive others.
8a240fd01Sdan#    May you share freely, never taking more than you give.
9a240fd01Sdan#
10a240fd01Sdan#*************************************************************************
11a240fd01Sdan# This file implements regression tests for SQLite library.  The
12a240fd01Sdan# focus of this script is testing the FTS3 module.
13a240fd01Sdan#
14a240fd01Sdan
15a240fd01Sdanset testdir [file dirname $argv0]
16a240fd01Sdansource $testdir/tester.tcl
17a240fd01Sdanifcapable !fts3 { finish_test ; return }
18a240fd01Sdanset ::testprefix fts3comp1
19a240fd01Sdan
20767f9a8fSdan# Create a pretend compression system.
21767f9a8fSdan#
22767f9a8fSdan# Each time the [zip] function is called, an entry is added to the ::strings
23767f9a8fSdan# array mapping from an integer key to the string argument to zip. The key
24767f9a8fSdan# is returned. Later on, when the key is passed to [unzip], the original
25767f9a8fSdan# string is retrieved from the ::strings array and returned.
26767f9a8fSdan#
27a240fd01Sdanset next_x 0
28a240fd01Sdanproc zip {x} {
29a240fd01Sdan incr ::next_x
30a240fd01Sdan set ::strings($::next_x) $x
31a240fd01Sdan return $::next_x
32a240fd01Sdan}
33a240fd01Sdanproc unzip {x} {
34a240fd01Sdan  return $::strings($x)
35a240fd01Sdan}
36a240fd01Sdan
37767f9a8fSdanforeach {tn zip unzip} {
38767f9a8fSdan  1   zip   unzip
39767f9a8fSdan  2   {z.i.p!!}    {un "zip"}
40767f9a8fSdan} {
41a240fd01Sdan
42767f9a8fSdan  set next_x 0
43767f9a8fSdan  catch {db close}
44767f9a8fSdan  forcedelete test.db
45767f9a8fSdan  sqlite3 db test.db
46767f9a8fSdan  db func $zip zip
47767f9a8fSdan  db func $unzip unzip
48767f9a8fSdan
49767f9a8fSdan  # Create a table that uses zip/unzip. Check that content inserted into
50767f9a8fSdan  # the table can be read back (using a full-scan query). Check that the
51767f9a8fSdan  # underlying %_content table contains the compressed (integer) values.
52767f9a8fSdan  #
53767f9a8fSdan  do_execsql_test 1.$tn.0 "
54a240fd01Sdan    CREATE VIRTUAL TABLE t1 USING fts4(
55a240fd01Sdan      a, b,
56767f9a8fSdan      compress='$zip', uncompress='$unzip'
57a240fd01Sdan    );
58767f9a8fSdan  "
59767f9a8fSdan  do_execsql_test 1.$tn.1 {
60a240fd01Sdan    INSERT INTO t1 VALUES('one two three', 'two four six');
61a240fd01Sdan    SELECT a, b FROM t1;
62a240fd01Sdan  } {{one two three} {two four six}}
63767f9a8fSdan  do_execsql_test 1.$tn.2 {
64a240fd01Sdan    SELECT c0a, c1b FROM t1_content;
65a240fd01Sdan  } {1 2}
66a240fd01Sdan
67767f9a8fSdan  # Insert another row and check that it can be read back. Also that the
68767f9a8fSdan  # %_content table still contains all compressed content. This time, try
69767f9a8fSdan  # full-text index and by-docid queries too.
70767f9a8fSdan  #
71767f9a8fSdan  do_execsql_test 1.$tn.3 {
72a240fd01Sdan    INSERT INTO t1 VALUES('three six nine', 'four eight twelve');
73a240fd01Sdan    SELECT a, b FROM t1;
74a240fd01Sdan  } {{one two three} {two four six} {three six nine} {four eight twelve}}
75767f9a8fSdan  do_execsql_test 1.$tn.4 {
76a240fd01Sdan    SELECT c0a, c1b FROM t1_content;
77a240fd01Sdan  } {1 2 3 4}
78a240fd01Sdan
79767f9a8fSdan  do_execsql_test 1.$tn.5 {
80767f9a8fSdan    SELECT a, b FROM t1 WHERE docid = 2
81767f9a8fSdan  } {{three six nine} {four eight twelve}}
82767f9a8fSdan  do_execsql_test 1.$tn.6 {
83767f9a8fSdan    SELECT a, b FROM t1 WHERE t1 MATCH 'two'
84767f9a8fSdan  } {{one two three} {two four six}}
85767f9a8fSdan
86767f9a8fSdan  # Delete a row and check that the full-text index is correctly updated.
87767f9a8fSdan  # Inspect the full-text index using an fts4aux table.
88767f9a8fSdan  #
89767f9a8fSdan  do_execsql_test 1.$tn.7 {
90a240fd01Sdan    CREATE VIRTUAL TABLE terms USING fts4aux(t1);
91*4c600ac3Sdan    SELECT term, documents, occurrences FROM terms WHERE col = '*';
92a240fd01Sdan  } {
93a240fd01Sdan    eight 1 1    four 2 2    nine 1 1    one 1 1
94a240fd01Sdan    six 2 2      three 2 2   twelve 1 1  two 1 2
95a240fd01Sdan  }
96767f9a8fSdan  do_execsql_test 1.$tn.8 {
97a240fd01Sdan    DELETE FROM t1 WHERE docid = 1;
98*4c600ac3Sdan    SELECT term, documents, occurrences FROM terms WHERE col = '*';
99a240fd01Sdan  } {
100a240fd01Sdan    eight 1 1   four 1 1    nine 1 1
101a240fd01Sdan    six 1 1     three 1 1   twelve 1 1
102a240fd01Sdan  }
103767f9a8fSdan  do_execsql_test 1.$tn.9 { SELECT c0a, c1b FROM t1_content } {3 4}
104767f9a8fSdan}
105a240fd01Sdan
106767f9a8fSdan# Test that is an error to specify just one of compress and uncompress.
107767f9a8fSdan#
108767f9a8fSdando_catchsql_test 2.1 {
109767f9a8fSdan  CREATE VIRTUAL TABLE t2 USING fts4(x, compress=zip)
110767f9a8fSdan} {1 {missing uncompress parameter in fts4 constructor}}
111767f9a8fSdando_catchsql_test 2.2 {
112767f9a8fSdan  CREATE VIRTUAL TABLE t2 USING fts4(x, uncompress=unzip)
113767f9a8fSdan} {1 {missing compress parameter in fts4 constructor}}
114a240fd01Sdan
115a240fd01Sdanfinish_test
116