gecko/dom/indexedDB/test/test_complex_keyPaths.html

220 lines
9.0 KiB
HTML
Raw Normal View History

<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
<title>Indexed Database Property Test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="text/javascript;version=1.7">
function testSteps()
{
const nsIIDBObjectStore = Components.interfaces.nsIIDBObjectStore;
const nsIIDBTransaction = Components.interfaces.nsIIDBTransaction;
// Test object stores
const name = window.location.pathname;
const keyPaths = [
{ keyPath: "id", value: { id: 5 }, key: 5 },
{ keyPath: "id", value: { id: "14", iid: 12 }, key: "14" },
{ keyPath: "id", value: { iid: "14", id: 12 }, key: 12 },
{ keyPath: "id", value: {} },
{ keyPath: "id", value: { id: {} } },
{ keyPath: "id", value: { id: /x/ } },
{ keyPath: "id", value: 2 },
{ keyPath: "id", value: undefined },
{ keyPath: "foo.id", value: { foo: { id: 7 } }, key: 7 },
{ keyPath: "foo.id", value: { id: 7, foo: { id: "asdf" } }, key: "asdf" },
{ keyPath: "foo.id", value: { foo: { id: undefined } } },
{ keyPath: "foo.id", value: { foo: 47 } },
{ keyPath: "foo.id", value: {} },
{ keyPath: "", value: "foopy", key: "foopy" },
{ keyPath: "", value: 2, key: 2 },
{ keyPath: "", value: undefined },
{ keyPath: "", value: { id: 12 } },
{ keyPath: "", value: /x/ },
{ keyPath: "foo.bar", value: { baz: 1, foo: { baz2: 2, bar: "xo" } }, key: "xo" },
{ keyPath: "foo.bar.baz", value: { foo: { bar: { bazz: 16, baz: 17 } } }, key: 17 },
{ keyPath: "foo..id", exception: true },
{ keyPath: "foo.", exception: true },
{ keyPath: "fo o", exception: true },
{ keyPath: "foo ", exception: true },
{ keyPath: "foo[bar]",exception: true },
{ keyPath: "$('id').stuff", exception: true },
{ keyPath: "foo.2.bar", exception: true },
{ keyPath: "foo. .bar", exception: true },
{ keyPath: ".bar", exception: true },
];
let openRequest = mozIndexedDB.open(name, 1);
openRequest.onerror = errorHandler;
openRequest.onupgradeneeded = grabEventAndContinueHandler;
openRequest.onsuccess = unexpectedSuccessHandler;
let event = yield;
let db = event.target.result;
let stores = {};
// Test creating object stores and inserting data
for (let i = 0; i < keyPaths.length; i++) {
let info = keyPaths[i];
let test = " for objectStore test " + JSON.stringify(info);
if (!stores[info.keyPath]) {
try {
let objectStore = db.createObjectStore(info.keyPath, { keyPath: info.keyPath });
ok(!("exception" in info), "shouldn't throw" + test);
is(objectStore.keyPath, info.keyPath, "correct keyPath property" + test);
stores[info.keyPath] = objectStore;
} catch (e) {
ok("exception" in info, "should throw" + test);
ok(e instanceof DOMException, "Got a DOM Exception" + test);
is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test);
continue;
}
}
let store = stores[info.keyPath];
try {
request = store.add(info.value);
ok("key" in info, "successfully created request to insert value" + test);
} catch (e) {
ok(!("key" in info), "threw when attempted to insert" + test);
ok(e instanceof IDBDatabaseException, "Got a IDBDatabaseException" + test);
is(e.code, IDBDatabaseException.DATA_ERR, "expect a DATA_ERR error" + test);
continue;
}
request.onerror = errorHandler;
request.onsuccess = grabEventAndContinueHandler;
let e = yield;
is(e.type, "success", "inserted successfully" + test);
is(e.target, request, "expected target" + test);
is(request.result, info.key, "found correct key" + test);
store.clear().onsuccess = grabEventAndContinueHandler;
yield;
}
// Attempt to create indexes and insert data
let store = db.createObjectStore("indexStore");
let indexes = {};
for (let i = 0; i < keyPaths.length; i++) {
let test = " for index test " + JSON.stringify(info);
let info = keyPaths[i];
if (!indexes[info.keyPath]) {
try {
let index = store.createIndex(info.keyPath, info.keyPath);
ok(!("exception" in info), "shouldn't throw" + test);
is(index.keyPath, info.keyPath, "index has correct keyPath property" + test);
indexes[info.keyPath] = index;
} catch (e) {
ok("exception" in info, "should throw" + test);
ok(e instanceof DOMException, "Got a DOM Exception" + test);
is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test);
continue;
}
}
let index = indexes[info.keyPath];
request = store.add(info.value, 1);
if ("key" in info) {
index.getKey(info.key).onsuccess = grabEventAndContinueHandler;
e = yield;
is(e.target.result, 1, "found value when reading" + test);
}
else {
index.count().onsuccess = grabEventAndContinueHandler;
e = yield;
is(e.target.result, 0, "should be empty" + test);
}
store.clear().onsuccess = grabEventAndContinueHandler;
yield;
}
// Autoincrement and complex key paths
let aitests = [{ v: {}, k: 1, res: { foo: { id: 1 }} },
{ v: { value: "x" }, k: 2, res: { value: "x", foo: { id: 2 }} },
{ v: { value: "x", foo: {} }, k: 3, res: { value: "x", foo: { id: 3 }} },
{ v: { v: "x", foo: { x: "y" } }, k: 4, res: { v: "x", foo: { x: "y", id: 4 }} },
{ v: { value: 2, foo: { id: 10 }}, k: 10 },
{ v: { value: 2 }, k: 11, res: { value: 2, foo: { id: 11 }} },
{ v: true, },
{ v: { value: 2, foo: 12 }, },
{ v: { foo: { id: true }}, },
{ v: { foo: { x: 5, id: {} }}, },
{ v: undefined, },
{ v: { foo: undefined }, },
{ v: { foo: { id: undefined }}, },
{ v: null, },
{ v: { foo: null }, },
{ v: { foo: { id: null }}, },
];
store = db.createObjectStore("gen", { keyPath: "foo.id", autoIncrement: true });
for (let i = 0; i < aitests.length; ++i) {
let info = aitests[i];
let test = " for autoIncrement test " + JSON.stringify(info);
let preValue = JSON.stringify(info.v);
if ("k" in info) {
store.add(info.v).onsuccess = grabEventAndContinueHandler;
is(JSON.stringify(info.v), preValue, "put didn't modify value" + test);
}
else {
try {
store.add(info.v);
ok(false, "should throw" + test);
}
catch(e) {
ok(true, "did throw" + test);
ok(e instanceof IDBDatabaseException, "Got a IDBDatabaseException" + test);
is(e.code, IDBDatabaseException.DATA_ERR, "expect a DATA_ERR" + test);
is(JSON.stringify(info.v), preValue, "failing put didn't modify value" + test);
continue;
}
}
let e = yield;
is(e.target.result, info.k, "got correct return key" + test);
store.get(info.k).onsuccess = grabEventAndContinueHandler;
e = yield;
is(JSON.stringify(e.target.result), JSON.stringify(info.res || info.v),
"expected value stored" + test);
}
// Can't handle autoincrement and empty keypath
try {
store = db.createObjectStore("storefail", { keyPath: "", autoIncrement: true });
ok(false, "Should have thrown when creating empty-keypath autoincrement store");
}
catch(e) {
ok(true, "Did throw when creating empty-keypath autoincrement store");
ok(e instanceof DOMException, "Got a DOMException when creating empty-keypath autoincrement store");
is(e.code, DOMException.INVALID_ACCESS_ERR, "expect a INVALID_ACCESS_ERR when creating empty-keypath autoincrement store");
}
openRequest.onsuccess = grabEventAndContinueHandler;
yield;
finishTest();
yield;
}
</script>
<script type="text/javascript;version=1.7" src="helpers.js"></script>
</head>
<body onload="runTest();"></body>
</html>