storage/test/unit/test_storage_aggregates.js

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

mercurial