test_indexes_bad_values.js (3916B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 var testGenerator = testSteps(); 7 8 function* testSteps() { 9 const name = this.window ? window.location.pathname : "Splendid Test"; 10 11 const objectStoreName = "People"; 12 13 const objectStoreData = [ 14 { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, 15 { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, 16 { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }, 17 { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, 18 { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, 19 { key: "237-23-7737", value: { name: "Pat", height: 65 } }, 20 { key: "237-23-7738", value: { name: "Mel", height: 66, weight: {} } }, 21 ]; 22 23 const badObjectStoreData = [ 24 { key: "237-23-7739", value: { name: "Rob", height: 65 } }, 25 { key: "237-23-7740", value: { name: "Jen", height: 66, weight: {} } }, 26 ]; 27 28 const indexData = [ 29 { name: "weight", keyPath: "weight", options: { unique: false } }, 30 ]; 31 32 const objectStoreDataWeightSort = [ 33 { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, 34 { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, 35 { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, 36 { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, 37 { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }, 38 ]; 39 40 let request = indexedDB.open(name, 1); 41 request.onerror = errorHandler; 42 request.onupgradeneeded = grabEventAndContinueHandler; 43 request.onsuccess = grabEventAndContinueHandler; 44 let event = yield undefined; 45 46 let db = event.target.result; 47 48 let objectStore = db.createObjectStore(objectStoreName, {}); 49 50 let addedData = 0; 51 for (let i in objectStoreData) { 52 request = objectStore.add(objectStoreData[i].value, objectStoreData[i].key); 53 request.onerror = errorHandler; 54 request.onsuccess = function (event) { 55 if (++addedData == objectStoreData.length) { 56 testGenerator.next(event); 57 } 58 }; 59 } 60 event = yield undefined; 61 62 for (let i in indexData) { 63 objectStore.createIndex( 64 indexData[i].name, 65 indexData[i].keyPath, 66 indexData[i].options 67 ); 68 } 69 70 addedData = 0; 71 for (let i in badObjectStoreData) { 72 request = objectStore.add( 73 badObjectStoreData[i].value, 74 badObjectStoreData[i].key 75 ); 76 request.onerror = errorHandler; 77 request.onsuccess = function () { 78 if (++addedData == badObjectStoreData.length) { 79 executeSoon(function () { 80 testGenerator.next(); 81 }); 82 } 83 }; 84 } 85 yield undefined; 86 yield undefined; 87 88 objectStore = db.transaction(objectStoreName).objectStore(objectStoreName); 89 90 let keyIndex = 0; 91 92 request = objectStore.index("weight").openKeyCursor(); 93 request.onerror = errorHandler; 94 request.onsuccess = function (event) { 95 let cursor = event.target.result; 96 if (cursor) { 97 is( 98 cursor.key, 99 objectStoreDataWeightSort[keyIndex].value.weight, 100 "Correct key" 101 ); 102 is( 103 cursor.primaryKey, 104 objectStoreDataWeightSort[keyIndex].key, 105 "Correct value" 106 ); 107 keyIndex++; 108 109 cursor.continue(); 110 } else { 111 testGenerator.next(); 112 } 113 }; 114 yield undefined; 115 116 is(keyIndex, objectStoreDataWeightSort.length, "Saw all weights"); 117 118 keyIndex = 0; 119 120 request = objectStore.openCursor(); 121 request.onerror = errorHandler; 122 request.onsuccess = function (event) { 123 let cursor = event.target.result; 124 if (cursor) { 125 keyIndex++; 126 cursor.continue(); 127 } else { 128 testGenerator.next(); 129 } 130 }; 131 yield undefined; 132 133 is( 134 keyIndex, 135 objectStoreData.length + badObjectStoreData.length, 136 "Saw all people" 137 ); 138 139 finishTest(); 140 }