test_unique_index_update.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. let request = indexedDB.open(this.window ? window.location.pathname : "Splendid Test", 1);
  9. request.onerror = errorHandler;
  10. request.onupgradeneeded = grabEventAndContinueHandler;
  11. request.onsuccess = grabEventAndContinueHandler;
  12. let event = yield undefined;
  13. let db = event.target.result;
  14. for (let autoIncrement of [false, true]) {
  15. let objectStore =
  16. db.createObjectStore(autoIncrement, { keyPath: "id",
  17. autoIncrement: autoIncrement });
  18. objectStore.createIndex("", "index", { unique: true });
  19. for (let i = 0; i < 10; i++) {
  20. objectStore.add({ id: i, index: i });
  21. }
  22. }
  23. event = yield undefined;
  24. is(event.type, "success", "expect a success event");
  25. for (let autoIncrement of [false, true]) {
  26. objectStore = db.transaction(autoIncrement, "readwrite")
  27. .objectStore(autoIncrement);
  28. request = objectStore.put({ id: 5, index: 6 });
  29. request.onsuccess = unexpectedSuccessHandler;
  30. request.addEventListener("error", new ExpectError("ConstraintError", true));
  31. event = yield undefined;
  32. event.preventDefault();
  33. let keyRange = IDBKeyRange.only(5);
  34. objectStore.index("").openCursor(keyRange).onsuccess = function(event) {
  35. let cursor = event.target.result;
  36. ok(cursor, "Must have a cursor here");
  37. is(cursor.value.index, 5, "Still have the right index value");
  38. cursor.value.index = 6;
  39. request = cursor.update(cursor.value);
  40. request.onsuccess = unexpectedSuccessHandler;
  41. request.addEventListener("error", new ExpectError("ConstraintError", true));
  42. };
  43. yield undefined;
  44. }
  45. finishTest();
  46. yield undefined;
  47. }