rbufts.test 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # 2014 August 30
  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 RBU module. More specifically, it
  13. # contains tests to ensure that RBU works with FTS tables.
  14. #
  15. source [file join [file dirname [info script]] rbu_common.tcl]
  16. if_no_rbu_support { finish_test ; return }
  17. set ::testprefix rbufts
  18. ifcapable !fts3 {
  19. finish_test
  20. return
  21. }
  22. proc step_rbu {target rbu} {
  23. while 1 {
  24. sqlite3rbu rbu $target $rbu
  25. set rc [rbu step]
  26. rbu close
  27. if {$rc != "SQLITE_OK"} break
  28. }
  29. set rc
  30. }
  31. proc apply_rbu_update {target sql} {
  32. forcedelete rbu.db
  33. sqlite3 dbrbu rbu.db
  34. execsql $sql dbrbu
  35. dbrbu close
  36. step_rbu $target rbu.db
  37. }
  38. do_execsql_test 1.1.0 {
  39. CREATE TABLE t1(i INTEGER PRIMARY KEY, a, b);
  40. CREATE VIRTUAL TABLE xx USING fts4(content=t1, a, b);
  41. INSERT INTO t1(rowid, a, b) VALUES(10, 'a b c', 'c b a');
  42. INSERT INTO t1(rowid, a, b) VALUES(20, 'a b c', 'd e f');
  43. INSERT INTO t1(rowid, a, b) VALUES(30, 'd e f', 'a b c');
  44. INSERT INTO t1(rowid, a, b) VALUES(40, 'd e f', 'd e f');
  45. }
  46. do_execsql_test 1.1.1 {
  47. INSERT INTO xx(xx) VALUES('rebuild');
  48. INSERT INTO xx(xx) VALUES('integrity-check');
  49. }
  50. do_test 1.1.2 {
  51. apply_rbu_update test.db {
  52. CREATE TABLE data_t1(i, a, b, rbu_control);
  53. INSERT INTO data_t1 VALUES(20, NULL, NULL, 1); -- delete
  54. INSERT INTO data_t1 VALUES(30, 'x y z', NULL, '.x.'); -- update
  55. INSERT INTO data_t1 VALUES(50, '1 2 3', 'x y z', 0); -- insert
  56. CREATE VIEW data0_xx AS
  57. SELECT i AS rbu_rowid, a, b,
  58. CASE WHEN rbu_control IN (0, 1)
  59. THEN rbu_control ELSE substr(rbu_control, 2) END AS rbu_control
  60. FROM data_t1;
  61. }
  62. } {SQLITE_DONE}
  63. do_execsql_test 1.1.3 {
  64. INSERT INTO xx(xx) VALUES('integrity-check');
  65. }
  66. reset_db
  67. do_execsql_test 1.2.1 {
  68. CREATE TABLE ccc(addr, text);
  69. CREATE VIRTUAL TABLE ccc_fts USING fts4(addr, text, content=ccc);
  70. INSERT INTO ccc VALUES('a b c', 'd e f');
  71. INSERT INTO ccc VALUES('a b c', 'd e f');
  72. INSERT INTO ccc_fts(ccc_fts) VALUES('rebuild');
  73. INSERT INTO ccc_fts(ccc_fts) VALUES('integrity-check');
  74. }
  75. do_test 1.2.2 {
  76. apply_rbu_update test.db {
  77. CREATE TABLE data_ccc(addr, text, rbu_rowid, rbu_control);
  78. CREATE VIEW data0_ccc_fts AS SELECT * FROM data_ccc;
  79. INSERT INTO data_ccc VALUES(NULL, NULL, 1, 1);
  80. INSERT INTO data_ccc VALUES('x y z', NULL, 2, 'x.');
  81. INSERT INTO data_ccc VALUES('y y y', '1 1 1', 3, 0);
  82. }
  83. } {SQLITE_DONE}
  84. do_execsql_test 1.2.3 {
  85. INSERT INTO ccc_fts(ccc_fts) VALUES('integrity-check');
  86. }
  87. do_execsql_test 1.2.4 {
  88. SELECT rowid, * FROM ccc_fts;
  89. } {2 {x y z} {d e f} 3 {y y y} {1 1 1}}
  90. #-------------------------------------------------------------------------
  91. # Test the outcome of attempting to delete or update a row within a
  92. # contentless FTS table using RBU. An error.
  93. #
  94. reset_db
  95. do_execsql_test 3.1 {
  96. CREATE VIRTUAL TABLE ft USING fts4(x, content=);
  97. INSERT INTO ft(rowid, x) VALUES(1, '1 2 3');
  98. INSERT INTO ft(rowid, x) VALUES(2, '4 5 6');
  99. }
  100. do_test 3.2 {
  101. list [catch { apply_rbu_update test.db {
  102. CREATE TABLE data_ft(x, rbu_rowid, rbu_control);
  103. INSERT INTO data_ft VALUES(NULL, 2, 1);
  104. } } msg] $msg]
  105. } {1 {SQLITE_ERROR - SQL logic error]}}
  106. do_test 3.3 {
  107. list [catch { apply_rbu_update test.db {
  108. CREATE TABLE data_ft(x, rbu_rowid, rbu_control);
  109. INSERT INTO data_ft VALUES('7 8 9', 1, 'x');
  110. } } msg] $msg]
  111. } {1 {SQLITE_ERROR - SQL logic error]}}
  112. finish_test