test_complex_keyPaths.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var testGenerator = testSteps();
  6. function testSteps()
  7. {
  8. // Test object stores
  9. const name = "test_complex_keyPaths";
  10. const keyPaths = [
  11. { keyPath: "id", value: { id: 5 }, key: 5 },
  12. { keyPath: "id", value: { id: "14", iid: 12 }, key: "14" },
  13. { keyPath: "id", value: { iid: "14", id: 12 }, key: 12 },
  14. { keyPath: "id", value: {} },
  15. { keyPath: "id", value: { id: {} } },
  16. { keyPath: "id", value: { id: /x/ } },
  17. { keyPath: "id", value: 2 },
  18. { keyPath: "id", value: undefined },
  19. { keyPath: "foo.id", value: { foo: { id: 7 } }, key: 7 },
  20. { keyPath: "foo.id", value: { id: 7, foo: { id: "asdf" } }, key: "asdf" },
  21. { keyPath: "foo.id", value: { foo: { id: undefined } } },
  22. { keyPath: "foo.id", value: { foo: 47 } },
  23. { keyPath: "foo.id", value: {} },
  24. { keyPath: "", value: "foopy", key: "foopy" },
  25. { keyPath: "", value: 2, key: 2 },
  26. { keyPath: "", value: undefined },
  27. { keyPath: "", value: { id: 12 } },
  28. { keyPath: "", value: /x/ },
  29. { keyPath: "foo.bar", value: { baz: 1, foo: { baz2: 2, bar: "xo" } }, key: "xo" },
  30. { keyPath: "foo.bar.baz", value: { foo: { bar: { bazz: 16, baz: 17 } } }, key: 17 },
  31. { keyPath: "foo..id", exception: true },
  32. { keyPath: "foo.", exception: true },
  33. { keyPath: "fo o", exception: true },
  34. { keyPath: "foo ", exception: true },
  35. { keyPath: "foo[bar]",exception: true },
  36. { keyPath: "foo[1]", exception: true },
  37. { keyPath: "$('id').stuff", exception: true },
  38. { keyPath: "foo.2.bar", exception: true },
  39. { keyPath: "foo. .bar", exception: true },
  40. { keyPath: ".bar", exception: true },
  41. { keyPath: [], exception: true },
  42. { keyPath: ["foo", "bar"], value: { foo: 1, bar: 2 }, key: [1, 2] },
  43. { keyPath: ["foo"], value: { foo: 1, bar: 2 }, key: [1] },
  44. { keyPath: ["foo", "bar", "bar"], value: { foo: 1, bar: "x" }, key: [1, "x", "x"] },
  45. { keyPath: ["x", "y"], value: { x: [], y: "x" }, key: [[], "x"] },
  46. { keyPath: ["x", "y"], value: { x: [[1]], y: "x" }, key: [[[1]], "x"] },
  47. { keyPath: ["x", "y"], value: { x: [[1]], y: new Date(1) }, key: [[[1]], new Date(1)] },
  48. { keyPath: ["x", "y"], value: { x: [[1]], y: [new Date(3)] }, key: [[[1]], [new Date(3)]] },
  49. { keyPath: ["x", "y.bar"], value: { x: "hi", y: { bar: "x"} }, key: ["hi", "x"] },
  50. { keyPath: ["x.y", "y.bar"], value: { x: { y: "hello" }, y: { bar: "nurse"} }, key: ["hello", "nurse"] },
  51. { keyPath: ["", ""], value: 5, key: [5, 5] },
  52. { keyPath: ["x", "y"], value: { x: 1 } },
  53. { keyPath: ["x", "y"], value: { y: 1 } },
  54. { keyPath: ["x", "y"], value: { x: 1, y: undefined } },
  55. { keyPath: ["x", "y"], value: { x: null, y: 1 } },
  56. { keyPath: ["x", "y.bar"], value: { x: null, y: { bar: "x"} } },
  57. { keyPath: ["x", "y"], value: { x: 1, y: false } },
  58. { keyPath: ["x", "y", "z"], value: { x: 1, y: false, z: "a" } },
  59. { keyPath: [".x", "y", "z"], exception: true },
  60. { keyPath: ["x", "y ", "z"], exception: true },
  61. ];
  62. let openRequest = indexedDB.open(name, 1);
  63. openRequest.onerror = errorHandler;
  64. openRequest.onupgradeneeded = grabEventAndContinueHandler;
  65. openRequest.onsuccess = unexpectedSuccessHandler;
  66. let event = yield undefined;
  67. let db = event.target.result;
  68. let stores = {};
  69. // Test creating object stores and inserting data
  70. for (let i = 0; i < keyPaths.length; i++) {
  71. let info = keyPaths[i];
  72. let test = " for objectStore test " + JSON.stringify(info);
  73. let indexName = JSON.stringify(info.keyPath);
  74. if (!stores[indexName]) {
  75. try {
  76. let objectStore = db.createObjectStore(indexName, { keyPath: info.keyPath });
  77. ok(!("exception" in info), "shouldn't throw" + test);
  78. is(JSON.stringify(objectStore.keyPath), JSON.stringify(info.keyPath),
  79. "correct keyPath property" + test);
  80. ok(objectStore.keyPath === objectStore.keyPath,
  81. "object identity should be preserved");
  82. stores[indexName] = objectStore;
  83. } catch (e) {
  84. ok("exception" in info, "should throw" + test);
  85. is(e.name, "SyntaxError", "expect a SyntaxError" + test);
  86. ok(e instanceof DOMException, "Got a DOM Exception" + test);
  87. is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test);
  88. continue;
  89. }
  90. }
  91. let store = stores[indexName];
  92. try {
  93. request = store.add(info.value);
  94. ok("key" in info, "successfully created request to insert value" + test);
  95. } catch (e) {
  96. ok(!("key" in info), "threw when attempted to insert" + test);
  97. ok(e instanceof DOMException, "Got a DOMException" + test);
  98. is(e.name, "DataError", "expect a DataError" + test);
  99. is(e.code, 0, "expect zero" + test);
  100. continue;
  101. }
  102. request.onerror = errorHandler;
  103. request.onsuccess = grabEventAndContinueHandler;
  104. let e = yield undefined;
  105. is(e.type, "success", "inserted successfully" + test);
  106. is(e.target, request, "expected target" + test);
  107. ok(compareKeys(request.result, info.key), "found correct key" + test);
  108. is(indexedDB.cmp(request.result, info.key), 0, "returned key compares correctly" + test);
  109. store.get(info.key).onsuccess = grabEventAndContinueHandler;
  110. e = yield undefined;
  111. isnot(e.target.result, undefined, "Did find entry");
  112. // Check that cursor.update work as expected
  113. request = store.openCursor();
  114. request.onerror = errorHandler;
  115. request.onsuccess = grabEventAndContinueHandler;
  116. e = yield undefined;
  117. let cursor = e.target.result;
  118. request = cursor.update(info.value);
  119. request.onerror = errorHandler;
  120. request.onsuccess = grabEventAndContinueHandler;
  121. yield undefined;
  122. ok(true, "Successfully updated cursor" + test);
  123. // Check that cursor.update throws as expected when key is changed
  124. let newValue = cursor.value;
  125. let destProp = Array.isArray(info.keyPath) ? info.keyPath[0] : info.keyPath;
  126. if (destProp) {
  127. eval("newValue." + destProp + " = 'newKeyValue'");
  128. }
  129. else {
  130. newValue = 'newKeyValue';
  131. }
  132. let didThrow;
  133. try {
  134. cursor.update(newValue);
  135. }
  136. catch (ex) {
  137. didThrow = ex;
  138. }
  139. ok(didThrow instanceof DOMException, "Got a DOMException" + test);
  140. is(didThrow.name, "DataError", "expect a DataError" + test);
  141. is(didThrow.code, 0, "expect zero" + test);
  142. // Clear object store to prepare for next test
  143. store.clear().onsuccess = grabEventAndContinueHandler;
  144. yield undefined;
  145. }
  146. // Attempt to create indexes and insert data
  147. let store = db.createObjectStore("indexStore");
  148. let indexes = {};
  149. for (let i = 0; i < keyPaths.length; i++) {
  150. let info = keyPaths[i];
  151. let test = " for index test " + JSON.stringify(info);
  152. let indexName = JSON.stringify(info.keyPath);
  153. if (!indexes[indexName]) {
  154. try {
  155. let index = store.createIndex(indexName, info.keyPath);
  156. ok(!("exception" in info), "shouldn't throw" + test);
  157. is(JSON.stringify(index.keyPath), JSON.stringify(info.keyPath),
  158. "index has correct keyPath property" + test);
  159. ok(index.keyPath === index.keyPath,
  160. "object identity should be preserved");
  161. indexes[indexName] = index;
  162. } catch (e) {
  163. ok("exception" in info, "should throw" + test);
  164. is(e.name, "SyntaxError", "expect a SyntaxError" + test);
  165. ok(e instanceof DOMException, "Got a DOM Exception" + test);
  166. is(e.code, DOMException.SYNTAX_ERR, "expect a syntax error" + test);
  167. continue;
  168. }
  169. }
  170. let index = indexes[indexName];
  171. request = store.add(info.value, 1);
  172. if ("key" in info) {
  173. index.getKey(info.key).onsuccess = grabEventAndContinueHandler;
  174. e = yield undefined;
  175. is(e.target.result, 1, "found value when reading" + test);
  176. }
  177. else {
  178. index.count().onsuccess = grabEventAndContinueHandler;
  179. e = yield undefined;
  180. is(e.target.result, 0, "should be empty" + test);
  181. }
  182. store.clear().onsuccess = grabEventAndContinueHandler;
  183. yield undefined;
  184. }
  185. // Autoincrement and complex key paths
  186. let aitests = [{ v: {}, k: 1, res: { foo: { id: 1 }} },
  187. { v: { value: "x" }, k: 2, res: { value: "x", foo: { id: 2 }} },
  188. { v: { value: "x", foo: {} }, k: 3, res: { value: "x", foo: { id: 3 }} },
  189. { v: { v: "x", foo: { x: "y" } }, k: 4, res: { v: "x", foo: { x: "y", id: 4 }} },
  190. { v: { value: 2, foo: { id: 10 }}, k: 10 },
  191. { v: { value: 2 }, k: 11, res: { value: 2, foo: { id: 11 }} },
  192. { v: true, },
  193. { v: { value: 2, foo: 12 }, },
  194. { v: { foo: { id: true }}, },
  195. { v: { foo: { x: 5, id: {} }}, },
  196. { v: undefined, },
  197. { v: { foo: undefined }, },
  198. { v: { foo: { id: undefined }}, },
  199. { v: null, },
  200. { v: { foo: null }, },
  201. { v: { foo: { id: null }}, },
  202. ];
  203. store = db.createObjectStore("gen", { keyPath: "foo.id", autoIncrement: true });
  204. for (let i = 0; i < aitests.length; ++i) {
  205. let info = aitests[i];
  206. let test = " for autoIncrement test " + JSON.stringify(info);
  207. let preValue = JSON.stringify(info.v);
  208. if ("k" in info) {
  209. store.add(info.v).onsuccess = grabEventAndContinueHandler;
  210. is(JSON.stringify(info.v), preValue, "put didn't modify value" + test);
  211. }
  212. else {
  213. try {
  214. store.add(info.v);
  215. ok(false, "should throw" + test);
  216. }
  217. catch(e) {
  218. ok(true, "did throw" + test);
  219. ok(e instanceof DOMException, "Got a DOMException" + test);
  220. is(e.name, "DataError", "expect a DataError" + test);
  221. is(e.code, 0, "expect zero" + test);
  222. is(JSON.stringify(info.v), preValue, "failing put didn't modify value" + test);
  223. continue;
  224. }
  225. }
  226. let e = yield undefined;
  227. is(e.target.result, info.k, "got correct return key" + test);
  228. store.get(info.k).onsuccess = grabEventAndContinueHandler;
  229. e = yield undefined;
  230. is(JSON.stringify(e.target.result), JSON.stringify(info.res || info.v),
  231. "expected value stored" + test);
  232. }
  233. openRequest.onsuccess = grabEventAndContinueHandler;
  234. yield undefined;
  235. finishTest();
  236. yield undefined;
  237. }