storage/test/unit/test_storage_aggregates.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // This file tests the custom aggregate functions
michael@0 6
michael@0 7 var testNums = [1, 2, 3, 4];
michael@0 8
michael@0 9 function setup()
michael@0 10 {
michael@0 11 getOpenedDatabase().createTable("function_tests", "id INTEGER PRIMARY KEY");
michael@0 12
michael@0 13 var stmt = createStatement("INSERT INTO function_tests (id) VALUES(?1)");
michael@0 14 for(var i = 0; i < testNums.length; ++i) {
michael@0 15 stmt.bindByIndex(0, testNums[i]);
michael@0 16 stmt.execute();
michael@0 17 }
michael@0 18 stmt.reset();
michael@0 19 stmt.finalize();
michael@0 20 }
michael@0 21
michael@0 22 var testSquareAndSumFunction = {
michael@0 23 calls: 0,
michael@0 24 _sas: 0,
michael@0 25
michael@0 26 reset: function() {
michael@0 27 this.calls = 0;
michael@0 28 this._sas = 0;
michael@0 29 },
michael@0 30
michael@0 31 onStep: function(val) {
michael@0 32 ++this.calls;
michael@0 33 this._sas += val.getInt32(0) * val.getInt32(0);
michael@0 34 },
michael@0 35
michael@0 36 onFinal: function() {
michael@0 37 var retval = this._sas;
michael@0 38 this._sas = 0; // Prepare for next group
michael@0 39 return retval;
michael@0 40 }
michael@0 41 };
michael@0 42
michael@0 43 function test_aggregate_registration()
michael@0 44 {
michael@0 45 var msc = getOpenedDatabase();
michael@0 46 msc.createAggregateFunction("test_sas_aggr", 1, testSquareAndSumFunction);
michael@0 47 }
michael@0 48
michael@0 49 function test_aggregate_no_double_registration()
michael@0 50 {
michael@0 51 var msc = getOpenedDatabase();
michael@0 52 try {
michael@0 53 msc.createAggregateFunction("test_sas_aggr", 2, testSquareAndSumFunction);
michael@0 54 do_throw("We shouldn't get here!");
michael@0 55 } catch (e) {
michael@0 56 do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
michael@0 57 }
michael@0 58 }
michael@0 59
michael@0 60 function test_aggregate_removal()
michael@0 61 {
michael@0 62 var msc = getOpenedDatabase();
michael@0 63 msc.removeFunction("test_sas_aggr");
michael@0 64 // Should be Ok now
michael@0 65 msc.createAggregateFunction("test_sas_aggr", 1, testSquareAndSumFunction);
michael@0 66 }
michael@0 67
michael@0 68 function test_aggregate_no_aliases()
michael@0 69 {
michael@0 70 var msc = getOpenedDatabase();
michael@0 71 try {
michael@0 72 msc.createAggregateFunction("test_sas_aggr2", 1, testSquareAndSumFunction);
michael@0 73 do_throw("We shouldn't get here!");
michael@0 74 } catch (e) {
michael@0 75 do_check_eq(Cr.NS_ERROR_FAILURE, e.result);
michael@0 76 }
michael@0 77 }
michael@0 78
michael@0 79 function test_aggregate_call()
michael@0 80 {
michael@0 81 var stmt = createStatement("SELECT test_sas_aggr(id) FROM function_tests");
michael@0 82 while(stmt.executeStep());
michael@0 83 do_check_eq(testNums.length, testSquareAndSumFunction.calls);
michael@0 84 testSquareAndSumFunction.reset();
michael@0 85 stmt.finalize();
michael@0 86 }
michael@0 87
michael@0 88 function test_aggregate_result()
michael@0 89 {
michael@0 90 var sas = 0;
michael@0 91 for(var i = 0; i < testNums.length; ++i) {
michael@0 92 sas += testNums[i] * testNums[i];
michael@0 93 }
michael@0 94 var stmt = createStatement("SELECT test_sas_aggr(id) FROM function_tests");
michael@0 95 stmt.executeStep();
michael@0 96 do_check_eq(sas, stmt.getInt32(0));
michael@0 97 testSquareAndSumFunction.reset();
michael@0 98 stmt.finalize();
michael@0 99 }
michael@0 100
michael@0 101 var tests = [test_aggregate_registration, test_aggregate_no_double_registration,
michael@0 102 test_aggregate_removal, test_aggregate_no_aliases, test_aggregate_call,
michael@0 103 test_aggregate_result];
michael@0 104
michael@0 105 function run_test()
michael@0 106 {
michael@0 107 setup();
michael@0 108
michael@0 109 for (var i = 0; i < tests.length; i++) {
michael@0 110 tests[i]();
michael@0 111 }
michael@0 112
michael@0 113 cleanup();
michael@0 114 }

mercurial