summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbobjectstore_getKey.any.js
blob: f0dc13d45cfeb950eb73418c3b99414a309b6476 (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
// META: title=IndexedDB: Test IDBObjectStore.getKey()
// META: script=resources/support.js

'use strict';

function getkey_test(func, name) {
  indexeddb_test(
    (t, db, tx) => {
      const basic = db.createObjectStore('basic');
      const key_path_store = db.createObjectStore('key path',
        { keyPath: 'id' });
      const key_generator_store = db.createObjectStore('key generator',
        { autoIncrement: true });
      const key_generator_and_path_store = db.createObjectStore(
        'key generator and key path',
        { autoIncrement: true, key_path: 'id' });

      for (let i = 1; i <= 10; ++i) {
        basic.put(`value: ${i}`, i);
        key_path_store.put({ id: i });
        key_generator_store.put(`value: ${i}`);
        key_generator_and_path_store.put({});
      }
    },
    func,
    name
  );
}

getkey_test((t, db) => {
  const tx = db.transaction('basic', 'readonly', {durability: 'relaxed'});
  const store = tx.objectStore('basic');
  assert_throws_js(TypeError, () => store.getKey());
  assert_throws_dom('DataError', () => store.getKey(null));
  assert_throws_dom('DataError', () => store.getKey({}));
  t.done();
}, 'IDBObjectStore.getKey() - invalid parameters');

[
  'basic',
  'key path',
  'key generator',
  'key generator and key path'
].forEach(store_name => {
  getkey_test((t, db) => {
    const tx = db.transaction(store_name);
    const store = tx.objectStore(store_name);
    const request = store.getKey(5);
    request.onerror = t.unreached_func('request failed');
    request.onsuccess = t.step_func(() =>
      assert_equals(request.result, 5));
    tx.onabort = t.unreached_func('transaction aborted');
    tx.oncomplete = t.step_func(() => t.done());
  }, `IDBObjectStore.getKey() - ${store_name} - key`);

  getkey_test((t, db) => {
    const tx = db.transaction(store_name);
    const store = tx.objectStore(store_name);
    const request = store.getKey(IDBKeyRange.lowerBound(4.5));
    request.onerror = t.unreached_func('request failed');
    request.onsuccess = t.step_func(() =>
      assert_equals(request.result, 5));
    tx.onabort = t.unreached_func('transaction aborted');
    tx.oncomplete = t.step_func(() => t.done());
  }, `IDBObjectStore.getKey() - ${store_name} - range`);

  getkey_test((t, db) => {
    const tx = db.transaction(store_name);
    const store = tx.objectStore(store_name);
    const request = store.getKey(11);
    request.onerror = t.unreached_func('request failed');
    request.onsuccess = t.step_func(() =>
      assert_equals(request.result, undefined));
    tx.onabort = t.unreached_func('transaction aborted');
    tx.oncomplete = t.step_func(() => t.done());
  }, `IDBObjectStore.getKey() - ${store_name} - key - no match`);

  getkey_test((t, db) => {
    const tx = db.transaction(store_name);
    const store = tx.objectStore(store_name);
    const request = store.getKey(IDBKeyRange.lowerBound(11));
    request.onerror = t.unreached_func('request failed');
    request.onsuccess = t.step_func(() =>
      assert_equals(request.result, undefined)
    );
    tx.onabort = t.unreached_func('transaction aborted');
    tx.oncomplete = t.step_func(() => t.done());
  }, `IDBObjectStore.getKey() - ${store_name} - range - no match`);
});