summaryrefslogtreecommitdiffstats
path: root/storage/test/unit/test_storage_fulltextindex.js
blob: bcc9491a9300c41bc4f8ac075ad16da1b4892da5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This file tests support for the fts3 (full-text index) module.

// Example statements in these tests are taken from the Full Text Index page
// on the SQLite wiki: http://www.sqlite.org/cvstrac/wiki?p=FullTextIndex

function test_table_creation() {
  var msc = getOpenedUnsharedDatabase();

  msc.executeSimpleSQL(
    "CREATE VIRTUAL TABLE recipe USING fts3(name, ingredients)"
  );

  Assert.ok(msc.tableExists("recipe"));
}

function test_insertion() {
  var msc = getOpenedUnsharedDatabase();

  msc.executeSimpleSQL(
    "INSERT INTO recipe (name, ingredients) VALUES " +
      "('broccoli stew', 'broccoli peppers cheese tomatoes')"
  );
  msc.executeSimpleSQL(
    "INSERT INTO recipe (name, ingredients) VALUES " +
      "('pumpkin stew', 'pumpkin onions garlic celery')"
  );
  msc.executeSimpleSQL(
    "INSERT INTO recipe (name, ingredients) VALUES " +
      "('broccoli pie', 'broccoli cheese onions flour')"
  );
  msc.executeSimpleSQL(
    "INSERT INTO recipe (name, ingredients) VALUES " +
      "('pumpkin pie', 'pumpkin sugar flour butter')"
  );

  var stmt = msc.createStatement("SELECT COUNT(*) FROM recipe");
  stmt.executeStep();

  Assert.equal(stmt.getInt32(0), 4);

  stmt.reset();
  stmt.finalize();
}

function test_selection() {
  var msc = getOpenedUnsharedDatabase();

  var stmt = msc.createStatement(
    "SELECT rowid, name, ingredients FROM recipe WHERE name MATCH 'pie'"
  );

  Assert.ok(stmt.executeStep());
  Assert.equal(stmt.getInt32(0), 3);
  Assert.equal(stmt.getString(1), "broccoli pie");
  Assert.equal(stmt.getString(2), "broccoli cheese onions flour");

  Assert.ok(stmt.executeStep());
  Assert.equal(stmt.getInt32(0), 4);
  Assert.equal(stmt.getString(1), "pumpkin pie");
  Assert.equal(stmt.getString(2), "pumpkin sugar flour butter");

  Assert.ok(!stmt.executeStep());

  stmt.reset();
  stmt.finalize();
}

var tests = [test_table_creation, test_insertion, test_selection];

function run_test() {
  // It's extra important to start from scratch, since these tests won't work
  // with an existing shared cache connection, so we do it even though the last
  // test probably did it already.
  cleanup();

  try {
    for (var i = 0; i < tests.length; i++) {
      tests[i]();
    }
  } finally {
    // It's extra important to clean up afterwards, since later tests that use
    // a shared cache connection will not be able to read the database we create,
    // so we do this in a finally block to ensure it happens even if some of our
    // tests fail.
    cleanup();
  }
}