summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbindex_get.any.js
blob: 0da228f73825e50c5a19cbf9b7504a946ae7b7df (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
// META: global=window,worker
// META: title=IDBIndex.get()
// META: script=resources/support.js
// @author Microsoft <https://www.microsoft.com>
// @author Intel <http://www.intel.com>

'use_strict';

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

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

    objStore.add(record);
  };

  open_rq.onsuccess = function(e) {
    const rq = db.transaction("store", "readonly", { durability: 'relaxed' })
      .objectStore("store")
      .index("index")
      .get(record.indexedProperty);

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

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;
    const 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")
      .get("data");

    rq.onsuccess = t.step_func(function(e) {
      assert_equals(e.target.result.key, records[0].key);
      t.done();
    });
  };
}, 'get() returns the record 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")
      .get(1);

    rq.onsuccess = t.step_func(function(e) {
      assert_equals(e.target.result, undefined);
      t.done();
    });
  };
}, 'get() attempts to retrieve a record that does not 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")
      .get(IDBKeyRange.bound('data4', 'data7'));

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

      step_timeout(function () { t.done(); }, 4);
    });
  };
}, 'get() returns the record with the first key in 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.get(NaN);
    });
    t.done();
  };
}, 'get() 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.get("data");
    });
    t.done();
  };
}, 'get() 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.get("data");
    });
    t.done();
  };
}, 'get() 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.get("data");
    });
    t.done();
  };
}, 'get() throws InvalidStateError on index deleted by aborted upgrade');