test_add_put.js (5851B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 6 /* exported testGenerator */ 7 var testGenerator = testSteps(); 8 9 function* testSteps() { 10 const name = this.window ? window.location.pathname : "Splendid Test"; 11 let openRequest = indexedDB.open(name, 1); 12 openRequest.onerror = errorHandler; 13 openRequest.onupgradeneeded = grabEventAndContinueHandler; 14 openRequest.onsuccess = unexpectedSuccessHandler; 15 let event = yield undefined; 16 let db = event.target.result; 17 18 for (let autoincrement of [true, false]) { 19 for (let keypath of [false, true, "missing", "invalid"]) { 20 for (let method of ["put", "add"]) { 21 for (let explicit of [true, false, undefined, "invalid"]) { 22 for (let existing of [true, false]) { 23 let speccedNoKey = (!keypath || keypath == "missing") && !explicit; 24 25 // We can't do 'existing' checks if we use autogenerated key 26 if (speccedNoKey && autoincrement && existing) { 27 continue; 28 } 29 30 // Create store 31 if (db.objectStoreNames.contains("mystore")) { 32 db.deleteObjectStore("mystore"); 33 } 34 let store = db.createObjectStore("mystore", { 35 autoIncrement: autoincrement, 36 keyPath: keypath ? "id" : null, 37 }); 38 39 let test = 40 " for test " + 41 JSON.stringify({ 42 autoincrement, 43 keypath, 44 method, 45 explicit: explicit === undefined ? "undefined" : explicit, 46 existing, 47 }); 48 49 // Insert "existing" data if needed 50 if (existing) { 51 if (keypath) { 52 store.add({ 53 existing: "data", 54 id: 5, 55 }).onsuccess = grabEventAndContinueHandler; 56 } else { 57 store.add({ existing: "data" }, 5).onsuccess = 58 grabEventAndContinueHandler; 59 } 60 61 let e = yield undefined; 62 is(e.type, "success", "success inserting existing" + test); 63 is(e.target.result, 5, "inserted correct key" + test); 64 } 65 66 // Set up value to be inserted 67 let value = { theObj: true }; 68 if (keypath === true) { 69 value.id = 5; 70 } else if (keypath === "invalid") { 71 value.id = /x/; 72 } 73 74 // Which arguments are passed to function 75 let args = [value]; 76 if (explicit === true) { 77 args.push(5); 78 } else if (explicit === undefined) { 79 args.push(undefined); 80 } else if (explicit === "invalid") { 81 args.push(/x/); 82 } 83 84 let expected = expectedResult( 85 method, 86 keypath, 87 explicit, 88 autoincrement, 89 existing 90 ); 91 92 let valueJSON = JSON.stringify(value); 93 94 ok(true, "making call" + test); 95 96 // Make function call for throwing functions 97 if (expected === "throw") { 98 try { 99 store[method].apply(store, args); 100 ok(false, "should have thrown" + test); 101 } catch (ex) { 102 ok(true, "did throw" + test); 103 ok(ex instanceof DOMException, "Got a DOMException" + test); 104 is(ex.name, "DataError", "expect a DataError" + test); 105 is(ex.code, 0, "expect zero" + test); 106 is( 107 JSON.stringify(value), 108 valueJSON, 109 "call didn't modify value" + test 110 ); 111 } 112 continue; 113 } 114 115 // Make non-throwing function call 116 let req = store[method].apply(store, args); 117 is( 118 JSON.stringify(value), 119 valueJSON, 120 "call didn't modify value" + test 121 ); 122 123 req.onsuccess = req.onerror = grabEventAndContinueHandler; 124 let e = yield undefined; 125 126 // Figure out what key we used 127 let key = 5; 128 if (autoincrement && speccedNoKey) { 129 key = 1; 130 } 131 132 // Adjust value if expected 133 if (autoincrement && keypath && speccedNoKey) { 134 value.id = key; 135 } 136 137 // Check result 138 if (expected === "error") { 139 is(e.type, "error", "write should fail" + test); 140 e.preventDefault(); 141 e.stopPropagation(); 142 continue; 143 } 144 145 is(e.type, "success", "write should succeed" + test); 146 is(e.target.result, key, "write should return correct key" + test); 147 148 store.get(key).onsuccess = grabEventAndContinueHandler; 149 e = yield undefined; 150 is(e.type, "success", "read back should succeed" + test); 151 is( 152 JSON.stringify(e.target.result), 153 JSON.stringify(value), 154 "read back should return correct value" + test 155 ); 156 } 157 } 158 } 159 } 160 } 161 162 function expectedResult(method, keypath, explicit, autoincrement, existing) { 163 if (keypath && explicit) { 164 return "throw"; 165 } 166 if (!keypath && !explicit && !autoincrement) { 167 return "throw"; 168 } 169 if (keypath == "invalid") { 170 return "throw"; 171 } 172 if (keypath == "missing" && !autoincrement) { 173 return "throw"; 174 } 175 if (explicit == "invalid") { 176 return "throw"; 177 } 178 179 if (method == "add" && existing) { 180 return "error"; 181 } 182 183 return "success"; 184 } 185 186 openRequest.onsuccess = grabEventAndContinueHandler; 187 yield undefined; 188 189 finishTest(); 190 }