storage/test/unit/test_storage_function.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/test/unit/test_storage_function.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// This file tests the custom functions
     1.9 +
    1.10 +var testNums = [1, 2, 3, 4];
    1.11 +
    1.12 +function setup()
    1.13 +{
    1.14 +  getOpenedDatabase().createTable("function_tests", "id INTEGER PRIMARY KEY");
    1.15 +
    1.16 +  var stmt = createStatement("INSERT INTO function_tests (id) VALUES(?1)");
    1.17 +  for(var i = 0; i < testNums.length; ++i) {
    1.18 +    stmt.bindByIndex(0, testNums[i]);
    1.19 +    stmt.execute();
    1.20 +  }
    1.21 +  stmt.reset();
    1.22 +  stmt.finalize();
    1.23 +}
    1.24 +
    1.25 +var testSquareFunction = {
    1.26 +  calls: 0,
    1.27 +
    1.28 +  onFunctionCall: function(val) {
    1.29 +    ++this.calls;
    1.30 +    return val.getInt32(0) * val.getInt32(0);
    1.31 +  }
    1.32 +};
    1.33 +
    1.34 +function test_function_registration()
    1.35 +{
    1.36 +  var msc = getOpenedDatabase();
    1.37 +  msc.createFunction("test_square", 1, testSquareFunction);
    1.38 +}
    1.39 +
    1.40 +function test_function_no_double_registration()
    1.41 +{
    1.42 +  var msc = getOpenedDatabase();
    1.43 +  try {
    1.44 +    msc.createFunction("test_square", 2, testSquareFunction);
    1.45 +    do_throw("We shouldn't get here!");
    1.46 +  } catch (e) {
    1.47 +    do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
    1.48 +  }
    1.49 +}
    1.50 +
    1.51 +function test_function_removal()
    1.52 +{
    1.53 +  var msc = getOpenedDatabase();
    1.54 +  msc.removeFunction("test_square");
    1.55 +  // Should be Ok now
    1.56 +  msc.createFunction("test_square", 1, testSquareFunction);
    1.57 +}
    1.58 +
    1.59 +function test_function_aliases()
    1.60 +{
    1.61 +  var msc = getOpenedDatabase();
    1.62 +  msc.createFunction("test_square2", 1, testSquareFunction);
    1.63 +}
    1.64 +
    1.65 +function test_function_call()
    1.66 +{
    1.67 +  var stmt = createStatement("SELECT test_square(id) FROM function_tests");
    1.68 +  while(stmt.executeStep());
    1.69 +  do_check_eq(testNums.length, testSquareFunction.calls);
    1.70 +  testSquareFunction.calls = 0;
    1.71 +  stmt.finalize();
    1.72 +}
    1.73 +
    1.74 +function test_function_result()
    1.75 +{
    1.76 +  var stmt = createStatement("SELECT test_square(42) FROM function_tests");
    1.77 +  stmt.executeStep();
    1.78 +  do_check_eq(42*42, stmt.getInt32(0));
    1.79 +  testSquareFunction.calls = 0;
    1.80 +  stmt.finalize();
    1.81 +}
    1.82 +
    1.83 +var tests = [test_function_registration, test_function_no_double_registration,
    1.84 +             test_function_removal, test_function_aliases, test_function_call,
    1.85 +             test_function_result];
    1.86 +
    1.87 +function run_test()
    1.88 +{
    1.89 +  setup();
    1.90 +
    1.91 +  for (var i = 0; i < tests.length; i++) {
    1.92 +    tests[i]();
    1.93 +  }
    1.94 +
    1.95 +  cleanup();
    1.96 +}

mercurial