summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbindex_getKey.any.js
blob: 677a70e7cb43e4ad2675a8f03413bd55d8e7aa58 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// META: global=window,worker
// META: title=IDBIndex.getKey()
// META: script=resources/support.js
// @author Microsoft <https://www.microsoft.com>
// @author Intel <http://www.intel.com>

'use_strict';

async_test(t => {
    let db;
    const record = { key: 1, indexedProperty: "data" };

    const open_rq = createdb(t);
    open_rq.onupgradeneeded = function(e) {
      db = e.target.result;
      const objStore = db.createObjectStore("test", { keyPath: "key" });
      objStore.createIndex("index", "indexedProperty");

      objStore.add(record);
    };

    open_rq.onsuccess = function(e) {
      let rq = db.transaction("test", "readonly", { durability: 'relaxed' })
        .objectStore("test");

      rq = rq.index("index");

      rq = rq.getKey("data");

      rq.onsuccess = t.step_func(function(e) {
        assert_equals(e.target.result, record.key);
        t.done();
      });
    };
}, 'getKey() returns the record\'s primary key');

async_test(t => {
    let db;
    const records = [
      { key: 1, indexedProperty: "data" },
      { key: 2, indexedProperty: "data" },
      { key: 3, indexedProperty: "data" }
    ];

    const open_rq = createdb(t);
    open_rq.onupgradeneeded = function(e) {
      db = e.target.result;
      var objStore = db.createObjectStore("test", { keyPath: "key" });
      objStore.createIndex("index", "indexedProperty");

      for (let i = 0; i < records.length; i++)
        objStore.add(records[i]);
    };

    open_rq.onsuccess = function(e) {
      const rq = db.transaction("test", "readonly", { durability: 'relaxed' })
        .objectStore("test")
        .index("index")
        .getKey("data");

      rq.onsuccess = t.step_func(function(e) {
        assert_equals(e.target.result, records[0].key);
        t.done();
      });
    };
}, 'getKey() returns the record\'s primary key where the index contains duplicate values');

async_test(t => {
    let db;
    const open_rq = createdb(t);

    open_rq.onupgradeneeded = function(e) {
      db = e.target.result;
      const rq = db.createObjectStore("test", { keyPath: "key" })
                  .createIndex("index", "indexedProperty")
                  .getKey(1);

      rq.onsuccess = t.step_func(function(e) {
          assert_equals(e.target.result, undefined);
          t.done();
      });
    };
}, 'getKey() attempt to retrieve the primary key of a record that doesn\'t exist');

async_test(t => {
    let db;

    const open_rq = createdb(t);

    open_rq.onupgradeneeded = function(e) {
      db = e.target.result;
      const store = db.createObjectStore("store", { keyPath: "key" });
      store.createIndex("index", "indexedProperty");

      for (let i = 0; i < 10; i++) {
        store.add({ key: i, indexedProperty: "data" + i });
      }
    };

    open_rq.onsuccess = function(e) {
      const rq = db.transaction("store", "readonly", { durability: 'relaxed' })
        .objectStore("store")
        .index("index")
        .getKey(IDBKeyRange.bound('data4', 'data7'));

      rq.onsuccess = t.step_func(function(e) {
        assert_equals(e.target.result, 4);

        step_timeout(function () { t.done(); }, 4)
      });
    };
}, 'getKey() returns the key of the first record within the range');

async_test(t => {
    let db;
    const open_rq = createdb(t);

    open_rq.onupgradeneeded = function(e) {
      db = e.target.result;

      const index = db.createObjectStore("test", { keyPath: "key" })
        .createIndex("index", "indexedProperty");

      assert_throws_dom("DataError", function () {
        index.getKey(NaN);
      });
      t.done();
    };
}, 'getKey() throws DataError when using invalid key');

async_test(t => {
    let db;
    const open_rq = createdb(t);

    open_rq.onupgradeneeded = function(e) {
      db = e.target.result;
      const store = db.createObjectStore("store", { keyPath: "key" });
      const index = store.createIndex("index", "indexedProperty");

      store.add({ key: 1, indexedProperty: "data" });
      store.deleteIndex("index");

      assert_throws_dom("InvalidStateError", function () {
        index.getKey("data");
      });
      t.done();
    };
}, 'getKey() throws InvalidStateError when the index is deleted');

async_test(t => {
    let db;

    const open_rq = createdb(t);
    open_rq.onupgradeneeded = function(e) {
      db = e.target.result;
      const store = db.createObjectStore("store", { keyPath: "key" });
      const index = store.createIndex("index", "indexedProperty");
      store.add({ key: 1, indexedProperty: "data" });
    };

    open_rq.onsuccess = function(e) {
      db = e.target.result;
      const tx = db.transaction('store', 'readonly', { durability: 'relaxed' });
      const index = tx.objectStore('store').index('index');
      tx.abort();

      assert_throws_dom("TransactionInactiveError", function () {
        index.getKey("data");
      });
      t.done();
    };
}, 'getKey() throws TransactionInactiveError on aborted transaction');

async_test(t => {
    let db;

    const open_rq = createdb(t);
    open_rq.onupgradeneeded = function(e) {
        db = e.target.result;
        const store = db.createObjectStore("store", { keyPath: "key" });
        const index = store.createIndex("index", "indexedProperty");
        store.add({ key: 1, indexedProperty: "data" });

        e.target.transaction.abort();

        assert_throws_dom("InvalidStateError", function () {
        index.getKey("data");
        });
        t.done();
    };
}, 'getKey() throws InvalidStateError on index deleted by aborted upgrade');