test_storage_value_array.js (5011B)
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 functions of mozIStorageValueArray 6 7 add_task(async function setup() { 8 getOpenedDatabase().createTable( 9 "test", 10 "id INTEGER PRIMARY KEY, name TEXT," + 11 "number REAL, nuller NULL, blobber BLOB" 12 ); 13 14 var stmt = createStatement( 15 "INSERT INTO test (name, number, blobber) VALUES (?1, ?2, ?3)" 16 ); 17 stmt.bindByIndex(0, "foo"); 18 stmt.bindByIndex(1, 2.34); 19 stmt.bindBlobByIndex(2, [], 0); 20 stmt.execute(); 21 22 stmt.bindByIndex(0, ""); 23 stmt.bindByIndex(1, 1.23); 24 stmt.bindBlobByIndex(2, [1, 2], 2); 25 stmt.execute(); 26 27 stmt.reset(); 28 stmt.finalize(); 29 30 registerCleanupFunction(cleanup); 31 }); 32 33 add_task(async function test_getIsNull_for_null() { 34 var stmt = createStatement("SELECT nuller, blobber FROM test WHERE id = ?1"); 35 stmt.bindByIndex(0, 1); 36 Assert.ok(stmt.executeStep()); 37 38 Assert.ok(stmt.getIsNull(0)); // null field 39 Assert.ok(stmt.getIsNull(1)); // data is null if size is 0 40 stmt.reset(); 41 stmt.finalize(); 42 }); 43 44 add_task(async function test_getIsNull_for_non_null() { 45 var stmt = createStatement("SELECT name, blobber FROM test WHERE id = ?1"); 46 stmt.bindByIndex(0, 2); 47 Assert.ok(stmt.executeStep()); 48 49 Assert.ok(!stmt.getIsNull(0)); 50 Assert.ok(!stmt.getIsNull(1)); 51 stmt.reset(); 52 stmt.finalize(); 53 }); 54 55 add_task(async function test_value_type_null() { 56 var stmt = createStatement("SELECT nuller FROM test WHERE id = ?1"); 57 stmt.bindByIndex(0, 1); 58 Assert.ok(stmt.executeStep()); 59 60 Assert.equal( 61 Ci.mozIStorageValueArray.VALUE_TYPE_NULL, 62 stmt.getTypeOfIndex(0) 63 ); 64 stmt.reset(); 65 stmt.finalize(); 66 }); 67 68 add_task(async function test_value_type_integer() { 69 var stmt = createStatement("SELECT id FROM test WHERE id = ?1"); 70 stmt.bindByIndex(0, 1); 71 Assert.ok(stmt.executeStep()); 72 73 Assert.equal( 74 Ci.mozIStorageValueArray.VALUE_TYPE_INTEGER, 75 stmt.getTypeOfIndex(0) 76 ); 77 stmt.reset(); 78 stmt.finalize(); 79 }); 80 81 add_task(async function test_value_type_float() { 82 var stmt = createStatement("SELECT number FROM test WHERE id = ?1"); 83 stmt.bindByIndex(0, 1); 84 Assert.ok(stmt.executeStep()); 85 86 Assert.equal( 87 Ci.mozIStorageValueArray.VALUE_TYPE_FLOAT, 88 stmt.getTypeOfIndex(0) 89 ); 90 stmt.reset(); 91 stmt.finalize(); 92 }); 93 94 add_task(async function test_value_type_text() { 95 var stmt = createStatement("SELECT name FROM test WHERE id = ?1"); 96 stmt.bindByIndex(0, 1); 97 Assert.ok(stmt.executeStep()); 98 99 Assert.equal( 100 Ci.mozIStorageValueArray.VALUE_TYPE_TEXT, 101 stmt.getTypeOfIndex(0) 102 ); 103 stmt.reset(); 104 stmt.finalize(); 105 }); 106 107 add_task(async function test_value_type_blob() { 108 var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1"); 109 stmt.bindByIndex(0, 2); 110 Assert.ok(stmt.executeStep()); 111 112 Assert.equal( 113 Ci.mozIStorageValueArray.VALUE_TYPE_BLOB, 114 stmt.getTypeOfIndex(0) 115 ); 116 stmt.reset(); 117 stmt.finalize(); 118 }); 119 120 add_task(async function test_numEntries_one() { 121 var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1"); 122 stmt.bindByIndex(0, 2); 123 Assert.ok(stmt.executeStep()); 124 125 Assert.equal(1, stmt.numEntries); 126 stmt.reset(); 127 stmt.finalize(); 128 }); 129 130 add_task(async function test_numEntries_all() { 131 var stmt = createStatement("SELECT * FROM test WHERE id = ?1"); 132 stmt.bindByIndex(0, 2); 133 Assert.ok(stmt.executeStep()); 134 135 Assert.equal(5, stmt.numEntries); 136 stmt.reset(); 137 stmt.finalize(); 138 }); 139 140 add_task(async function test_getInt() { 141 var stmt = createStatement("SELECT id FROM test WHERE id = ?1"); 142 stmt.bindByIndex(0, 2); 143 Assert.ok(stmt.executeStep()); 144 145 Assert.equal(2, stmt.getInt32(0)); 146 Assert.equal(2, stmt.getInt64(0)); 147 stmt.reset(); 148 stmt.finalize(); 149 }); 150 151 add_task(async function test_getDouble() { 152 var stmt = createStatement("SELECT number FROM test WHERE id = ?1"); 153 stmt.bindByIndex(0, 2); 154 Assert.ok(stmt.executeStep()); 155 156 Assert.equal(1.23, stmt.getDouble(0)); 157 stmt.reset(); 158 stmt.finalize(); 159 }); 160 161 add_task(async function test_getUTF8String() { 162 var stmt = createStatement("SELECT name FROM test WHERE id = ?1"); 163 stmt.bindByIndex(0, 1); 164 Assert.ok(stmt.executeStep()); 165 166 Assert.equal("foo", stmt.getUTF8String(0)); 167 stmt.reset(); 168 stmt.finalize(); 169 }); 170 171 add_task(async function test_getString() { 172 var stmt = createStatement("SELECT name FROM test WHERE id = ?1"); 173 stmt.bindByIndex(0, 2); 174 Assert.ok(stmt.executeStep()); 175 176 Assert.equal("", stmt.getString(0)); 177 stmt.reset(); 178 stmt.finalize(); 179 }); 180 181 add_task(async function test_getBlob() { 182 var stmt = createStatement("SELECT blobber FROM test WHERE id = ?1"); 183 stmt.bindByIndex(0, 2); 184 Assert.ok(stmt.executeStep()); 185 186 var count = { value: 0 }; 187 var arr = { value: null }; 188 stmt.getBlob(0, count, arr); 189 Assert.equal(2, count.value); 190 Assert.equal(1, arr.value[0]); 191 Assert.equal(2, arr.value[1]); 192 stmt.reset(); 193 stmt.finalize(); 194 });