# 2017 August 10
#
# 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.
#
#***********************************************************************
#
# The tests in this file focus on testing the fts5vocab module.
#

source [file join [file dirname [info script]] fts5_common.tcl]
set testprefix fts5vocab2

# If SQLITE_ENABLE_FTS5 is not defined, omit this file.
ifcapable !fts5 {
  finish_test
  return
}

do_execsql_test 1.0 {
  CREATE VIRTUAL TABLE t1 USING fts5(a, b);
  CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);

  INSERT INTO t1 VALUES('one two', 'two three');
  INSERT INTO t1 VALUES('three four', 'four five five five');
}

do_execsql_test 1.1 {
  SELECT * FROM v1;
} {
  five  2 b 1
  five  2 b 2
  five  2 b 3
  four  2 a 1
  four  2 b 0
  one   1 a 0
  three 1 b 1
  three 2 a 0
  two   1 a 1
  two   1 b 0
}

do_execsql_test 1.2 {
  SELECT * FROM v1 WHERE term='three';
} {
  three 1 b 1
  three 2 a 0
}

do_execsql_test 1.3 {
  BEGIN;
    DELETE FROM t1 WHERE rowid=2;
    SELECT * FROM v1;
  ROLLBACK;
} {
  one   1 a 0
  three 1 b 1
  two   1 a 1
  two   1 b 0
}

do_execsql_test 1.4 {
  BEGIN;
    DELETE FROM t1 WHERE rowid=1;
    SELECT * FROM v1;
  ROLLBACK;
} {
  five  2 b 1
  five  2 b 2
  five  2 b 3
  four  2 a 1
  four  2 b 0
  three 2 a 0
}

do_execsql_test 1.5 {
  DELETE FROM t1;
  SELECT * FROM v1;
} {}

#-------------------------------------------------------------------------
#
do_execsql_test 2.0 {
  DROP TABLE IF EXISTS t1;
  DROP TABLE IF EXISTS v1;

  CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=column);
  CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);

  INSERT INTO t1 VALUES('one two', 'two three');
  INSERT INTO t1 VALUES('three four', 'four five five five');
}

do_execsql_test 2.1 {
  SELECT * FROM v1;
} {
  five  2 b {}
  four  2 a {}
  four  2 b {}
  one   1 a {}
  three 1 b {}
  three 2 a {}
  two   1 a {}
  two   1 b {}
}

do_execsql_test 2.2 {
  SELECT * FROM v1 WHERE term='three';
} {
  three 1 b {}
  three 2 a {}
}

do_execsql_test 2.3 {
  BEGIN;
    DELETE FROM t1 WHERE rowid=2;
    SELECT * FROM v1;
  ROLLBACK;
} {
  one   1 a {}
  three 1 b {}
  two   1 a {}
  two   1 b {}
}

do_execsql_test 2.4 {
  BEGIN;
    DELETE FROM t1 WHERE rowid=1;
    SELECT * FROM v1;
  ROLLBACK;
} {
  five  2 b {}
  four  2 a {}
  four  2 b {}
  three 2 a {}
}

do_execsql_test 2.5 {
  DELETE FROM t1;
  SELECT * FROM v1;
} {}

#-------------------------------------------------------------------------
#
do_execsql_test 3.0 {
  DROP TABLE IF EXISTS t1;
  DROP TABLE IF EXISTS v1;

  CREATE VIRTUAL TABLE t1 USING fts5(a, b, detail=none);
  CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);

  INSERT INTO t1 VALUES('one two', 'two three');
  INSERT INTO t1 VALUES('three four', 'four five five five');
}

do_execsql_test 3.1 {
  SELECT * FROM v1;
} {
  five  2 {} {}
  four  2 {} {}
  one   1 {} {}
  three 1 {} {}
  three 2 {} {}
  two   1 {} {}
}

do_execsql_test 3.2 {
  SELECT * FROM v1 WHERE term='three';
} {
  three 1 {} {}
  three 2 {} {}
}

do_execsql_test 3.3 {
  BEGIN;
    DELETE FROM t1 WHERE rowid=2;
    SELECT * FROM v1;
  ROLLBACK;
} {
  one   1 {} {}
  three 1 {} {}
  two   1 {} {}
}

do_execsql_test 3.4 {
  BEGIN;
    DELETE FROM t1 WHERE rowid=1;
    SELECT * FROM v1;
  ROLLBACK;
} {
  five  2 {} {}
  four  2 {} {}
  three 2 {} {}
}

do_execsql_test 3.5 {
  DELETE FROM t1;
  SELECT * FROM v1;
} {}

#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 4.0 {
  CREATE VIRTUAL TABLE v1 USING fts5vocab(nosuchtable, col);
}

do_catchsql_test 4.1 {
  SELECT * FROM v1 WHERE term=='nosuchterm';
} {1 {no such fts5 table: main.nosuchtable}}

do_execsql_test 4.2.1 {
  CREATE TABLE nosuchtable(nosuchtable, y, z);
}
do_catchsql_test 4.2.2 {
  SELECT * FROM v1 WHERE term=='nosuchterm';
} {1 {no such fts5 table: main.nosuchtable}}

ifcapable fts3 {
  do_execsql_test 4.3.1 {
    DROP TABLE nosuchtable;
    CREATE VIRTUAL TABLE nosuchtable USING fts3(a, b);
  } {}
  do_catchsql_test 4.3.2 {
    SELECT * FROM v1 WHERE term=='nosuchterm';
  } {1 {no such fts5 table: main.nosuchtable}}
  do_catchsql_test 4.3.3 {
    INSERT INTO nosuchtable VALUES('id', '*id');
    SELECT * FROM v1 WHERE term=='nosuchterm';
  } {1 {no such fts5 table: main.nosuchtable}}
}

#-------------------------------------------------------------------------
# Check that the fts5 table cannot be written while there are vocab 
# cursors open.
reset_db
do_execsql_test 5.0 {
  CREATE VIRTUAL TABLE t1 USING fts5(a);
  CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
  INSERT INTO t1 VALUES('one'), ('two'), ('three'), ('four');
}

do_test 5.1 {
  list [catch {
    db eval { SELECT * FROM v1 } {
      db eval {INSERT INTO t1 VALUES('five')}
    }
  } msg] $msg
} {1 {query aborted}}

do_execsql_test 5.2 {
  SELECT * FROM t1
} {one two three four five}

#-------------------------------------------------------------------------
# Check that the fts5 table cannot be written while there are vocab 
# cursors open.
reset_db
do_execsql_test 5.0 {
  CREATE VIRTUAL TABLE t1 USING fts5(a);
  CREATE VIRTUAL TABLE v1 USING fts5vocab(t1, instance);
  WITH s(i) AS (
    VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<10000
  )
  INSERT INTO t1 SELECT 
    'State Emergency Service (SES), Rural Fire Service (RFS) and Volunteers'
  FROM s;
}

do_catchsql_test 5.1 {
  INSERT INTO t1 SELECT rowid FROM v1
} {1 {query aborted}}

do_catchsql_test 5.2 {
  DELETE FROM t1 WHERE rowid>100;
  INSERT INTO t1 SELECT randomblob(3000) FROM v1
} {1 {query aborted}}

#-------------------------------------------------------------------------
reset_db
sqlite3_fts5_may_be_corrupt 1

do_execsql_test 6.0 {
  BEGIN TRANSACTION;
    CREATE VIRTUAL TABLE t1 USING fts5(a,b unindexed,c,tokenize="porter ascii",tokendata=1);
    REPLACE INTO t1_data VALUES(1,X'03090009');
    REPLACE INTO t1_data VALUES(10,X'000000000103030003010101020101030101');
    REPLACE INTO t1_data VALUES(137438953473,X'0000002e023061010202010162010203010163010204010167010601020201016801060102030101690106010204040606060808');
    REPLACE INTO t1_data VALUES(274877906945,X'0000001f013067020802010202010168020803010203010169020804010204040909');
    REPLACE INTO t1_data VALUES(412316860417,X'0000002e023061030202010162030203010163030204010167030601020201016803060102030101690306010204040606060808');
  COMMIT;
}

do_execsql_test 6.1 {
  CREATE VIRTUAL TABLE t3 USING fts5vocab('t1', 'row');
}

do_catchsql_test 6.2 {
  SELECT * FROM t3;
} {1 {database disk image is malformed}}

sqlite3_fts5_may_be_corrupt 0

finish_test


