1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/storage/test/unit/test_storage_aggregates.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,114 @@ 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 aggregate 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 testSquareAndSumFunction = { 1.26 + calls: 0, 1.27 + _sas: 0, 1.28 + 1.29 + reset: function() { 1.30 + this.calls = 0; 1.31 + this._sas = 0; 1.32 + }, 1.33 + 1.34 + onStep: function(val) { 1.35 + ++this.calls; 1.36 + this._sas += val.getInt32(0) * val.getInt32(0); 1.37 + }, 1.38 + 1.39 + onFinal: function() { 1.40 + var retval = this._sas; 1.41 + this._sas = 0; // Prepare for next group 1.42 + return retval; 1.43 + } 1.44 +}; 1.45 + 1.46 +function test_aggregate_registration() 1.47 +{ 1.48 + var msc = getOpenedDatabase(); 1.49 + msc.createAggregateFunction("test_sas_aggr", 1, testSquareAndSumFunction); 1.50 +} 1.51 + 1.52 +function test_aggregate_no_double_registration() 1.53 +{ 1.54 + var msc = getOpenedDatabase(); 1.55 + try { 1.56 + msc.createAggregateFunction("test_sas_aggr", 2, testSquareAndSumFunction); 1.57 + do_throw("We shouldn't get here!"); 1.58 + } catch (e) { 1.59 + do_check_eq(Cr.NS_ERROR_FAILURE, e.result); 1.60 + } 1.61 +} 1.62 + 1.63 +function test_aggregate_removal() 1.64 +{ 1.65 + var msc = getOpenedDatabase(); 1.66 + msc.removeFunction("test_sas_aggr"); 1.67 + // Should be Ok now 1.68 + msc.createAggregateFunction("test_sas_aggr", 1, testSquareAndSumFunction); 1.69 +} 1.70 + 1.71 +function test_aggregate_no_aliases() 1.72 +{ 1.73 + var msc = getOpenedDatabase(); 1.74 + try { 1.75 + msc.createAggregateFunction("test_sas_aggr2", 1, testSquareAndSumFunction); 1.76 + do_throw("We shouldn't get here!"); 1.77 + } catch (e) { 1.78 + do_check_eq(Cr.NS_ERROR_FAILURE, e.result); 1.79 + } 1.80 +} 1.81 + 1.82 +function test_aggregate_call() 1.83 +{ 1.84 + var stmt = createStatement("SELECT test_sas_aggr(id) FROM function_tests"); 1.85 + while(stmt.executeStep()); 1.86 + do_check_eq(testNums.length, testSquareAndSumFunction.calls); 1.87 + testSquareAndSumFunction.reset(); 1.88 + stmt.finalize(); 1.89 +} 1.90 + 1.91 +function test_aggregate_result() 1.92 +{ 1.93 + var sas = 0; 1.94 + for(var i = 0; i < testNums.length; ++i) { 1.95 + sas += testNums[i] * testNums[i]; 1.96 + } 1.97 + var stmt = createStatement("SELECT test_sas_aggr(id) FROM function_tests"); 1.98 + stmt.executeStep(); 1.99 + do_check_eq(sas, stmt.getInt32(0)); 1.100 + testSquareAndSumFunction.reset(); 1.101 + stmt.finalize(); 1.102 +} 1.103 + 1.104 +var tests = [test_aggregate_registration, test_aggregate_no_double_registration, 1.105 + test_aggregate_removal, test_aggregate_no_aliases, test_aggregate_call, 1.106 + test_aggregate_result]; 1.107 + 1.108 +function run_test() 1.109 +{ 1.110 + setup(); 1.111 + 1.112 + for (var i = 0; i < tests.length; i++) { 1.113 + tests[i](); 1.114 + } 1.115 + 1.116 + cleanup(); 1.117 +}