test_indexes_bad_values.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. const name = this.window ? window.location.pathname : "Splendid Test";
  9. const objectStoreName = "People";
  10. const objectStoreData = [
  11. { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } },
  12. { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } },
  13. { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } },
  14. { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } },
  15. { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } },
  16. { key: "237-23-7737", value: { name: "Pat", height: 65 } },
  17. { key: "237-23-7738", value: { name: "Mel", height: 66, weight: {} } }
  18. ];
  19. const badObjectStoreData = [
  20. { key: "237-23-7739", value: { name: "Rob", height: 65 } },
  21. { key: "237-23-7740", value: { name: "Jen", height: 66, weight: {} } }
  22. ];
  23. const indexData = [
  24. { name: "weight", keyPath: "weight", options: { unique: false } }
  25. ];
  26. const objectStoreDataWeightSort = [
  27. { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } },
  28. { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } },
  29. { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } },
  30. { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } },
  31. { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }
  32. ];
  33. let request = indexedDB.open(name, 1);
  34. request.onerror = errorHandler;
  35. request.onupgradeneeded = grabEventAndContinueHandler;
  36. request.onsuccess = grabEventAndContinueHandler;
  37. let event = yield undefined;
  38. let db = event.target.result;
  39. let objectStore = db.createObjectStore(objectStoreName, { } );
  40. let addedData = 0;
  41. for (let i in objectStoreData) {
  42. request = objectStore.add(objectStoreData[i].value,
  43. objectStoreData[i].key);
  44. request.onerror = errorHandler;
  45. request.onsuccess = function(event) {
  46. if (++addedData == objectStoreData.length) {
  47. testGenerator.send(event);
  48. }
  49. }
  50. }
  51. event = yield undefined;
  52. for (let i in indexData) {
  53. objectStore.createIndex(indexData[i].name, indexData[i].keyPath,
  54. indexData[i].options);
  55. }
  56. addedData = 0;
  57. for (let i in badObjectStoreData) {
  58. request = objectStore.add(badObjectStoreData[i].value,
  59. badObjectStoreData[i].key);
  60. request.onerror = errorHandler;
  61. request.onsuccess = function(event) {
  62. if (++addedData == badObjectStoreData.length) {
  63. executeSoon(function() { testGenerator.next() });
  64. }
  65. }
  66. }
  67. yield undefined;
  68. yield undefined;
  69. objectStore = db.transaction(objectStoreName)
  70. .objectStore(objectStoreName);
  71. let keyIndex = 0;
  72. request = objectStore.index("weight").openKeyCursor();
  73. request.onerror = errorHandler;
  74. request.onsuccess = function (event) {
  75. let cursor = event.target.result;
  76. if (cursor) {
  77. is(cursor.key, objectStoreDataWeightSort[keyIndex].value.weight,
  78. "Correct key");
  79. is(cursor.primaryKey, objectStoreDataWeightSort[keyIndex].key,
  80. "Correct value");
  81. keyIndex++;
  82. cursor.continue();
  83. }
  84. else {
  85. testGenerator.next();
  86. }
  87. }
  88. yield undefined;
  89. is(keyIndex, objectStoreDataWeightSort.length, "Saw all weights");
  90. keyIndex = 0;
  91. request = objectStore.openCursor();
  92. request.onerror = errorHandler;
  93. request.onsuccess = function (event) {
  94. let cursor = event.target.result;
  95. if (cursor) {
  96. keyIndex++;
  97. cursor.continue();
  98. }
  99. else {
  100. testGenerator.next();
  101. }
  102. }
  103. yield undefined;
  104. is(keyIndex, objectStoreData.length + badObjectStoreData.length,
  105. "Saw all people");
  106. finishTest();
  107. yield undefined;
  108. }