OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <meta charset="utf-8"> |
| 3 <title>Key Generator behavior with explicit keys and value injection</title> |
| 4 <link rel=help href="https://w3c.github.io/IndexedDB/#inject-key-into-value"> |
| 5 <script src="/resources/testharness.js"></script> |
| 6 <script src="/resources/testharnessreport.js"></script> |
| 7 <script src="support.js"></script> |
| 8 <script> |
| 9 |
| 10 indexeddb_test( |
| 11 (t, db) => { |
| 12 db.createObjectStore('store', {autoIncrement: true, keyPath: 'id'}); |
| 13 }, |
| 14 (t, db) => { |
| 15 const tx = db.transaction('store', 'readwrite'); |
| 16 t.onabort = t.unreached_func('transaction should not abort'); |
| 17 |
| 18 const store = tx.objectStore('store'); |
| 19 |
| 20 store.put({name: 'n'}).onsuccess = t.step_func(e => { |
| 21 const key = e.target.result; |
| 22 assert_equals(key, 1, 'Key generator initial value should be 1'); |
| 23 store.get(key).onsuccess = t.step_func(e => { |
| 24 const value = e.target.result; |
| 25 assert_equals(typeof value, 'object', 'Result should be object'); |
| 26 assert_equals(value.name, 'n', 'Result should have name property'); |
| 27 assert_equals(value.id, key, 'Key should be injected'); |
| 28 t.done(); |
| 29 }); |
| 30 }); |
| 31 }, |
| 32 'Key is injected into value - single segment path'); |
| 33 |
| 34 indexeddb_test( |
| 35 (t, db) => { |
| 36 db.createObjectStore('store', {autoIncrement: true, keyPath: 'a.b.id'}); |
| 37 }, |
| 38 (t, db) => { |
| 39 const tx = db.transaction('store', 'readwrite'); |
| 40 t.onabort = t.unreached_func('transaction should not abort'); |
| 41 |
| 42 const store = tx.objectStore('store'); |
| 43 |
| 44 store.put({name: 'n'}).onsuccess = t.step_func(e => { |
| 45 const key = e.target.result; |
| 46 assert_equals(key, 1, 'Key generator initial value should be 1'); |
| 47 store.get(key).onsuccess = t.step_func(e => { |
| 48 const value = e.target.result; |
| 49 assert_equals(typeof value, 'object', 'Result should be object'); |
| 50 assert_equals(value.name, 'n', 'Result should have name property'); |
| 51 assert_equals(value.a.b.id, key, 'Key should be injected'); |
| 52 t.done(); |
| 53 }); |
| 54 }); |
| 55 }, |
| 56 'Key is injected into value - multi-segment path'); |
| 57 |
| 58 indexeddb_test( |
| 59 (t, db) => { |
| 60 db.createObjectStore('store', {autoIncrement: true, keyPath: 'a.b.id'}); |
| 61 }, |
| 62 (t, db) => { |
| 63 const tx = db.transaction('store', 'readwrite'); |
| 64 t.onabort = t.unreached_func('transaction should not abort'); |
| 65 |
| 66 const store = tx.objectStore('store'); |
| 67 |
| 68 store.put({name: 'n1', b: {name: 'n2'}}).onsuccess = t.step_func(e => { |
| 69 const key = e.target.result; |
| 70 assert_equals(key, 1, 'Key generator initial value should be 1'); |
| 71 store.get(key).onsuccess = t.step_func(e => { |
| 72 const value = e.target.result; |
| 73 assert_equals(typeof value, 'object', 'Result should be object'); |
| 74 assert_equals(value.name, 'n1', 'Result should have name property'); |
| 75 assert_equals(value.b.name, 'n2', 'Result should have name property'); |
| 76 assert_equals(value.a.b.id, key, 'Key should be injected'); |
| 77 t.done(); |
| 78 }); |
| 79 }); |
| 80 }, |
| 81 'Key is injected into value - multi-segment path, partially populated'); |
| 82 |
| 83 indexeddb_test( |
| 84 (t, db) => { |
| 85 db.createObjectStore('store', {autoIncrement: true, keyPath: 'id'}); |
| 86 }, |
| 87 (t, db) => { |
| 88 const tx = db.transaction('store', 'readwrite'); |
| 89 const store = tx.objectStore('store'); |
| 90 |
| 91 assert_throws('DataError', () => { |
| 92 store.put(123); |
| 93 }, 'Key path should be checked against value'); |
| 94 |
| 95 t.done(); |
| 96 }, |
| 97 'put() throws if key cannot be injected - single segment path'); |
| 98 |
| 99 indexeddb_test( |
| 100 (t, db) => { |
| 101 db.createObjectStore('store', {autoIncrement: true, keyPath: 'a.b.id'}); |
| 102 }, |
| 103 (t, db) => { |
| 104 const tx = db.transaction('store', 'readwrite'); |
| 105 const store = tx.objectStore('store'); |
| 106 |
| 107 assert_throws('DataError', () => { |
| 108 store.put({a: 123}); |
| 109 }, 'Key path should be checked against value'); |
| 110 |
| 111 assert_throws('DataError', () => { |
| 112 store.put({a: {b: 123} }); |
| 113 }, 'Key path should be checked against value'); |
| 114 |
| 115 t.done(); |
| 116 }, |
| 117 'put() throws if key cannot be injected - multi-segment path'); |
| 118 |
| 119 </script> |
OLD | NEW |