idbindex_count.any.js (3029B)
1 // META: global=window,worker 2 // META: title=IDBIndex.count() 3 // META: script=resources/support.js 4 // @author Microsoft <https://www.microsoft.com> 5 // @author Odin Hørthe Omdal <mailto:odinho@opera.com> 6 // @author Intel <http://www.intel.com> 7 'use strict'; 8 9 10 async_test(t => { 11 let db; 12 13 const open_rq = createdb(t); 14 open_rq.onupgradeneeded = function(e) { 15 db = e.target.result; 16 const store = db.createObjectStore("store", { autoIncrement: true }); 17 store.createIndex("index", "indexedProperty"); 18 for (let i = 0; i < 10; i++) { 19 store.add({ indexedProperty: "data" + i }); 20 } 21 }; 22 23 open_rq.onsuccess = function(e) { 24 const rq = db.transaction("store", "readonly") 25 .objectStore("store") 26 .index("index") 27 .count(); 28 29 rq.onsuccess = t.step_func(function(e) { 30 assert_equals(e.target.result, 10); 31 t.done(); 32 }); 33 }; 34 }, 'count() returns the number of records in the index'); 35 36 async_test(t => { 37 let db; 38 39 const open_rq = createdb(t); 40 open_rq.onupgradeneeded = function(e) { 41 db = e.target.result; 42 const store = db.createObjectStore("store", { autoIncrement: true }); 43 store.createIndex("index", "indexedProperty"); 44 45 for (let i = 0; i < 10; i++) { 46 store.add({ indexedProperty: "data" + i }); 47 } 48 }; 49 50 open_rq.onsuccess = function(e) { 51 const rq = db.transaction("store", "readonly") 52 .objectStore("store") 53 .index("index") 54 .count(IDBKeyRange.bound('data0', 'data4')); 55 56 rq.onsuccess = t.step_func(function(e) { 57 assert_equals(e.target.result, 5); 58 t.done(); 59 }); 60 }; 61 }, 'count() returns the number of records that have keys within the range'); 62 63 async_test(t => { 64 let db; 65 66 const open_rq = createdb(t); 67 open_rq.onupgradeneeded = function(e) { 68 db = e.target.result; 69 70 const store = db.createObjectStore("store", { autoIncrement: true }); 71 store.createIndex("myindex", "idx"); 72 73 for (let i = 0; i < 10; i++) 74 store.add({ idx: "data_" + (i%2) }); 75 76 store.index("myindex").count("data_0").onsuccess = t.step_func(function(e) { 77 assert_equals(e.target.result, 5, "count(data_0)"); 78 t.done(); 79 }); 80 }; 81 }, 'count() returns the number of records that have keys with the key'); 82 83 async_test(t => { 84 let db; 85 86 const open_rq = createdb(t); 87 open_rq.onupgradeneeded = function(e) { 88 db = e.target.result; 89 const store = db.createObjectStore("store", { autoIncrement: true }); 90 store.createIndex("index", "indexedProperty"); 91 92 for (let i = 0; i < 10; i++) { 93 store.add({ indexedProperty: "data" + i }); 94 } 95 }; 96 97 open_rq.onsuccess = function(e) { 98 const index = db.transaction("store", "readonly") 99 .objectStore("store") 100 .index("index"); 101 102 assert_throws_dom("DataError", function () { 103 index.count(NaN); 104 }); 105 106 t.done(); 107 }; 108 }, 'count() throws DataError when using invalid key');