|
1 /** |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
4 */ |
|
5 |
|
6 var testGenerator = testSteps(); |
|
7 |
|
8 function testSteps() |
|
9 { |
|
10 const name = this.window ? window.location.pathname : "Splendid Test"; |
|
11 const objectStoreName = "People"; |
|
12 |
|
13 const objectStoreData = [ |
|
14 { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, |
|
15 { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, |
|
16 { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }, |
|
17 { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, |
|
18 { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, |
|
19 { key: "237-23-7737", value: { name: "Pat", height: 65 } } |
|
20 ]; |
|
21 |
|
22 const indexData = [ |
|
23 { name: "name", keyPath: "name", options: { unique: true } }, |
|
24 { name: "height", keyPath: "height", options: { unique: false } }, |
|
25 { name: "weight", keyPath: "weight", options: { unique: false } } |
|
26 ]; |
|
27 |
|
28 const objectStoreDataNameSort = [ |
|
29 { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, |
|
30 { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, |
|
31 { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, |
|
32 { key: "237-23-7737", value: { name: "Pat", height: 65 } }, |
|
33 { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } }, |
|
34 { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } } |
|
35 ]; |
|
36 |
|
37 const objectStoreDataWeightSort = [ |
|
38 { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, |
|
39 { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, |
|
40 { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, |
|
41 { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, |
|
42 { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } } |
|
43 ]; |
|
44 |
|
45 const objectStoreDataHeightSort = [ |
|
46 { key: "237-23-7733", value: { name: "Ann", height: 52, weight: 110 } }, |
|
47 { key: "237-23-7735", value: { name: "Sue", height: 58, weight: 130 } }, |
|
48 { key: "237-23-7732", value: { name: "Bob", height: 60, weight: 120 } }, |
|
49 { key: "237-23-7736", value: { name: "Joe", height: 65, weight: 150 } }, |
|
50 { key: "237-23-7737", value: { name: "Pat", height: 65 } }, |
|
51 { key: "237-23-7734", value: { name: "Ron", height: 73, weight: 180 } } |
|
52 ]; |
|
53 |
|
54 let request = indexedDB.open(name, 1); |
|
55 request.onerror = errorHandler; |
|
56 request.onupgradeneeded = grabEventAndContinueHandler; |
|
57 request.onsuccess = grabEventAndContinueHandler; |
|
58 let event = yield undefined; |
|
59 |
|
60 let db = event.target.result; |
|
61 |
|
62 let objectStore = db.createObjectStore(objectStoreName, {}); |
|
63 |
|
64 // First, add all our data to the object store. |
|
65 let addedData = 0; |
|
66 for (let i in objectStoreData) { |
|
67 request = objectStore.add(objectStoreData[i].value, |
|
68 objectStoreData[i].key); |
|
69 request.onerror = errorHandler; |
|
70 request.onsuccess = function(event) { |
|
71 if (++addedData == objectStoreData.length) { |
|
72 testGenerator.send(event); |
|
73 } |
|
74 } |
|
75 } |
|
76 event = yield undefined; |
|
77 |
|
78 // Now create the indexes. |
|
79 for (let i in indexData) { |
|
80 objectStore.createIndex(indexData[i].name, indexData[i].keyPath, |
|
81 indexData[i].options); |
|
82 } |
|
83 |
|
84 is(objectStore.indexNames.length, indexData.length, "Good index count"); |
|
85 yield undefined; |
|
86 |
|
87 objectStore = db.transaction(objectStoreName) |
|
88 .objectStore(objectStoreName); |
|
89 |
|
90 request = objectStore.index("height").mozGetAll(65); |
|
91 request.onerror = errorHandler; |
|
92 request.onsuccess = grabEventAndContinueHandler; |
|
93 event = yield undefined; |
|
94 |
|
95 is(event.target.result instanceof Array, true, "Got an array object"); |
|
96 is(event.target.result.length, 2, "Correct length"); |
|
97 |
|
98 for (let i in event.target.result) { |
|
99 let result = event.target.result[i]; |
|
100 let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
|
101 |
|
102 is(result.name, testObj.name, "Correct name"); |
|
103 is(result.height, testObj.height, "Correct height"); |
|
104 |
|
105 if (testObj.hasOwnProperty("weight")) { |
|
106 is(result.weight, testObj.weight, "Correct weight"); |
|
107 } |
|
108 } |
|
109 |
|
110 request = objectStore.index("height").mozGetAll(65, 0); |
|
111 request.onerror = errorHandler; |
|
112 request.onsuccess = grabEventAndContinueHandler; |
|
113 event = yield undefined; |
|
114 |
|
115 is(event.target.result instanceof Array, true, "Got an array object"); |
|
116 is(event.target.result.length, 2, "Correct length"); |
|
117 |
|
118 for (let i in event.target.result) { |
|
119 let result = event.target.result[i]; |
|
120 let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
|
121 |
|
122 is(result.name, testObj.name, "Correct name"); |
|
123 is(result.height, testObj.height, "Correct height"); |
|
124 |
|
125 if (testObj.hasOwnProperty("weight")) { |
|
126 is(result.weight, testObj.weight, "Correct weight"); |
|
127 } |
|
128 } |
|
129 |
|
130 request = objectStore.index("height").mozGetAll(65, null); |
|
131 request.onerror = errorHandler; |
|
132 request.onsuccess = grabEventAndContinueHandler; |
|
133 event = yield undefined; |
|
134 |
|
135 is(event.target.result instanceof Array, true, "Got an array object"); |
|
136 is(event.target.result.length, 2, "Correct length"); |
|
137 |
|
138 for (let i in event.target.result) { |
|
139 let result = event.target.result[i]; |
|
140 let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
|
141 |
|
142 is(result.name, testObj.name, "Correct name"); |
|
143 is(result.height, testObj.height, "Correct height"); |
|
144 |
|
145 if (testObj.hasOwnProperty("weight")) { |
|
146 is(result.weight, testObj.weight, "Correct weight"); |
|
147 } |
|
148 } |
|
149 |
|
150 request = objectStore.index("height").mozGetAll(65, undefined); |
|
151 request.onerror = errorHandler; |
|
152 request.onsuccess = grabEventAndContinueHandler; |
|
153 event = yield undefined; |
|
154 |
|
155 is(event.target.result instanceof Array, true, "Got an array object"); |
|
156 is(event.target.result.length, 2, "Correct length"); |
|
157 |
|
158 for (let i in event.target.result) { |
|
159 let result = event.target.result[i]; |
|
160 let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
|
161 |
|
162 is(result.name, testObj.name, "Correct name"); |
|
163 is(result.height, testObj.height, "Correct height"); |
|
164 |
|
165 if (testObj.hasOwnProperty("weight")) { |
|
166 is(result.weight, testObj.weight, "Correct weight"); |
|
167 } |
|
168 } |
|
169 |
|
170 request = objectStore.index("height").mozGetAll(); |
|
171 request.onerror = errorHandler; |
|
172 request.onsuccess = grabEventAndContinueHandler; |
|
173 event = yield undefined; |
|
174 |
|
175 is(event.target.result instanceof Array, true, "Got an array object"); |
|
176 is(event.target.result.length, objectStoreDataHeightSort.length, |
|
177 "Correct length"); |
|
178 |
|
179 for (let i in event.target.result) { |
|
180 let result = event.target.result[i]; |
|
181 let testObj = objectStoreDataHeightSort[i].value; |
|
182 |
|
183 is(result.name, testObj.name, "Correct name"); |
|
184 is(result.height, testObj.height, "Correct height"); |
|
185 |
|
186 if (testObj.hasOwnProperty("weight")) { |
|
187 is(result.weight, testObj.weight, "Correct weight"); |
|
188 } |
|
189 } |
|
190 |
|
191 request = objectStore.index("height").mozGetAll(null, 4); |
|
192 request.onerror = errorHandler; |
|
193 request.onsuccess = grabEventAndContinueHandler; |
|
194 event = yield undefined; |
|
195 |
|
196 is(event.target.result instanceof Array, true, "Got an array object"); |
|
197 is(event.target.result.length, 4, "Correct length"); |
|
198 |
|
199 for (let i in event.target.result) { |
|
200 let result = event.target.result[i]; |
|
201 let testObj = objectStoreDataHeightSort[i].value; |
|
202 |
|
203 is(result.name, testObj.name, "Correct name"); |
|
204 is(result.height, testObj.height, "Correct height"); |
|
205 |
|
206 if (testObj.hasOwnProperty("weight")) { |
|
207 is(result.weight, testObj.weight, "Correct weight"); |
|
208 } |
|
209 } |
|
210 |
|
211 request = objectStore.index("height").mozGetAll(65, 1); |
|
212 request.onerror = errorHandler; |
|
213 request.onsuccess = grabEventAndContinueHandler; |
|
214 event = yield undefined; |
|
215 |
|
216 is(event.target.result instanceof Array, true, "Got an array object"); |
|
217 is(event.target.result.length, 1, "Correct length"); |
|
218 |
|
219 for (let i in event.target.result) { |
|
220 let result = event.target.result[i]; |
|
221 let testObj = objectStoreDataHeightSort[parseInt(i) + 3].value; |
|
222 |
|
223 is(result.name, testObj.name, "Correct name"); |
|
224 is(result.height, testObj.height, "Correct height"); |
|
225 |
|
226 if (testObj.hasOwnProperty("weight")) { |
|
227 is(result.weight, testObj.weight, "Correct weight"); |
|
228 } |
|
229 } |
|
230 |
|
231 finishTest(); |
|
232 yield undefined; |
|
233 } |