idbobjectstore_get.any.js (3484B)
1 // META: global=window,worker 2 // META: title=IDBObjectStore.get() 3 // META: script=resources/support.js 4 5 "use strict"; 6 7 function createDbRecordAndValidate(record, t) { 8 const openRequest = createdb(t); 9 10 openRequest.onupgradeneeded = t.step_func(event => { 11 const db = event.target.result; 12 const store = db.createObjectStore('store', {keyPath: 'key'}); 13 store.add(record); 14 15 openRequest.onsuccess = t.step_func(event => { 16 const rq = db.transaction('store', 'readonly') 17 .objectStore('store') 18 .get(record.key); 19 20 rq.onsuccess = t.step_func(event => { 21 const result = event.target.result; 22 assert_equals(result.key.valueOf(), result.key.valueOf()); 23 assert_equals(result.property, record.property); 24 t.done(); 25 }); 26 }); 27 }); 28 } 29 30 async_test(t => { 31 const record = {key: 3.14159265, property: 'data'}; 32 createDbRecordAndValidate(record, t); 33 }, 'Key is a number'); 34 35 async_test(t => { 36 const record = {key: 'this is a key that\'s a string', property: 'data'}; 37 createDbRecordAndValidate(record, t); 38 }, 'Key is a string'); 39 40 async_test(t => { 41 const record = {key: new Date(), property: 'data'}; 42 createDbRecordAndValidate(record, t); 43 }, 'Key is a date'); 44 45 async_test(t => { 46 const open_rq = createdb(t); 47 48 open_rq.onupgradeneeded = t.step_func(event => { 49 const db = event.target.result; 50 const rq = db.createObjectStore('store', {keyPath: 'key'}).get(1); 51 52 rq.onsuccess = t.step_func(event => { 53 assert_equals(event.target.result, undefined); 54 t.done(); 55 }); 56 }); 57 }, 'Attempts to retrieve a record that doesn\'t exist'); 58 59 async_test(t => { 60 let db; 61 const open_rq = createdb(t); 62 63 open_rq.onupgradeneeded = t.step_func(event => { 64 db = event.target.result; 65 const os = db.createObjectStore('store'); 66 67 for (let i = 0; i < 10; i++) { 68 os.add(`data${i}`, i); 69 } 70 }); 71 72 open_rq.onsuccess = t.step_func(event => { 73 const rq = db.transaction('store', 'readonly') 74 .objectStore('store') 75 .get(IDBKeyRange.bound(3, 6)); 76 77 rq.onsuccess = t.step_func(event => { 78 assert_equals(event.target.result, 'data3', 'get(3-6)'); 79 t.done(); 80 }); 81 }); 82 }, 'Returns the record with the first key in the range'); 83 84 async_test(t => { 85 let db; 86 const open_rq = createdb(t); 87 88 open_rq.onupgradeneeded = t.step_func(event => { 89 db = event.target.result; 90 db.createObjectStore('store', {keyPath: 'key'}); 91 }); 92 93 open_rq.onsuccess = t.step_func(event => { 94 const store = db.transaction('store', 'readonly').objectStore('store'); 95 96 // Abort the transaction immediately. 97 store.transaction.abort(); 98 99 // Accessing the store after the transaction aborts must throw 100 // TransactionInactiveError. 101 assert_throws_dom('TransactionInactiveError', () => { 102 store.get(1); 103 }); 104 105 t.done(); 106 }); 107 }, 'When a transaction is aborted, throw TransactionInactiveError'); 108 109 async_test(t => { 110 let db; 111 const open_rq = createdb(t); 112 113 open_rq.onupgradeneeded = t.step_func(event => { 114 db = event.target.result; 115 db.createObjectStore('store', {keyPath: 'key'}); 116 }); 117 118 open_rq.onsuccess = t.step_func(event => { 119 const store = db.transaction('store', 'readonly').objectStore('store'); 120 121 // Attempt to use an invalid key (null) 122 assert_throws_dom('DataError', () => { 123 store.get(null); 124 }); 125 126 t.done(); 127 }); 128 }, 'When an invalid key is used, throw DataError');