dom/indexedDB/test/unit/test_complex_keyPaths.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/unit/test_complex_keyPaths.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,266 @@
     1.4 +/**
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +
     1.9 +var testGenerator = testSteps();
    1.10 +
    1.11 +function testSteps()
    1.12 +{
    1.13 +  // Test object stores
    1.14 +
    1.15 +  const name = "test_complex_keyPaths";
    1.16 +  const keyPaths = [
    1.17 +    { keyPath: "id",      value: { id: 5 },                      key: 5 },
    1.18 +    { keyPath: "id",      value: { id: "14", iid: 12 },          key: "14" },
    1.19 +    { keyPath: "id",      value: { iid: "14", id: 12 },          key: 12 },
    1.20 +    { keyPath: "id",      value: {} },
    1.21 +    { keyPath: "id",      value: { id: {} } },
    1.22 +    { keyPath: "id",      value: { id: /x/ } },
    1.23 +    { keyPath: "id",      value: 2 },
    1.24 +    { keyPath: "id",      value: undefined },
    1.25 +    { keyPath: "foo.id",  value: { foo: { id: 7 } },             key: 7 },
    1.26 +    { keyPath: "foo.id",  value: { id: 7, foo: { id: "asdf" } }, key: "asdf" },
    1.27 +    { keyPath: "foo.id",  value: { foo: { id: undefined } } },
    1.28 +    { keyPath: "foo.id",  value: { foo: 47 } },
    1.29 +    { keyPath: "foo.id",  value: {} },
    1.30 +    { keyPath: "",        value: "foopy",                        key: "foopy" },
    1.31 +    { keyPath: "",        value: 2,                              key: 2 },
    1.32 +    { keyPath: "",        value: undefined },
    1.33 +    { keyPath: "",        value: { id: 12 } },
    1.34 +    { keyPath: "",        value: /x/ },
    1.35 +    { keyPath: "foo.bar", value: { baz: 1, foo: { baz2: 2, bar: "xo" } },     key: "xo" },
    1.36 +    { keyPath: "foo.bar.baz", value: { foo: { bar: { bazz: 16, baz: 17 } } }, key: 17 },
    1.37 +    { keyPath: "foo..id", exception: true },
    1.38 +    { keyPath: "foo.",    exception: true },
    1.39 +    { keyPath: "fo o",    exception: true },
    1.40 +    { keyPath: "foo ",    exception: true },
    1.41 +    { keyPath: "foo[bar]",exception: true },
    1.42 +    { keyPath: "foo[1]",  exception: true },
    1.43 +    { keyPath: "$('id').stuff", exception: true },
    1.44 +    { keyPath: "foo.2.bar", exception: true },
    1.45 +    { keyPath: "foo. .bar", exception: true },
    1.46 +    { keyPath: ".bar",    exception: true },
    1.47 +    { keyPath: [],        exception: true },
    1.48 +
    1.49 +    { keyPath: ["foo", "bar"],        value: { foo: 1, bar: 2 },              key: [1, 2] },
    1.50 +    { keyPath: ["foo"],               value: { foo: 1, bar: 2 },              key: [1] },
    1.51 +    { keyPath: ["foo", "bar", "bar"], value: { foo: 1, bar: "x" },            key: [1, "x", "x"] },
    1.52 +    { keyPath: ["x", "y"],            value: { x: [],  y: "x" },              key: [[], "x"] },
    1.53 +    { keyPath: ["x", "y"],            value: { x: [[1]],  y: "x" },           key: [[[1]], "x"] },
    1.54 +    { keyPath: ["x", "y"],            value: { x: [[1]],  y: new Date(1) },   key: [[[1]], new Date(1)] },
    1.55 +    { keyPath: ["x", "y"],            value: { x: [[1]],  y: [new Date(3)] }, key: [[[1]], [new Date(3)]] },
    1.56 +    { keyPath: ["x", "y.bar"],        value: { x: "hi", y: { bar: "x"} },     key: ["hi", "x"] },
    1.57 +    { keyPath: ["x.y", "y.bar"],      value: { x: { y: "hello" }, y: { bar: "nurse"} }, key: ["hello", "nurse"] },
    1.58 +    { keyPath: ["", ""],              value: 5,                               key: [5, 5] },
    1.59 +    { keyPath: ["x", "y"],            value: { x: 1 } },
    1.60 +    { keyPath: ["x", "y"],            value: { y: 1 } },
    1.61 +    { keyPath: ["x", "y"],            value: { x: 1, y: undefined } },
    1.62 +    { keyPath: ["x", "y"],            value: { x: null, y: 1 } },
    1.63 +    { keyPath: ["x", "y.bar"],        value: { x: null, y: { bar: "x"} } },
    1.64 +    { keyPath: ["x", "y"],            value: { x: 1, y: false } },
    1.65 +    { keyPath: ["x", "y", "z"],       value: { x: 1, y: false, z: "a" } },
    1.66 +    { keyPath: [".x", "y", "z"],      exception: true },
    1.67 +    { keyPath: ["x", "y ", "z"],      exception: true },
    1.68 +  ];
    1.69 +
    1.70 +  let openRequest = indexedDB.open(name, 1);
    1.71 +  openRequest.onerror = errorHandler;
    1.72 +  openRequest.onupgradeneeded = grabEventAndContinueHandler;
    1.73 +  openRequest.onsuccess = unexpectedSuccessHandler;
    1.74 +  let event = yield undefined;
    1.75 +  let db = event.target.result;
    1.76 +
    1.77 +  let stores = {};
    1.78 +
    1.79 +  // Test creating object stores and inserting data
    1.80 +  for (let i = 0; i < keyPaths.length; i++) {
    1.81 +    let info = keyPaths[i];
    1.82 +
    1.83 +    let test = " for objectStore test " + JSON.stringify(info);
    1.84 +    let indexName = JSON.stringify(info.keyPath);
    1.85 +    if (!stores[indexName]) {
    1.86 +      try {
    1.87 +        let objectStore = db.createObjectStore(indexName, { keyPath: info.keyPath });
    1.88 +        ok(!("exception" in info), "shouldn't throw" + test);
    1.89 +        is(JSON.stringify(objectStore.keyPath), JSON.stringify(info.keyPath),
    1.90 +           "correct keyPath property" + test);
    1.91 +        ok(objectStore.keyPath === objectStore.keyPath,
    1.92 +           "object identity should be preserved");
    1.93 +        stores[indexName] = objectStore;
    1.94 +      } catch (e) {
    1.95 +        ok("exception" in info, "should throw" + test);
    1.96 +        is(e.name, "SyntaxError", "expect a SyntaxError" + test);
    1.97 +        ok(e instanceof DOMException, "Got a DOM Exception" + test);
    1.98 +        is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test);
    1.99 +        continue;
   1.100 +      }
   1.101 +    }
   1.102 +
   1.103 +    let store = stores[indexName];
   1.104 +
   1.105 +    try {
   1.106 +      request = store.add(info.value);
   1.107 +      ok("key" in info, "successfully created request to insert value" + test);
   1.108 +    } catch (e) {
   1.109 +      ok(!("key" in info), "threw when attempted to insert" + test);
   1.110 +      ok(e instanceof DOMException, "Got a DOMException" + test);
   1.111 +      is(e.name, "DataError", "expect a DataError" + test);
   1.112 +      is(e.code, 0, "expect zero" + test);
   1.113 +      continue;
   1.114 +    }
   1.115 +
   1.116 +    request.onerror = errorHandler;
   1.117 +    request.onsuccess = grabEventAndContinueHandler;
   1.118 +
   1.119 +    let e = yield undefined;
   1.120 +    is(e.type, "success", "inserted successfully" + test);
   1.121 +    is(e.target, request, "expected target" + test);
   1.122 +    ok(compareKeys(request.result, info.key), "found correct key" + test);
   1.123 +    is(indexedDB.cmp(request.result, info.key), 0, "returned key compares correctly" + test);
   1.124 +
   1.125 +    store.get(info.key).onsuccess = grabEventAndContinueHandler;
   1.126 +    e = yield undefined;
   1.127 +    isnot(e.target.result, undefined, "Did find entry");
   1.128 +
   1.129 +    // Check that cursor.update work as expected
   1.130 +    request = store.openCursor();
   1.131 +    request.onerror = errorHandler;
   1.132 +    request.onsuccess = grabEventAndContinueHandler;
   1.133 +    e = yield undefined;
   1.134 +    let cursor = e.target.result;
   1.135 +    request = cursor.update(info.value);
   1.136 +    request.onerror = errorHandler;
   1.137 +    request.onsuccess = grabEventAndContinueHandler;
   1.138 +    yield undefined;
   1.139 +    ok(true, "Successfully updated cursor" + test);
   1.140 +
   1.141 +    // Check that cursor.update throws as expected when key is changed
   1.142 +    let newValue = cursor.value;
   1.143 +    let destProp = Array.isArray(info.keyPath) ? info.keyPath[0] : info.keyPath;
   1.144 +    if (destProp) {
   1.145 +      eval("newValue." + destProp + " = 'newKeyValue'");
   1.146 +    }
   1.147 +    else {
   1.148 +      newValue = 'newKeyValue';
   1.149 +    }
   1.150 +    let didThrow;
   1.151 +    try {
   1.152 +      cursor.update(newValue);
   1.153 +    }
   1.154 +    catch (ex) {
   1.155 +      didThrow = ex;
   1.156 +    }
   1.157 +    ok(didThrow instanceof DOMException, "Got a DOMException" + test);
   1.158 +    is(didThrow.name, "DataError", "expect a DataError" + test);
   1.159 +    is(didThrow.code, 0, "expect zero" + test);
   1.160 +
   1.161 +    // Clear object store to prepare for next test
   1.162 +    store.clear().onsuccess = grabEventAndContinueHandler;
   1.163 +    yield undefined;
   1.164 +  }
   1.165 +
   1.166 +  // Attempt to create indexes and insert data
   1.167 +  let store = db.createObjectStore("indexStore");
   1.168 +  let indexes = {};
   1.169 +  for (let i = 0; i < keyPaths.length; i++) {
   1.170 +    let test = " for index test " + JSON.stringify(info);
   1.171 +    let info = keyPaths[i];
   1.172 +    let indexName = JSON.stringify(info.keyPath);
   1.173 +    if (!indexes[indexName]) {
   1.174 +      try {
   1.175 +        let index = store.createIndex(indexName, info.keyPath);
   1.176 +        ok(!("exception" in info), "shouldn't throw" + test);
   1.177 +        is(JSON.stringify(index.keyPath), JSON.stringify(info.keyPath),
   1.178 +           "index has correct keyPath property" + test);
   1.179 +        ok(index.keyPath === index.keyPath,
   1.180 +           "object identity should be preserved");
   1.181 +        indexes[indexName] = index;
   1.182 +      } catch (e) {
   1.183 +        ok("exception" in info, "should throw" + test);
   1.184 +        is(e.name, "SyntaxError", "expect a SyntaxError" + test);
   1.185 +        ok(e instanceof DOMException, "Got a DOM Exception" + test);
   1.186 +        is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test);
   1.187 +        continue;
   1.188 +      }
   1.189 +    }
   1.190 +    
   1.191 +    let index = indexes[indexName];
   1.192 +
   1.193 +    request = store.add(info.value, 1);
   1.194 +    if ("key" in info) {
   1.195 +      index.getKey(info.key).onsuccess = grabEventAndContinueHandler;
   1.196 +      e = yield undefined;
   1.197 +      is(e.target.result, 1, "found value when reading" + test);
   1.198 +    }
   1.199 +    else {
   1.200 +      index.count().onsuccess = grabEventAndContinueHandler;
   1.201 +      e = yield undefined;
   1.202 +      is(e.target.result, 0, "should be empty" + test);
   1.203 +    }
   1.204 +
   1.205 +    store.clear().onsuccess = grabEventAndContinueHandler;
   1.206 +    yield undefined;
   1.207 +  }
   1.208 +
   1.209 +  // Autoincrement and complex key paths
   1.210 +  let aitests = [{ v: {},                           k: 1, res: { foo: { id: 1 }} },
   1.211 +                 { v: { value: "x" },               k: 2, res: { value: "x", foo: { id: 2 }} },
   1.212 +                 { v: { value: "x", foo: {} },      k: 3, res: { value: "x", foo: { id: 3 }} },
   1.213 +                 { v: { v: "x", foo: { x: "y" } },  k: 4, res: { v: "x", foo: { x: "y", id: 4 }} },
   1.214 +                 { v: { value: 2, foo: { id: 10 }}, k: 10 },
   1.215 +                 { v: { value: 2 },                 k: 11, res: { value: 2, foo: { id: 11 }} },
   1.216 +                 { v: true,                         },
   1.217 +                 { v: { value: 2, foo: 12 },        },
   1.218 +                 { v: { foo: { id: true }},         },
   1.219 +                 { v: { foo: { x: 5, id: {} }},     },
   1.220 +                 { v: undefined,                    },
   1.221 +                 { v: { foo: undefined },           },
   1.222 +                 { v: { foo: { id: undefined }},    },
   1.223 +                 { v: null,                         },
   1.224 +                 { v: { foo: null },                },
   1.225 +                 { v: { foo: { id: null }},         },
   1.226 +                 ];
   1.227 +
   1.228 +  store = db.createObjectStore("gen", { keyPath: "foo.id", autoIncrement: true });
   1.229 +  for (let i = 0; i < aitests.length; ++i) {
   1.230 +    let info = aitests[i];
   1.231 +    let test = " for autoIncrement test " + JSON.stringify(info);
   1.232 +
   1.233 +    let preValue = JSON.stringify(info.v);
   1.234 +    if ("k" in info) {
   1.235 +      store.add(info.v).onsuccess = grabEventAndContinueHandler;
   1.236 +      is(JSON.stringify(info.v), preValue, "put didn't modify value" + test);
   1.237 +    }
   1.238 +    else {
   1.239 +      try {
   1.240 +        store.add(info.v);
   1.241 +        ok(false, "should throw" + test);
   1.242 +      }
   1.243 +      catch(e) {
   1.244 +        ok(true, "did throw" + test);
   1.245 +        ok(e instanceof DOMException, "Got a DOMException" + test);
   1.246 +        is(e.name, "DataError", "expect a DataError" + test);
   1.247 +        is(e.code, 0, "expect zero" + test);
   1.248 +
   1.249 +        is(JSON.stringify(info.v), preValue, "failing put didn't modify value" + test);
   1.250 +
   1.251 +        continue;
   1.252 +      }
   1.253 +    }
   1.254 +
   1.255 +    let e = yield undefined;
   1.256 +    is(e.target.result, info.k, "got correct return key" + test);
   1.257 +
   1.258 +    store.get(info.k).onsuccess = grabEventAndContinueHandler;
   1.259 +    e = yield undefined;
   1.260 +    is(JSON.stringify(e.target.result), JSON.stringify(info.res || info.v),
   1.261 +       "expected value stored" + test);
   1.262 +  }
   1.263 +
   1.264 +  openRequest.onsuccess = grabEventAndContinueHandler;
   1.265 +  yield undefined;
   1.266 +
   1.267 +  finishTest();
   1.268 +  yield undefined;
   1.269 +}

mercurial