Debian Codegraph @

dojox/store/tests/DynamoDB.js

raw symbols SCIP
1 define([
2 'intern!object',
3 'intern/chai!assert',
4 'dojo/request/registry',
5 'dojo/_base/lang',
6 'dojo/when',
7 '../DynamoDB'
8 ], function (
9 registerSuite,
10 assert,
11 registry,
12 lang,
13 when,
14 DynamoDB
15 ) {
16 var handles = [];
17 var store;
18
19 var testData = [
20 {
21 id: { S: '10001' },
22 someMap: {
23 M: {
24 stringProperty: { S: 'Sam Smith' },
25 listProperty: { L: [
26 { S: 'foo' },
27 { N: '2' },
28 { BOOL: false }
29 ] },
30 nullProperty: { NULL: true }
31 }
32 },
33 someBool: { BOOL: false },
34 someList: { L: [
35 { S: 'bar' },
36 { NULL: true }
37 ] }
38 },
39 {
40 id: { S: '10002' },
41 someMap: {
42 M: {
43 stringProperty: { S: 'Bob Bobson' },
44 listProperty: { L: [
45 { S: 'bar' },
46 { N: '3' },
47 { BOOL: true }
48 ] }
49 }
50 },
51 someBool: { BOOL: true },
52 someList: { L: [
53 { S: 'baz' },
54 { L: [ { N: 1 }, { N: 2 } ] }
55 ] }
56 }
57 ];
58
59 registerSuite({
60 name: 'store/DynamoDB',
61
62 beforeEach: function () {
63 store = new DynamoDB({
64 endpointUrl: 'endpoint',
65 region: 'us-east-1'
66 });
67 },
68
69 afterEach: function () {
70 var handle;
71 while ((handle = handles.pop())) {
72 handle.remove();
73 }
74 },
75
76 // check that store accesses expected endpoint URL
77 endpointUrl: {
78 generated: function () {
79 var region = 'foo';
80 var dfd = this.async();
81 store = new DynamoDB({ region: region });
82
83 handles.push(registry.register(/foo/, function (url) {
84 dfd.callback(function () {
85 assert.equal(url, 'https://dynamodb.' + region + '.amazonaws.com');
86 })();
87 return when({});
88 }));
89
90 store.get('one');
91 },
92
93 specified: function () {
94 var dfd = this.async();
95 store = new DynamoDB({ endpointUrl: 'foo' });
96
97 handles.push(registry.register(/foo/, function (url) {
98 dfd.callback(function () {
99 assert.equal(url, 'foo');
100 })();
101 return when({});
102 }));
103
104 store.get('one');
105 }
106 },
107
108 query: (function () {
109 var countRequests;
110 var dataRequests;
111
112 function registerRequestHandler(dfd, countHandler, dataHandler) {
113 handles.push(registry.register(store.endpointUrl, function (url, options) {
114 var returnValue;
115 dfd.callback(function () {
116 assert.match(options.headers['x-amz-target'], /\.Query$/);
117 var data = JSON.parse(options.data);
118
119 if (data.Select === 'COUNT') {
120 countRequests.push(data);
121 returnValue = countHandler(data);
122 }
123 else {
124 dataRequests.push(data);
125 returnValue = dataHandler(data);
126 }
127 })();
128 return when(returnValue);
129 }));
130 }
131
132 return {
133 beforeEach: function () {
134 countRequests = [];
135 dataRequests = [];
136 },
137
138 small: function () {
139 // The deferred should resolve 3 times -- two calls to the endpoint and when the query promise
140 // resolves
141 var dfd = this.async(5000, 3);
142
143 registerRequestHandler(dfd, function () {
144 return { Count: testData.length, ScannedCount: testData.length };
145 }, function () {
146 return {
147 Items: testData,
148 Count: testData.length,
149 ScannedCount: testData.length
150 };
151 });
152
153 store.query().then(dfd.callback(function (items) {
154 assert.lengthOf(countRequests, 1);
155 assert.deepEqual(countRequests[0], {
156 Select: 'COUNT',
157 ConsistentRead: false,
158 KeyConditions: {},
159 TableName: store.tableName
160 });
161 assert.lengthOf(dataRequests, 1);
162 assert.deepEqual(dataRequests[0], {
163 ConsistentRead: false,
164 KeyConditions: {},
165 TableName: store.tableName
166 });
167 assert.lengthOf(items, testData.length);
168 }));
169 },
170
171 large: function () {
172 // For a large request, the server will return a LastEvaluatedKey entry that contains the ID of the
173 // last key returned in the resultset. To get more data, the store must make an additional request,
174 // providing the LastEvaluatedKey from the previous request as the ExclusiveStartKey for the next
175 // request.
176
177 // The deferred should resolve 4 times -- three calls to the endpoint and when the query promise
178 // resolves
179 var dfd = this.async(5000, 4);
180
181 registerRequestHandler(dfd, function () {
182 return { Count: 2, ScannedCount: 2 };
183 }, function (data) {
184 if (data.ExclusiveStartKey) {
185 return {
186 Items: [ testData[1] ],
187 Count: 1,
188 ScannedCount: 1
189 };
190 }
191 else {
192 return {
193 Items: [ testData[0] ],
194 Count: 1,
195 ScannedCount: 1,
196 LastEvaluatedKey: {
197 id: testData[0].id
198 }
199 };
200 }
201 });
202
203 store.query().then(dfd.callback(function (items) {
204 assert.lengthOf(countRequests, 1);
205 assert.deepEqual(countRequests[0], {
206 Select: 'COUNT',
207 ConsistentRead: false,
208 KeyConditions: {},
209 TableName: store.tableName
210 });
211
212 assert.lengthOf(dataRequests, 2);
213 assert.deepEqual(dataRequests[0], {
214 ConsistentRead: false,
215 KeyConditions: {},
216 TableName: store.tableName
217 });
218 assert.deepEqual(dataRequests[1], {
219 ConsistentRead: false,
220 KeyConditions: {},
221 TableName: store.tableName,
222 ExclusiveStartKey: {
223 id: testData[0].id
224 }
225 });
226 assert.lengthOf(items, testData.length);
227 }));
228 },
229
230 filter: function () {
231 // The deferred should resolve 3 times -- two calls to the endpoint and when the query promise
232 // resolves
233 var dfd = this.async(5000, 3);
234
235 registerRequestHandler(dfd, function () {
236 return { Count: testData.length, ScannedCount: testData.length };
237 }, function () {
238 return {
239 Items: testData,
240 Count: testData.length,
241 ScannedCount: testData.length
242 };
243 });
244
245 var expectedConditions = {
246 ExpressionAttributeNames: {
247 AttributeValueList: [
248 { M: { '#n': { S: 'name' } } }
249 ],
250 ComparisonOperator: 'EQ'
251 },
252 ExpressionAttributeValues: {
253 AttributeValueList: [
254 { M: { ':word': { S: 'Name' } } }
255 ],
256 ComparisonOperator: 'EQ'
257 },
258 FilterExpression: {
259 AttributeValueList: [
260 { S: 'contains(#n, :word)' }
261 ],
262 ComparisonOperator: 'EQ'
263 }
264 };
265
266 store.query({
267 FilterExpression: 'contains(#n, :word)',
268 ExpressionAttributeNames: { '#n': 'name' },
269 ExpressionAttributeValues: { ':word': 'Name' }
270 }).then(dfd.callback(function () {
271 assert.lengthOf(countRequests, 1);
272 assert.deepEqual(countRequests[0], {
273 Select: 'COUNT',
274 ConsistentRead: false,
275 KeyConditions: expectedConditions,
276 TableName: store.tableName
277 });
278 assert.lengthOf(dataRequests, 1);
279 assert.deepEqual(dataRequests[0], {
280 ConsistentRead: false,
281 KeyConditions: expectedConditions,
282 TableName: store.tableName
283 });
284 }));
285 }
286 };
287 })(),
288
289 get: function () {
290 var dfd = this.async();
291 var id = '123';
292
293 handles.push(registry.register(store.endpointUrl, function (url, options) {
294 dfd.callback(function () {
295 assert.match(options.headers['x-amz-target'], /\.GetItem$/);
296 })();
297 return when({});
298 }));
299
300 store.get(id);
301 },
302
303 put: function () {
304 var dfd = this.async();
305 var item = {
306 id: 1,
307 stringValue: 'foo',
308 boolValue: true
309 };
310
311 handles.push(registry.register(store.endpointUrl, function (url, options) {
312 dfd.callback(function () {
313 assert.match(options.headers['x-amz-target'], /\.PutItem$/);
314 assert.deepEqual(JSON.parse(options.data), {
315 Item: {
316 id: { N: String(item.id) },
317 stringValue: { S: item.stringValue },
318 boolValue: { BOOL: item.boolValue }
319 },
320 TableName: store.tableName
321 });
322 })();
323 return when({});
324 }));
325
326 store.put(item);
327 },
328
329 remove: function () {
330 var dfd = this.async();
331 var removeId = '123';
332
333 handles.push(registry.register(store.endpointUrl, function (url, options) {
334 dfd.callback(function () {
335 assert.match(options.headers['x-amz-target'], /\.DeleteItem$/);
336 assert.deepEqual(JSON.parse(options.data), {
337 Key: { id: { S: removeId } },
338 ReturnValues: 'ALL_OLD',
339 TableName: store.tableName
340 });
341 })();
342 return when({});
343 }));
344
345 store.remove(removeId);
346 },
347
348 sign: (function () {
349 var request;
350 var headers;
351 var date = new Date(2006, 0, 2, 15, 4, 5, 6);
352
353 return {
354 beforeEach: function () {
355 headers = {};
356 request = {
357 body: 'foo',
358 host: 'bar',
359 method: 'POST',
360 headers: headers
361 };
362 store.credentials = {
363 AccessKeyId: 'ABC123',
364 SecretAccessKey: 'DEF456'
365 };
366 },
367
368 'without token': function () {
369 store._signRequest(request, date);
370 assert.deepEqual(headers, {
371 'x-amz-date': '20060102T200405Z',
372 // jshint maxlen: 250
373 'authorization': 'AWS4-HMAC-SHA256 Credential=ABC123/20060102/us-east-1/dynamodb/aws4_request,SignedHeaders=host;x-amz-date;x-amz-target,Signature=10ae50b25e9adafe66134589a071bfd29e464850cf1908e99ad722edd9e962f7'
374 });
375 },
376
377 'with token': function () {
378 store.credentials.SessionToken = 'GHI789';
379 store._signRequest(request, date);
380 assert.deepEqual(headers, {
381 'x-amz-date': '20060102T200405Z',
382 'x-amz-security-token': 'GHI789',
383 // jshint maxlen: 280
384 'authorization': 'AWS4-HMAC-SHA256 Credential=ABC123/20060102/us-east-1/dynamodb/aws4_request,SignedHeaders=host;x-amz-date;x-amz-target;x-amz-security-token,Signature=df743b4f3447ba764f55eb387a42090bd62c0a55be0298535039d2ee3ff9d78b'
385 });
386 }
387 };
388 })()
389 });
390 });