summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbobjectstore_createIndex15-autoincrement.htm
blob: 679b5d05aa1bf61062a3f9f00aadc59fb177221d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<meta charset=utf-8>
<title>IDBObjectStore.createIndex() - AutoIncrement in Compound Index</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/support.js"></script>
<script>
  indexeddb_test(
    function(t, db, txn) {
      // No auto-increment
      var store = db.createObjectStore("Store1", {keyPath: "id"});
      store.createIndex("CompoundKey", ["num", "id"]);

      // Add data
      store.put({id: 1, num: 100});
    },
    function(t, db) {
      var store = db.transaction("Store1", "readwrite", {durability: 'relaxed'}).objectStore("Store1");

      store.openCursor().onsuccess = t.step_func(function(e) {
        var item = e.target.result.value;
        store.index("CompoundKey").get([item.num, item.id]).onsuccess = t.step_func(function(e) {
          assert_equals(e.target.result ? e.target.result.num : null, 100, 'Expected 100.');
          t.done();
        });
      });
    },
    "Explicit Primary Key"
  );

  indexeddb_test(
    function(t, db, txn) {
      // Auto-increment
      var store = db.createObjectStore("Store2", {keyPath: "id", autoIncrement: true});
      store.createIndex("CompoundKey", ["num", "id"]);

      // Add data
      store.put({num: 100});
    },
    function(t, db) {
      var store = db.transaction("Store2", "readwrite", {durability: 'relaxed'}).objectStore("Store2");
      store.openCursor().onsuccess = t.step_func(function(e) {
        var item = e.target.result.value;
        store.index("CompoundKey").get([item.num, item.id]).onsuccess = t.step_func(function(e) {
          assert_equals(e.target.result ? e.target.result.num : null, 100, 'Expected 100.');
          t.done();
        });
      });
    },
    "Auto-Increment Primary Key"
  );

  indexeddb_test(
    function(t, db, txn) {
      // Auto-increment
      var store = db.createObjectStore("Store3", {keyPath: "id", autoIncrement: true});
      store.createIndex("CompoundKey", ["num", "id", "other"]);

      var num = 100;

      // Add data to Store3 - valid keys
      // Objects will be stored in Store3 and keys will get added
      // to the CompoundKeys index.
      store.put({num: num++, other: 0});
      store.put({num: num++, other: [0]});

      // Add data - missing key
      // Objects will be stored in Store3 but keys won't get added to
      // the CompoundKeys index because the 'other' keypath doesn't
      // resolve to a value.
      store.put({num: num++});

      // Add data to Store3 - invalid keys
      // Objects will be stored in Store3 but keys won't get added to
      // the CompoundKeys index because the 'other' property values
      // aren't valid keys.
      store.put({num: num++, other: null});
      store.put({num: num++, other: {}});
      store.put({num: num++, other: [null]});
      store.put({num: num++, other: [{}]});
    },
    function(t, db) {
      var store = db.transaction("Store3", "readwrite", {durability: 'relaxed'}).objectStore("Store3");
      const keys = [];
      let count;
      store.count().onsuccess = t.step_func(e => { count = e.target.result; });
      store.index("CompoundKey").openCursor().onsuccess = t.step_func(function(e) {
        const cursor = e.target.result;
        if (cursor !== null) {
          keys.push(cursor.key);
          cursor.continue();
          return;
        }

        // Done iteration, check results.
        assert_equals(count, 7, 'Expected all 7 records to be stored.');
        assert_equals(keys.length, 2, 'Expected exactly two index entries.');
        assert_array_equals(keys[0], [100, 1, 0]);
        assert_object_equals(keys[1], [101, 2, [0]]);
        t.done();
      });
    },
    "Auto-Increment Primary Key - invalid key values elsewhere"
  );
</script>