rbufts.test 3.6 KB

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