tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_storage_ext_fts5.js (3439B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // This file tests support for the fts5 extension.
      6 
      7 // Some example statements in this tests are taken from the SQLite FTS5
      8 // documentation page: https://sqlite.org/fts5.html
      9 
     10 add_setup(async function () {
     11  cleanup();
     12 });
     13 
     14 add_task(async function test_synchronous() {
     15  info("Testing synchronous connection");
     16  let conn = getOpenedUnsharedDatabase();
     17  Assert.throws(
     18    () =>
     19      conn.executeSimpleSQL(
     20        "CREATE VIRTUAL TABLE email USING fts5(sender, title, body);"
     21      ),
     22    /NS_ERROR_FAILURE/,
     23    "Should not be able to use FTS5 without loading the extension"
     24  );
     25 
     26  await loadFTS5Extension(conn);
     27 
     28  conn.executeSimpleSQL(
     29    "CREATE VIRTUAL TABLE email USING fts5(sender, title, body, tokenize='trigram');"
     30  );
     31 
     32  conn.executeSimpleSQL(
     33    `INSERT INTO email(sender, title, body) VALUES
     34    ('Mark', 'Fox', 'The quick brown fox jumps over the lazy dog.'),
     35    ('Marco', 'Cat', 'The quick brown cat jumps over the lazy dog.'),
     36    ('James', 'Hamster', 'The quick brown hamster jumps over the lazy dog.')`
     37  );
     38 
     39  var stmt = conn.createStatement(
     40    `SELECT sender, title, highlight(email, 2, '<', '>')
     41     FROM email
     42     WHERE email MATCH 'ham'
     43     ORDER BY bm25(email)`
     44  );
     45  Assert.ok(stmt.executeStep());
     46  Assert.equal(stmt.getString(0), "James");
     47  Assert.equal(stmt.getString(1), "Hamster");
     48  Assert.equal(
     49    stmt.getString(2),
     50    "The quick brown <ham>ster jumps over the lazy dog."
     51  );
     52  stmt.reset();
     53  stmt.finalize();
     54 
     55  cleanup();
     56 });
     57 
     58 add_task(async function test_asynchronous() {
     59  info("Testing asynchronous connection");
     60  let conn = await openAsyncDatabase(getTestDB());
     61 
     62  await Assert.rejects(
     63    executeSimpleSQLAsync(
     64      conn,
     65      "CREATE VIRTUAL TABLE email USING fts5(sender, title, body);"
     66    ),
     67    err => err.message.startsWith("no such module"),
     68    "Should not be able to use FTS5 without loading the extension"
     69  );
     70 
     71  await loadFTS5Extension(conn);
     72 
     73  await executeSimpleSQLAsync(
     74    conn,
     75    "CREATE VIRTUAL TABLE email USING fts5(sender, title, body);"
     76  );
     77 
     78  await asyncClose(conn);
     79  await IOUtils.remove(getTestDB().path, { ignoreAbsent: true });
     80 });
     81 
     82 add_task(async function test_clone() {
     83  info("Testing cloning synchronous connection loads extensions in clone");
     84  let conn1 = getOpenedUnsharedDatabase();
     85  await loadFTS5Extension(conn1);
     86 
     87  let conn2 = conn1.clone(false);
     88  conn2.executeSimpleSQL(
     89    "CREATE VIRTUAL TABLE email USING fts5(sender, title, body);"
     90  );
     91 
     92  conn2.close();
     93  cleanup();
     94 });
     95 
     96 add_task(async function test_asyncClone() {
     97  info("Testing asynchronously cloning connection loads extensions in clone");
     98  let conn1 = getOpenedUnsharedDatabase();
     99  await loadFTS5Extension(conn1);
    100 
    101  let conn2 = await asyncClone(conn1, false);
    102  await executeSimpleSQLAsync(
    103    conn2,
    104    "CREATE VIRTUAL TABLE email USING fts5(sender, title, body);"
    105  );
    106 
    107  await asyncClose(conn2);
    108  await asyncClose(conn1);
    109  await IOUtils.remove(getTestDB().path, { ignoreAbsent: true });
    110 });
    111 
    112 async function loadFTS5Extension(conn) {
    113  await new Promise((resolve, reject) => {
    114    conn.loadExtension("fts5", status => {
    115      if (Components.isSuccessCode(status)) {
    116        resolve();
    117      } else {
    118        reject(status);
    119      }
    120    });
    121  });
    122 }