Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(101)

Side by Side Diff: gears/test/testcases/database2_tests.js

Issue 717: Database2Values, argument conversion implemented (Closed) SVN Base: http://google-gears.googlecode.com/svn/contrib/dimitri.glazkov/database2/
Patch Set: Created 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2008, Google Inc. 1 // Copyright 2008, Google Inc.
2 // 2 //
3 // Redistribution and use in source and binary forms, with or without 3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met: 4 // modification, are permitted provided that the following conditions are met:
5 // 5 //
6 // 1. Redistributions of source code must retain the above copyright notice, 6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer. 7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice, 8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation 9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution. 10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be 11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without 12 // used to endorse or promote products derived from this software without
13 // specific prior written permission. 13 // specific prior written permission.
14 // 14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 25
26 var db_manager = google.gears.factory.create('beta.databasemanager'); 26 var db_manager = google.gears.factory.create('beta.databasemanager');
27 27
28 function testDatabaseManagerCreation() { 28 function testDatabaseManagerCreation() {
29 assertNotNull(db_manager, 'Database manager should be a value'); 29 assertNotNull(db_manager, 'Database manager should be a value');
30 } 30 }
31 31
32 function testDatabaseManagerApiSig() { 32 function testDatabaseManagerApiSig() {
33 var method = 'DatabaseManager.openDatabase()'; 33 var method = 'DatabaseManager.openDatabase()';
34 assert(db_manager.openDatabase, method + ' should be present'); 34 assert(db_manager.openDatabase, method + ' should be present');
35 assertError(function() { 35 assertError(function() {
36 db_manager.openDatabase(); 36 db_manager.openDatabase();
37 }, null, method + ' has required parameters'); 37 }, null, method + ' has required parameters');
38 assertError(function() { 38 assertError(function() {
39 db_manager.openDatabase('unit_test_db'); 39 db_manager.openDatabase('unit_test_db');
40 }, null, method + ' requires a version parameter'); 40 }, null, method + ' requires a version parameter');
41 } 41 }
42 42
43 function testDatabaseOpening() { 43 function testDatabaseOpening() {
44 var db = db_manager.openDatabase('unit_test_db', '', 'ignored_description', 44 var db = db_manager.openDatabase('unit_test_db', '', 'ignored_description',
45 'ignored_estimated_size'); 45 'ignored_estimated_size');
46 assert(db, 'Database should be a value'); 46 assert(db, 'Database should be a value');
47 } 47 }
48 48
49 function testDatabaseOptionalParams() { 49 function testDatabaseOptionalParams() {
50 var db = db_manager.openDatabase('unit_test_db', ''); 50 var db = db_manager.openDatabase('unit_test_db', '');
51 assert(db, 'Database should be a value'); 51 assert(db, 'Database should be a value');
52 } 52 }
53 53
54 function testDatabaseVersionNull() { 54 function testDatabaseVersionNull() {
55 assertError(function() { 55 assertError(function() {
56 var db = db_manager.openDatabase('unit_test_db', null); 56 var db = db_manager.openDatabase('unit_test_db', null);
57 }, "Required argument 2 is missing."); 57 }, "Required argument 2 is missing.");
58 } 58 }
59 59
60 function testDatabaseVersionNonNull() { 60 function testDatabaseVersionNonNull() {
61 var db = db_manager.openDatabase('unit_test_db', '1.0', 'ignored_description', 61 var db = db_manager.openDatabase('unit_test_db', '1.0', 'ignored_description',
62 'ignored_estimated_size'); 62 'ignored_estimated_size');
63 assert(db, 'Database should be a value'); 63 assert(db, 'Database should be a value');
64 } 64 }
65 65
66 function testDatabaseApiSig() { 66 function testDatabaseApiSig() {
67 withDb(function(db) { 67 withDb(function(db) {
68 var method = 'Database2.transaction()'; 68 var method = 'Database2.transaction()';
69 assert(db.transaction, method + ' should be present'); 69 assert(db.transaction, method + ' should be present');
70 assertError(function() { 70 assertError(function() {
71 db.transaction(); 71 db.transaction();
72 }, null, method + ' requires a callback'); 72 }, null, method + ' requires a callback');
73 73
74 var method = 'Database2.synchronousTransaction()'; 74 var method = 'Database2.synchronousTransaction()';
75 assert(db.synchronousTransaction, method + ' should be present'); 75 assert(db.synchronousTransaction, method + ' should be present');
76 76
77 var method = 'Database2.changeVersion()'; 77 var method = 'Database2.changeVersion()';
78 assert(db.changeVersion, method + ' should be present'); 78 assert(db.changeVersion, method + ' should be present');
79 }); 79 });
80 } 80 }
81 81
82 function testDatabaseTransaction() { 82 function testDatabaseTransaction() {
83 withDb(function(db) { 83 withDb(function(db) {
84 var method = 'Database2.transaction()'; 84 var method = 'Database2.transaction()';
85 var inFlight; 85 var inFlight;
86 var outOfOrder = 0; 86 var outOfOrder = 0;
87 // this tests the fact that the transaction callback is called 87 // this tests the fact that the transaction callback is called
88 // asynchronously and whether the callback is invoked in a proper sequence 88 // asynchronously and whether the callback is invoked in a proper sequence
89 // if startAsync times out, the callback is not invoked at all 89 // if startAsync times out, the callback is not invoked at all
90 db.transaction(function(tx) { 90 db.transaction(function(tx) {
91 inFlight = true; 91 inFlight = true;
92 outOfOrder--; 92 outOfOrder--;
93 if (outOfOrder) { 93 if (outOfOrder) {
94 assert(false, method + ' invokes callback out of sequence'); 94 assert(false, method + ' invokes callback out of sequence');
95 } 95 }
96 completeAsync(); 96 completeAsync();
97 }); 97 });
98 outOfOrder++; 98 outOfOrder++;
99 if (inFlight) { 99 if (inFlight) {
100 assert(false, method + ' is not called asynchronously'); 100 assert(false, method + ' is not called asynchronously');
101 } 101 }
102 startAsync(); 102 startAsync();
103 }); 103 });
104 } 104 }
105 105
106 function testDatabaseSynchronousTransaction() { 106 function testDatabaseSynchronousTransaction() {
107 withDb(function(db) { 107 withDb(function(db) {
108 var method = 'Database2.synchronousTransaction() '; 108 var method = 'Database2.synchronousTransaction() ';
109 var inFlight; 109 var inFlight;
110 var outOfOrder = 0; 110 var outOfOrder = 0;
111 // this tests the fact that the transaction callback is called 111 // this tests the fact that the transaction callback is called
112 // synchronously and whether the callback is invoked in a proper sequence 112 // synchronously and whether the callback is invoked in a proper sequence
113 db.synchronousTransaction(function(tx) { 113 db.synchronousTransaction(function(tx) {
114 inFlight = true; 114 inFlight = true;
115 if (outOfOrder) { 115 if (outOfOrder) {
116 assert(false, method + ' invokes callback out of sequence'); 116 assert(false, method + ' invokes callback out of sequence');
117 } 117 }
118 }); 118 });
119 outOfOrder++; 119 outOfOrder++;
120 if (!inFlight) { 120 if (!inFlight) {
121 assert(false, method + ' is not called synchronously'); 121 assert(false, method + ' is not called synchronously');
122 } 122 }
123 123
124 // test statement execution 124 // test statement execution
125 db.synchronousTransaction(function(tx) { 125 db.synchronousTransaction(function(tx) {
126 var method = 'Database2Transaction.executeSql()'; 126 var method = 'Database2Transaction.executeSql()';
127 var rs = tx.executeSql('SELECT * FROM Pages;'); 127 var rs = tx.executeSql('SELECT * FROM Pages;');
128 assert(rs, method + ' should return a result set'); 128 assert(rs, method + ' should return a result set');
129 }); 129 });
130 }); 130 });
131 } 131 }
132 132
133 function testStatementArguments() { 133 function testStatementArguments() {
134 withDb(function(db) { 134 withDb(function(db) {
135 db.synchronousTransaction(function(tx) { 135 db.synchronousTransaction(function(tx) {
136 // valid arguments 136 // valid arguments
137 tx.executeSql('SELECT * FROM Pages WHERE pageId = ? and version = ?', 137 tx.executeSql('SELECT * FROM Pages WHERE pageId = ? and version = ?',
138 [ 1972, '1.0.0.0' ]); 138 [ 1972, '1.0.0.0' ]);
139 // arguments with null
140 tx.executeSql('SELECT * FROM Pages WHERE pageId = ? and version = ?',
141 [ 1972, null ]);
142 // null arguments
143 tx.executeSql('SELECT * FROM Pages', null);
144 // invalid arguments: function
145 assertError(function() {
146 tx.executeSql('SELECT * FROM Pages WHERE pageId = ? and version = ?',
147 [ function() {}, '1.0.0.0' ]);
148 }, null, 'Function arguments are not allowed');
149 // invalid arguments: object
150 assertError(function() {
151 tx.executeSql('SELECT * FROM Pages WHERE pageId = ? and version = ?',
152 [ { you: 'is wrong' }, '1.0.0.0' ]);
153 }, null, 'Function arguments are not allowed');
139 }); 154 });
140 }); 155 });
141 } 156 }
142 157
143 function testSQLTransactionApiSig() { 158 function testSQLTransactionApiSig() {
144 withDb(function(db) { 159 withDb(function(db) {
145 var method = 'SQLTransaction.executeSql'; 160 var method = 'SQLTransaction.executeSql';
146 db.transaction(function(tx) { 161 db.transaction(function(tx) {
147 assert(tx.executeSQL, method + ' should be present'); 162 assert(tx.executeSQL, method + ' should be present');
148 }); 163 });
149 }); 164 });
150 } 165 }
151 166
152 function withDb(fn, version) { 167 function withDb(fn, version) {
153 db_manager && fn && fn.call( 168 db_manager && fn && fn.call(
154 this, db_manager.openDatabase('unit_test_db', version || '')); 169 this, db_manager.openDatabase('unit_test_db', version || ''));
155 } 170 }
156 171
OLDNEW

Powered by Google App Engine
This is Rietveld r305