test_create_index.js (4167B)
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 const objectStoreInfo = [ 12 { name: "a", options: { keyPath: "id", autoIncrement: true } }, 13 { name: "b", options: { keyPath: "id", autoIncrement: false } }, 14 ]; 15 const indexInfo = [ 16 { name: "1", keyPath: "unique_value", options: { unique: true } }, 17 { name: "2", keyPath: "value", options: { unique: false } }, 18 { name: "3", keyPath: "value", options: { unique: false } }, 19 { name: "", keyPath: "value", options: { unique: false } }, 20 { name: null, keyPath: "value", options: { unique: false } }, 21 { name: undefined, keyPath: "value", options: { unique: false } }, 22 ]; 23 24 let request = indexedDB.open(name, 1); 25 request.onerror = errorHandler; 26 request.onupgradeneeded = grabEventAndContinueHandler; 27 request.onsuccess = unexpectedSuccessHandler; 28 let event = yield undefined; 29 let db = event.target.result; 30 31 for (let i = 0; i < objectStoreInfo.length; i++) { 32 let info = objectStoreInfo[i]; 33 let objectStore = info.hasOwnProperty("options") 34 ? db.createObjectStore(info.name, info.options) 35 : db.createObjectStore(info.name); 36 37 try { 38 request = objectStore.createIndex("Hola"); 39 ok(false, "createIndex with no keyPath should throw"); 40 } catch (e) { 41 ok(true, "createIndex with no keyPath should throw"); 42 } 43 44 let ex; 45 try { 46 objectStore.createIndex("Hola", ["foo"], { multiEntry: true }); 47 } catch (e) { 48 ex = e; 49 } 50 ok(ex, "createIndex with array keyPath and multiEntry should throw"); 51 is(ex.name, "InvalidAccessError", "should throw right exception"); 52 ok(ex instanceof DOMException, "should throw right exception"); 53 is( 54 ex.code, 55 DOMException.INVALID_ACCESS_ERR, 56 "should throw right exception" 57 ); 58 59 try { 60 objectStore.createIndex("foo", "bar", 10); 61 ok(false, "createIndex with bad options should throw"); 62 } catch (e) { 63 ok(true, "createIndex with bad options threw"); 64 } 65 66 ok( 67 objectStore.createIndex("foo", "bar", { foo: "" }), 68 "createIndex with unknown options should not throw" 69 ); 70 objectStore.deleteIndex("foo"); 71 72 // Test index creation, and that it ends up in indexNames. 73 let objectStoreName = info.name; 74 for (let j = 0; j < indexInfo.length; j++) { 75 let info = indexInfo[j]; 76 let count = objectStore.indexNames.length; 77 let index = info.hasOwnProperty("options") 78 ? objectStore.createIndex(info.name, info.keyPath, info.options) 79 : objectStore.createIndex(info.name, info.keyPath); 80 81 let name = info.name; 82 if (name === null) { 83 name = "null"; 84 } else if (name === undefined) { 85 name = "undefined"; 86 } 87 88 is(index.name, name, "correct name"); 89 is(index.keyPath, info.keyPath, "correct keyPath"); 90 is(index.unique, info.options.unique, "correct uniqueness"); 91 92 is(objectStore.indexNames.length, count + 1, "indexNames grew in size"); 93 let found = false; 94 for (let k = 0; k < objectStore.indexNames.length; k++) { 95 if (objectStore.indexNames.item(k) == name) { 96 found = true; 97 break; 98 } 99 } 100 ok(found, "Name is on objectStore.indexNames"); 101 102 ok(event.target.transaction, "event has a transaction"); 103 ok(event.target.transaction.db === db, "transaction has the right db"); 104 is( 105 event.target.transaction.mode, 106 "versionchange", 107 "transaction has the correct mode" 108 ); 109 is( 110 event.target.transaction.objectStoreNames.length, 111 i + 1, 112 "transaction only has one object store" 113 ); 114 ok( 115 event.target.transaction.objectStoreNames.contains(objectStoreName), 116 "transaction has the correct object store" 117 ); 118 } 119 } 120 121 request.onsuccess = grabEventAndContinueHandler; 122 request.onupgradeneeded = unexpectedSuccessHandler; 123 124 event = yield undefined; 125 126 finishTest(); 127 }