michael@0: /** michael@0: * Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: var testGenerator = testSteps(); michael@0: michael@0: function testSteps() michael@0: { michael@0: // Test object stores michael@0: michael@0: const name = "test_complex_keyPaths"; michael@0: const keyPaths = [ michael@0: { keyPath: "id", value: { id: 5 }, key: 5 }, michael@0: { keyPath: "id", value: { id: "14", iid: 12 }, key: "14" }, michael@0: { keyPath: "id", value: { iid: "14", id: 12 }, key: 12 }, michael@0: { keyPath: "id", value: {} }, michael@0: { keyPath: "id", value: { id: {} } }, michael@0: { keyPath: "id", value: { id: /x/ } }, michael@0: { keyPath: "id", value: 2 }, michael@0: { keyPath: "id", value: undefined }, michael@0: { keyPath: "foo.id", value: { foo: { id: 7 } }, key: 7 }, michael@0: { keyPath: "foo.id", value: { id: 7, foo: { id: "asdf" } }, key: "asdf" }, michael@0: { keyPath: "foo.id", value: { foo: { id: undefined } } }, michael@0: { keyPath: "foo.id", value: { foo: 47 } }, michael@0: { keyPath: "foo.id", value: {} }, michael@0: { keyPath: "", value: "foopy", key: "foopy" }, michael@0: { keyPath: "", value: 2, key: 2 }, michael@0: { keyPath: "", value: undefined }, michael@0: { keyPath: "", value: { id: 12 } }, michael@0: { keyPath: "", value: /x/ }, michael@0: { keyPath: "foo.bar", value: { baz: 1, foo: { baz2: 2, bar: "xo" } }, key: "xo" }, michael@0: { keyPath: "foo.bar.baz", value: { foo: { bar: { bazz: 16, baz: 17 } } }, key: 17 }, michael@0: { keyPath: "foo..id", exception: true }, michael@0: { keyPath: "foo.", exception: true }, michael@0: { keyPath: "fo o", exception: true }, michael@0: { keyPath: "foo ", exception: true }, michael@0: { keyPath: "foo[bar]",exception: true }, michael@0: { keyPath: "foo[1]", exception: true }, michael@0: { keyPath: "$('id').stuff", exception: true }, michael@0: { keyPath: "foo.2.bar", exception: true }, michael@0: { keyPath: "foo. .bar", exception: true }, michael@0: { keyPath: ".bar", exception: true }, michael@0: { keyPath: [], exception: true }, michael@0: michael@0: { keyPath: ["foo", "bar"], value: { foo: 1, bar: 2 }, key: [1, 2] }, michael@0: { keyPath: ["foo"], value: { foo: 1, bar: 2 }, key: [1] }, michael@0: { keyPath: ["foo", "bar", "bar"], value: { foo: 1, bar: "x" }, key: [1, "x", "x"] }, michael@0: { keyPath: ["x", "y"], value: { x: [], y: "x" }, key: [[], "x"] }, michael@0: { keyPath: ["x", "y"], value: { x: [[1]], y: "x" }, key: [[[1]], "x"] }, michael@0: { keyPath: ["x", "y"], value: { x: [[1]], y: new Date(1) }, key: [[[1]], new Date(1)] }, michael@0: { keyPath: ["x", "y"], value: { x: [[1]], y: [new Date(3)] }, key: [[[1]], [new Date(3)]] }, michael@0: { keyPath: ["x", "y.bar"], value: { x: "hi", y: { bar: "x"} }, key: ["hi", "x"] }, michael@0: { keyPath: ["x.y", "y.bar"], value: { x: { y: "hello" }, y: { bar: "nurse"} }, key: ["hello", "nurse"] }, michael@0: { keyPath: ["", ""], value: 5, key: [5, 5] }, michael@0: { keyPath: ["x", "y"], value: { x: 1 } }, michael@0: { keyPath: ["x", "y"], value: { y: 1 } }, michael@0: { keyPath: ["x", "y"], value: { x: 1, y: undefined } }, michael@0: { keyPath: ["x", "y"], value: { x: null, y: 1 } }, michael@0: { keyPath: ["x", "y.bar"], value: { x: null, y: { bar: "x"} } }, michael@0: { keyPath: ["x", "y"], value: { x: 1, y: false } }, michael@0: { keyPath: ["x", "y", "z"], value: { x: 1, y: false, z: "a" } }, michael@0: { keyPath: [".x", "y", "z"], exception: true }, michael@0: { keyPath: ["x", "y ", "z"], exception: true }, michael@0: ]; michael@0: michael@0: let openRequest = indexedDB.open(name, 1); michael@0: openRequest.onerror = errorHandler; michael@0: openRequest.onupgradeneeded = grabEventAndContinueHandler; michael@0: openRequest.onsuccess = unexpectedSuccessHandler; michael@0: let event = yield undefined; michael@0: let db = event.target.result; michael@0: michael@0: let stores = {}; michael@0: michael@0: // Test creating object stores and inserting data michael@0: for (let i = 0; i < keyPaths.length; i++) { michael@0: let info = keyPaths[i]; michael@0: michael@0: let test = " for objectStore test " + JSON.stringify(info); michael@0: let indexName = JSON.stringify(info.keyPath); michael@0: if (!stores[indexName]) { michael@0: try { michael@0: let objectStore = db.createObjectStore(indexName, { keyPath: info.keyPath }); michael@0: ok(!("exception" in info), "shouldn't throw" + test); michael@0: is(JSON.stringify(objectStore.keyPath), JSON.stringify(info.keyPath), michael@0: "correct keyPath property" + test); michael@0: ok(objectStore.keyPath === objectStore.keyPath, michael@0: "object identity should be preserved"); michael@0: stores[indexName] = objectStore; michael@0: } catch (e) { michael@0: ok("exception" in info, "should throw" + test); michael@0: is(e.name, "SyntaxError", "expect a SyntaxError" + test); michael@0: ok(e instanceof DOMException, "Got a DOM Exception" + test); michael@0: is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: let store = stores[indexName]; michael@0: michael@0: try { michael@0: request = store.add(info.value); michael@0: ok("key" in info, "successfully created request to insert value" + test); michael@0: } catch (e) { michael@0: ok(!("key" in info), "threw when attempted to insert" + test); michael@0: ok(e instanceof DOMException, "Got a DOMException" + test); michael@0: is(e.name, "DataError", "expect a DataError" + test); michael@0: is(e.code, 0, "expect zero" + test); michael@0: continue; michael@0: } michael@0: michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = grabEventAndContinueHandler; michael@0: michael@0: let e = yield undefined; michael@0: is(e.type, "success", "inserted successfully" + test); michael@0: is(e.target, request, "expected target" + test); michael@0: ok(compareKeys(request.result, info.key), "found correct key" + test); michael@0: is(indexedDB.cmp(request.result, info.key), 0, "returned key compares correctly" + test); michael@0: michael@0: store.get(info.key).onsuccess = grabEventAndContinueHandler; michael@0: e = yield undefined; michael@0: isnot(e.target.result, undefined, "Did find entry"); michael@0: michael@0: // Check that cursor.update work as expected michael@0: request = store.openCursor(); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = grabEventAndContinueHandler; michael@0: e = yield undefined; michael@0: let cursor = e.target.result; michael@0: request = cursor.update(info.value); michael@0: request.onerror = errorHandler; michael@0: request.onsuccess = grabEventAndContinueHandler; michael@0: yield undefined; michael@0: ok(true, "Successfully updated cursor" + test); michael@0: michael@0: // Check that cursor.update throws as expected when key is changed michael@0: let newValue = cursor.value; michael@0: let destProp = Array.isArray(info.keyPath) ? info.keyPath[0] : info.keyPath; michael@0: if (destProp) { michael@0: eval("newValue." + destProp + " = 'newKeyValue'"); michael@0: } michael@0: else { michael@0: newValue = 'newKeyValue'; michael@0: } michael@0: let didThrow; michael@0: try { michael@0: cursor.update(newValue); michael@0: } michael@0: catch (ex) { michael@0: didThrow = ex; michael@0: } michael@0: ok(didThrow instanceof DOMException, "Got a DOMException" + test); michael@0: is(didThrow.name, "DataError", "expect a DataError" + test); michael@0: is(didThrow.code, 0, "expect zero" + test); michael@0: michael@0: // Clear object store to prepare for next test michael@0: store.clear().onsuccess = grabEventAndContinueHandler; michael@0: yield undefined; michael@0: } michael@0: michael@0: // Attempt to create indexes and insert data michael@0: let store = db.createObjectStore("indexStore"); michael@0: let indexes = {}; michael@0: for (let i = 0; i < keyPaths.length; i++) { michael@0: let test = " for index test " + JSON.stringify(info); michael@0: let info = keyPaths[i]; michael@0: let indexName = JSON.stringify(info.keyPath); michael@0: if (!indexes[indexName]) { michael@0: try { michael@0: let index = store.createIndex(indexName, info.keyPath); michael@0: ok(!("exception" in info), "shouldn't throw" + test); michael@0: is(JSON.stringify(index.keyPath), JSON.stringify(info.keyPath), michael@0: "index has correct keyPath property" + test); michael@0: ok(index.keyPath === index.keyPath, michael@0: "object identity should be preserved"); michael@0: indexes[indexName] = index; michael@0: } catch (e) { michael@0: ok("exception" in info, "should throw" + test); michael@0: is(e.name, "SyntaxError", "expect a SyntaxError" + test); michael@0: ok(e instanceof DOMException, "Got a DOM Exception" + test); michael@0: is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: let index = indexes[indexName]; michael@0: michael@0: request = store.add(info.value, 1); michael@0: if ("key" in info) { michael@0: index.getKey(info.key).onsuccess = grabEventAndContinueHandler; michael@0: e = yield undefined; michael@0: is(e.target.result, 1, "found value when reading" + test); michael@0: } michael@0: else { michael@0: index.count().onsuccess = grabEventAndContinueHandler; michael@0: e = yield undefined; michael@0: is(e.target.result, 0, "should be empty" + test); michael@0: } michael@0: michael@0: store.clear().onsuccess = grabEventAndContinueHandler; michael@0: yield undefined; michael@0: } michael@0: michael@0: // Autoincrement and complex key paths michael@0: let aitests = [{ v: {}, k: 1, res: { foo: { id: 1 }} }, michael@0: { v: { value: "x" }, k: 2, res: { value: "x", foo: { id: 2 }} }, michael@0: { v: { value: "x", foo: {} }, k: 3, res: { value: "x", foo: { id: 3 }} }, michael@0: { v: { v: "x", foo: { x: "y" } }, k: 4, res: { v: "x", foo: { x: "y", id: 4 }} }, michael@0: { v: { value: 2, foo: { id: 10 }}, k: 10 }, michael@0: { v: { value: 2 }, k: 11, res: { value: 2, foo: { id: 11 }} }, michael@0: { v: true, }, michael@0: { v: { value: 2, foo: 12 }, }, michael@0: { v: { foo: { id: true }}, }, michael@0: { v: { foo: { x: 5, id: {} }}, }, michael@0: { v: undefined, }, michael@0: { v: { foo: undefined }, }, michael@0: { v: { foo: { id: undefined }}, }, michael@0: { v: null, }, michael@0: { v: { foo: null }, }, michael@0: { v: { foo: { id: null }}, }, michael@0: ]; michael@0: michael@0: store = db.createObjectStore("gen", { keyPath: "foo.id", autoIncrement: true }); michael@0: for (let i = 0; i < aitests.length; ++i) { michael@0: let info = aitests[i]; michael@0: let test = " for autoIncrement test " + JSON.stringify(info); michael@0: michael@0: let preValue = JSON.stringify(info.v); michael@0: if ("k" in info) { michael@0: store.add(info.v).onsuccess = grabEventAndContinueHandler; michael@0: is(JSON.stringify(info.v), preValue, "put didn't modify value" + test); michael@0: } michael@0: else { michael@0: try { michael@0: store.add(info.v); michael@0: ok(false, "should throw" + test); michael@0: } michael@0: catch(e) { michael@0: ok(true, "did throw" + test); michael@0: ok(e instanceof DOMException, "Got a DOMException" + test); michael@0: is(e.name, "DataError", "expect a DataError" + test); michael@0: is(e.code, 0, "expect zero" + test); michael@0: michael@0: is(JSON.stringify(info.v), preValue, "failing put didn't modify value" + test); michael@0: michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: let e = yield undefined; michael@0: is(e.target.result, info.k, "got correct return key" + test); michael@0: michael@0: store.get(info.k).onsuccess = grabEventAndContinueHandler; michael@0: e = yield undefined; michael@0: is(JSON.stringify(e.target.result), JSON.stringify(info.res || info.v), michael@0: "expected value stored" + test); michael@0: } michael@0: michael@0: openRequest.onsuccess = grabEventAndContinueHandler; michael@0: yield undefined; michael@0: michael@0: finishTest(); michael@0: yield undefined; michael@0: }