modernc.org/cc@v1.0.1/v2/testdata/_sqlite/ext/fts5/test/fts5content.test (about)

     1  # 2014 Dec 20
     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 contains tests for the content= and content_rowid= options.
    13  #
    14  
    15  source [file join [file dirname [info script]] fts5_common.tcl]
    16  set testprefix fts5content
    17  
    18  # If SQLITE_ENABLE_FTS5 is defined, omit this file.
    19  ifcapable !fts5 {
    20    finish_test
    21    return
    22  }
    23  
    24  #-------------------------------------------------------------------------
    25  # Contentless tables
    26  #
    27  do_execsql_test 1.1 {
    28    CREATE VIRTUAL TABLE f1 USING fts5(a, b, content='');
    29    INSERT INTO f1(rowid, a, b) VALUES(1, 'one',   'o n e');
    30    INSERT INTO f1(rowid, a, b) VALUES(2, 'two',   't w o');
    31    INSERT INTO f1(rowid, a, b) VALUES(3, 'three', 't h r e e');
    32  }
    33  
    34  do_execsql_test 1.2 {
    35    SELECT rowid FROM f1 WHERE f1 MATCH 'o';
    36  } {1 2}
    37  
    38  do_execsql_test 1.3 {
    39    INSERT INTO f1(a, b) VALUES('four',   'f o u r');
    40    SELECT rowid FROM f1 WHERE f1 MATCH 'o';
    41  } {1 2 4}
    42  
    43  do_execsql_test 1.4 {
    44    SELECT rowid, a, b FROM f1 WHERE f1 MATCH 'o';
    45  } {1 {} {} 2 {} {} 4 {} {}}
    46  
    47  do_execsql_test 1.5 {
    48    SELECT rowid, highlight(f1, 0, '[', ']') FROM f1 WHERE f1 MATCH 'o';
    49  } {1 {} 2 {} 4 {}}
    50  
    51  do_execsql_test 1.6 {
    52    SELECT rowid, highlight(f1, 0, '[', ']') IS NULL FROM f1 WHERE f1 MATCH 'o';
    53  } {1 1 2 1 4 1}
    54  
    55  do_execsql_test 1.7 {
    56    SELECT rowid, snippet(f1, -1, '[', ']', '...', 5) IS NULL 
    57    FROM f1 WHERE f1 MATCH 'o';
    58  } {1 1 2 1 4 1}
    59  
    60  do_execsql_test 1.8 {
    61    SELECT rowid, snippet(f1, 1, '[', ']', '...', 5) IS NULL 
    62    FROM f1 WHERE f1 MATCH 'o';
    63  } {1 1 2 1 4 1}
    64  
    65  do_execsql_test 1.9 {
    66    SELECT rowid FROM f1;
    67  } {1 2 3 4}
    68  
    69  do_execsql_test 1.10 {
    70    SELECT * FROM f1;
    71  } {{} {}  {} {}  {} {}  {} {}}
    72  
    73  do_execsql_test 1.11 {
    74    SELECT rowid, a, b FROM f1 ORDER BY rowid ASC;
    75  } {1 {} {}  2 {} {}  3 {} {}  4 {} {}}
    76  
    77  do_execsql_test 1.12 {
    78    SELECT a IS NULL FROM f1;
    79  } {1 1 1 1}
    80  
    81  do_catchsql_test 1.13 {
    82    DELETE FROM f1 WHERE rowid = 2;
    83  } {1 {cannot DELETE from contentless fts5 table: f1}}
    84  
    85  do_catchsql_test 1.14 {
    86    UPDATE f1 SET a = 'a b c' WHERE rowid = 2;
    87  } {1 {cannot UPDATE contentless fts5 table: f1}}
    88  
    89  do_execsql_test 1.15 {
    90    INSERT INTO f1(f1, rowid, a, b) VALUES('delete', 2, 'two', 't w o');
    91  } {}
    92  
    93  do_execsql_test 1.16 {
    94    SELECT rowid FROM f1 WHERE f1 MATCH 'o';
    95  } {1 4}
    96  
    97  do_execsql_test 1.17 {
    98    SELECT rowid FROM f1;
    99  } {1 3 4}
   100  
   101  #-------------------------------------------------------------------------
   102  # External content tables
   103  #
   104  reset_db
   105  do_execsql_test 2.1 {
   106    -- Create a table. And an external content fts5 table to index it.
   107    CREATE TABLE tbl(a INTEGER PRIMARY KEY, b, c);
   108    CREATE VIRTUAL TABLE fts_idx USING fts5(b, c, content='tbl', content_rowid='a');
   109  
   110    -- Triggers to keep the FTS index up to date.
   111    CREATE TRIGGER tbl_ai AFTER INSERT ON tbl BEGIN
   112      INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
   113    END;
   114    CREATE TRIGGER tbl_ad AFTER DELETE ON tbl BEGIN
   115      INSERT INTO fts_idx(fts_idx, rowid, b, c) 
   116          VALUES('delete', old.a, old.b, old.c);
   117    END;
   118    CREATE TRIGGER tbl_au AFTER UPDATE ON tbl BEGIN
   119      INSERT INTO fts_idx(fts_idx, rowid, b, c) 
   120          VALUES('delete', old.a, old.b, old.c);
   121      INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
   122    END;
   123  }
   124  
   125  do_execsql_test 2.2 {
   126    INSERT INTO tbl VALUES(1, 'one', 'o n e');
   127    INSERT INTO tbl VALUES(NULL, 'two', 't w o');
   128    INSERT INTO tbl VALUES(3, 'three', 't h r e e');
   129  }
   130  
   131  do_execsql_test 2.3 {
   132    INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
   133  }
   134  
   135  do_execsql_test 2.4 {
   136    DELETE FROM tbl WHERE rowid=2;
   137    INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
   138  }
   139  
   140  do_execsql_test 2.5 {
   141    UPDATE tbl SET c = c || ' x y z';
   142    INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
   143  }
   144  
   145  do_execsql_test 2.6 {
   146    SELECT * FROM fts_idx WHERE fts_idx MATCH 't AND x';
   147  } {three {t h r e e x y z}}
   148  
   149  do_execsql_test 2.7 {
   150    SELECT highlight(fts_idx, 1, '[', ']') FROM fts_idx 
   151    WHERE fts_idx MATCH 't AND x';
   152  } {{[t] h r e e [x] y z}}
   153  
   154  #-------------------------------------------------------------------------
   155  # Quick tests of the 'delete-all' command.
   156  #
   157  do_execsql_test 3.1 {
   158    CREATE VIRTUAL TABLE t3 USING fts5(x, content='');
   159    INSERT INTO t3 VALUES('a b c');
   160    INSERT INTO t3 VALUES('d e f');
   161  }
   162  
   163  do_execsql_test 3.2 {
   164    SELECT count(*) FROM t3_docsize;
   165    SELECT count(*) FROM t3_data;
   166  } {2 4}
   167  
   168  do_execsql_test 3.3 {
   169    INSERT INTO t3(t3) VALUES('delete-all');
   170    SELECT count(*) FROM t3_docsize;
   171    SELECT count(*) FROM t3_data;
   172  } {0 2}
   173  
   174  do_execsql_test 3.4 {
   175    INSERT INTO t3 VALUES('a b c');
   176    INSERT INTO t3 VALUES('d e f');
   177    SELECT rowid FROM t3 WHERE t3 MATCH 'e';
   178  } {2}
   179  
   180  do_execsql_test 3.5 {
   181    SELECT rowid FROM t3 WHERE t3 MATCH 'c';
   182  } {1}
   183  
   184  do_execsql_test 3.6 {
   185    SELECT count(*) FROM t3_docsize;
   186    SELECT count(*) FROM t3_data;
   187  } {2 4}
   188  
   189  do_execsql_test 3.7 {
   190    CREATE VIRTUAL TABLE t4 USING fts5(x);
   191  } {}
   192  do_catchsql_test 3.8 {
   193    INSERT INTO t4(t4) VALUES('delete-all');
   194  } {1 {'delete-all' may only be used with a contentless or external content fts5 table}}
   195  
   196  #-------------------------------------------------------------------------
   197  # Test an external content table with a more interesting schema.
   198  #
   199  do_execsql_test 4.1 {
   200    CREATE TABLE x2(a, "key col" PRIMARY KEY, b, c) WITHOUT ROWID;
   201    INSERT INTO x2 VALUES('a b',   1, 'c d' , 'e f');
   202    INSERT INTO x2 VALUES('x y', -40, 'z z' , 'y x');
   203  
   204    CREATE VIRTUAL TABLE t2 USING fts5(a, c, content=x2, content_rowid='key col');
   205    INSERT INTO t2(t2) VALUES('rebuild');
   206  }
   207  
   208  do_execsql_test 4.2 { SELECT rowid FROM t2 } {-40 1}
   209  do_execsql_test 4.3 { SELECT rowid FROM t2 WHERE t2 MATCH 'c'} {}
   210  do_execsql_test 4.4 { SELECT rowid FROM t2 WHERE t2 MATCH 'a'} {1}
   211  do_execsql_test 4.5 { SELECT rowid FROM t2 WHERE t2 MATCH 'x'} {-40}
   212  
   213  do_execsql_test 4.6 { INSERT INTO t2(t2) VALUES('integrity-check') } {}
   214  
   215  do_execsql_test 4.7 { 
   216    DELETE FROM x2 WHERE "key col" = 1;
   217    INSERT INTO t2(t2, rowid, a, c) VALUES('delete', 1, 'a b', 'e f');
   218    INSERT INTO t2(t2) VALUES('integrity-check');
   219  }
   220  
   221  do_execsql_test 4.8 { SELECT rowid FROM t2 WHERE t2 MATCH 'b'} {}
   222  do_execsql_test 4.9 { SELECT rowid FROM t2 WHERE t2 MATCH 'y'} {-40}
   223  
   224  #-------------------------------------------------------------------------
   225  # Test that if the 'rowid' field of a 'delete' is not an integer, no
   226  # changes are made to the FTS index.
   227  #
   228  do_execsql_test 5.0 {
   229    CREATE VIRTUAL TABLE t5 USING fts5(a, b, content=);
   230    INSERT INTO t5(rowid, a, b) VALUES(-1, 'one',   'two');
   231    INSERT INTO t5(rowid, a, b) VALUES( 0, 'three', 'four');
   232    INSERT INTO t5(rowid, a, b) VALUES( 1, 'five',  'six');
   233  }
   234  
   235  set ::checksum [execsql {SELECT md5sum(id, block) FROM t5_data}]
   236  
   237  do_execsql_test 5.1 {
   238    INSERT INTO t5(t5, rowid, a, b) VALUES('delete', NULL, 'three', 'four');
   239    SELECT md5sum(id, block) FROM t5_data;
   240  } $::checksum
   241  
   242  
   243  #-------------------------------------------------------------------------
   244  # Check that a contentless table can be dropped.
   245  #
   246  reset_db
   247  do_execsql_test 6.1 {
   248    CREATE VIRTUAL TABLE xx USING fts5(x, y, content="");
   249    SELECT name FROM sqlite_master;
   250  } {xx xx_data xx_idx xx_docsize xx_config}
   251  do_execsql_test 6.2 {
   252    DROP TABLE xx;
   253    SELECT name FROM sqlite_master;
   254  } {}
   255  
   256  
   257  finish_test