-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
242 lines (216 loc) · 5.37 KB
/
test.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
function debug() {
let args = Array.slice(arguments);
args.unshift("DEBUG");
console.log.apply(console, args);
}
const DB_NAME = "testquery";
const DB_VERSION = 1;
const STORE_NAME = "lemonscars";
let sampleRecords = [
{name: "ECTO-1",
year: 1989,
make: "BMW",
model: "325i",
races: 1},
{name: "ECTO-2",
year: 1984,
make: "BMW",
model: "325e",
races: 3},
{name: "Cheesy",
year: 1984,
make: "BMW",
model: "325e",
races: 9},
{name: "Pikachubaru",
year: 2001,
make: "Subaru",
model: "Legacy Outback",
races: 5},
{name: "Ferdinand the Bug",
year: 1971,
make: "Volkswagen",
model: "Super Beetle",
races: 0}
];
function openDB(callback) {
let indexedDB = window.mozIndexedDB;
let request = indexedDB.open(DB_NAME, DB_VERSION);
request.onsuccess = function (event) {
debug("Opened database:", DB_NAME, DB_VERSION);
callback(request.result);
};
request.onupgradeneeded = function (event) {
debug("Database needs upgrade:", DB_NAME,
event.oldVersion, event.newVersion);
debug("Correct old database version:", event.oldVersion == 0);
debug("Correct new database version:", event.newVersion == DB_VERSION);
let db = request.result;
let store = db.createObjectStore(STORE_NAME, {keyPath: "name"});
store.createIndex("year", "year", { unique: false });
store.createIndex("make", "make", { unique: false });
store.createIndex("model", "model", { unique: false });
store.createIndex("races", "races", { unique: false });
};
request.onerror = function (event) {
debug("Failed to open database", DB_NAME);
};
request.onblocked = function (event) {
debug("Opening database request is blocked.");
};
}
let gDB;
function populateDB(callback) {
openDB(function (db) {
gDB = db;
let txn = gDB.transaction([STORE_NAME], IDBTransaction.READ_WRITE);
let store = txn.objectStore(STORE_NAME);
txn.oncomplete = function oncomplete() {
console.debug("Populate transaction completed.");
callback();
};
sampleRecords.forEach(function (record) {
debug("Storing", record);
store.put(record);
});
});
}
function openStore() {
let txn = gDB.transaction([STORE_NAME], IDBTransaction.READ_ONLY);
txn.oncomplete = run_next_test;
txn.onabort = function () {
console.error("The transaction was aborted because an error occurred.");
};
let store = txn.objectStore(STORE_NAME);
return store;
}
function compareKeys(keys, expectedKeys) {
//TODO for now we don't care about order
debug("Comparing", keys, expectedKeys);
do_check_eq(keys.length, expectedKeys.length);
do_check_eq(arrayUnion(keys, expectedKeys).length, keys.length);
}
function test(query, expectedKeys) {
add_test(function test_openCursor() {
debug("Testing " + query + ".openCursor");
let request = query.openCursor(openStore());
let keys = [];
request.onsuccess = function onsuccess() {
if (request.result == undefined) {
compareKeys(keys, expectedKeys);
return;
}
keys.push(request.result.name);
};
});
add_test(function test_openKeyCursor() {
debug("Testing " + query + ".openKeyCursor");
let request = query.openKeyCursor(openStore());
let keys = [];
request.onsuccess = function onsuccess() {
if (request.result == undefined) {
compareKeys(keys, expectedKeys);
return;
}
keys.push(request.result);
};
});
add_test(function test_getAll() {
debug("Testing " + query + ".getAll");
let request = query.getAll(openStore());
request.onsuccess = function onsuccess() {
let keys = request.result.map(function (item) { return item.name; });
compareKeys(keys, expectedKeys);
};
});
add_test(function test_getAllKeys() {
debug("Testing " + query + ".getAllKeys");
let request = query.getAllKeys(openStore());
request.onsuccess = function onsuccess() {
compareKeys(request.result, expectedKeys);
};
});
}
function run_tests() {
populateDB(run_next_test);
}
/*** Tests start here ***/
// eq
test(
Index("make").eq("Chevrolet"),
[]
);
test(
Index("make").eq("BMW"),
["ECTO-1", "ECTO-2", "Cheesy"]
);
// neq
test(
Index("make").neq("BMW"),
["Pikachubaru", "Ferdinand the Bug"]
);
// lt
test(
Index("year").lt(1971), //TODO 1971 fails on getAll?!?
[]
);
test(
Index("year").lt(1984), //TODO 1984 fails on getAll?!?
["Ferdinand the Bug"]
);
// lteq
test(
Index("year").lteq(1970),
[]
);
test(
Index("year").lteq(1984),
["Ferdinand the Bug", "Cheesy", "ECTO-2"]
);
// gt
test(
Index("year").gt(2001),
[]
);
test(
Index("year").gt(1989),
["Pikachubaru"]
);
// gteq
test(
Index("year").gteq(2002),
[]
);
test(
Index("year").gteq(1989),
["Pikachubaru", "ECTO-1"]
);
// oneof
test(
Index("make").oneof("Chevrolet", "Ford"),
[]
);
test(
Index("make").oneof("Volkswagen", "Subaru"),
["Pikachubaru", "Ferdinand the Bug"]
);
// between
test(
Index("year").between(1960, 1970),
[]
);
test(
Index("year").between(1980, 1990),
["ECTO-1", "ECTO-2", "Cheesy"]
);
// Composite queries
test(
Index("make").eq("BMW").and(Index("model").eq("325e")),
["ECTO-2", "Cheesy"]
);
test(
Index("make").eq("Volkswagen").or(Index("make").eq("Subaru")),
["Pikachubaru", "Ferdinand the Bug"]
);