idbindex_getKey.any.js (5453B)
1 // META: global=window,worker 2 // META: title=IDBIndex.getKey() 3 // META: script=resources/support.js 4 // @author Microsoft <https://www.microsoft.com> 5 // @author Intel <http://www.intel.com> 6 'use strict'; 7 8 9 async_test(t => { 10 let db; 11 const record = { key: 1, indexedProperty: "data" }; 12 13 const open_rq = createdb(t); 14 open_rq.onupgradeneeded = function(e) { 15 db = e.target.result; 16 const objStore = db.createObjectStore("test", { keyPath: "key" }); 17 objStore.createIndex("index", "indexedProperty"); 18 19 objStore.add(record); 20 }; 21 22 open_rq.onsuccess = function(e) { 23 let rq = db.transaction("test", "readonly") 24 .objectStore("test"); 25 26 rq = rq.index("index"); 27 28 rq = rq.getKey("data"); 29 30 rq.onsuccess = t.step_func(function(e) { 31 assert_equals(e.target.result, record.key); 32 t.done(); 33 }); 34 }; 35 }, 'getKey() returns the record\'s primary key'); 36 37 async_test(t => { 38 let db; 39 const records = [ 40 { key: 1, indexedProperty: "data" }, 41 { key: 2, indexedProperty: "data" }, 42 { key: 3, indexedProperty: "data" } 43 ]; 44 45 const open_rq = createdb(t); 46 open_rq.onupgradeneeded = function(e) { 47 db = e.target.result; 48 var objStore = db.createObjectStore("test", { keyPath: "key" }); 49 objStore.createIndex("index", "indexedProperty"); 50 51 for (let i = 0; i < records.length; i++) 52 objStore.add(records[i]); 53 }; 54 55 open_rq.onsuccess = function(e) { 56 const rq = db.transaction("test", "readonly") 57 .objectStore("test") 58 .index("index") 59 .getKey("data"); 60 61 rq.onsuccess = t.step_func(function(e) { 62 assert_equals(e.target.result, records[0].key); 63 t.done(); 64 }); 65 }; 66 }, 'getKey() returns the record\'s primary key where the index contains duplicate values'); 67 68 async_test(t => { 69 let db; 70 const open_rq = createdb(t); 71 72 open_rq.onupgradeneeded = function(e) { 73 db = e.target.result; 74 const rq = db.createObjectStore("test", { keyPath: "key" }) 75 .createIndex("index", "indexedProperty") 76 .getKey(1); 77 78 rq.onsuccess = t.step_func(function(e) { 79 assert_equals(e.target.result, undefined); 80 t.done(); 81 }); 82 }; 83 }, 'getKey() attempt to retrieve the primary key of a record that doesn\'t exist'); 84 85 async_test(t => { 86 let db; 87 88 const open_rq = createdb(t); 89 90 open_rq.onupgradeneeded = function(e) { 91 db = e.target.result; 92 const store = db.createObjectStore("store", { keyPath: "key" }); 93 store.createIndex("index", "indexedProperty"); 94 95 for (let i = 0; i < 10; i++) { 96 store.add({ key: i, indexedProperty: "data" + i }); 97 } 98 }; 99 100 open_rq.onsuccess = function(e) { 101 const rq = db.transaction("store", "readonly") 102 .objectStore("store") 103 .index("index") 104 .getKey(IDBKeyRange.bound('data4', 'data7')); 105 106 rq.onsuccess = t.step_func(function(e) { 107 assert_equals(e.target.result, 4); 108 109 step_timeout(function () { t.done(); }, 4) 110 }); 111 }; 112 }, 'getKey() returns the key of the first record within the range'); 113 114 async_test(t => { 115 let db; 116 const open_rq = createdb(t); 117 118 open_rq.onupgradeneeded = function(e) { 119 db = e.target.result; 120 121 const index = db.createObjectStore("test", { keyPath: "key" }) 122 .createIndex("index", "indexedProperty"); 123 124 assert_throws_dom("DataError", function () { 125 index.getKey(NaN); 126 }); 127 t.done(); 128 }; 129 }, 'getKey() throws DataError when using invalid key'); 130 131 async_test(t => { 132 let db; 133 const open_rq = createdb(t); 134 135 open_rq.onupgradeneeded = function(e) { 136 db = e.target.result; 137 const store = db.createObjectStore("store", { keyPath: "key" }); 138 const index = store.createIndex("index", "indexedProperty"); 139 140 store.add({ key: 1, indexedProperty: "data" }); 141 store.deleteIndex("index"); 142 143 assert_throws_dom("InvalidStateError", function () { 144 index.getKey("data"); 145 }); 146 t.done(); 147 }; 148 }, 'getKey() throws InvalidStateError when the index is deleted'); 149 150 async_test(t => { 151 let db; 152 153 const open_rq = createdb(t); 154 open_rq.onupgradeneeded = function(e) { 155 db = e.target.result; 156 const store = db.createObjectStore("store", { keyPath: "key" }); 157 const index = store.createIndex("index", "indexedProperty"); 158 store.add({ key: 1, indexedProperty: "data" }); 159 }; 160 161 open_rq.onsuccess = function(e) { 162 db = e.target.result; 163 const tx = db.transaction('store', 'readonly'); 164 const index = tx.objectStore('store').index('index'); 165 tx.abort(); 166 167 assert_throws_dom("TransactionInactiveError", function () { 168 index.getKey("data"); 169 }); 170 t.done(); 171 }; 172 }, 'getKey() throws TransactionInactiveError on aborted transaction'); 173 174 async_test(t => { 175 let db; 176 177 const open_rq = createdb(t); 178 open_rq.onupgradeneeded = function(e) { 179 db = e.target.result; 180 const store = db.createObjectStore("store", { keyPath: "key" }); 181 const index = store.createIndex("index", "indexedProperty"); 182 store.add({ key: 1, indexedProperty: "data" }); 183 184 e.target.transaction.abort(); 185 186 assert_throws_dom("InvalidStateError", function () { 187 index.getKey("data"); 188 }); 189 t.done(); 190 }; 191 }, 'getKey() throws InvalidStateError on index deleted by aborted upgrade');