|
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 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 testSquareFunction = { |
|
23 calls: 0, |
|
24 |
|
25 onFunctionCall: function(val) { |
|
26 ++this.calls; |
|
27 return val.getInt32(0) * val.getInt32(0); |
|
28 } |
|
29 }; |
|
30 |
|
31 function test_function_registration() |
|
32 { |
|
33 var msc = getOpenedDatabase(); |
|
34 msc.createFunction("test_square", 1, testSquareFunction); |
|
35 } |
|
36 |
|
37 function test_function_no_double_registration() |
|
38 { |
|
39 var msc = getOpenedDatabase(); |
|
40 try { |
|
41 msc.createFunction("test_square", 2, testSquareFunction); |
|
42 do_throw("We shouldn't get here!"); |
|
43 } catch (e) { |
|
44 do_check_eq(Cr.NS_ERROR_FAILURE, e.result); |
|
45 } |
|
46 } |
|
47 |
|
48 function test_function_removal() |
|
49 { |
|
50 var msc = getOpenedDatabase(); |
|
51 msc.removeFunction("test_square"); |
|
52 // Should be Ok now |
|
53 msc.createFunction("test_square", 1, testSquareFunction); |
|
54 } |
|
55 |
|
56 function test_function_aliases() |
|
57 { |
|
58 var msc = getOpenedDatabase(); |
|
59 msc.createFunction("test_square2", 1, testSquareFunction); |
|
60 } |
|
61 |
|
62 function test_function_call() |
|
63 { |
|
64 var stmt = createStatement("SELECT test_square(id) FROM function_tests"); |
|
65 while(stmt.executeStep()); |
|
66 do_check_eq(testNums.length, testSquareFunction.calls); |
|
67 testSquareFunction.calls = 0; |
|
68 stmt.finalize(); |
|
69 } |
|
70 |
|
71 function test_function_result() |
|
72 { |
|
73 var stmt = createStatement("SELECT test_square(42) FROM function_tests"); |
|
74 stmt.executeStep(); |
|
75 do_check_eq(42*42, stmt.getInt32(0)); |
|
76 testSquareFunction.calls = 0; |
|
77 stmt.finalize(); |
|
78 } |
|
79 |
|
80 var tests = [test_function_registration, test_function_no_double_registration, |
|
81 test_function_removal, test_function_aliases, test_function_call, |
|
82 test_function_result]; |
|
83 |
|
84 function run_test() |
|
85 { |
|
86 setup(); |
|
87 |
|
88 for (var i = 0; i < tests.length; i++) { |
|
89 tests[i](); |
|
90 } |
|
91 |
|
92 cleanup(); |
|
93 } |